npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-file-utils

v0.91.20

Published

A simple set of file utilities suitable for command line projects...

Downloads

42

Readme

Node File Utils


NPM version Build Status Dependency Status

A simple set of file utility and mock classes. Modules are a stand-alone class-like objects constructed with injected variable options. The intent is to provide small specific helpers to solve typical linux/unix file system problems.

Installation

npm install node-file-utils --save

How to use

The examples folder contains a few use cases that demonstrate how to define, instantiate and use the available utilities. The examples below also show use.

Note: modules use standard production level logging and must be constructed with a logger, e.g., winston, log4js, etc.

FileCopier

FileCopier copies the original contents and the file's mode from source to destination. The destination path must exist prior to a copy. (use a mkdirp utility for that)

var FileCopier = require('node-file-utils').FileCopier,
	log = require('simple-node-logger').createSimpleLogger('FileCopier'),
	copier = new FileCopier( { log:log } );
	
copier.copy('mysource', 'mydestination', function(err) {
	if (err) {
		log.error( err );
		throw err;
	}
	
	log.info('file copied...');
});

// or

copier.onComplete(stats => console.log( stats ));

copier.copy('mysource', 'myfolder/myNestedFolder/dest');

File Copy Events

File copy has a progress event that fires when data arrives in chunks. A simple progress percentage is fired for this event.

var progressHandler = function(percent) {
	log.info('percent complete: ', percent, '%');
};

copier.onProgress( progressHandler );

File Tree Walker

TreeWalker's walk method traverses a file system from a specified start point and returns a list of all files. The find method returns files that match a specified pattern.

var TreeWalker = require('node-file-utils').TreeWalker,
	log = require('simple-node-logger').createSimpleLogger(),
	walker = new TreeWalker({ log:log }),
	callback;
	
callback = function(err, files) {
	if (err) throw err;

	files.forEach(function(file) {
    	log.info( file );
	}); 

	log.info( 'file list length: ', files.length );
};

// return all the files
walker.walk( 'myfolder', callback );

// find and return just the javascript files
walker.find( 'myfolder', /.js$/, callback );

Tree Walker Events

Events are triggered by TreeWalker when a new folder or file is located. Files that are skipped based on age or pattern don't trigger events. All new folders trigger events.

onDirectory

When a new directory is located a 'onDirectory' event is fired. The event handler receives the director's path.

var directoryHandler = function(path) {
	log.info( 'new folder: ', path );
};

walker.onDirectory( directoryHandler );

onFile

When a new file is added to the list a 'onFile' event is fired with the file's full path.

var fileHandler = function(path) {
	log.info( 'new file: ', path );
};

walker.onFile( fileHandler );

FileArchiver

A simple example of a file purge based on files older than 30 days.

var FileArchiver = require('node-file-utils'). FileArchiver,
	archiver = FileArchiver.createInstance(),
	config = {
		folders:[ 'logs/' ],
		cwd: process.env.HOME,
		olderThanDays: 30
	};
	

archiver.onProgress(function(err, file) { console.log('file removed: ', file); });

archiver.onComplete(function() { console.log('archive/purge complete...'); });

archiver.purge( config );

Examples

Examples of file copier and tree walker can be found in the examples folder. The javascript scripts can be run like this:

node examples/file-copy.js

// and

node examples/tree-walker.js

// or

node examples/purge-files.js

Unit Tests

All unit tests are written in mocha/chai/should and can be run from the command line by doing this:

make test

// or

make jshint

// or

npm test

There is also a source file watcher that can be invoked with this:

make watch

Mocks

Mocks used for testing include MockFileSystem. Typically you would use MockFileSystem for unit tests like this:

var MockFileSystem = require('node-file-utils').mocks.MockFileSystem;

var fs = new MockFileSystem();

fs.readFile('filename', callback);

License

Apache 2.0