feat: enhance and clean -v output

This commit is contained in:
Remy Sharp 2016-04-19 11:10:19 +01:00
parent bd0b7c1be3
commit 32b9ba57ad
4 changed files with 78 additions and 14 deletions

View File

@ -2,6 +2,7 @@
var readFileSync = require('fs').readFileSync;
var Promise = require('es6-promise').Promise; // jshint ignore:line
var ansi = require('ansi-escapes');
main();
@ -48,6 +49,7 @@ function main() {
});
}
var time = process.hrtime();
p.then(function (source) {
var inliner = new Inliner(source, argv, function result(error, html) {
if (error) {
@ -76,19 +78,66 @@ function main() {
pkg: defaults(pkg, { version: '0.0.0' }),
}).notify();
inliner.on('warning', function progress(event) {
console.warn('warning: ' + event);
});
if (argv.verbose) {
inliner.on('progress', function progress(event) {
console.error(event);
var styles = require('ansi-styles');
console.warn(ansi.cursorHide + '\n\n' + ansi.cursorUp() +
ansi.cursorSavePosition);
var jobs = {};
var progress = '';
var update = function () {
var remaining = jobs.breakdown.join(', ');
if (remaining) {
remaining = ' remaining: ' + remaining;
}
var str = styles.green.open +
(100 - (jobs.todo / jobs.total * 100 | 0)) + '%' +
styles.green.close +
remaining +
styles.gray.open +
'\nLast job: ' + progress +
styles.gray.close;
process.stderr.write(
ansi.cursorRestorePosition +
ansi.cursorLeft +
ansi.eraseLines(2) +
str.trim() + '\n');
};
inliner.on('progress', function progressEvent(event) {
progress = event;
update();
});
inliner.on('jobs', function jobs(event) {
console.error(event);
inliner.on('jobs', function jobsEvent(event) {
jobs = event;
update();
});
inliner.on('warning', function warningEvent(event) {
progress = event;
update();
});
inliner.on('end', function () {
var diff = process.hrtime(time);
process.stderr.write(styles.green.open + 'Time: ' + diff[0] + 's ' +
(diff[1]/1e6).toFixed(3) + 'ms\n' + styles.green.close);
process.stderr.write(ansi.cursorShow);
});
'exit SIGINT SIGTERM'.split(' ').map(function (event) {
process.once(event, function () {
process.stderr.write(ansi.cursorShow); // put the cursor back
try { process.kill(process.pid, event); } catch (e) {}
});
});
} else {
inliner.on('warning', function progress(event) {
console.warn('warning: ' + event);
});
}
});
}
}

View File

@ -49,4 +49,4 @@ function image(url) {
debug('image %s failed to load', url, error);
throw error;
});
}
}

View File

@ -311,6 +311,7 @@ function main() {
})
.then(function then(html) {
inliner.callback(null, html);
inliner.emit('end');
})
.catch(function errHandler(error) {
debug('fail', error.stack);
@ -371,7 +372,18 @@ function updateTodo() {
return acc + jobs.breakdown[key];
}, 0);
this.emit('jobs', (jobs.total - jobs.todo) + '/' + jobs.total);
var breakdown = Object.keys(jobs.breakdown).map(function (key) {
if (jobs.breakdown[key]) {
return key + '(' + jobs.breakdown[key] + ')';
}
return false;
}).filter(Boolean);
this.emit('jobs', {
total: jobs.total,
todo: jobs.todo,
breakdown: breakdown
});
}
function addJob(type) {

View File

@ -27,6 +27,9 @@
},
"author": "Remy Sharp",
"dependencies": {
"ansi-escapes": "^1.4.0",
"ansi-styles": "^2.2.1",
"chalk": "^1.1.3",
"charset": "^1.0.0",
"cheerio": "^0.19.0",
"debug": "^2.2.0",
@ -38,10 +41,10 @@
"lodash.foreach": "^3.0.3",
"mime": "^1.3.4",
"minimist": "^1.1.3",
"request": "^2.60.0",
"request": "^2.72.0",
"svgo": "^0.5.6",
"then-fs": "^2.0.0",
"uglify-js": "^2.4.24",
"uglify-js": "^2.6.2",
"update-notifier": "^0.5.0"
},
"repository": {