-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulp-cache.ts
138 lines (115 loc) · 3.5 KB
/
gulp-cache.ts
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { fs, jsonParseWithCircularRefs, jsonStringifyWithCircularRefs, path, writefile } from 'sbg-utility';
import crypto from 'crypto';
import { Readable, Stream, Transform } from 'stream';
import through2 from 'through2';
let ONEXIT: (value: string, index?: number, array?: string[]) => void;
interface Opt {
/**
* cache file location
*/
cacheFile: string;
}
class cache {
cache: any = {};
options: Opt;
changes = false;
constructor(options?: Opt) {
this.options = Object.assign(
{
cacheFile: __dirname + '/tmp/gulpCache'
},
options || {}
);
// Load the cache file if any, synchronously.
if (fs.existsSync(this.options.cacheFile)) this.fromFile(this.options.cacheFile);
}
bindOnExit() {
ONEXIT = function (event: string) {
process.on(event, this.onexit);
}.bind(this);
// Save the cache on exits and ctrl+c
['exit', 'SIGINT'].forEach(ONEXIT);
}
start() {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const theClass = this;
return through2.obj(function (file, _enc, callback) {
const self = this;
if (file.isNull() || file.isDirectory()) return callback(null, file);
// Ensure we have a stream
const fileStream = through2();
// Pipe in the contents
if (file.isStream()) {
file.contents.pipe(fileStream);
} else if (file.isBuffer()) {
Readable.from(file.contents).pipe(fileStream);
}
// Test if the file has changed (md5 regardless)
theClass.changed(file.path, fileStream, function (err, changed) {
// console.log({ err, changed });
if (changed) return callback(err, file);
self.emit('cached', file.path);
self.push(file);
callback(err);
});
});
}
changed(name: string, stream: Transform, callback: (...args: any[]) => any) {
const run = function (err: any, hash: any) {
// console.log({ err, hash });
if (err) return callback(err);
// Get the old hash
const currentHash = this.cache[name];
// Update the hash
this.cache[name] = hash;
// console.log(currentHash, hash, !currentHash || currentHash !== hash);
// Compare
if (!currentHash || currentHash !== hash) {
this.changes = true;
callback(null, true);
} else {
callback(null, false);
}
};
this.md5(stream, run.bind(this));
}
toFile(file: string) {
writefile(file, jsonStringifyWithCircularRefs(this.cache));
}
fromFile(file: string) {
this.cache = jsonParseWithCircularRefs(fs.readFileSync(file, 'utf-8'));
}
md5(stream: Stream, callback: (...args: any[]) => any) {
const hash = crypto.createHash('md5');
hash.setEncoding('hex');
stream.on('error', callback).on('end', function () {
hash.end();
callback(null, hash.read());
});
stream.pipe(hash);
}
save() {
// console.log('onExit', this.changes);
if (this.changes) {
try {
this.toFile(this.options.cacheFile);
} catch (err) {
console.warn('Unable to save cache file to %s.', this.options.cacheFile);
if (err.code === 'ENOENT')
console.warn('The directory %s does not exist.', path.dirname(this.options.cacheFile));
}
}
}
pipeOnExit() {
const self = this;
return through2.obj((vinyl, _enc, cb) => {
self.save();
cb(null, vinyl);
});
}
}
export default cache;
export function gulpCache() {
const c = new cache();
return c.start().on('end', c.save);
}