Created
September 9, 2016 04:47
-
-
Save wuchengwei/66b5e199c61c491b94fd32ad566a6e14 to your computer and use it in GitHub Desktop.
traverse directory and shasum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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