diff --git a/updatefile.js b/updatefile.js index af4f6fd..0aab912 100644 --- a/updatefile.js +++ b/updatefile.js @@ -15,12 +15,31 @@ module.exports = function(app) { return updateExample(app, 'bar'); }); - // register a "sub-updater" + app.task('baz', function() { + return updateExample(app, 'baz'); + }); + + app.task('undo', function() { + var cwd = app.options.dest || app.cwd; + return app.src('example.txt', {cwd: cwd}) + .pipe(erase(app.options.n)) + .pipe(app.dest(cwd)); + }); + + /** + * "Sub-updater" + */ + app.register('abc', function(abc) { this.task('default', function() { return updateExample(app, 'abc:default'); }); + + this.task('xyz', function() { + return updateExample(app, 'abc:xyz'); + }); }); + app.task('default', ['foo']); }; @@ -57,3 +76,20 @@ function append(str) { next(null, file); }); } + +/** + * Erase the given number of lines from the end of a string + * + * @param {String} `str` + * @return {Stream} vinyl stream + * @api public + */ + +function erase(num) { + var n = typeof num === 'number' ? num : 1; + return through.obj(function(file, enc, next) { + var lines = file.contents.toString().trim().split('\n'); + file.contents = new Buffer(lines.slice(0, -n).join('\n') + '\n'); + next(null, file); + }); +}