-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (45 loc) · 1.38 KB
/
index.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
55
var crypto = require('crypto'),
fs = require('fs'),
path = require('path');
var through = require('through2');
var format = require('./format');
function getMd5(p) {
var str = fs.readFileSync(p, 'utf-8');
var md5um = crypto.createHash('md5');
md5um.update(str);
return md5um.digest('hex');
}
var cacheFile = function(name, opt) {
var lastConfig;
try {
var configBuffer = fs.readFileSync("./crypto.config");
lastConfig = JSON.parse(configBuffer);
} catch (e) {
console.log(e);
}
lastConfig = lastConfig || {};
var newConfig = {};
var stream = through.obj(function(file, enc, callback) {
var contents = file.checksum;
if (!contents) {
if (file.isStream()) {
this.push(file);
return callback();
}
if (file.isBuffer()) {
contents = file.contents.toString('utf8');
newConfig[file.path] = getMd5(file.path);
}
if (lastConfig[file.path] == newConfig[file.path]) {
callback();
} else {
console.log('not hit! ' + file.path);
this.push(file);
callback();
}
}
fs.writeFileSync("./crypto.config", format(JSON.stringify(newConfig)), 'utf8');
});
return stream;
};
module.exports = cacheFile;