-
Notifications
You must be signed in to change notification settings - Fork 682
Expand file tree
/
Copy pathserve.ts
More file actions
636 lines (517 loc) · 20.3 KB
/
serve.ts
File metadata and controls
636 lines (517 loc) · 20.3 KB
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
import { BaseError, MetadataGroup, ParsedArgs, unparseArgs } from '@ionic/cli-framework';
import { LOGGER_LEVELS, createPrefixedFormatter } from '@ionic/cli-framework-output';
import { PromptModule } from '@ionic/cli-framework-prompts';
import { str2num } from '@ionic/cli-framework/utils/string';
import { NetworkInterface, getExternalIPv4Interfaces, isHostConnectable } from '@ionic/utils-network';
import { createProcessEnv, killProcessTree, onBeforeExit, processExit } from '@ionic/utils-process';
import chalk from 'chalk';
import { debug as Debug } from 'debug';
import { EventEmitter } from 'events';
import * as lodash from 'lodash';
import split2 from 'split2';
import * as stream from 'stream';
import { CommandLineInputs, CommandLineOptions, CommandMetadata, CommandMetadataOption, IConfig, ILogger, IProject, IShell, IonicEnvironmentFlags, NpmClient, Runner, ServeDetails, ServeOptions } from '../definitions';
import { ancillary, input, strong, weak } from './color';
import { FatalException, ServeCLIProgramNotFoundException } from './errors';
import { emit } from './events';
import { Hook } from './hooks';
import { openUrl } from './open';
import { createDefaultLoggerHandlers } from './utils/logger';
const debug = Debug('ionic:lib:serve');
export const DEFAULT_DEV_LOGGER_PORT = 53703;
export const DEFAULT_LIVERELOAD_PORT = 35729;
export const DEFAULT_SERVER_PORT = 8100;
export const DEFAULT_DEVAPP_COMM_PORT = 53233;
export const DEFAULT_ADDRESS = 'localhost';
export const BIND_ALL_ADDRESS = '0.0.0.0';
export const LOCAL_ADDRESSES = ['localhost', '127.0.0.1'];
export const BROWSERS = ['safari', 'firefox', process.platform === 'win32' ? 'chrome' : (process.platform === 'darwin' ? 'google chrome' : 'google-chrome')];
// npm script name
export const SERVE_SCRIPT = 'ionic:serve';
export const COMMON_SERVE_COMMAND_OPTIONS: readonly CommandMetadataOption[] = [
{
name: 'external',
summary: `Host dev server on all network interfaces (i.e. ${input('--host=0.0.0.0')})`,
type: Boolean,
},
{
name: 'address', // keep this here so the option is parsed with its value
summary: '',
groups: [MetadataGroup.HIDDEN],
},
{
name: 'host',
summary: 'Use specific host for the dev server',
default: DEFAULT_ADDRESS,
groups: [MetadataGroup.ADVANCED],
},
{
name: 'port',
summary: 'Use specific port for the dev server',
default: DEFAULT_SERVER_PORT.toString(),
aliases: ['p'],
groups: [MetadataGroup.ADVANCED],
},
{
name: 'public-host',
summary: 'The host used for the browser or web view',
groups: [MetadataGroup.ADVANCED],
spec: { value: 'host' },
},
{
name: 'livereload',
summary: 'Do not spin up dev server--just serve files',
type: Boolean,
default: true,
},
{
name: 'engine',
summary: `Target engine (e.g. ${['browser', 'cordova'].map(e => input(e)).join(', ')})`,
groups: [MetadataGroup.HIDDEN, MetadataGroup.ADVANCED],
},
{
name: 'platform',
summary: `Target platform on chosen engine (e.g. ${['ios', 'android'].map(e => input(e)).join(', ')})`,
groups: [MetadataGroup.HIDDEN, MetadataGroup.ADVANCED],
},
];
export interface ServeRunnerDeps {
readonly config: IConfig;
readonly flags: IonicEnvironmentFlags;
readonly log: ILogger;
readonly project: IProject;
readonly prompt: PromptModule;
readonly shell: IShell;
}
export abstract class ServeRunner<T extends ServeOptions> implements Runner<T, ServeDetails> {
protected devAppConnectionMade = false;
protected abstract readonly e: ServeRunnerDeps;
abstract getCommandMetadata(): Promise<Partial<CommandMetadata>>;
abstract serveProject(options: T): Promise<ServeDetails>;
abstract modifyOpenUrl(url: string, options: T): string;
getPkgManagerServeCLI(): PkgManagerServeCLI {
const pkgManagerCLIs = {
npm: NpmServeCLI,
pnpm: PnpmServeCLI,
yarn: YarnServeCLI,
bun: BunServeCLI,
};
const client = this.e.config.get('npmClient');
const CLI = pkgManagerCLIs[client];
if (CLI) {
return new CLI(this.e);
}
throw new ServeCLIProgramNotFoundException('Unknown CLI client: ' + client);
}
createOptionsFromCommandLine(inputs: CommandLineInputs, options: CommandLineOptions): ServeOptions {
const separatedArgs = options['--'];
if (options['external'] && options['host'] === DEFAULT_ADDRESS) {
options['host'] = '0.0.0.0';
}
if (options['address'] && options['host'] === DEFAULT_ADDRESS) {
this.e.log.warn(
`The ${input('--address')} option is deprecated in favor of ${input('--host')}.\n` +
`Please use the ${input('--host')} option (e.g. ${input(`--host=${options['address']}`)}) to specify the host of the dev server.\n`
);
options['host'] = options['address'];
}
const engine = this.determineEngineFromCommandLine(options);
const host = options['host'] ? String(options['host']) : DEFAULT_ADDRESS;
const port = str2num(options['port'], DEFAULT_SERVER_PORT);
const [platform] = options['platform'] ? [String(options['platform'])] : inputs;
return {
'--': separatedArgs ? separatedArgs : [],
host,
browser: options['browser'] ? String(options['browser']) : undefined,
browserOption: options['browseroption'] ? String(options['browseroption']) : undefined,
engine,
externalAddressRequired: !!options['externalAddressRequired'],
livereload: typeof options['livereload'] === 'boolean' ? Boolean(options['livereload']) : true,
open: !!options['open'],
platform,
port,
proxy: typeof options['proxy'] === 'boolean' ? Boolean(options['proxy']) : true,
project: options['project'] ? String(options['project']) : undefined,
publicHost: options['public-host'] ? String(options['public-host']) : undefined,
verbose: !!options['verbose'],
};
}
determineEngineFromCommandLine(options: CommandLineOptions): string {
if (options['engine']) {
return String(options['engine']);
}
if (options['cordova']) {
return 'cordova';
}
return 'browser';
}
async beforeServe(options: T) {
const hook = new ServeBeforeHook(this.e);
try {
await hook.run({ name: hook.name, serve: options });
} catch (e: any) {
if (e instanceof BaseError) {
throw new FatalException(e.message);
}
throw e;
}
}
async run(options: T): Promise<ServeDetails> {
debug('serve options: %O', options);
await this.beforeServe(options);
const details = await this.serveProject(options);
const localAddress = `${details.protocol}://${options.publicHost ? options.publicHost : 'localhost'}:${details.port}`;
const fmtExternalAddress = (host: string) => `${details.protocol}://${host}:${details.port}`;
this.e.log.nl();
this.e.log.info(
`Development server running!` +
`\nLocal: ${strong(localAddress)}` +
(details.externalNetworkInterfaces.length > 0 ? `\nExternal: ${details.externalNetworkInterfaces.map(v => strong(fmtExternalAddress(v.address))).join(', ')}` : '') +
`\n\n${chalk.yellow('Use Ctrl+C to quit this process')}`
);
this.e.log.nl();
if (options.open) {
const openAddress = localAddress;
const url = this.modifyOpenUrl(openAddress, options);
await openUrl(url, { app: options.browser });
this.e.log.info(`Browser window opened to ${strong(url)}!`);
this.e.log.nl();
}
emit('serve:ready', details);
debug('serve details: %O', details);
this.scheduleAfterServe(options, details);
return details;
}
async afterServe(options: T, details: ServeDetails) {
const hook = new ServeAfterHook(this.e);
try {
await hook.run({ name: hook.name, serve: lodash.assign({}, options, details) });
} catch (e: any) {
if (e instanceof BaseError) {
throw new FatalException(e.message);
}
throw e;
}
}
scheduleAfterServe(options: T, details: ServeDetails) {
onBeforeExit(async () => this.afterServe(options, details));
}
getUsedPorts(options: T, details: ServeDetails): number[] {
return [details.port];
}
async selectExternalIP(options: T): Promise<[string, NetworkInterface[]]> {
let availableInterfaces: NetworkInterface[] = [];
let chosenIP = options.host;
if (options.host === BIND_ALL_ADDRESS) {
// ignore link-local addresses
availableInterfaces = getExternalIPv4Interfaces().filter(i => !i.address.startsWith('169.254'));
if (options.publicHost) {
chosenIP = options.publicHost;
} else {
if (availableInterfaces.length === 0) {
if (options.externalAddressRequired) {
throw new FatalException(
`No external network interfaces detected. In order to use the dev server externally you will need one.\n` +
`Are you connected to a local network?\n`
);
}
} else if (availableInterfaces.length === 1) {
chosenIP = availableInterfaces[0].address;
} else if (availableInterfaces.length > 1) {
if (options.externalAddressRequired) {
if (this.e.flags.interactive) {
this.e.log.warn(
'Multiple network interfaces detected!\n' +
`You will be prompted to select an external-facing IP for the dev server that your device or emulator can access. Make sure your device is on the same Wi-Fi network as your computer. Learn more about Live Reload in the docs${ancillary('[1]')}.\n\n` +
`To bypass this prompt, use the ${input('--public-host')} option (e.g. ${input(`--public-host=${availableInterfaces[0].address}`)}). You can alternatively bind the dev server to a specific IP (e.g. ${input(`--host=${availableInterfaces[0].address}`)}).\n\n` +
`${ancillary('[1]')}: ${strong('https://ion.link/livereload-docs')}\n`
);
const promptedIp = await this.e.prompt({
type: 'list',
name: 'promptedIp',
message: 'Please select which IP to use:',
choices: availableInterfaces.map(i => ({
name: `${i.address} ${weak(`(${i.device})`)}`,
value: i.address,
})),
});
chosenIP = promptedIp;
} else {
throw new FatalException(
`Multiple network interfaces detected!\n` +
`You must select an external-facing IP for the dev server that your device or emulator can access with the ${input('--public-host')} option.`
);
}
}
}
}
} else if (options.externalAddressRequired && LOCAL_ADDRESSES.includes(options.host)) {
this.e.log.warn(
'An external host may be required to serve for this target device/platform.\n' +
'If you get connection issues on your device or emulator, try connecting the device to the same Wi-Fi network and selecting an accessible IP address for your computer on that network.\n\n' +
`You can use ${input('--external')} to run the dev server on all network interfaces, in which case an external address will be selected.\n`
);
}
return [chosenIP, availableInterfaces];
}
}
class ServeBeforeHook extends Hook {
readonly name = 'serve:before';
}
class ServeAfterHook extends Hook {
readonly name = 'serve:after';
}
export interface ServeCLIOptions {
readonly host: string;
readonly port: number;
}
export interface ServeCLI<T extends ServeCLIOptions> {
emit(event: 'compile', chunks: number): boolean;
emit(event: 'ready'): boolean;
on(event: 'compile', handler: (chunks: number) => void): this;
on(event: 'ready', handler: () => void): this;
once(event: 'compile', handler: (chunks: number) => void): this;
once(event: 'ready', handler: () => void): this;
}
export abstract class ServeCLI<T extends ServeCLIOptions> extends EventEmitter {
/**
* The pretty name of this Serve CLI.
*/
abstract readonly name: string;
/**
* The npm package of this Serve CLI.
*/
abstract readonly pkg: string;
/**
* The bin program to use for this Serve CLI.
*/
abstract readonly program: string;
/**
* The prefix to use for log statements.
*/
abstract readonly prefix: string;
/**
* If specified, `package.json` is inspected for this script to use instead
* of `program`.
*/
abstract readonly script?: string;
/**
* If true, the Serve CLI will not prompt to be installed.
*/
readonly global: boolean = false;
private _resolvedProgram?: string;
constructor(protected readonly e: ServeRunnerDeps) {
super();
}
get resolvedProgram() {
if (this._resolvedProgram) {
return this._resolvedProgram;
}
return this.program;
}
/**
* Build the arguments for starting this Serve CLI. Called by `this.start()`.
*/
protected abstract buildArgs(options: T): Promise<string[]>;
/**
* Build the environment variables to be passed to the Serve CLI. Called by `this.start()`;
*/
protected async buildEnvVars(options: T): Promise<NodeJS.ProcessEnv> {
return process.env;
}
/**
* Called whenever a line of stdout is received.
*
* If `false` is returned, the line is not emitted to the log.
*
* By default, the CLI is considered ready whenever stdout is emitted. This
* method should be overridden to more accurately portray readiness.
*
* @param line A line of stdout.
*/
protected stdoutFilter(line: string): boolean {
this.emit('ready');
return true;
}
/**
* Called whenever a line of stderr is received.
*
* If `false` is returned, the line is not emitted to the log.
*/
protected stderrFilter(line: string): boolean {
return true;
}
async resolveScript(): Promise<string | undefined> {
if (typeof this.script === 'undefined') {
return;
}
const [pkg] = await this.e.project.getPackageJson(undefined, { logErrors: false });
if (!pkg) {
return;
}
return pkg.scripts && pkg.scripts[this.script];
}
async serve(options: T): Promise<void> {
this._resolvedProgram = await this.resolveProgram();
await this.spawnWrapper(options);
const interval = setInterval(() => {
this.e.log.info(`Waiting for connectivity with ${input(this.resolvedProgram)}...`);
}, 5000);
debug('awaiting TCP connection to %s:%d', options.host, options.port);
await isHostConnectable(options.host, options.port);
clearInterval(interval);
}
protected async spawnWrapper(options: T): Promise<void> {
try {
return await this.spawn(options);
} catch (e: any) {
if (!(e instanceof ServeCLIProgramNotFoundException)) {
throw e;
}
if (this.global) {
this.e.log.nl();
throw new FatalException(`${input(this.pkg)} is required for this command to work properly.`);
}
this.e.log.nl();
this.e.log.info(
`Looks like ${input(this.pkg)} isn't installed in this project.\n` +
`This package is required for this command to work properly. The package provides a CLI utility, but the ${input(this.resolvedProgram)} binary was not found in your PATH.`
);
const installed = await this.promptToInstall();
if (!installed) {
this.e.log.nl();
throw new FatalException(`${input(this.pkg)} is required for this command to work properly.`);
}
return this.spawn(options);
}
}
protected async spawn(options: T): Promise<void> {
const args = await this.buildArgs(options);
const env = await this.buildEnvVars(options);
const p = await this.e.shell.spawn(this.resolvedProgram, args, { stdio: ['inherit', 'pipe', 'pipe'], cwd: this.e.project.directory, env: createProcessEnv(env) });
return new Promise<void>((resolve, reject) => {
const errorHandler = (err: NodeJS.ErrnoException) => {
debug('received error for %s: %o', this.resolvedProgram, err);
if (this.resolvedProgram === this.program && err.code === 'ENOENT') {
p.removeListener('close', closeHandler); // do not exit Ionic CLI, we can gracefully ask to install this CLI
reject(new ServeCLIProgramNotFoundException(`${strong(this.resolvedProgram)} command not found.`));
} else {
reject(err);
}
};
const closeHandler = (code: number | null) => {
if (code !== null) {
debug('received unexpected close for %s (code: %d)', this.resolvedProgram, code);
this.e.log.nl();
this.e.log.error(
`${input(this.resolvedProgram)} has unexpectedly closed (exit code ${code}).\n` +
'The Ionic CLI will exit. Please check any output above for error details.'
);
processExit(1);
}
};
p.on('error', errorHandler);
p.on('close', closeHandler);
onBeforeExit(async () => {
p.removeListener('close', closeHandler);
if (p.pid) {
await killProcessTree(p.pid);
}
});
const ws = this.createLoggerStream();
p.stdout?.pipe(split2()).pipe(this.createStreamFilter(line => this.stdoutFilter(line))).pipe(ws);
p.stderr?.pipe(split2()).pipe(this.createStreamFilter(line => this.stderrFilter(line))).pipe(ws);
this.once('ready', () => {
resolve();
});
});
}
protected createLoggerStream(): NodeJS.WritableStream {
const log = this.e.log.clone();
log.handlers = createDefaultLoggerHandlers(createPrefixedFormatter(weak(`[${this.resolvedProgram === this.program ? this.prefix : this.resolvedProgram}]`)));
return log.createWriteStream(LOGGER_LEVELS.INFO);
}
protected async resolveProgram(): Promise<string> {
if (typeof this.script !== 'undefined') {
debug(`Looking for ${ancillary(this.script)} npm script.`);
if (await this.resolveScript()) {
debug(`Using ${ancillary(this.script)} npm script.`);
return this.e.config.get('npmClient');
}
}
return this.program;
}
protected createStreamFilter(filter: (line: string) => boolean): stream.Transform {
return new stream.Transform({
transform(chunk, enc, callback) {
const str = chunk.toString();
if (filter(str)) {
this.push(chunk);
}
callback();
},
});
}
protected async promptToInstall(): Promise<boolean> {
const { pkgManagerArgs } = await import('./utils/npm');
const [manager, ...managerArgs] = await pkgManagerArgs(this.e.config.get('npmClient'), { command: 'install', pkg: this.pkg, saveDev: true, saveExact: true });
this.e.log.nl();
const confirm = await this.e.prompt({
name: 'confirm',
message: `Install ${input(this.pkg)}?`,
type: 'confirm',
});
if (!confirm) {
this.e.log.warn(`Not installing--here's how to install manually: ${input(`${manager} ${managerArgs.join(' ')}`)}`);
return false;
}
await this.e.shell.run(manager, managerArgs, { cwd: this.e.project.directory });
return true;
}
}
abstract class PkgManagerServeCLI extends ServeCLI<ServeOptions> {
readonly abstract program: NpmClient;
readonly global = true;
readonly script = SERVE_SCRIPT;
protected async resolveProgram(): Promise<string> {
return this.program;
}
protected async buildArgs(options: ServeOptions): Promise<string[]> {
const { pkgManagerArgs } = await import('./utils/npm');
// The Ionic CLI decides the host/port of the dev server, so --host and
// --port are provided to the downstream npm script as a best-effort
// attempt.
const args: ParsedArgs = {
_: [],
host: options.host,
port: options.port.toString(),
};
const scriptArgs = [...unparseArgs(args), ...options['--'] || []];
const [, ...pkgArgs] = await pkgManagerArgs(this.program, { command: 'run', script: this.script, scriptArgs });
return pkgArgs;
}
}
export class NpmServeCLI extends PkgManagerServeCLI {
readonly name = 'npm CLI';
readonly pkg = 'npm';
readonly program = 'npm';
readonly prefix = 'npm';
}
export class PnpmServeCLI extends PkgManagerServeCLI {
readonly name = 'pnpm CLI';
readonly pkg = 'pnpm';
readonly program = 'pnpm';
readonly prefix = 'pnpm';
}
export class YarnServeCLI extends PkgManagerServeCLI {
readonly name = 'Yarn';
readonly pkg = 'yarn';
readonly program = 'yarn';
readonly prefix = 'yarn';
}
export class BunServeCLI extends PkgManagerServeCLI {
readonly name = 'Bun';
readonly pkg = 'bun';
readonly program = 'bun';
readonly prefix = 'bun';
}