/
cli.js
executable file
Β·500 lines (438 loc) Β· 14.4 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
// @flow
import type {InitialParcelOptions} from '@parcel/types';
import {BuildError} from '@parcel/core';
import {NodeFS} from '@parcel/fs';
import ThrowableDiagnostic from '@parcel/diagnostic';
import {prettyDiagnostic, openInBrowser} from '@parcel/utils';
import {Disposable} from '@parcel/events';
import {INTERNAL_ORIGINAL_CONSOLE} from '@parcel/logger';
import chalk from 'chalk';
import commander from 'commander';
import path from 'path';
import getPort from 'get-port';
import {version} from '../package.json';
require('v8-compile-cache');
const program = new commander.Command();
// Exit codes in response to signals are traditionally
// 128 + signal value
// https://tldp.org/LDP/abs/html/exitcodes.html
const SIGINT_EXIT_CODE = 130;
async function logUncaughtError(e: mixed) {
if (e instanceof ThrowableDiagnostic) {
for (let diagnostic of e.diagnostics) {
let {message, codeframe, stack, hints, documentation} =
await prettyDiagnostic(diagnostic);
INTERNAL_ORIGINAL_CONSOLE.error(chalk.red(message));
if (codeframe || stack) {
INTERNAL_ORIGINAL_CONSOLE.error('');
}
INTERNAL_ORIGINAL_CONSOLE.error(codeframe);
INTERNAL_ORIGINAL_CONSOLE.error(stack);
if ((stack || codeframe) && hints.length > 0) {
INTERNAL_ORIGINAL_CONSOLE.error('');
}
for (let h of hints) {
INTERNAL_ORIGINAL_CONSOLE.error(chalk.blue(h));
}
if (documentation) {
INTERNAL_ORIGINAL_CONSOLE.error(chalk.magenta.bold(documentation));
}
}
} else {
INTERNAL_ORIGINAL_CONSOLE.error(e);
}
// A hack to definitely ensure we logged the uncaught exception
await new Promise(resolve => setTimeout(resolve, 100));
}
const handleUncaughtException = async exception => {
try {
await logUncaughtError(exception);
} catch (err) {
console.error(exception);
console.error(err);
}
process.exit(1);
};
process.on('unhandledRejection', handleUncaughtException);
program.storeOptionsAsProperties();
program.version(version);
// --no-cache, --cache-dir, --no-source-maps, --no-autoinstall, --global?, --public-url, --log-level
// --no-content-hash, --experimental-scope-hoisting, --detailed-report
const commonOptions = {
'--no-cache': 'disable the filesystem cache',
'--config <path>':
'specify which config to use. can be a path or a package name',
'--cache-dir <path>': 'set the cache directory. defaults to ".parcel-cache"',
'--no-source-maps': 'disable sourcemaps',
'--target [name]': [
'only build given target(s)',
(val, list) => list.concat([val]),
[],
],
'--log-level <level>': new commander.Option(
'--log-level <level>',
'set the log level',
).choices(['none', 'error', 'warn', 'info', 'verbose']),
'--dist-dir <dir>':
'output directory to write to when unspecified by targets',
'--no-autoinstall': 'disable autoinstall',
'--profile': 'enable build profiling',
'-V, --version': 'output the version number',
'--detailed-report [count]': [
'print the asset timings and sizes in the build report',
parseOptionInt,
],
'--reporter <name>': [
'additional reporters to run',
(val, acc) => {
acc.push(val);
return acc;
},
[],
],
};
var hmrOptions = {
'--no-hmr': 'disable hot module replacement',
'-p, --port <port>': [
'set the port to serve on. defaults to $PORT or 1234',
process.env.PORT,
],
'--host <host>':
'set the host to listen on, defaults to listening on all interfaces',
'--https': 'serves files over HTTPS',
'--cert <path>': 'path to certificate to use with HTTPS',
'--key <path>': 'path to private key to use with HTTPS',
'--hmr-port <port>': ['hot module replacement port', process.env.HMR_PORT],
'--hmr-host <host>': ['hot module replacement host', process.env.HMR_HOST],
};
function applyOptions(cmd, options) {
for (let opt in options) {
const option = options[opt];
if (option instanceof commander.Option) {
cmd.addOption(option);
} else {
cmd.option(opt, ...(Array.isArray(option) ? option : [option]));
}
}
}
let serve = program
.command('serve [input...]')
.description('starts a development server')
.option('--public-url <url>', 'the path prefix for absolute urls')
.option(
'--open [browser]',
'automatically open in specified browser, defaults to default browser',
)
.option('--watch-for-stdin', 'exit when stdin closes')
.option(
'--lazy',
'Build async bundles on demand, when requested in the browser',
)
.action(runCommand);
applyOptions(serve, hmrOptions);
applyOptions(serve, commonOptions);
let watch = program
.command('watch [input...]')
.description('starts the bundler in watch mode')
.option('--public-url <url>', 'the path prefix for absolute urls')
.option('--no-content-hash', 'disable content hashing')
.option('--watch-for-stdin', 'exit when stdin closes')
.action(runCommand);
applyOptions(watch, hmrOptions);
applyOptions(watch, commonOptions);
let build = program
.command('build [input...]')
.description('bundles for production')
.option('--no-optimize', 'disable minification')
.option('--no-scope-hoist', 'disable scope-hoisting')
.option('--public-url <url>', 'the path prefix for absolute urls')
.option('--no-content-hash', 'disable content hashing')
.action(runCommand);
applyOptions(build, commonOptions);
program
.command('help [command]')
.description('display help information for a command')
.action(function (command) {
let cmd = program.commands.find(c => c.name() === command) || program;
cmd.help();
});
program.on('--help', function () {
INTERNAL_ORIGINAL_CONSOLE.log('');
INTERNAL_ORIGINAL_CONSOLE.log(
' Run `' +
chalk.bold('parcel help <command>') +
'` for more information on specific commands',
);
INTERNAL_ORIGINAL_CONSOLE.log('');
});
// Override to output option description if argument was missing
// $FlowFixMe[prop-missing]
commander.Command.prototype.optionMissingArgument = function (option) {
INTERNAL_ORIGINAL_CONSOLE.error(
"error: option `%s' argument missing",
option.flags,
);
INTERNAL_ORIGINAL_CONSOLE.log(program.createHelp().optionDescription(option));
process.exit(1);
};
// Make serve the default command except for --help
var args = process.argv;
if (args[2] === '--help' || args[2] === '-h') args[2] = 'help';
if (!args[2] || !program.commands.some(c => c.name() === args[2])) {
args.splice(2, 0, 'serve');
}
program.parse(args);
function runCommand(...args) {
run(...args).catch(handleUncaughtException);
}
async function run(
entries: Array<string>,
_opts: any, // using pre v7 Commander options as properties
command: any,
) {
if (entries.length === 0) {
entries = ['.'];
}
entries = entries.map(entry => path.resolve(entry));
let Parcel = require('@parcel/core').default;
let fs = new NodeFS();
let options = await normalizeOptions(command, fs);
let parcel = new Parcel({
entries,
// $FlowFixMe[extra-arg] - flow doesn't know about the `paths` option (added in Node v8.9.0)
defaultConfig: require.resolve('@parcel/config-default', {
paths: [fs.cwd(), __dirname],
}),
shouldPatchConsole: true,
...options,
});
let disposable = new Disposable();
let unsubscribe: () => Promise<mixed>;
let isExiting;
async function exit(exitCode: number = 0) {
if (isExiting) {
return;
}
isExiting = true;
if (unsubscribe != null) {
await unsubscribe();
} else if (parcel.isProfiling) {
await parcel.stopProfiling();
}
if (process.stdin.isTTY && process.stdin.isRaw) {
// $FlowFixMe
process.stdin.setRawMode(false);
}
disposable.dispose();
process.exit(exitCode);
}
const isWatching = command.name() === 'watch' || command.name() === 'serve';
if (process.stdin.isTTY) {
// $FlowFixMe
process.stdin.setRawMode(true);
require('readline').emitKeypressEvents(process.stdin);
let stream = process.stdin.on('keypress', async (char, key) => {
if (!key.ctrl) {
return;
}
switch (key.name) {
case 'c':
// Detect the ctrl+c key, and gracefully exit after writing the asset graph to the cache.
// This is mostly for tools that wrap Parcel as a child process like yarn and npm.
//
// Setting raw mode prevents SIGINT from being sent in response to ctrl-c:
// https://nodejs.org/api/tty.html#tty_readstream_setrawmode_mode
//
// We don't use the SIGINT event for this because when run inside yarn, the parent
// yarn process ends before Parcel and it appears that Parcel has ended while it may still
// be cleaning up. Handling events from stdin prevents this impression.
// Enqueue a busy message to be shown if Parcel doesn't shut down
// within the timeout.
setTimeout(
() =>
INTERNAL_ORIGINAL_CONSOLE.log(
chalk.bold.yellowBright('Parcel is shutting down...'),
),
500,
);
// When watching, a 0 success code is acceptable when Parcel is interrupted with ctrl-c.
// When building, fail with a code as if we received a SIGINT.
await exit(isWatching ? 0 : SIGINT_EXIT_CODE);
break;
case 'e':
await (parcel.isProfiling
? parcel.stopProfiling()
: parcel.startProfiling());
break;
case 'y':
await parcel.takeHeapSnapshot();
break;
}
});
disposable.add(() => {
stream.destroy();
});
}
if (isWatching) {
({unsubscribe} = await parcel.watch(err => {
if (err) {
throw err;
}
}));
if (command.open && options.serveOptions) {
await openInBrowser(
`${options.serveOptions.https ? 'https' : 'http'}://${
options.serveOptions.host || 'localhost'
}:${options.serveOptions.port}`,
command.open,
);
}
if (command.watchForStdin) {
process.stdin.on('end', async () => {
INTERNAL_ORIGINAL_CONSOLE.log('STDIN closed, ending');
await exit();
});
process.stdin.resume();
}
// In non-tty cases, respond to SIGINT by cleaning up. Since we're watching,
// a 0 success code is acceptable.
process.on('SIGINT', exit);
process.on('SIGTERM', exit);
} else {
try {
await parcel.run();
} catch (err) {
// If an exception is thrown during Parcel.build, it is given to reporters in a
// buildFailure event, and has been shown to the user.
if (!(err instanceof BuildError)) {
await logUncaughtError(err);
}
await exit(1);
}
await exit();
}
}
function parsePort(portValue: string): number {
let parsedPort = Number(portValue);
// Throw an error if port value is invalid...
if (!Number.isInteger(parsedPort)) {
throw new Error(`Port ${portValue} is not a valid integer.`);
}
return parsedPort;
}
function parseOptionInt(value) {
const parsedValue = parseInt(value, 10);
if (isNaN(parsedValue)) {
throw new commander.InvalidOptionArgumentError('Must be an integer.');
}
return parsedValue;
}
async function normalizeOptions(
command,
inputFS,
): Promise<InitialParcelOptions> {
let nodeEnv;
if (command.name() === 'build') {
nodeEnv = process.env.NODE_ENV || 'production';
// Autoinstall unless explicitly disabled or we detect a CI environment.
command.autoinstall = !(command.autoinstall === false || process.env.CI);
} else {
nodeEnv = process.env.NODE_ENV || 'development';
}
// Set process.env.NODE_ENV to a default if undefined so that it is
// available in JS configs and plugins.
process.env.NODE_ENV = nodeEnv;
let https = !!command.https;
if (command.cert && command.key) {
https = {
cert: command.cert,
key: command.key,
};
}
let serveOptions = false;
let {host} = command;
// Ensure port is valid and available
let port = parsePort(command.port || '1234');
let originalPort = port;
if (command.name() === 'serve' || command.hmr) {
try {
port = await getPort({port, host});
} catch (err) {
throw new ThrowableDiagnostic({
diagnostic: {
message: `Could not get available port: ${err.message}`,
origin: 'parcel',
stack: err.stack,
},
});
}
if (port !== originalPort) {
let errorMessage = `Port "${originalPort}" could not be used`;
if (command.port != null) {
// Throw the error if the user defined a custom port
throw new Error(errorMessage);
} else {
// Parcel logger is not set up at this point, so just use native INTERNAL_ORIGINAL_CONSOLE
INTERNAL_ORIGINAL_CONSOLE.warn(errorMessage);
}
}
}
if (command.name() === 'serve') {
let {publicUrl} = command;
serveOptions = {
https,
port,
host,
publicUrl,
};
}
let hmrOptions = null;
if (command.name() !== 'build' && command.hmr !== false) {
let hmrport = command.hmrPort ? parsePort(command.hmrPort) : port;
let hmrhost = command.hmrHost ? command.hmrHost : host;
hmrOptions = {port: hmrport, host: hmrhost};
}
if (command.detailedReport === true) {
command.detailedReport = '10';
}
let additionalReporters = [
{packageName: '@parcel/reporter-cli', resolveFrom: __filename},
...(command.reporter: Array<string>).map(packageName => ({
packageName,
resolveFrom: path.join(inputFS.cwd(), 'index'),
})),
];
let mode = command.name() === 'build' ? 'production' : 'development';
return {
shouldDisableCache: command.cache === false,
cacheDir: command.cacheDir,
config: command.config,
mode,
hmrOptions,
shouldContentHash: hmrOptions ? false : command.contentHash,
serveOptions,
targets: command.target.length > 0 ? command.target : null,
shouldAutoInstall: command.autoinstall ?? true,
logLevel: command.logLevel,
shouldProfile: command.profile,
shouldBuildLazily: command.lazy,
detailedReport:
command.detailedReport != null
? {
assetsPerBundle: parseInt(command.detailedReport, 10),
}
: null,
env: {
NODE_ENV: nodeEnv,
},
additionalReporters,
defaultTargetOptions: {
shouldOptimize:
command.optimize != null ? command.optimize : mode === 'production',
sourceMaps: command.sourceMaps ?? true,
shouldScopeHoist: command.scopeHoist,
publicUrl: command.publicUrl,
distDir: command.distDir,
},
};
}