Skip to content

Instantly share code, notes, and snippets.

@wuchengwei
Created September 9, 2016 04:47
Show Gist options
  • Save wuchengwei/66b5e199c61c491b94fd32ad566a6e14 to your computer and use it in GitHub Desktop.
Save wuchengwei/66b5e199c61c491b94fd32ad566a6e14 to your computer and use it in GitHub Desktop.
traverse directory and shasum
var fs = require('fs');
var path = require('path');
var crypto = require('crypto');
function walk(dir, done) {
var results = [];
fs.readdir(dir, function(err, list) {
if (err) return done(err);
var pending = list.length;
if (!pending) return done(null, results);
list.forEach(function(file) {
file = path.resolve(dir, file);
fs.stat(file, function(err, stat) {
if (stat && stat.isDirectory()) {
walk(file, function(err, res) {
results = results.concat(res);
if (!--pending) done(null, results);
});
} else {
results.push(path.relative(rootdir, file));
if (!--pending) done(null, results);
}
});
});
});
}
function shasum(file) {
var fileContent = fs.readFileSync(file);
var sum = crypto.createHash('sha1');
sum.update(fileContent);
return sum.digest('hex');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment