forked from addyosmani/critical
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.js
executable file
·230 lines (202 loc) · 5.98 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env node
'use strict';
const os = require('os');
const chalk = require('chalk');
const meow = require('meow');
const groupArgs = require('group-args');
const indentString = require('indent-string');
const stdin = require('get-stdin');
const reduce = require('lodash/reduce');
const isString = require('lodash/isString');
const isObject = require('lodash/isObject');
const escapeRegExp = require('lodash/escapeRegExp');
const {validate} = require('./src/config');
const critical = require('.');
const 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
-i, --inline Generate the HTML with inlined critical-path CSS
-e, --extract Extract inlined styles from referenced stylesheets
--inlineImages Inline images
--ignore RegExp, @type or selector to ignore
--ignore-[OPTION] Pass options to postcss-discard. See https://goo.gl/HGo5YV
--include RegExp, @type or selector to include
--include-[OPTION] Pass options to inline-critical. See https://goo.gl/w6SHJM
--assetPaths Directories/Urls where the inliner should start looking for assets.
--user RFC2617 basic authorization user
--pass RFC2617 basic authorization password
--penthouse-[OPTION] Pass options to penthouse. See https://goo.gl/PQ5HLL
--ua, --userAgent User agent to use when fetching remote src
`;
const meowOpts = {
flags: {
base: {
type: 'string',
alias: 'b',
},
css: {
type: 'string',
alias: 'c',
},
width: {
alias: 'w',
},
height: {
alias: 'h',
},
inline: {
type: 'boolean',
alias: 'i',
},
extract: {
type: 'boolean',
alias: 'e',
default: false,
},
inlineImages: {
type: 'boolean',
},
ignore: {
type: 'string',
},
user: {
type: 'string',
},
pass: {
type: 'string',
},
userAgent: {
type: 'string',
alias: 'ua',
},
},
};
const cli = meow(help, meowOpts);
const groupKeys = ['ignore', 'inline', 'penthouse', 'target', 'request'];
// Group args for inline-critical and penthouse
const grouped = {
...cli.flags,
...groupArgs(
groupKeys,
{
delimiter: '-',
},
meowOpts
),
};
/**
* Check if key is an alias
* @param {string} key Key to check
* @returns {boolean} True for alias
*/
const isAlias = (key) => {
if (isString(key) && key.length > 1) {
return false;
}
const aliases = Object.keys(meowOpts.flags) // eslint-disable-line unicorn/prefer-set-has
.filter((k) => meowOpts.flags[k].alias)
.map((k) => meowOpts.flags[k].alias);
return aliases.includes(key);
};
/**
* Check if value is an empty object
* @param {mixed} val Value to check
* @returns {boolean} Whether or not this is an empty object
*/
const isEmptyObj = (val) => isObject(val) && Object.keys(val).length === 0;
/**
* Check if value is transformed to {default: val}
* @param {mixed} val Value to check
* @returns {boolean} True if it's been converted to {default: value}
*/
const isGroupArgsDefault = (val) => isObject(val) && Object.keys(val).length === 1 && val.default;
/**
* Return regex if value is a string like this: '/.../g'
* @param {mixed} val Value to process
* @returns {mixed} Mapped values
*/
const mapRegExpStr = (val) => {
if (isString(val)) {
const {groups} = val.match(/^\/(?<regex>[^/]+)(?:\/?(?<flags>[igmy]+))?\/$/) || {};
const {regex, flags} = groups || {};
return (groups && new RegExp(escapeRegExp(regex), flags)) || val;
}
if (Array.isArray(val)) {
return val.map((v) => mapRegExpStr(v));
}
return val;
};
const normalizedFlags = reduce(
grouped,
(res, val, key) => {
// Cleanup groupArgs mess ;)
if (groupKeys.includes(key)) {
// An empty object means param without value, just true
if (isEmptyObj(val)) {
val = true;
} else if (isGroupArgsDefault(val)) {
val = val.default;
}
}
// Cleanup camelized group keys
if (groupKeys.find((k) => key.includes(k)) && !validate(key, val)) {
return res;
}
if (!isAlias(key)) {
res[key] = mapRegExpStr(val);
}
return res;
},
{}
);
function showError(err) {
process.stderr.write(indentString(chalk.red('Error: ') + err.message || err, 3));
process.stderr.write(os.EOL);
process.stderr.write(indentString(help, 3));
process.exit(1);
}
function run(data) {
const {_: inputs = [], css, ...opts} = {...normalizedFlags};
// Detect css globbing
const cssBegin = process.argv.findIndex((el) => ['--css', '-c'].includes(el));
const cssEnd = process.argv.findIndex((el, index) => index > cssBegin && el.startsWith('-'));
const cssCheck = cssBegin >= 0 ? process.argv.slice(cssBegin, cssEnd > 0 ? cssEnd : undefined) : [];
const additionalCss = inputs.filter((file) => cssCheck.includes(file));
// Just take the first html input as we don't support multiple html sources for
const [input] = inputs.filter((file) => !additionalCss.includes(file));
if (Array.isArray(css)) {
opts.css = [...css, ...additionalCss].filter((file) => file);
} else if (css || additionalCss.length > 0) {
opts.css = [css, ...additionalCss].filter((file) => file);
}
if (data) {
opts.html = data;
} else {
opts.src = input;
}
try {
critical.generate(opts, (error, val) => {
if (error) {
showError(error);
} else if (opts.inline) {
process.stdout.write(val.html, process.exit);
} else if (opts.extract) {
process.stdout.write(val.uncritical, process.exit);
} else {
process.stdout.write(val.css, process.exit);
}
});
} catch (error) {
showError(error);
}
}
if (cli.input[0]) {
run();
} else {
// Get stdin
stdin().then(run); /* eslint-disable-line promise/prefer-await-to-then */
}