forked from addyosmani/critical
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·171 lines (156 loc) · 4.91 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env node
'use strict';
var os = require('os');
var path = require('path');
var chalk = require('chalk');
var meow = require('meow');
var indentString = require('indent-string');
var stdin = require('get-stdin');
var _ = require('lodash');
var file = require('./lib/file-helper');
var critical = require('./');
var ok;
var help = [
'Usage: critical <input> [<option>]',
'',
'Options:',
' -b, --base Your base directory',
' -c, --css Your CSS Files (optional)',
' -w, --width Viewport width',
' -h, --height Viewport height',
' -m, --minify Minify critical-path CSS when inlining',
' -i, --inline Generate the HTML with inlined critical-path CSS',
' -e, --extract Extract inlined styles from referenced stylesheets',
' -p, --pathPrefix Path to prepend CSS assets with (defaults to /) ',
' -f, --folder HTML Subfolder (default: \'\')',
' --ii, --inlineImages Inline images',
' --ignore RegExp, @type or selector to ignore',
' --include RegExp, @type or selector to include',
' --maxFileSize Sets a max file size (in bytes) for base64 inlined images',
' --assetPaths Directories/Urls where the inliner should start looking for assets.',
' --timeout Sets the maximum timeout (in milliseconds) for the operation (defaults to 30000 ms).',
' ----------------------------------------------------------------------.',
' Deprecated - use "--inline" to retrieve the modified HTML',
' critical source.html --inline > dest.html',
' -----------------------------------------------------------------------',
' -H, --htmlTarget Target for final HTML output',
' -S, --styleTarget Target for generated critical-path CSS (which we inline)'
];
var cli = meow({
help: help
}, {
alias: {
b: 'base',
c: 'css',
w: 'width',
h: 'height',
f: 'folder',
H: 'htmlTarget',
i: 'inline',
I: 'ignore',
S: 'styleTarget',
m: 'minify',
e: 'extract',
p: 'pathPrefix',
ii: 'inlineImages'
}
});
// cleanup cli flags and assert cammelcase keeps camelcase
cli.flags = _.reduce(cli.flags, function (res, val, key) {
if (key.length <= 1) {
return res;
}
switch (key) {
case 'htmltarget':
res.htmlTarget = val;
break;
case 'styletarget':
res.styleTarget = val;
break;
case 'pathprefix':
res.pathPrefix = val;
break;
case 'inline':
res.inline = (val && val !== 'false') || typeof val === 'undefined';
break;
case 'inlineimages':
res.inlineImages = val;
break;
case 'maxfilesize':
res.maxFileSize = val;
break;
case 'timeout':
res.timeout = val;
break;
case 'assetpaths':
case 'assetPaths':
if (_.isString(val)) {
val = [val];
}
res.assetPaths = val;
break;
case 'include':
case 'ignore':
if (_.isString(val) || _.isRegExp(val)) {
val = [val];
}
res[key] = _.map(val || [], function (entry) {
// check regex
var match = entry.match(/^\/(.*)\/([igmy]+)?$/);
if (match) {
return new RegExp(_.escapeRegExp(match[1]), match[2]);
}
return entry;
});
break;
default:
res[key] = val;
break;
}
return res;
}, {});
function error(err) {
process.stderr.write(indentString((chalk.red('Error: ') + err.message || err), 3));
process.stderr.write(os.EOL);
process.stderr.write(indentString(help.join(os.EOL), 3));
process.exit(1);
}
function run(data) {
var opts = _.assign({base: process.cwd()}, cli.flags);
var command = opts.htmlTarget || opts.inline ? 'generateInline' : 'generate';
if (command === 'generate') {
opts.dest = opts.styleTarget || '';
}
ok = true;
if (data) {
opts.html = data;
} else {
opts.src = cli.input[0];
if (opts.src && !file.isExternal(opts.src)) {
opts.src = path.resolve(cli.input[0]);
}
}
try {
critical[command](opts, function (err, val) {
if (err) {
error(err);
} else {
process.stdout.write(val, process.exit);
}
});
} catch (err) {
error(err);
}
}
if (cli.input[0]) {
run();
} else {
// get stdin
stdin().then(run);
setTimeout(function () {
if (ok) {
return;
}
run();
}, 100);
}