Skip to content

Commit

Permalink
Add transformPath option (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
petergoes authored and sindresorhus committed Feb 18, 2017
1 parent b2aa51f commit 3a061b1
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions fixture/different.transformPath/src/a
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a
1 change: 1 addition & 0 deletions fixture/different.transformPath/src/b
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
b
1 change: 1 addition & 0 deletions fixture/different.transformPath/trg/c/a
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a
1 change: 1 addition & 0 deletions fixture/different.transformPath/trg/c/b
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
b!
12 changes: 12 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ module.exports = (dest, opts) => {
throw new gutil.PluginError('gulp-changed', '`dest` required');
}

if (opts.transformPath !== undefined && typeof opts.transformPath !== 'function') {
throw new gutil.PluginError('gulp-changed', '`opts.transformPath` needs to be a function');
}

return through.obj(function (file, enc, cb) {
const dest2 = typeof dest === 'function' ? dest(file) : dest;
let newPath = path.resolve(opts.cwd, dest2, file.relative);
Expand All @@ -74,6 +78,14 @@ module.exports = (dest, opts) => {
newPath = gutil.replaceExtension(newPath, opts.extension);
}

if (opts.transformPath) {
newPath = opts.transformPath(newPath);

if (typeof newPath !== 'string') {
throw new gutil.PluginError('gulp-changed', '`opts.transformPath` needs to return a string');
}
}

opts.hasChanged(this, cb, file, newPath);
});
};
Expand Down
17 changes: 17 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,23 @@ You can also supply a custom comparator function which will receive the followin
- `sourceFile` *([Vinyl file object](https://github.com/wearefractal/vinyl#file))*
- `destPath` *(string)* - Destination for `sourceFile` as an absolute path

##### transformPath

Type: `Function`

Function to transform the path to the destination file. Should return the absolute path to the (renamed) destination file.

Useful if you rename your file later on, like in the below example:

```js
gulp.task('marked', () =>
gulp.src('src/content/about.md')
.pipe(changed('dist', {transformPath: newPath => path.join(path.dirname(newPath), path.basename(newPath, '.md'), 'index.html')}))
.pipe(marked())
.pipe(rename(newPath => path.join(path.dirname(newPath), path.basename(newPath, '.md'), 'index.html'))))
.pipe(gulp.dest('dist'))
);
```

## In-place change monitoring

Expand Down
16 changes: 16 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,22 @@ describe('compareSha1Digest', () => {
});
});

it('should only pass through changed files using transformPath', () => {
const stream = gulp.src('fixture/different.transformPath/src/*')
.pipe(changed('fixture/different.transformPath/trg', {
hasChanged: changed.compareSha1Digest,
transformPath: newPath => {
const pathParsed = path.parse(newPath);
return path.join(pathParsed.dir, 'c', pathParsed.base);
}
}));

return getStream.array(stream).then(files => {
assert.equal(files.length, 1);
assert.equal(path.basename(files[0].path), 'b');
});
});

it('should only pass through changed files using extension .coffee', () => {
const stream = gulp.src('fixture/different.ext/src/*')
.pipe(changed('fixture/different.ext/trg', {
Expand Down

0 comments on commit 3a061b1

Please sign in to comment.