-
Notifications
You must be signed in to change notification settings - Fork 371
/
cli.js
executable file
·255 lines (228 loc) · 6.64 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/usr/bin/env node
import os from 'node:os';
import process from 'node:process';
import stdin from 'get-stdin';
import groupArgs from 'group-args';
import indentString from 'indent-string';
import {escapeRegExp, isObject, isString, reduce} from 'lodash-es';
import meow from 'meow';
import pico from 'picocolors';
import {validate} from './src/config.js';
import {generate} from './index.js';
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
--dimensions Pass dimensions e.g. 1300x900
--ignore RegExp, @type or selector to ignore
--ignore-[OPTION] Pass options to postcss-discard. See https://goo.gl/HGo5YV
--ignoreInlinedStyles Ignore inlined stylesheets
--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
--strict Throw an error on css parsing errors or if no css is found
`;
const meowOpts = {
importMeta: import.meta,
flags: {
base: {
type: 'string',
shortFlag: 'b',
},
css: {
type: 'string',
shortFlag: 'c',
isMultiple: true,
},
width: {
shortFlag: 'w',
},
height: {
shortFlag: 'h',
},
inline: {
type: 'boolean',
shortFlag: 'i',
},
extract: {
type: 'boolean',
shortFlag: 'e',
default: false,
},
inlineImages: {
type: 'boolean',
},
ignoreInlinedStyles: {
type: 'boolean',
default: false,
},
ignore: {
type: 'string',
},
user: {
type: 'string',
},
strict: {
type: 'boolean',
default: false,
},
pass: {
type: 'string',
},
userAgent: {
type: 'string',
shortFlag: 'ua',
},
dimensions: {
type: 'string',
isMultiple: true,
},
},
};
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)
.filter((k) => meowOpts.flags[k].shortFlag)
.map((k) => meowOpts.flags[k].shortFlag);
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.some((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(pico.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)); // eslint-disable-line unicorn/prefer-array-find
if (Array.isArray(opts.dimensions)) {
opts.dimensions = opts.dimensions.reduce(
(result, data) => [
...result,
...data.split(',').map((dimension) => {
const [width, height] = dimension.split('x');
return {width: Number.parseInt(width, 10), height: Number.parseInt(height, 10)};
}),
],
[]
);
}
if (Array.isArray(css)) {
opts.css = [...css, ...additionalCss].filter(Boolean);
} else if (css || additionalCss.length > 0) {
opts.css = [css, ...additionalCss].filter(Boolean);
}
if (data) {
opts.html = data;
} else {
opts.src = input;
}
try {
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 {
const data = await stdin();
run(data);
}