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

Promisify fs methods #62

Merged
merged 7 commits into from
Apr 22, 2017
Merged
Changes from 1 commit
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
17 changes: 7 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is okay, but after Promise we run cb() either way?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. You still need to pass the file down the stream even though its contents is null.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, thanks! Fixed it now.

return;
}

Expand All @@ -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) => {
Expand Down Expand Up @@ -84,7 +81,7 @@ module.exports = (dest, opts) => {
}
}

opts.hasChanged(this, cb, file, newPath);
opts.hasChanged(this, file, newPath).then(() => cb());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can now move the fsOperationFailed error handling here instead of catching it in both compareLastModifiedTime and compareSha1Digest.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right! Thank you, and sorry for doing this line by line!

Will do in 20

});
};

Expand Down