Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add number of reversions option #3

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
'use strict';
var through = require('through2');

module.exports = function () {
module.exports = function (reversions) {
reversions = typeof reversions === 'number' ? reversions : 1;

return through.obj(function (file, enc, cb) {
var history = file.history;
var highestIndex = history.length - 1;

if (history.length > 1) {
history.pop();
file.path = history[history.length - 1];
if (reversions > highestIndex) {
reversions = highestIndex;
}

history.splice(-reversions, reversions);
file.path = history[history.length - 1];

cb(null, file);
});
};
9 changes: 9 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,22 @@ $ npm install --save-dev gulp-revert-path
var gulp = require('gulp');
var babel = require('gulp-babel');
var revertPath = require('gulp-revert-path');
var rename = require('gulp-rename');

gulp.task('default', function () {
return gulp.src('src/app.jsx')
.pipe(babel()) // file.path => src/app.js
.pipe(revertPath()) // file.path => src/app.jsx
.pipe(gulp.dest('dist'));
});

gulp.task('es2015', function () {
return gulp.src('src/app.txt')
.pipe(rename('src/app.jsx')) // file.path => src/app.jsx
.pipe(babel()) // file.path => src/app.js
.pipe(revertPath(2)) // file.path => src/app.txt
.pipe(gulp.dest('dist'));
});
```


Expand Down
78 changes: 70 additions & 8 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ var gutil = require('gulp-util');
var through = require('through2');
var revertPath = require('./');

it('revert the path to the previous one', function (done) {
it('reverts the path to the previous one', function (done) {
var s = through.obj(function (file, enc, cb) {
assert.strictEqual(path.extname(file.path), '.foo');
file.path = gutil.replaceExtension(file.path, '.bar');
assert.strictEqual(path.extname(file.path), '.bar');
assert.strictEqual(file.history.length, 2);
cb(null, file);
});

s.pipe(revertPath()).pipe(through.obj(function (file, enc, cb) {
assert.strictEqual(path.extname(file.path), '.foo');
assert.strictEqual(path.extname(file.history[0]), '.foo');
assert.strictEqual(file.history.length, 1);
cb();
}));

Expand All @@ -31,18 +33,48 @@ it('revert the path to the previous one', function (done) {
s.end();
});

it('successfully processes files with unmodified paths', function (done) {
var path = __dirname + '/fixture/fixture.foo';
it('reverts the path to the previous two', function (done) {
var s = through.obj(function (file, enc, cb) {
assert.strictEqual(path.extname(file.path), '.foo');
file.path = gutil.replaceExtension(file.path, '.bar');
assert.strictEqual(path.extname(file.path), '.bar');
file.path = gutil.replaceExtension(file.path, '.baz');
assert.strictEqual(path.extname(file.path), '.baz');
assert.strictEqual(file.history.length, 3);
cb(null, file);
});

s.pipe(revertPath(2)).pipe(through.obj(function (file, enc, cb) {
assert.strictEqual(path.extname(file.path), '.foo');
assert.strictEqual(path.extname(file.history[0]), '.foo');
assert.strictEqual(file.history.length, 1);
cb();
}));

s.on('end', done);

s.write(new gutil.File({
cwd: __dirname,
base: __dirname + '/fixture',
path: __dirname + '/fixture/fixture.foo',
contents: new Buffer('')
}));

s.end();
});

it('successfully processes files with unmodified paths', function (done) {
var s = through.obj(function (file, enc, cb) {
assert.strictEqual(file.path, path);
assert.deepEqual(file.history, [path]);
assert.strictEqual(path.extname(file.path), '.foo');
assert.deepEqual(path.extname(file.history[0]), '.foo');
assert.strictEqual(file.history.length, 1);
cb(null, file);
});

s.pipe(revertPath()).pipe(through.obj(function (file, enc, cb) {
assert.strictEqual(file.path, path);
assert.deepEqual(file.history, [path]);
assert.strictEqual(path.extname(file.path), '.foo');
assert.deepEqual(path.extname(file.history[0]), '.foo');
assert.strictEqual(file.history.length, 1);
cb();
}));

Expand All @@ -51,7 +83,37 @@ it('successfully processes files with unmodified paths', function (done) {
s.write(new gutil.File({
cwd: __dirname,
base: __dirname + '/fixture',
path: path,
path: __dirname + '/fixture/fixture.foo',
contents: new Buffer('')
}));

s.end();
});

it('reverts as much as possible', function (done) {
var s = through.obj(function (file, enc, cb) {
assert.strictEqual(path.extname(file.path), '.foo');
file.path = gutil.replaceExtension(file.path, '.bar');
assert.strictEqual(path.extname(file.path), '.bar');
file.path = gutil.replaceExtension(file.path, '.baz');
assert.strictEqual(path.extname(file.path), '.baz');
assert.strictEqual(file.history.length, 3);
cb(null, file);
});

s.pipe(revertPath(100)).pipe(through.obj(function (file, enc, cb) {
assert.strictEqual(path.extname(file.path), '.foo');
assert.deepEqual(path.extname(file.history[0]), '.foo');
assert.strictEqual(file.history.length, 1);
cb();
}));

s.on('end', done);

s.write(new gutil.File({
cwd: __dirname,
base: __dirname + '/fixture',
path: __dirname + '/fixture/fixture.foo',
contents: new Buffer('')
}));

Expand Down