From 6a1dec4f4c9898e1223e968353f1047a8ce06312 Mon Sep 17 00:00:00 2001 From: Remy Sharp Date: Sun, 31 Jul 2016 12:59:08 +0100 Subject: [PATCH] fix: better reporting of progress Also fixed left over count on styles --- .travis.yml | 1 + cli/index.js | 44 ++++++-------------------------- cli/progress.js | 61 ++++++++++++++++++++++++++++++++++++++++++++ lib/css.js | 3 ++- lib/get.js | 9 ++++--- lib/image.js | 3 ++- lib/index.js | 3 --- lib/tasks/favicon.js | 3 ++- lib/tasks/js.js | 3 ++- lib/tasks/links.js | 3 ++- lib/tasks/styles.js | 1 + 11 files changed, 87 insertions(+), 47 deletions(-) create mode 100644 cli/progress.js diff --git a/.travis.yml b/.travis.yml index f52eded..bab8e06 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,7 @@ cache: notifications: email: false node_js: + - 6 - 4 - '0.12' - '0.10' diff --git a/cli/index.js b/cli/index.js index 790abdb..42dd1ba 100755 --- a/cli/index.js +++ b/cli/index.js @@ -78,58 +78,30 @@ function main() { }).notify(); if (argv.verbose) { - var styles = require('ansi-styles'); - console.warn(ansi.cursorHide + '\n\n' + ansi.cursorUp() + - ansi.cursorSavePosition); - var jobs = {}; + var update = require('./progress'); 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; - - if (argv.debug) { - process.stderr.write( - str.trim() + '\n'); - } else { - process.stderr.write( - ansi.cursorRestorePosition + - ansi.cursorLeft + - ansi.eraseLines(2) + - str.trim() + '\n'); - } - }; inliner.on('progress', function progressEvent(event) { progress = event; - update(); + // console.log(JSON.stringify({ type: 'progress', progress, jobs })); + update(event, jobs, argv.debug); }); inliner.on('jobs', function jobsEvent(event) { jobs = event; - update(); + // console.log(JSON.stringify({ type: 'jobs', progress, jobs })); + update(progress, jobs, argv.debug); }); inliner.on('warning', function warningEvent(event) { progress = event; - update(); + // console.log(JSON.stringify({ type: 'warning', progress, jobs })); + update(event, jobs, true); }); 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); + update.end(time); }); 'exit SIGINT SIGTERM'.split(' ').map(function (event) { diff --git a/cli/progress.js b/cli/progress.js new file mode 100644 index 0000000..e41bc6b --- /dev/null +++ b/cli/progress.js @@ -0,0 +1,61 @@ +var styles = require('ansi-styles'); +var ansi = require('ansi-escapes'); + +console.warn(ansi.cursorHide + '\n\n' + ansi.cursorUp() + + ansi.cursorSavePosition); + +module.exports = function (progress, jobs, keep) { + if (keep) { + process.stderr.write( + ansi.cursorRestorePosition + + ansi.cursorLeft + + ansi.eraseLines(2) + + styles.red.open + '‣ ' + styles.red.close + + styles.gray.open + progress + styles.gray.close + + '\n\n' + + ansi.cursorSavePosition + + '\n\n' + ); + } + + var remaining = jobs.breakdown.join(', '); + if (remaining) { + remaining = ' remaining: ' + remaining; + } + + var t = jobs.todo / jobs.total * 100; + if (t < 1) { + t = 0; + } else { + t = t | 0; + } + + var str = styles.green.open + + (100 - t) + '%' + + 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'); +}; + +module.exports.end = function (time) { + 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); +}; + +// tidy up cursor +'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) {} + }); +}); diff --git a/lib/css.js b/lib/css.js index 360e6ed..ca669a0 100644 --- a/lib/css.js +++ b/lib/css.js @@ -7,6 +7,7 @@ module.exports = { var Promise = require('es6-promise').Promise; // jshint ignore:line var debug = require('debug')('inliner'); +var parse = require('path').parse; function getImages(root, css) { var inliner = this; @@ -62,7 +63,7 @@ function getImports(root, css) { return inliner.get(resolvedURL).then(function then(res) { var importedCSS = res.body; inliner.jobs.done.links(); - inliner.emit('progress', 'import ' + resolvedURL); + inliner.emit('progress', 'import ' + parse(resolvedURL).base); if (url.length > 1) { url.shift(); importedCSS = '@media ' + url.join(' ') + '{' + importedCSS + '}'; diff --git a/lib/get.js b/lib/get.js index 60769e6..bb26b38 100644 --- a/lib/get.js +++ b/lib/get.js @@ -4,6 +4,7 @@ var debug = require('debug')('inliner'); var Promise = require('es6-promise').Promise; // jshint ignore:line var fs = require('then-fs'); var mime = require('mime'); +var parse = require('path').parse; var cache = {}; @@ -24,7 +25,9 @@ module.exports = function get(url, options) { return cache[url]; } - this.emit('progress', 'loading ' + url); + var base = parse(url).base; + + this.emit('progress', 'loading ' + base); if (this.isFile && url.indexOf('http') !== 0) { debug('inliner.get file: %s', url); @@ -37,7 +40,7 @@ module.exports = function get(url, options) { }; }).catch(function (error) { if (error.code === 'ENOENT') { - inliner.emit('warning', 'no such file: ' + url); + inliner.emit('warning', 'no such file: ' + base); } return { @@ -70,7 +73,7 @@ module.exports = function get(url, options) { debug('response: %s %s', res.statusCode, url); if (res.statusCode !== 200) { - inliner.emit('warning', res.statusCode + ' on ' + url); + inliner.emit('warning', res.statusCode + ' on ' + base); } if (res.statusCode >= 400) { diff --git a/lib/image.js b/lib/image.js index 248a8c5..feb7ab3 100644 --- a/lib/image.js +++ b/lib/image.js @@ -4,12 +4,13 @@ var debug = require('debug')('inliner'); var mime = require('mime'); var SVGO = require('svgo'); var svgo = new SVGO(); +var parse = require('path').parse; var Promise = require('es6-promise').Promise; // jshint ignore:line function image(url) { url = url.replace(/\??#.*$/, ''); var inliner = this; - this.emit('progress', 'get image ' + url); + this.emit('progress', 'get image ' + parse(url).base); return this.get(url, { encoding: 'binary' }).then(function then(res) { if (url.indexOf('data:') === 0) { return url; diff --git a/lib/index.js b/lib/index.js index 29b64d1..309845b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -294,12 +294,9 @@ function main() { inliner.jobs.add(key, todo.length); var tasks = taskRunner[key](inliner, todo.get(), $); - // console.log(key, typeof tasks); - // console.log('----------------'); promises = promises.concat(tasks); }); - return Promise.all(promises).then(function then() { var html = ''; inliner.removeComments($(':root')[0], $); diff --git a/lib/tasks/favicon.js b/lib/tasks/favicon.js index 489a724..4cd608e 100644 --- a/lib/tasks/favicon.js +++ b/lib/tasks/favicon.js @@ -1,11 +1,12 @@ module.exports = resolve; var debug = require('debug')('inliner'); +var parse = require('path').parse; function resolve(inliner, todo, $) { debug('start %s favicon', todo.length); return todo.map(function links(link) { - inliner.emit('progress', 'processing favicon ' + url); var url = inliner.resolve(inliner.url, $(link).attr('href')); + inliner.emit('progress', 'processing favicon ' + parse(url).base); return inliner.image(url).then(function then(dataURL) { $(link).attr('href', dataURL); }).then(inliner.jobs.done.favicon); diff --git a/lib/tasks/js.js b/lib/tasks/js.js index 0048c9d..c53a6fa 100644 --- a/lib/tasks/js.js +++ b/lib/tasks/js.js @@ -2,6 +2,7 @@ module.exports = resolve; var Promise = require('es6-promise').Promise; // jshint ignore:line var debug = require('debug')('inliner'); +var parse = require('path').parse; function resolve(inliner, todo, $) { debug('start %s scripts', todo.length); @@ -23,7 +24,7 @@ function resolve(inliner, todo, $) { isMinified = src.indexOf('.min.') !== -1; if (isMinified && !inliner.options.inlinemin) { debug('skipping pre-minified script'); - inliner.emit('progress', 'skipping minified script ' + src); + inliner.emit('progress', 'skipping minified script ' + parse(src).base); inliner.jobs.done.js(); // ignore scripts with .min. in them - i.e. avoid minify // scripts that are already minifed diff --git a/lib/tasks/links.js b/lib/tasks/links.js index 76dae86..238304c 100644 --- a/lib/tasks/links.js +++ b/lib/tasks/links.js @@ -1,5 +1,6 @@ module.exports = resolve; var debug = require('debug')('inliner'); +var parse = require('path').parse; function resolve(inliner, todo, $) { debug('start %s links', todo.length); @@ -14,7 +15,7 @@ function resolve(inliner, todo, $) { inliner.emit('progress', 'skipping remote links'); return false; } - inliner.emit('progress', 'processing external css ' + url); + inliner.emit('progress', 'processing external css ' + parse(url).base); return inliner.get(url).then(function then(res) { var css = res.body; inliner.jobs.done.links(); diff --git a/lib/tasks/styles.js b/lib/tasks/styles.js index 8596264..66f7169 100644 --- a/lib/tasks/styles.js +++ b/lib/tasks/styles.js @@ -10,6 +10,7 @@ function resolve(inliner, todo, $) { .then(inliner.cssImages.bind(inliner, inliner.url)) .then(function then(css) { $(style).text(css); + inliner.jobs.done.styles(); }); }); }