Skip to content

Commit 53b5621

Browse files
committed
Show diff against master when running stats
1 parent d92ef7f commit 53b5621

File tree

2 files changed

+75
-35
lines changed

2 files changed

+75
-35
lines changed

Gruntfile.js

+71-32
Original file line numberDiff line numberDiff line change
@@ -127,41 +127,80 @@ module.exports = function(grunt) {
127127
});
128128

129129

130+
var Promise = require("bluebird");
130131
var exec = require('child_process').exec;
131132

133+
function execp(cmd) {
134+
var resolve, reject;
135+
var promise = new Promise(function(_resolve, _reject) {
136+
resolve = _resolve;
137+
reject = _reject;
138+
});
139+
try {
140+
exec(cmd, function (error, out) {
141+
if (error) {
142+
reject(error);
143+
} else {
144+
resolve(out);
145+
}
146+
});
147+
} catch (error) {
148+
reject(error);
149+
}
150+
return promise;
151+
}
152+
132153
grunt.registerTask('stats', function () {
133-
var done = this.async();
134-
exec('cat dist/immutable.js | wc -c', function (error, out) {
135-
if (error) throw new Error(error);
136-
var rawBytes = parseInt(out);
137-
console.log(' Concatenated: ' +
138-
(rawBytes + ' bytes').cyan);
139-
exec('gzip -c dist/immutable.js | wc -c', function (error, out) {
140-
if (error) throw new Error(error);
141-
var zippedBytes = parseInt(out);
142-
var pctOfA = Math.floor(10000 * (1 - (zippedBytes / rawBytes))) / 100;
143-
console.log(' Compressed: ' +
144-
(zippedBytes + ' bytes').cyan + ' ' +
145-
(pctOfA + '%').green);
146-
exec('cat dist/immutable.min.js | wc -c', function (error, out) {
147-
if (error) throw new Error(error);
148-
var minifiedBytes = parseInt(out);
149-
var pctOfA = Math.floor(10000 * (1 - (minifiedBytes / rawBytes))) / 100;
150-
console.log(' Minified: ' +
151-
(minifiedBytes + ' bytes').cyan + ' ' +
152-
(pctOfA + '%').green);
153-
exec('gzip -c dist/immutable.min.js | wc -c', function (error, out) {
154-
if (error) throw new Error(error);
155-
var zippedMinBytes = parseInt(out);
156-
var pctOfA = Math.floor(10000 * (1 - (zippedMinBytes / rawBytes))) / 100;
157-
console.log(' Min\'d & Cmprs\'d: ' +
158-
(zippedMinBytes + ' bytes').cyan + ' ' +
159-
(pctOfA + '%').green);
160-
done();
161-
})
162-
})
163-
})
164-
})
154+
Promise.all([
155+
execp('cat dist/immutable.js | wc -c'),
156+
execp('git show master:dist/immutable.js | wc -c'),
157+
execp('cat dist/immutable.min.js | wc -c'),
158+
execp('git show master:dist/immutable.min.js | wc -c'),
159+
execp('cat dist/immutable.min.js | gzip -c | wc -c'),
160+
execp('git show master:dist/immutable.min.js | gzip -c | wc -c'),
161+
]).then(function (results) {
162+
return results.map(function (result) { return parseInt(result); });
163+
}).then(function (results) {
164+
var rawNew = results[0];
165+
var rawOld = results[1];
166+
var minNew = results[2];
167+
var minOld = results[3];
168+
var zipNew = results[4];
169+
var zipOld = results[5];
170+
171+
function space(n, s) {
172+
return Array(Math.max(0, 10 + n - (s||'').length)).join(' ') + (s||'');
173+
}
174+
175+
function bytes(b) {
176+
return b.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',') + ' bytes';
177+
}
178+
179+
function diff(n, o) {
180+
var d = n - o;
181+
return d === 0 ? '' : d < 0 ? (' ' + bytes(d)).green : (' +' + bytes(d)).red;
182+
}
183+
184+
function pct(s, b) {
185+
var p = Math.floor(10000 * (1 - (s / b))) / 100;
186+
return (' ' + p + '%').grey;
187+
}
188+
189+
console.log(' Raw: ' +
190+
space(14, bytes(rawNew).cyan) + ' ' + space(15, diff(rawNew, rawOld))
191+
);
192+
console.log(' Min: ' +
193+
space(14, bytes(minNew).cyan) + pct(minNew, rawNew) + space(15, diff(minNew, minOld))
194+
);
195+
console.log(' Zip: ' +
196+
space(14, bytes(zipNew).cyan) + pct(zipNew, rawNew) + space(15, diff(zipNew, zipOld))
197+
);
198+
199+
}).then(this.async()).catch(function (error) {
200+
setTimeout(function () {
201+
throw error;
202+
}, 0);
203+
});
165204
});
166205

167206
grunt.registerTask('init_ts_compiler', function () {

package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
},
2929
"devDependencies": {
3030
"acorn": "^0.11.0",
31+
"bluebird": "^2.7.1",
32+
"es6-transpiler": "^0.7.18",
3133
"esperanto": "^0.6.0",
3234
"estraverse": "^1.9.1",
3335
"grunt": "^0.4.5",
@@ -40,8 +42,7 @@
4042
"magic-string": "^0.2.6",
4143
"react-tools": "^0.11.1",
4244
"ts-compiler": "^2.0.0",
43-
"uglify-js": "^2.4.15",
44-
"es6-transpiler": "^0.7.18"
45+
"uglify-js": "^2.4.15"
4546
},
4647
"engines": {
4748
"node": ">=0.8.0"
@@ -66,4 +67,4 @@
6667
"iteration"
6768
],
6869
"license": "BSD"
69-
}
70+
}

0 commit comments

Comments
 (0)