-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (64 loc) · 1.67 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
var crypto = require('crypto');
var fs = require('fs');
var path = require('path');
var Transform = require('stream').Transform;
var Vinyl = require('vinyl');
module.exports = function(bundles, rename) {
var result = {};
return new Transform({
objectMode: true,
highWaterMark: 16,
transform(file, enc, cb) {
var f = path.parse(file.path);
if (f.ext === '.txt') {
result[f.name] = file.contents.toString();
}
else if ((a = f.base.split('-')).length > 1) {
if (result[a = a[0] + f.ext]) {
fs.unlinkSync(file.path);
}
else {
result[a] = f.base;
}
}
else {
var a = f.name + '-' +
crypto
.createHash('md5')
.update(file.contents)
.digest('hex')
.slice(0, 12) + f.ext;
if (rename) {
if (result[f.base] === a) {
fs.unlinkSync(file.path);
}
else {
if (result[f.base]) {
fs.unlinkSync(path.join(f.dir, result[f.base]));
}
fs.renameSync(file.path, path.join(f.dir, a));
}
}
result[f.base] = a;
}
cb();
},
flush(cb) {
if (Object.keys(result).length > 0) {
bundles.forEach(k => {
var o = Object.assign({ starter: k + '.js' }, result);
bundles.forEach(n => {
if (k !== n) {
delete o[n + '.js'];
}
});
this.push(new Vinyl({
path: k + '.json',
contents: Buffer.from(JSON.stringify(o)),
}));
});
}
cb();
},
});
};