inline-critical/cli.js

138 lines
3.2 KiB
JavaScript
Raw Normal View History

2015-06-25 06:27:18 +02:00
#!/usr/bin/env node
'use strict';
2017-03-20 22:42:19 +01:00
const os = require('os');
const fs = require('fs');
const meow = require('meow');
const indentString = require('indent-string');
const stdin = require('get-stdin');
const css = require('css');
const _ = require('lodash');
const inlineCritical = require('./');
2015-06-25 06:27:18 +02:00
2017-03-20 22:42:19 +01:00
let ok;
const help = [
2015-11-13 22:59:10 +01:00
'Usage: inline-critical <input> [<option>]',
'',
'Options:',
' -c, --css Path to CSS file',
' -h, --html Path to HTML file',
' -i, --ignore Skip matching stylesheets',
2016-04-17 00:58:04 +02:00
' -m, --minify Minify the styles before inlining (default)',
2015-11-13 22:59:10 +01:00
' -e, --extract Remove the inlined styles from any stylesheets referenced in the HTML',
' -b, --base Is used when extracting styles to find the files references by `href` attributes',
' -s, --selector Optionally defines the element used by loadCSS as a reference for inlining'
2015-06-25 06:27:18 +02:00
];
2017-03-20 22:42:19 +01:00
const cli = meow({
help
2015-06-25 06:27:18 +02:00
}, {
2015-11-13 22:59:10 +01:00
alias: {
c: 'css',
h: 'html',
i: 'ignore',
m: 'minify',
b: 'base',
e: 'extract',
s: 'selector'
}
2015-06-25 06:27:18 +02:00
});
2017-03-20 22:42:19 +01:00
// Cleanup cli flags
cli.flags = _.reduce(cli.flags, (res, val, key) => {
2015-11-13 22:59:10 +01:00
if (key.length <= 1) {
return res;
}
2015-06-25 06:27:18 +02:00
2015-11-13 22:59:10 +01:00
switch (key) {
case 'css':
case 'html':
try {
res[key] = read(val);
} catch (err) {
}
break;
case 'base':
res.basePath = val;
break;
case 'ignore':
if (_.isString(val) || _.isRegExp(val)) {
val = [val];
}
2017-03-20 22:42:19 +01:00
res.ignore = _.map(val || [], ignore => {
// Check regex
const match = ignore.match(/^\/(.*)\/([igmy]+)?$/);
2015-11-13 22:59:10 +01:00
if (match) {
return new RegExp(_.escapeRegExp(match[1]), match[2]);
}
return ignore;
});
break;
default:
res[key] = val;
break;
}
2015-06-25 06:27:18 +02:00
2015-11-13 22:59:10 +01:00
return res;
2015-06-25 06:27:18 +02:00
}, {});
function error(err) {
2016-09-02 23:52:54 +02:00
process.stderr.write(indentString('Error: ' + (err.message || err), 4));
2015-11-13 22:59:10 +01:00
process.stderr.write(os.EOL);
2016-09-02 23:52:54 +02:00
process.stderr.write(indentString(help.join(os.EOL), 4));
2015-11-13 22:59:10 +01:00
process.exit(1);
2015-06-25 06:27:18 +02:00
}
function read(file) {
2015-11-13 22:59:10 +01:00
try {
return fs.readFileSync(file, 'utf8');
} catch (err) {
error(err);
}
2015-06-25 06:27:18 +02:00
}
function run(data) {
2017-03-20 22:42:19 +01:00
const opts = _.defaults(cli.flags, {basePath: process.cwd()});
2015-11-13 22:59:10 +01:00
ok = true;
if (data) {
2017-03-20 22:42:19 +01:00
// Detect html
2015-11-13 22:59:10 +01:00
try {
css.parse(data);
opts.css = data;
} catch (err) {
opts.html = data;
}
}
2015-06-25 06:27:18 +02:00
2017-03-20 22:42:19 +01:00
_.forEach(cli.input, file => {
const tmp = read(file);
2015-11-13 22:59:10 +01:00
try {
css.parse(tmp);
opts.css = tmp;
} catch (err) {
opts.html = tmp;
}
});
if (!opts.html || !opts.css) {
cli.showHelp();
2015-06-25 06:27:18 +02:00
}
try {
2017-03-20 22:42:19 +01:00
const out = inlineCritical(opts.html, opts.css, opts);
2016-04-18 06:35:26 +02:00
process.stdout.write(out.toString(), process.exit);
2015-06-25 06:27:18 +02:00
} catch (err) {
2015-11-13 22:59:10 +01:00
error(err);
2015-06-25 06:27:18 +02:00
}
}
2017-03-20 22:42:19 +01:00
// Get stdin
2015-09-16 06:28:54 +02:00
stdin().then(run);
2017-03-20 22:42:19 +01:00
setTimeout(() => {
2015-11-13 22:59:10 +01:00
if (ok) {
return;
}
run();
}, 100);