New major version of folder-hash (2.0)
2018-04-14
A while ago I released a library that creates checksums over folders and files for node.js. Now I prepared a new major version, which adds the possibility to specify more sophisticated include and exclude rules. I also added CircleCI configuration
Example usage:
const { hashElement } = require('folder-hash');
const options = {
folders: { exclude: ['.*', 'node_modules', 'test_coverage'] },
files: { include: ['*.js', '*.json'] }
};
console.log('Creating a hash over the current folder:');
hashElement('.', options)
.then(hash => {
console.log(hash.toString());
})
.catch(error => {
console.error('hashing failed: ', error);
});
This results in the following output when run on the folder of the library:
Creating a hash over the current folder:
{ name: '.', hash: 'YZOrKDx9LCLd8X39PoFTflXGpRU=,'
children: [
{ name: 'examples', hash: 'aG8wg8np5SGddTnw1ex74PC9EnM=,'
children: [
{ name: 'readme-example1.js', hash: 'Xlw8S2iomJWbxOJmmDBnKcauyQ8=' }
{ name: 'readme-with-callbacks.js', hash: 'ybvTHLCQBvWHeKZtGYZK7+6VPUw=' }
{ name: 'readme-with-promises.js', hash: '43i9tE0kSFyJYd9J2O0nkKC+tmI=' }
{ name: 'sample.js', hash: 'PRTD9nsZw3l73O/w5B2FH2qniFk=' }
]}
{ name: 'index.js', hash: 'kQQWXdgKuGfBf7ND3rxjThTLVNA=' }
{ name: 'package.json', hash: 'w7F0S11l6VefDknvmIy8jmKx+Ng=' }
{ name: 'test', hash: 'H5x0JDoV7dEGxI65e8IsencDZ1A=,'
children: [
{ name: 'parameters.js', hash: '3gCEobqzHGzQiHmCDe5yX8weq7M=' }
{ name: 'test.js', hash: 'kg7p8lbaVf1CPtWLAIvkHkdu1oo=' }
]}
]}
Default options
The options object defaults to the following values if any are omitted:
{
algo: 'sha1', // see crypto.getHashes() for options
encoding: 'base64', // 'base64', 'hex' or 'binary'
files: {
exclude: [],
include: [],
matchBasename: true,
matchPath: false
},
folders: {
exclude: [],
include: [],
matchBasename: true,
matchPath: false
}
}
Migrating from 1.x
If you did not specify any excludes rules, you can use the library just like before.
If you for example had code like this:
var hasher = require('folder-hash');
var options = { excludes: ['.*'], match: { basename: true, path: false } };
hasher.hashElement(__dirname, options)
.then(function (hash) {
console.log('Result for folder "' + __dirname + '":');
console.log(hash.toString());
})
.catch(function (error) {
return console.error('hashing failed:', error);
});
You will get the same output if you change the options object to
options = {
files: { exclude: ['.*'], matchBasename: true, matchPath: false },
folders: { exclude: ['.*'], matchBasename: true, matchPath: false }
};
Test coverage
Execute yarn cover
or npm run cover --silent
:
=============================== Coverage summary ===============================
Statements : 96.4% ( 134/139 )
Branches : 93.65% ( 59/63 )
Functions : 100% ( 18/18 )
Lines : 96.38% ( 133/138 )
================================================================================