Skip to content
Open
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
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ a29089cc5e3f8bf6ae15ea6b9cd5eaefb14bbb12e3baa2c56ee5c21422250c75

### hashFiles([options], callback)

Performs a hash of the contents of the given files ansynchronously.
Performs a hash of the contents of the given files asynchronously.

* `options` - (Object) see below for more details
* `callback` - (Function) called when an error occurs or the hash is completed. Should expect the following arguments:
Expand All @@ -63,6 +63,13 @@ Performs a hash of the contents of the given files synchronously.
* `options` - (Object) see below for more details
* returns `hash` - (String) the value of the computed hash

### hashFiles.promise([options])

Performs a hash of the contents of the given files asynchronously and returns a promise.

* `options` - (Object) see below for more details
* returns `Promise` - (Promise) resolves to the value of the computed hash, or rejects if an error occurred

### Options

* `files` - (optional) A collection of file paths to hash the contents of. Defaults to `['./**']` (all the files in the current working directory)
Expand Down
16 changes: 16 additions & 0 deletions lib/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@ function hashFilesSync(options) {
return hash.digest('hex');
}

function hashFilesPromise(options, other) {
options = options || {};

if (other) {
throw new Error('hashFilesPromise only takes 1 parameter. Consider using hashFiles if you would like to use a callback instead of a promise.');
}

return new Promise(function(resolve, reject) {
hashFiles(options, function(err, hash) {
if (err) { reject(err); }
else { resolve(hash); }
});
});
}

hashFiles.sync = hashFilesSync;
hashFiles.promise = hashFilesPromise;

module.exports = hashFiles;
84 changes: 84 additions & 0 deletions test/hash.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,88 @@ describe('hash-files Unit Tests', function() {

});

/*
888 888 .d888 d8b 888 d8b
888 888 d88P" Y8P 888 Y8P
888 888 888 888
88888b. 8888b. .d8888b 88888b. 888888 888 888 .d88b. .d8888b 88888b. 888d888 .d88b. 88888b.d88b. 888 .d8888b .d88b.
888 "88b "88b 88K 888 "88b 888 888 888 d8P Y8b 88K 888 "88b 888P" d88""88b 888 "888 "88b 888 88K d8P Y8b
888 888 .d888888 "Y8888b. 888 888 888888 888 888 888 88888888 "Y8888b. 888888 888 888 888 888 888 888 888 888 888 "Y8888b. 88888888
888 888 888 888 X88 888 888 888 888 888 Y8b. X88 888 d88P 888 Y88..88P 888 888 888 888 X88 Y8b.
888 888 "Y888888 88888P' 888 888 888 888 888 "Y8888 88888P' 88888P" 888 "Y88P" 888 888 888 888 88888P' "Y8888
888
888
888
*/
describe('hash-files-promise', function() {

it('should return a promise that resolves to hash contents of directory with defaults', function(done) {
var hash = hashFiles.promise();
hash.then(function(hash) {
assert(hash === expectedContentsSha1);
done();
})
});

it('should return a promise that resolves to hash contents of files with md5 algorithm', function(done) {
var hash = hashFiles.promise({ files: ['/some/root/*'], algorithm: 'md5' });
hash.then(function(hash) {
assert(hash === expectedContentsMd5);
done();
});
});

it('should return a promise that resolves to hash contents of files without globbing', function(done) {
var hash = hashFiles.promise({ files: ['z_file1.txt', 'some/dir/file2.txt'], noGlob: true });
hash.then(function(hash) {
assert(hash === expectedContentsSha1);
done();
});
});

it('should reject proimse if unknown algorithm', function(done) {
// throw new Error
var hash = hashFiles.promise({ algorithm: 'fnord' });
hash.catch(function(err) {
assert.throws(function(err) {
throw new Error;
});
});
done();
});

it('should reject promise if globbing fails', function(done) {

failGlobbing = true;

var hash = hashFiles.promise();
hash.catch(function(err) {
assert.throws(function(err) {
throw new Err;
});
});
done();
});

it('should reject promise if readFiles fails', function(done) {

failReadFile = true;

var hash = hashFiles.promise();
hash.catch(function(err) {
assert.throws(function(err) {
throw new Err;
});
});
done();
});

it('should throw an error if more than one parameter is entered', function(done) {
assert.throws(function() {
hashFiles.promise({ files: ['/some/root/*'] }, 1)
});
done();
});
});

});
2 changes: 1 addition & 1 deletion test/jshint/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"boss": true,
"debug": false,
"eqnull": true,
"esnext": false,
"esnext": true,
"evil": false,
"expr": false,
"funcscope": false,
Expand Down