inline-critical/cli.js

159 lines
3.5 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');
2018-03-05 20:56:57 +01:00
const inlineCritical = require('.');
2015-06-25 06:27:18 +02:00
2017-03-20 22:42:19 +01:00
let ok;
2018-03-05 20:56:57 +01:00
const help = `
Usage: inline-critical <input> [<option>]
2015-06-25 06:27:18 +02:00
2018-03-05 20:56:57 +01:00
Options:
-c, --css Path to CSS file
-h, --html Path to HTML file
-i, --ignore Skip matching stylesheets
-m, --minify Minify the styles before inlining (default)
-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
`;
const cli = meow(help, {
autoHelp: true,
autoVersion: true,
flags: {
css: {
type: 'string',
alias: 'c'
},
html: {
type: 'string',
alias: 'h'
},
ignore: {
2018-03-05 21:03:35 +01:00
type: 'string',
2018-03-05 20:56:57 +01:00
alias: 'i'
},
minify: {
type: 'boolean',
alias: 'm'
},
extract: {
type: 'boolean',
alias: 'e'
},
base: {
type: 'string',
alias: 'b'
},
selector: {
type: 'string',
alias: 's'
}
2015-11-13 22:59:10 +01:00
}
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);
2018-03-05 20:56:57 +01:00
process.stderr.write(indentString(help, 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);