rewrite-files

1.0.1 • Public • Published

rewrite-files

Rewrite file contents in-place.

NPM

Use glob matching to discover a list of files, or specify them directly. Write a transformer function that receives filenames and file contents, which can asynchronously indicate the new file contents when done.

This can be usefully paired with something like falafel to parse JS into an AST and rewrite code.

Install

npm install --save rewrite-files

Examples

var rewriteFiles = require('rewrite-files');
 
function rewrite(fileName, contents, callback) {
  var newContents = '<wrap ' + contents + '>';
  callback(null, newContents);
}
 
exports.upgrade = function(next) {
  var basePath = config.get('webapp.js.src.basePath');
  var pattern = path.join(basePath, '**/*.js');
  rewriteFiles.withPattern(pattern, rewrite, function(err) {
    next(err ? (err.stack || err) : null);
  });
};

Methods

rewriteFiles

rewriteFiles(files, transformer, callback);
  • files: Array of filenames to operate on
  • tranformer: Function
    • Called like: transformer(fileName, fileContents, callback)
    • fileContents is a string
    • Should call callback like: callback(err, newContents)
    • newContents should be a string
  • callback: Function
    • Called like: callback(err)

rewriteFiles.withPattern

rewriteFiles.withPattern(pattern, transformer, callback);
  • pattern: Glob pattern used to extract list of files
  • tranformer: Function
    • Called like: transformer(fileName, fileContents, callback)
    • fileContents is a string
    • Should call callback like: callback(err, newContents)
    • newContents should be a string
  • callback: Function
    • Called like: callback(err)

License

MIT

Package Sidebar

Install

npm i rewrite-files

Weekly Downloads

1

Version

1.0.1

License

MIT

Last publish

Collaborators

  • mwolson