folder-hash
Advanced tools
Comparing version 2.0.0 to 2.1.0
34
index.js
@@ -13,3 +13,4 @@ const crypto = require('crypto'), | ||
matchBasename: true, | ||
matchPath: false | ||
matchPath: false, | ||
ignoreRootName: false | ||
}, | ||
@@ -20,3 +21,4 @@ folders: { | ||
matchBasename: true, | ||
matchPath: false | ||
matchPath: false, | ||
ignoreRootName: false | ||
} | ||
@@ -42,3 +44,3 @@ }; | ||
options.skipMatching = true; | ||
return hashElementPromise(basename, dir, options); | ||
return hashElementPromise(basename, dir, options, true); | ||
}) | ||
@@ -61,8 +63,8 @@ .then(result => { | ||
function hashElementPromise(basename, dirname, options) { | ||
function hashElementPromise(basename, dirname, options, isRootElement = false) { | ||
return stat(path.join(dirname, basename)).then(stats => { | ||
if (stats.isDirectory()) { | ||
return hashFolderPromise(basename, dirname, options); | ||
return hashFolderPromise(basename, dirname, options, isRootElement); | ||
} else if (stats.isFile()) { | ||
return hashFilePromise(basename, dirname, options); | ||
return hashFilePromise(basename, dirname, options, isRootElement); | ||
} else { | ||
@@ -89,3 +91,3 @@ return { | ||
function hashFolderPromise(name, dir, options) { | ||
function hashFolderPromise(name, dir, options, isRootElement = false) { | ||
const folderPath = path.join(dir, name); | ||
@@ -107,3 +109,3 @@ | ||
return Promise.all(children).then(children => { | ||
const hash = new HashedFolder(name, children.filter(notUndefined), options); | ||
const hash = new HashedFolder(name, children.filter(notUndefined), options, isRootElement); | ||
return hash; | ||
@@ -127,3 +129,3 @@ }); | ||
function hashFilePromise(name, dir, options) { | ||
function hashFilePromise(name, dir, options, isRootElement = false) { | ||
const filePath = path.join(dir, name); | ||
@@ -142,3 +144,7 @@ | ||
const hash = crypto.createHash(options.algo); | ||
hash.write(name); | ||
if (isRootElement && options.files.ignoreRootName) { | ||
log.match(`omitted name of ${filePath} from hash`) | ||
} else { | ||
hash.write(name); | ||
} | ||
@@ -183,3 +189,3 @@ const f = fs.createReadStream(filePath); | ||
const HashedFolder = function HashedFolder(name, children, options) { | ||
const HashedFolder = function HashedFolder(name, children, options, isRootElement = false) { | ||
this.name = name; | ||
@@ -189,3 +195,7 @@ this.children = children; | ||
const hash = crypto.createHash(options.algo); | ||
hash.write(name); | ||
if (isRootElement && options.folders.ignoreRootName) { | ||
log.match(`omitted name of folder ${name} from hash`) | ||
} else { | ||
hash.write(name); | ||
} | ||
children.forEach(child => { | ||
@@ -192,0 +202,0 @@ if (child.hash) { |
{ | ||
"name": "folder-hash", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Create a hash checksum over a folder and its content - its children and their content", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,2 +0,4 @@ | ||
Create a hash checksum over a folder or a file. | ||
# folder-hash | ||
Create a hash checksum over a folder or a file. | ||
The hashes are propagated upwards, the hash that is returned for a folder is generated over all the hashes of its children. | ||
@@ -358,3 +360,5 @@ The hashes are generated with the _sha1_ algorithm and returned in _base64_ encoding by default. | ||
The behavior is documented and verified in the unit tests. Execute `npm test` or `mocha test`, and have a look at the _test_ subfolder. | ||
You can also have a look at the [CircleCI report. ![CircleCI](https://circleci.com/gh/marc136/node-folder-hash/tree/master.svg?style=svg)](https://circleci.com/gh/marc136/node-folder-hash/tree/master) | ||
### Creating hashes over files | ||
@@ -361,0 +365,0 @@ **The hashes are the same if:** |
@@ -145,2 +145,15 @@ const { Volume } = require('memfs'), | ||
}); | ||
it('generates the same hash if only the name differs and ignoreRootName is set', async function () { | ||
const hashElement = prep(Volume.fromJSON({ | ||
'abc.txt': 'awesome content', | ||
'def/ghi.js': 'awesome content', | ||
})); | ||
const options = { files: { ignoreRootName: true } }; | ||
const abc = await hashElement('abc.txt', options); | ||
const def = await hashElement('def/ghi.js', options); | ||
abc.hash.should.equal(def.hash); | ||
}); | ||
}); | ||
@@ -376,20 +389,21 @@ | ||
}); | ||
}); | ||
describe('extra', function () { | ||
it('check include', function () { | ||
it('generates the same hash if the folders only differ in name and ignoreRootName is set', async function () { | ||
const hashElement = prep(Volume.fromJSON({ | ||
'one/abc/aa.js': 'aa', | ||
'one/def/aa.js': 'aa', | ||
'one/abc/abc/aa.js': 'aa' | ||
'abc/def/ghi': 'content of ghi', | ||
'abc/file1.js': '//just a comment', | ||
'def/def/ghi': 'content of ghi', | ||
'def/file1.js': '//just a comment', | ||
'def/def/.ignored': 'ignored' | ||
})); | ||
const options = { | ||
folders: { ignoreRootName: true }, | ||
files: { exclude: ['.*'] } | ||
}; | ||
return Promise.all([ | ||
hashElement('one', { folders: { include: ['**\abc'] } }), | ||
hashElement('one', { folders: { include: ['abc'], matchBasename: true } }) | ||
]).then(result => { | ||
//console.log(result.map(r => r.toString()).join('\n')); | ||
should.exist(result[0].hash); | ||
}); | ||
const abc = await hashElement('abc', options); | ||
const def = await hashElement('def', options); | ||
abc.hash.should.equal(def.hash); | ||
}); | ||
}); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
785
391
0
44206
10