rewrite-files
v1.0.1
Published
Rewrite file contents in-place using pattern matching or a list of files
Downloads
9
Readme
rewrite-files
Rewrite file contents in-place.
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 ontranformer
: Function- Called like:
transformer(fileName, fileContents, callback)
fileContents
is a string- Should call callback like:
callback(err, newContents)
newContents
should be a string
- Called like:
callback
: Function- Called like:
callback(err)
- Called like:
rewriteFiles.withPattern
rewriteFiles.withPattern(pattern, transformer, callback);
pattern
: Glob pattern used to extract list of filestranformer
: Function- Called like:
transformer(fileName, fileContents, callback)
fileContents
is a string- Should call
callback
like:callback(err, newContents)
newContents
should be a string
- Called like:
callback
: Function- Called like:
callback(err)
- Called like:
License
MIT