mirror of
https://github.com/remy/inliner.git
synced 2024-08-16 11:50:32 +03:00
fix: better reporting of progress
Also fixed left over count on styles
This commit is contained in:
parent
2f6cfc15be
commit
6a1dec4f4c
@ -6,6 +6,7 @@ cache:
|
||||
notifications:
|
||||
email: false
|
||||
node_js:
|
||||
- 6
|
||||
- 4
|
||||
- '0.12'
|
||||
- '0.10'
|
||||
|
44
cli/index.js
44
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) {
|
||||
|
61
cli/progress.js
Normal file
61
cli/progress.js
Normal file
@ -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) {}
|
||||
});
|
||||
});
|
@ -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 + '}';
|
||||
|
@ -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) {
|
||||
|
@ -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;
|
||||
|
@ -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], $);
|
||||
|
@ -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);
|
||||
|
@ -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
|
||||
|
@ -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();
|
||||
|
@ -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();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user