1 | var fs = require('fs')
|
2 | var crypto = require('crypto')
|
3 | var unyield = require('unyield')
|
4 | var thunkify = require('thunkify')
|
5 | var join = require('path').join
|
6 |
|
7 | var readFile = thunkify(fs.readFile)
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | exports.hashFile = unyield(function * (fname, digest) {
|
16 | var shasum = crypto.createHash(digest || 'sha1')
|
17 |
|
18 | try {
|
19 | var data = yield readFile(fname)
|
20 | } catch (e) {
|
21 | return null
|
22 | }
|
23 |
|
24 | shasum.update(data)
|
25 | return shasum.digest('hex')
|
26 | })
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | exports.hashFiles = unyield(function * (root, paths) {
|
33 | var hashes = yield paths.map(function (path) {
|
34 | return exports.hashFile(join(root, path))
|
35 | })
|
36 |
|
37 | var result = {}
|
38 |
|
39 | paths.forEach(function (path, idx) {
|
40 | result[path] = hashes[idx]
|
41 | })
|
42 |
|
43 | return result
|
44 | })
|