UNPKG

6.19 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const utils_process_1 = require("@ionic/utils-process");
4const lodash = require("lodash");
5const color_1 = require("../lib/color");
6const command_1 = require("../lib/command");
7const errors_1 = require("../lib/errors");
8const executor_1 = require("../lib/executor");
9const serve_1 = require("../lib/serve");
10class ServeCommand extends command_1.Command {
11 async getMetadata() {
12 const groups = [];
13 let options = [
14 ...serve_1.COMMON_SERVE_COMMAND_OPTIONS,
15 {
16 name: 'lab-host',
17 summary: 'Use specific host for Ionic Lab server',
18 default: 'localhost',
19 groups: ["advanced" /* ADVANCED */],
20 spec: { value: 'host' },
21 hint: color_1.weak('(--lab)'),
22 },
23 {
24 name: 'lab-port',
25 summary: 'Use specific port for Ionic Lab server',
26 default: serve_1.DEFAULT_LAB_PORT.toString(),
27 groups: ["advanced" /* ADVANCED */],
28 spec: { value: 'port' },
29 hint: color_1.weak('(--lab)'),
30 },
31 {
32 name: 'open',
33 summary: 'Do not open a browser window',
34 type: Boolean,
35 default: true,
36 },
37 {
38 name: 'browser',
39 summary: `Specifies the browser to use (${serve_1.BROWSERS.map(b => color_1.input(b)).join(', ')})`,
40 aliases: ['w'],
41 groups: ["advanced" /* ADVANCED */],
42 },
43 {
44 name: 'browseroption',
45 summary: `Specifies a path to open to (${color_1.input('/#/tab/dash')})`,
46 aliases: ['o'],
47 groups: ["advanced" /* ADVANCED */],
48 spec: { value: 'path' },
49 },
50 {
51 name: 'lab',
52 summary: 'Test your apps on multiple platform types in the browser',
53 type: Boolean,
54 aliases: ['l'],
55 },
56 ];
57 const exampleCommands = ['', '--external', '--lab'];
58 const footnotes = [];
59 let description = `
60Easily spin up a development server which launches in your browser. It watches for changes in your source files and automatically reloads with the updated build.
61
62By default, ${color_1.input('ionic serve')} boots up a development server on ${color_1.input('localhost')}. To serve to your LAN, specify the ${color_1.input('--external')} option, which will use all network interfaces and print the external address(es) on which your app is being served.
63
64Try the ${color_1.input('--lab')} option to see multiple platforms at once.`;
65 const runner = this.project && await this.project.getServeRunner();
66 if (runner) {
67 const libmetadata = await runner.getCommandMetadata();
68 groups.push(...libmetadata.groups || []);
69 options = lodash.uniqWith([...libmetadata.options || [], ...options], (optionA, optionB) => optionA.name === optionB.name);
70 description += `\n\n${(libmetadata.description || '').trim()}`;
71 footnotes.push(...libmetadata.footnotes || []);
72 exampleCommands.push(...libmetadata.exampleCommands || []);
73 }
74 return {
75 name: 'serve',
76 type: 'project',
77 summary: 'Start a local dev server for app dev/testing',
78 description,
79 footnotes,
80 groups,
81 exampleCommands,
82 options,
83 };
84 }
85 async preRun(inputs, options, { location }) {
86 const parts = executor_1.getFullCommandParts(location);
87 const alias = lodash.last(parts);
88 if (alias === 'lab') {
89 options['lab'] = true;
90 }
91 if (options['nolivereload']) {
92 this.env.log.warn(`The ${color_1.input('--nolivereload')} option has been deprecated. Please use ${color_1.input('--no-livereload')}.`);
93 options['livereload'] = false;
94 }
95 if (options['nobrowser']) {
96 this.env.log.warn(`The ${color_1.input('--nobrowser')} option has been deprecated. Please use ${color_1.input('--no-open')}.`);
97 options['open'] = false;
98 }
99 if (options['b']) {
100 options['open'] = false;
101 }
102 if (options['noproxy']) {
103 this.env.log.warn(`The ${color_1.input('--noproxy')} option has been deprecated. Please use ${color_1.input('--no-proxy')}.`);
104 options['proxy'] = false;
105 }
106 if (options['x']) {
107 options['proxy'] = false;
108 }
109 }
110 async run(inputs, options, runinfo) {
111 if (!this.project) {
112 throw new errors_1.FatalException(`Cannot run ${color_1.input('ionic serve')} outside a project directory.`);
113 }
114 try {
115 const runner = await this.project.requireServeRunner();
116 const runnerOpts = runner.createOptionsFromCommandLine(inputs, options);
117 await runner.run(runnerOpts);
118 }
119 catch (e) {
120 if (e instanceof errors_1.RunnerException) {
121 throw new errors_1.FatalException(e.message);
122 }
123 throw e;
124 }
125 await utils_process_1.sleepForever();
126 }
127}
128exports.ServeCommand = ServeCommand;
129class LabCommand extends ServeCommand {
130 async getMetadata() {
131 const metadata = await super.getMetadata();
132 const groups = [...metadata.groups || [], "hidden" /* HIDDEN */];
133 const exampleCommands = [...metadata.exampleCommands || []].filter(c => !c.includes('--lab'));
134 return {
135 ...metadata,
136 summary: 'Start Ionic Lab for multi-platform dev/testing',
137 description: `
138Start an instance of ${color_1.strong('Ionic Lab')}, a tool for developing Ionic apps for multiple platforms at once side-by-side.
139
140${color_1.input('ionic lab')} is just a convenient shortcut for ${color_1.input('ionic serve --lab')}.`,
141 groups,
142 exampleCommands,
143 };
144 }
145}
146exports.LabCommand = LabCommand;