forked from addyosmani/critical
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·163 lines (148 loc) · 4.54 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
#!/usr/bin/env node
'use strict';
var os = require('os');
var path = require('path');
var meow = require('meow');
var objectAssign = require('object-assign');
var indentString = require('indent-string');
var stdin = require('get-stdin');
var _ = require('lodash');
var critical = require('./');
var file = require('./lib/fileHelper');
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',
' -I, --ignore RegExp, @type or selector to ignore',
' -e, --extract Extract inlined styles from referenced stylesheets',
' -p, --pathPrefix Path to prepend CSS assets with (defaults to /) ',
' --ii, --inlineImages Inline images',
' --maxFileSize Sets a max file size (in bytes) for base64 inlined images',
' --assetPaths Directories/Urls where the inliner should start looking for assets.',
' ----------------------------------------------------------------------.',
' 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',
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 'assetpaths':
case 'assetPaths':
if (_.isString(val)) {
val = [val];
}
res.assetPaths = val;
break;
case 'ignore':
if (_.isString(val) || _.isRegExp(val)) {
val = [val];
}
res.ignore = _.map(val || [], function(ignore) {
// check regex
var match = ignore.match(/^\/(.*)\/([igmy]+)?$/);
if (match) {
return new RegExp(_.escapeRegExp(match[1]),match[2]);
}
return ignore;
});
break;
default:
res[key] = val;
break;
}
return res;
}, {});
function error(err) {
process.stderr.write(indentString(err.message || err, ' Error: '));
process.stderr.write(os.EOL);
process.stderr.write(indentString(help.join(os.EOL), ' '));
process.exit(1);
}
function run(data) {
var opts = objectAssign({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);
}
});
} catch (err) {
error(err);
}
}
if (cli.input[0]) {
run();
} else {
// get stdin
stdin().then(run);
setTimeout(function () {
if (ok) {
return;
}
run();
}, 100);
}