forked from jsdf/only-if-changed-webpack-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
digest.js
54 lines (45 loc) · 1.29 KB
/
digest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
var fs = require('fs');
var crypto = require('crypto');
var async = require('async');
function digestMD5(content) {
var md5sum = crypto.createHash('md5');
md5sum.update(content);
return md5sum.digest('hex');
}
function digestSHA1(content) {
var sha1sum = crypto.createHash('sha1');
sha1sum.update(content);
return sha1sum.digest('hex');
}
function getFilesChanges(filesHashes, concurrencyLimit, done) {
var changed = [];
var deleted = [];
var files = Object.keys(filesHashes);
function eachFile(file, fileDone) {
fs.readFile(file, function(err, contents) {
if (err) {
deleted.push(file);
return fileDone();
}
var hashNew = digestMD5(contents);
if (!(filesHashes[file] && hashNew && filesHashes[file] === hashNew)) {
changed.push(file);
}
fileDone();
});
}
async.eachLimit(files, concurrencyLimit, eachFile, function() {
done(deleted, changed);
});
}
function hasAnyFileChanged(filesHashes, concurrencyLimit, done) {
getFilesChanges(filesHashes, concurrencyLimit, function(deleted, changed) {
var numFilesChanged = deleted.length + changed.length;
done(null, numFilesChanged > 0);
});
}
module.exports = {
digestSHA1: digestSHA1,
digestMD5: digestMD5,
hasAnyFileChanged: hasAnyFileChanged,
};