Skip to content

Commit

Permalink
chore: merge branch 'feature/clean-progress-output'
Browse files Browse the repository at this point in the history
* feature/clean-progress-output:
  style: lint fixes
  feat: enhance and clean -v output
  fix: job counts (was missing JS + inline styles)
  feat: show status if output redirected
  fix: don't try to encode SVG if there's no body
  • Loading branch information
remy committed Apr 20, 2016
2 parents 072c387 + 93ecac7 commit 8caf2c6
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 16 deletions.
69 changes: 59 additions & 10 deletions cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var readFileSync = require('fs').readFileSync;
var Promise = require('es6-promise').Promise; // jshint ignore:line
var ansi = require('ansi-escapes');

main();

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 jobsEvent(event) {
jobs = event;
update();
});

inliner.on('jobs', function jobs(event) {
console.error(event);
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);
});
}
});

}
}
2 changes: 2 additions & 0 deletions cli/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,7 @@ function options(args) {

argv.useStdin = !process.stdin.isTTY;

argv.verbose = !process.stdout.isTTY;

return argv;
}
5 changes: 3 additions & 2 deletions lib/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ function image(url) {
debug('image loaded: %s', url);

// if the url is SVG, let's compress and use the XML directly
if (mime.lookup(url) === 'image/svg+xml' && !inliner.options.nosvg) {
if (res.body && mime.lookup(url) === 'image/svg+xml' &&
!inliner.options.nosvg) {
return new Promise(function (resolve, reject) {
svgo.optimize(res.body.toString(), function (result) {
debug('optimising svg');
Expand Down Expand Up @@ -48,4 +49,4 @@ function image(url) {
debug('image %s failed to load', url, error);
throw error;
});
}
}
14 changes: 13 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions lib/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ function uglify(source) {
source = source.trim();

if (source === '') {
this.jobs.done.js();
return '';
}

Expand All @@ -25,5 +26,7 @@ function uglify(source) {
result = source;
}

this.jobs.done.js();

return result;
}
2 changes: 2 additions & 0 deletions lib/tasks/js.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ function resolve(inliner, todo, $) {
if (isMinified && !inliner.options.inlinemin) {
debug('skipping pre-minified script');
inliner.emit('progress', 'skipping minified script ' + src);
inliner.jobs.done.js();
// ignore scripts with .min. in them - i.e. avoid minify
// scripts that are already minifed
return false;
} else if (src.indexOf('google-analytics') !== -1) {
debug('skipping analytics');
inliner.emit('progress', 'skipping analytics script');
inliner.jobs.done.js();
// ignore analytics
return false;
} else if (inliner.options.skipAbsoluteUrls &&
Expand Down
3 changes: 2 additions & 1 deletion lib/tasks/style-attrs.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ function resolve(inliner, todo, $) {
return inliner.cssImports(inliner.url, css)
.then(inliner.cssImages.bind(inliner, inliner.url))
.then(function then(css) {
inliner.jobs.done['style-attrs']();
$(style).attr('style', css);
});
});
}
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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": {
Expand Down

0 comments on commit 8caf2c6

Please sign in to comment.