UNPKG

7.76 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 project_1 = require("../lib/project");
10const serve_1 = require("../lib/serve");
11const emoji_1 = require("../lib/utils/emoji");
12class ServeCommand extends command_1.Command {
13 async getMetadata() {
14 const groups = [];
15 let options = [
16 ...serve_1.COMMON_SERVE_COMMAND_OPTIONS,
17 {
18 name: 'lab-host',
19 summary: 'Use specific address for Ionic Lab server',
20 default: 'localhost',
21 groups: ["advanced" /* ADVANCED */],
22 spec: { value: 'host' },
23 },
24 {
25 name: 'lab-port',
26 summary: 'Use specific port for Ionic Lab server',
27 default: serve_1.DEFAULT_LAB_PORT.toString(),
28 groups: ["advanced" /* ADVANCED */],
29 spec: { value: 'port' },
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
66To target the DevApp, use the ${color_1.input('--devapp')} option.`;
67 const runner = this.project && await this.project.getServeRunner();
68 if (runner) {
69 const libmetadata = await runner.getCommandMetadata();
70 groups.push(...libmetadata.groups || []);
71 options = lodash.uniqWith([...libmetadata.options || [], ...options], (optionA, optionB) => optionA.name === optionB.name);
72 description += `\n\n${(libmetadata.description || '').trim()}`;
73 footnotes.push(...libmetadata.footnotes || []);
74 exampleCommands.push(...libmetadata.exampleCommands || []);
75 }
76 return {
77 name: 'serve',
78 type: 'project',
79 summary: 'Start a local dev server for app dev/testing',
80 description,
81 footnotes,
82 groups,
83 exampleCommands,
84 options,
85 };
86 }
87 async preRun(inputs, options, { location }) {
88 const parts = executor_1.getFullCommandParts(location);
89 const alias = lodash.last(parts);
90 if (alias === 'lab') {
91 options['lab'] = true;
92 }
93 if (options['nolivereload']) {
94 this.env.log.warn(`The ${color_1.input('--nolivereload')} option has been deprecated. Please use ${color_1.input('--no-livereload')}.`);
95 options['livereload'] = false;
96 }
97 if (options['nobrowser']) {
98 this.env.log.warn(`The ${color_1.input('--nobrowser')} option has been deprecated. Please use ${color_1.input('--no-open')}.`);
99 options['open'] = false;
100 }
101 if (options['b']) {
102 options['open'] = false;
103 }
104 if (options['noproxy']) {
105 this.env.log.warn(`The ${color_1.input('--noproxy')} option has been deprecated. Please use ${color_1.input('--no-proxy')}.`);
106 options['proxy'] = false;
107 }
108 if (options['x']) {
109 options['proxy'] = false;
110 }
111 }
112 async run(inputs, options, runinfo) {
113 if (!this.project) {
114 throw new errors_1.FatalException(`Cannot run ${color_1.input('ionic serve')} outside a project directory.`);
115 }
116 try {
117 const { SUPPORTED_PROJECT_TYPES } = await Promise.resolve().then(() => require('../lib/integrations/cordova'));
118 const runner = await this.project.requireServeRunner();
119 const runnerOpts = runner.createOptionsFromCommandLine(inputs, options);
120 if (runnerOpts.devapp) {
121 if (!SUPPORTED_PROJECT_TYPES.includes(this.project.type)) {
122 throw new errors_1.FatalException(`Ionic doesn't support using the DevApp with ${color_1.input(project_1.prettyProjectName(this.project.type))} projects.\n` +
123 `This is because the DevApp runs on Cordova, but Cordova isn't supported by ${color_1.input(project_1.prettyProjectName(this.project.type))} projects. To develop your app on a device, we recommend using ${emoji_1.emoji('⚡️ ', '')}${color_1.strong('Capacitor')}${emoji_1.emoji(' ⚡️', '')} (${color_1.strong('https://ion.link/capacitor')})\n\n` +
124 `To learn more about running your project with Capacitor, see the docs for iOS${color_1.ancillary('[1]')} and Android${color_1.ancillary('[2]')}.\n\n` +
125 `${color_1.ancillary('[1]')}: ${color_1.strong('https://ion.link/running-ios-docs#running-with-the-ionic-cli')}\n` +
126 `${color_1.ancillary('[2]')}: ${color_1.strong('https://ion.link/running-android-docs#running-with-capacitor')}\n`);
127 }
128 const cordova = this.project.getIntegration('cordova');
129 if (!cordova) {
130 await executor_1.runCommand(runinfo, ['integrations', 'enable', 'cordova']);
131 }
132 }
133 await runner.run(runnerOpts);
134 }
135 catch (e) {
136 if (e instanceof errors_1.RunnerException) {
137 throw new errors_1.FatalException(e.message);
138 }
139 throw e;
140 }
141 await utils_process_1.sleepForever();
142 }
143}
144exports.ServeCommand = ServeCommand;
145class LabCommand extends ServeCommand {
146 async getMetadata() {
147 const metadata = await super.getMetadata();
148 const groups = [...metadata.groups || [], "hidden" /* HIDDEN */];
149 const exampleCommands = [...metadata.exampleCommands || []].filter(c => !c.includes('--lab'));
150 return {
151 ...metadata,
152 summary: 'Start Ionic Lab for multi-platform dev/testing',
153 description: `
154Start an instance of ${color_1.strong('Ionic Lab')}, a tool for developing Ionic apps for multiple platforms at once side-by-side.
155
156${color_1.input('ionic lab')} is just a convenient shortcut for ${color_1.input('ionic serve --lab')}.`,
157 groups,
158 exampleCommands,
159 };
160 }
161}
162exports.LabCommand = LabCommand;