folder-hash
Advanced tools
Comparing version 1.0.3 to 1.0.4
72
index.js
@@ -6,5 +6,5 @@ "use strict" | ||
var crypto = require('crypto'); | ||
var Q = require('q'); | ||
var Promise = Q.Promise; | ||
if (typeof Promise === 'undefined') require('when/es6-shim/Promise'); | ||
//Promise = require('when').Promise; | ||
@@ -22,4 +22,4 @@ var algo = 'sha1'; | ||
* Create a hash over a folder or file, using either promises or error-first-callbacks. | ||
* The parameter directoryPath is optional. This function may be called | ||
* as createHash(filename, folderpath, fn(err, hash) {}), createHash(filename, folderpath) | ||
* The parameter directoryPath is optional. This function may be called | ||
* as createHash(filename, folderpath, fn(err, hash) {}), createHash(filename, folderpath) | ||
* or as createHash(path, fn(err, hash) {}), createHash(path) | ||
@@ -32,18 +32,28 @@ */ | ||
return Promise(function (resolve, reject, notify) { | ||
if (!isString(name)) { | ||
reject(new TypeError('First argument must be a string')); | ||
var promise; | ||
if (!isString(name)) { | ||
promise = Promise.reject(new TypeError('First argument must be a string')); | ||
} | ||
if (!isString(directoryPath)) { | ||
if (typeof directoryPath === 'function') { | ||
callback = directoryPath; | ||
} | ||
if (!isString(directoryPath)) { | ||
if (typeof directoryPath === 'function') { | ||
callback = directoryPath; | ||
} | ||
directoryPath = path.dirname(name); | ||
name = path.basename(name); | ||
} | ||
resolve(hashElementPromise(name, directoryPath, callback)); | ||
}).nodeify(callback); | ||
directoryPath = path.dirname(name); | ||
name = path.basename(name); | ||
} | ||
promise = hashElementPromise(name, directoryPath, callback); | ||
return promise | ||
.then(function (result) { | ||
if (typeof callback === 'function') return callback(undefined, result); | ||
return result; | ||
}) | ||
.catch(function (reason) { | ||
if (typeof callback === 'function') return callback(reason); | ||
throw reason; | ||
}); | ||
} | ||
@@ -53,3 +63,3 @@ | ||
var filepath = path.join(directoryPath, name); | ||
return Promise(function (resolve, reject, notify) { | ||
return new Promise(function (resolve, reject, notify) { | ||
fs.stat(filepath, function (err, stats) { | ||
@@ -59,3 +69,3 @@ if (err) { | ||
} | ||
if (stats.isDirectory()) { | ||
@@ -76,3 +86,3 @@ resolve(hashFolderPromise(name, directoryPath)); | ||
var folderPath = path.join(directoryPath, foldername); | ||
return Promise(function (resolve, reject, notify) { | ||
return new Promise(function (resolve, reject, notify) { | ||
fs.readdir(folderPath, function (err, files) { | ||
@@ -83,9 +93,9 @@ if (err) { | ||
} | ||
var children = files.map(function (child) { | ||
return hashElementPromise(child, folderPath); | ||
}); | ||
var allChildren = Q.all(children); | ||
var allChildren = Promise.all(children); | ||
return allChildren.then(function (children) { | ||
@@ -101,10 +111,10 @@ var hash = new HashedFolder(foldername, children); | ||
function hashFilePromise(filename, directoryPath) { | ||
return Promise(function (resolve, reject, notify) { | ||
return new Promise(function (resolve, reject, notify) { | ||
try { | ||
var hash = crypto.createHash(algo); | ||
hash.write(filename); | ||
var f = fs.createReadStream(path.join(directoryPath, filename)); | ||
f.pipe(hash, { end: false }); | ||
f.on('end', function () { | ||
@@ -125,3 +135,3 @@ var hashedFile = new HashedFile(filename, hash); | ||
this.children = children; | ||
var hash = crypto.createHash(algo); | ||
@@ -134,3 +144,3 @@ hash.write(name); | ||
}); | ||
this.hash = hash.digest(encoding); | ||
@@ -137,0 +147,0 @@ } |
{ | ||
"name": "folder-hash", | ||
"version": "1.0.3", | ||
"description": "Create a hash checksum over a folder and its content - its children and their content", | ||
"main": "index.js", | ||
"scripts": { | ||
"start": "node sample.js", | ||
"test": "mocha --reporter spec test" | ||
}, | ||
"author": { | ||
"name": "Marc Walter", | ||
"email": "walter.marc@outlook.com" | ||
}, | ||
"license": "MIT", | ||
"repository" : { | ||
"type" : "git", | ||
"url" : "https://github.com/marc136/node-folder-hash.git" | ||
}, | ||
"dependencies": { | ||
"q": "^1.4.1" | ||
}, | ||
"devDependencies": { | ||
"chai": "^3.4.0", | ||
"chai-as-promised": "^5.1.0", | ||
"mocha": "^2.3.3", | ||
"rimraf": "^2.4.3" | ||
} | ||
"name": "folder-hash", | ||
"version": "1.0.4", | ||
"description": "Create a hash checksum over a folder and its content - its children and their content", | ||
"main": "index.js", | ||
"scripts": { | ||
"start": "node sample.js", | ||
"test": "mocha --reporter spec test" | ||
}, | ||
"author": { | ||
"name": "Marc Walter", | ||
"email": "walter.marc@outlook.com" | ||
}, | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/marc136/node-folder-hash.git" | ||
}, | ||
"dependencies": { | ||
"when": "^3.7.7" | ||
}, | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"chai-as-promised": "^5.2.0", | ||
"mocha": "^2.4.5", | ||
"rimraf": "^2.5.2" | ||
} | ||
} |
@@ -28,3 +28,3 @@ | ||
var content1 = 'Hello this is some sample text.\nWith two lines'; | ||
function ignoreExistsError(err) { | ||
@@ -35,3 +35,3 @@ if (err && err.code !== 'EEXIST') { | ||
} | ||
function dummyFolder(basepath) { | ||
@@ -42,3 +42,3 @@ mkdirSync(basepath); | ||
} | ||
return function (done) { | ||
@@ -50,3 +50,3 @@ rmrf(sampleFolder, function () { | ||
dummyFolder(path.join(sampleFolder, 'subfolder1')); | ||
mkdirSync(path.join(sampleFolder, 'f2')); | ||
@@ -56,3 +56,3 @@ writeFileSync(path.join(sampleFolder, 'f2', 'file1'), 'another text'); | ||
dummyFolder(path.join(sampleFolder, 'f2', 'subfolder2')); | ||
mkdirSync(path.join(sampleFolder, 'f3')); | ||
@@ -68,7 +68,7 @@ dummyFolder(path.join(sampleFolder, 'f3', 'subfolder1')) | ||
function ignoreExistError(fn, arg, arg) { | ||
var args = Array.from(arguments); | ||
var args = Array.from ? Array.from(arguments) : Array.prototype.slice.call(arguments) | ||
args.splice(0, 1); | ||
if (typeof fn !== 'function') throw new Error('The first argument must be of type function'); | ||
try { | ||
@@ -75,0 +75,0 @@ var result = fn.apply(null, args); |
| ||
if (typeof Promise === 'undefined') require('when/es6-shim/Promise'); | ||
var folderHash = require('../index'); | ||
@@ -8,3 +9,2 @@ | ||
var path = require('path'); | ||
var Q = require('q'); | ||
@@ -32,3 +32,3 @@ var assert = require('assert'); | ||
}); | ||
describe('when executed with an error-first callback', function () { | ||
@@ -44,3 +44,3 @@ it('with element and folder passed as two strings', function (done) { | ||
}); | ||
it('with element path passed as one string', function (done) { | ||
@@ -61,9 +61,8 @@ folderHash.hashElement(path.join(sampleFolder, 'file1'), function (err, hash) { | ||
var hash1; | ||
before(function (done) { | ||
before(function () { | ||
return folderHash.hashElement('file1', sampleFolder).then(function (hash) { | ||
hash1 = hash; | ||
done(); | ||
}); | ||
}); | ||
it('should return the same hash if a file was not changed', function () { | ||
@@ -74,3 +73,3 @@ return folderHash.hashElement('file1', sampleFolder).then(function (hash2) { | ||
}); | ||
it('should return the same hash if a file has the same name and content, but exists in a different folder', function () { | ||
@@ -81,3 +80,3 @@ return folderHash.hashElement('file1', path.join(sampleFolder, 'subfolder1')).then(function (hash2) { | ||
}); | ||
it('should return a different hash if the file has the same name but a different content', function () { | ||
@@ -103,10 +102,10 @@ return folderHash.hashElement('file1', path.join(sampleFolder, 'f2')).then(function (hash2) { | ||
} | ||
it('generates a hash over the folder name and over the combination hashes of all its children', function () { | ||
return folderHash.hashElement('f2', sampleFolder).then(recAssertHash); | ||
}); | ||
it('generates different hashes if the folders have the same content but different names', function () { | ||
return Q.all([ | ||
folderHash.hashElement('subfolder2', path.join(sampleFolder, 'f2')), | ||
return Promise.all([ | ||
folderHash.hashElement('subfolder2', path.join(sampleFolder, 'f2')), | ||
folderHash.hashElement('subfolder1', sampleFolder) | ||
@@ -118,6 +117,6 @@ ]).then(function (hashes) { | ||
}); | ||
it('generates different hashes if the folders have the same name but different content (one file content changed)', function () { | ||
return Q.all([ | ||
folderHash.hashElement('subfolder1', path.join(sampleFolder, 'f3')), | ||
return Promise.all([ | ||
folderHash.hashElement('subfolder1', path.join(sampleFolder, 'f3')), | ||
folderHash.hashElement('subfolder1', sampleFolder) | ||
@@ -131,4 +130,4 @@ ]).then(function (hashes) { | ||
it('generates the same hash if the folders have the same name and the same content', function () { | ||
return Q.all([ | ||
folderHash.hashElement('subfolder1', path.join(sampleFolder, 'f2')), | ||
return Promise.all([ | ||
folderHash.hashElement('subfolder1', path.join(sampleFolder, 'f2')), | ||
folderHash.hashElement('subfolder1', sampleFolder) | ||
@@ -135,0 +134,0 @@ ]).then(function (hashes) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18208
326
+ Addedwhen@^3.7.7
+ Addedwhen@3.7.8(transitive)
- Removedq@^1.4.1
- Removedq@1.5.1(transitive)