forked from ben-eb/gulp-bytediff
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
52 lines (45 loc) · 1.6 KB
/
index.js
File metadata and controls
52 lines (45 loc) · 1.6 KB
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
const colors = require("ansi-colors");
const filesize = require("filesize");
const log = require("fancy-log");
const mapstream = require("map-stream");
const path = require("path");
function defaultFormatter(data) {
const saved = data.savings > 0 ? " saved " : " gained ";
const color = data.savings > 0 ? "green" : "yellow";
const start = filesize(data.startSize);
const end = colors[color](filesize(data.endSize));
const report = ` (${start} -> ${end})`;
const filename = colors.magenta(data.fileName);
return filename + saved + filesize(Math.abs(data.savings)) + report;
}
const bytediff = {
start() {
return mapstream((file, cb) => {
// Persist the original size of the file for later
file.bytediff = {
startSize: file.contents ? file.contents.length : null
};
cb(null, file);
});
},
stop(formatter) {
return mapstream((file, cb) => {
if (typeof formatter !== "function") {
formatter = defaultFormatter;
}
if (file.bytediff.startSize) {
const endSize = file.contents.length;
const message = formatter({
fileName: path.basename(file.path),
startSize: file.bytediff.startSize,
endSize: endSize,
savings: file.bytediff.startSize - endSize,
percent: endSize / file.bytediff.startSize
});
log(message);
}
cb(null, file);
});
}
};
module.exports = bytediff;