1.0.1 • Published 9 years ago
rewrite-files v1.0.1
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-filesExamples
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) fileContentsis a string- Should call callback like:
callback(err, newContents) newContentsshould 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) fileContentsis a string- Should call
callbacklike:callback(err, newContents) newContentsshould be a string
- Called like:
callback: Function- Called like:
callback(err)
- Called like:
License
MIT
