From c3341919a021c2a9263c019e57c2b1b22ba1ce36 Mon Sep 17 00:00:00 2001 From: Adam Kiss Date: Sun, 2 Apr 2017 00:25:24 +0200 Subject: [PATCH 1/7] Fix tests: new Buffer() is deprecated, replace with Buffer.from() --- test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test.js b/test.js index a020b99..3f27667 100644 --- a/test.js +++ b/test.js @@ -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)) } @@ -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() } From ba4e0a50a2ccccd908ab503878870d6d71cdfa97 Mon Sep 17 00:00:00 2001 From: Adam Kiss Date: Sun, 2 Apr 2017 01:20:53 +0200 Subject: [PATCH 2/7] Promisify fs.stat & fs.readFile --- index.js | 50 +++++++++++++++++++++++--------------------------- package.json | 1 + 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/index.js b/index.js index eab34df..2673192 100644 --- a/index.js +++ b/index.js @@ -4,56 +4,52 @@ 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)) { - if (sourceFile.stat && sourceFile.stat.mtime > targetStat.mtime) { - stream.push(sourceFile); - } + stat(targetPath).then(targetStat => { + if (sourceFile.stat && sourceFile.stat.mtime > targetStat.mtime) { + stream.push(sourceFile); } - - cb(); - }); + }).catch(err => { + fsOperationFailed(stream, sourceFile, err); + }).then(() => cb()); } // Only push through files with different SHA1 than the destination files function compareSha1Digest(stream, cb, sourceFile, targetPath) { - fs.readFile(targetPath, (err, targetData) => { + readFile(targetPath).then(targetData => { if (sourceFile.isNull()) { cb(null, sourceFile); return; } - if (!fsOperationFailed(stream, sourceFile, err)) { - const sourceDigest = sha1(sourceFile.contents); - const targetDigest = sha1(targetData); + const sourceDigest = sha1(sourceFile.contents); + const targetDigest = sha1(targetData); - if (sourceDigest !== targetDigest) { - stream.push(sourceFile); - } + if (sourceDigest !== targetDigest) { + stream.push(sourceFile); } - - cb(); - }); + }).catch(err => { + fsOperationFailed(stream, sourceFile, err); + }).then(() => cb()); } module.exports = (dest, opts) => { diff --git a/package.json b/package.json index 7564d27..b4fc0c5 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,7 @@ ], "dependencies": { "gulp-util": "^3.0.0", + "pify": "^2.3.0", "through2": "^2.0.0" }, "devDependencies": { From b9ea3f929e33741465f44150988135a4b9d223a5 Mon Sep 17 00:00:00 2001 From: Adam Kiss Date: Sun, 2 Apr 2017 01:22:37 +0200 Subject: [PATCH 3/7] Reformat promises --- index.js | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index 2673192..0cc4bc6 100644 --- a/index.js +++ b/index.js @@ -24,32 +24,34 @@ 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) { - stat(targetPath).then(targetStat => { - if (sourceFile.stat && sourceFile.stat.mtime > targetStat.mtime) { - stream.push(sourceFile); - } - }).catch(err => { - fsOperationFailed(stream, sourceFile, err); - }).then(() => cb()); + stat(targetPath) + .then(targetStat => { + if (sourceFile.stat && sourceFile.stat.mtime > targetStat.mtime) { + stream.push(sourceFile); + } + }) + .catch(err => fsOperationFailed(stream, sourceFile, err)) + .then(() => cb()); } // Only push through files with different SHA1 than the destination files function compareSha1Digest(stream, cb, sourceFile, targetPath) { - readFile(targetPath).then(targetData => { - if (sourceFile.isNull()) { - cb(null, sourceFile); - return; - } + readFile(targetPath) + .then(targetData => { + if (sourceFile.isNull()) { + cb(null, sourceFile); + return; + } - const sourceDigest = sha1(sourceFile.contents); - const targetDigest = sha1(targetData); + const sourceDigest = sha1(sourceFile.contents); + const targetDigest = sha1(targetData); - if (sourceDigest !== targetDigest) { - stream.push(sourceFile); - } - }).catch(err => { - fsOperationFailed(stream, sourceFile, err); - }).then(() => cb()); + if (sourceDigest !== targetDigest) { + stream.push(sourceFile); + } + }) + .catch(err => fsOperationFailed(stream, sourceFile, err)) + .then(() => cb()); } module.exports = (dest, opts) => { From 35fe3dc5092d2705ab4296d85f2f6fef3b08f633 Mon Sep 17 00:00:00 2001 From: Adam Kiss Date: Mon, 17 Apr 2017 14:49:42 +0200 Subject: [PATCH 4/7] Pull cb() from methods to the call --- index.js | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 0cc4bc6..8d1ccd6 100644 --- a/index.js +++ b/index.js @@ -23,23 +23,21 @@ function fsOperationFailed(stream, sourceFile, err) { 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) { - stat(targetPath) +function compareLastModifiedTime(stream, sourceFile, targetPath) { + return stat(targetPath) .then(targetStat => { if (sourceFile.stat && sourceFile.stat.mtime > targetStat.mtime) { stream.push(sourceFile); } }) - .catch(err => fsOperationFailed(stream, sourceFile, err)) - .then(() => cb()); + .catch(err => fsOperationFailed(stream, sourceFile, err)); } // Only push through files with different SHA1 than the destination files -function compareSha1Digest(stream, cb, sourceFile, targetPath) { - readFile(targetPath) +function compareSha1Digest(stream, sourceFile, targetPath) { + return readFile(targetPath) .then(targetData => { if (sourceFile.isNull()) { - cb(null, sourceFile); return; } @@ -50,8 +48,7 @@ function compareSha1Digest(stream, cb, sourceFile, targetPath) { stream.push(sourceFile); } }) - .catch(err => fsOperationFailed(stream, sourceFile, err)) - .then(() => cb()); + .catch(err => fsOperationFailed(stream, sourceFile, err)); } module.exports = (dest, opts) => { @@ -84,7 +81,7 @@ module.exports = (dest, opts) => { } } - opts.hasChanged(this, cb, file, newPath); + opts.hasChanged(this, file, newPath).then(() => cb()); }); }; From acb91b939bc87a8c0a29675273e41d36c8824ce8 Mon Sep 17 00:00:00 2001 From: Adam Kiss Date: Mon, 17 Apr 2017 17:11:36 +0200 Subject: [PATCH 5/7] Push file to stream if isNull() --- index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/index.js b/index.js index 8d1ccd6..bf043bc 100644 --- a/index.js +++ b/index.js @@ -38,6 +38,7 @@ function compareSha1Digest(stream, sourceFile, targetPath) { return readFile(targetPath) .then(targetData => { if (sourceFile.isNull()) { + stream.push(sourceFile); return; } From d987f8fc3eaddd1cf38ffb168956513f98a9bef7 Mon Sep 17 00:00:00 2001 From: Adam Kiss Date: Tue, 18 Apr 2017 09:50:56 +0200 Subject: [PATCH 6/7] Move fsOperationFailed out of comparators --- index.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index bf043bc..69bf7f7 100644 --- a/index.js +++ b/index.js @@ -29,8 +29,7 @@ function compareLastModifiedTime(stream, sourceFile, targetPath) { if (sourceFile.stat && sourceFile.stat.mtime > targetStat.mtime) { stream.push(sourceFile); } - }) - .catch(err => fsOperationFailed(stream, sourceFile, err)); + }); } // Only push through files with different SHA1 than the destination files @@ -48,8 +47,7 @@ function compareSha1Digest(stream, sourceFile, targetPath) { if (sourceDigest !== targetDigest) { stream.push(sourceFile); } - }) - .catch(err => fsOperationFailed(stream, sourceFile, err)); + }); } module.exports = (dest, opts) => { @@ -82,7 +80,10 @@ module.exports = (dest, opts) => { } } - opts.hasChanged(this, file, newPath).then(() => cb()); + opts + .hasChanged(this, file, newPath) + .catch(err => fsOperationFailed(this, file, err)) + .then(() => cb()); }); }; From 9e80a4a4203bf3f69372be3818d5d663fd1b50ae Mon Sep 17 00:00:00 2001 From: Adam Kiss Date: Fri, 21 Apr 2017 22:00:23 +0200 Subject: [PATCH 7/7] Remove cb and add a return expectation --- readme.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/readme.md b/readme.md index 41c24d5..2cdc2a6 100644 --- a/readme.md +++ b/readme.md @@ -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