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

file-utils

v0.2.2

Published

Sync file utility for Node.js command line tools

Downloads

22,317

Readme

file-utils

This is a Grunt.file forks to allow the creation of scoped file utilities and the ability to add write filters.

Same as Grunt.file, this is a set of synchronous utility. As so, it should never be used on a Node.js server. This is meant for users/command line utilities.

File API

Upcoming. Meanwhile, check Grunt.file documentation as the same methods are available.

Setting options - file.option( name, [ value ])

// Set option
file.option('write', false);

// Get option
file.option('write');

Available Options

  • write (Boolean): If write is set to false, then no file will be written or deleted. Useful for test run without side effets.
  • logger (Logger object): Used internally to log information to the console. API still work in progress
  • encoding (String): Defaults utf8. Set the default encoding used for reading/writing. Note most methods allow you to overwridde it for a single run.
  • force (Boolean): force: true Force the deletion of folders and file outside the utility scope (or CWD if no scope).

ENV scope and filters

Creating an Env - file#createEnv([ options ]);

var file = require('file-utils');

var env = file.createEnv({
  base: 'my/scoped/path',
  dest: 'destination/path' // optionnal
});

// Alternatively, they can be functions returning a path:

var env = file.createEnv({
  base: function() {
    return 'my/scoped/path';
  },
  dest: function() { // optionnal
    return 'destination/path';
  }
});

The base directory will prefix any paths passed to mkdir, recurse, read, readJSON, write, delete, exists, isLink, isDir and isFile methods.

The dest directory will prefix the destination path provided in the copy method. Note that this option is optionnal and will default to the current working directory.

If options (logger, write, etc) are not passed, each Env instance inherit those of its parent.

Write Filters

Write filters are applied on env.write and env.copy.

They're used to modifiy the content or the filepath of a file.

Add a write filter - env.registerWriteFilter( name, filter )

options

  • name (String): The name under which registering the filter
  • filter (Function): The filter function

The filter function take a file object as parameter. This file object is a hash containing a path and a contents property. You can modify these two property as you like and returning the modified object.

env.registerWriteFilter( 'coffee', function( file ) {
  if (!path.extname(file) !== '.js') return file;

  file.path = file.path.replace(/(\.js)$/, '.coffee');
  file.content = convertJsToCoffee( file.contents );

  return file;
});

Remove a write filter - env.removeWriteFilter( name )

env.removeWriteFilter('coffee');

Async filter

The filter can also be asynchronous. This is done by calling this.async() and passing the return value to the callback provided.

env.registerWriteFilter( 'coffee', function( file ) {
  var done = this.async();

  // some process
  setTimeout(function() {
    done({ path: '/newfile', contents: 'filtered content' });
  }, 1000);
});

Caution: Using an asynchronous filter will change the way write and copy method are called to. This will make both of those method to run asynchronously too.

Validation Filters

Validation filters are applied on env.write and env.copy.

They're used to allow or disallow the write action.

Add a validation filter - env.registerValidationFilter( name, filter )

options

  • name (String): The name under which registering the filter
  • filter (Function): The filter function

The filter function take a file object as parameter. This file object is a hash containing a path (String) and a contents (String if text file, Buffer otherwise) property.

Return true to allow the file to be written. Return false or an error message String to disallow the write action.

env.registerValidationFilter( 'checkConflicts', function( toOutput ) {
  if ( file.exists(toOutput.path) ) {
    return 'file is already present';
  }
  return true;
});

Just like the write filters, this filter can be asynchronous.

Remove a validation filter - env.removeValidationFilter( name )

env.removeValidationFilter('checkConflicts');

Todos

  • Real Logging system