From a23f07269d68f603f4f71d33a493b2ab7ab4d78f Mon Sep 17 00:00:00 2001 From: ddukstas Date: Fri, 6 Mar 2015 14:25:11 +0200 Subject: [PATCH] added new param to specify folders which will be excluded from calculation of checksum of root directory --- lib/dirsum.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/dirsum.js b/lib/dirsum.js index 33e98dd..2ed1f07 100644 --- a/lib/dirsum.js +++ b/lib/dirsum.js @@ -24,16 +24,24 @@ function _summarize(method, hashes) { return obj; } -function digest(root, method, callback) { +function digest(root, method, exclude, callback) { if (!root || typeof(root) !== 'string') { throw new TypeError('root is required (string)'); } + if (method) { if (typeof(method) === 'string') { // NO-OP + if(typeof(exclude) === 'function') { + callback = exclude; + exclude = []; + }else if(typeof(exclude) !== 'object'){ + throw new TypeError('exclude must be an object'); + } } else if (typeof(method) === 'function') { callback = method; method = 'md5'; + exclude = []; } else { throw new TypeError('hash must be a string'); } @@ -49,12 +57,20 @@ function digest(root, method, callback) { fs.readdir(root, function(err, files) { if (err) return callback(err); + exclude.forEach(function(folder){ + var index = files.indexOf(folder); + if(index != -1){ + files.splice(index, 1); + } + }); + if (files.length === 0) { return callback(undefined, {hash: '', files: {}}); } - + var hashed = 0; files.forEach(function(f) { + var path = root + '/' + f; fs.stat(path, function(err, stats) { if (err) return callback(err);