Skip to content

Commit

Permalink
Promisify fs methods & change method signature for custom comparato…
Browse files Browse the repository at this point in the history
…rs (#62)
  • Loading branch information
adamkiss authored and sindresorhus committed Apr 22, 2017
1 parent d836d67 commit 1ffefba
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 32 deletions.
53 changes: 25 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,50 @@ const path = require('path');
const crypto = require('crypto');
const gutil = require('gulp-util');
const through = require('through2');
const pify = require('pify');

const readFile = pify(fs.readFile);
const stat = pify(fs.stat);

// Ignore missing file error
function fsOperationFailed(stream, sourceFile, err) {
if (err) {
if (err.code !== 'ENOENT') {
stream.emit('error', new gutil.PluginError('gulp-changed', err, {
fileName: sourceFile.path
}));
}

stream.push(sourceFile);
if (err.code !== 'ENOENT') {
stream.emit('error', new gutil.PluginError('gulp-changed', err, {
fileName: sourceFile.path
}));
}

return err;
stream.push(sourceFile);
}

const sha1 = buf => crypto.createHash('sha1').update(buf).digest('hex');

// Only push through files changed more recently than the destination files
function compareLastModifiedTime(stream, cb, sourceFile, targetPath) {
fs.stat(targetPath, (err, targetStat) => {
if (!fsOperationFailed(stream, sourceFile, err)) {
function compareLastModifiedTime(stream, sourceFile, targetPath) {
return stat(targetPath)
.then(targetStat => {
if (sourceFile.stat && sourceFile.stat.mtime > targetStat.mtime) {
stream.push(sourceFile);
}
}

cb();
});
});
}

// Only push through files with different SHA1 than the destination files
function compareSha1Digest(stream, cb, sourceFile, targetPath) {
fs.readFile(targetPath, (err, targetData) => {
if (sourceFile.isNull()) {
cb(null, sourceFile);
return;
}
function compareSha1Digest(stream, sourceFile, targetPath) {
return readFile(targetPath)
.then(targetData => {
if (sourceFile.isNull()) {
stream.push(sourceFile);
return;
}

if (!fsOperationFailed(stream, sourceFile, err)) {
const sourceDigest = sha1(sourceFile.contents);
const targetDigest = sha1(targetData);

if (sourceDigest !== targetDigest) {
stream.push(sourceFile);
}
}

cb();
});
});
}

module.exports = (dest, opts) => {
Expand Down Expand Up @@ -86,7 +80,10 @@ module.exports = (dest, opts) => {
}
}

opts.hasChanged(this, cb, file, newPath);
opts
.hasChanged(this, file, newPath)
.catch(err => fsOperationFailed(this, file, err))
.then(() => cb());
});
};

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
],
"dependencies": {
"gulp-util": "^3.0.0",
"pify": "^2.3.0",
"through2": "^2.0.0"
},
"devDependencies": {
Expand Down
3 changes: 1 addition & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,9 @@ gulp.task('jade', () =>
);
```

You can also supply a custom comparator function which will receive the following arguments:
You can also supply a custom comparator function which will receive the following arguments and should return `Promise`.

- `stream` *([transform object stream](https://github.com/rvagg/through2#transformfunction))* - Should be used to queue `sourceFile` if it passes some comparison
- `callback` *(function)* - Should be called when done
- `sourceFile` *([Vinyl file object](https://github.com/wearefractal/vinyl#file))*
- `destPath` *(string)* - Destination for `sourceFile` as an absolute path

Expand Down
4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function test(dest, opts) {
cwd: __dirname,
base: __dirname,
path: 'foo.js',
contents: new Buffer(''),
contents: Buffer.from(''),
stat: {
mtime: fs.statSync(path.join(dest, 'foo' + extension))
}
Expand All @@ -60,7 +60,7 @@ function test(dest, opts) {
stream.write(new gutil.File({
base: __dirname,
path: 'bar.js',
contents: new Buffer(''),
contents: Buffer.from(''),
stat: {
mtime: new Date()
}
Expand Down

0 comments on commit 1ffefba

Please sign in to comment.