UNPKG

@ionic/cli-utils

Version:
145 lines (143 loc) 6.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const chalk_1 = require("chalk"); const cli_framework_1 = require("@ionic/cli-framework"); const utils_network_1 = require("@ionic/utils-network"); const serve_1 = require("../../serve"); const NG_SERVE_OPTIONS = [ { name: 'configuration', summary: 'Specify the configuration to use.', type: String, groups: [cli_framework_1.OptionGroup.Advanced], hint: chalk_1.default.dim('[ng]'), }, ]; class AngularServeRunner extends serve_1.ServeRunner { constructor(e) { super(); this.e = e; } getCommandMetadata() { return tslib_1.__awaiter(this, void 0, void 0, function* () { return { groups: [cli_framework_1.CommandGroup.Beta], description: ` ${chalk_1.default.green('ionic serve')} uses the Angular CLI. Use ${chalk_1.default.green('ng serve --help')} to list all Angular CLI options for serving your app. See the ${chalk_1.default.green('ng serve')} docs${chalk_1.default.cyan('[1]')} for explanations. Options not listed below are considered advanced and can be passed to the Angular CLI using the ${chalk_1.default.green('--')} separator after the Ionic CLI arguments. See the examples. ${chalk_1.default.cyan('[1]')}: ${chalk_1.default.bold('https://github.com/angular/angular-cli/wiki/serve')}`, options: [ { name: 'prod', summary: `Flag to use the ${chalk_1.default.green('production')} configuration`, type: Boolean, hint: chalk_1.default.dim('[ng]'), }, { name: 'source-map', summary: 'Output sourcemaps', type: Boolean, groups: [cli_framework_1.OptionGroup.Advanced], hint: chalk_1.default.dim('[ng]'), }, ...NG_SERVE_OPTIONS, ], exampleCommands: [ '-- --proxy-config proxy.conf.json', ], }; }); } createOptionsFromCommandLine(inputs, options) { const baseOptions = super.createOptionsFromCommandLine(inputs, options); const prod = options['prod'] ? Boolean(options['prod']) : undefined; const configuration = options['configuration'] ? String(options['configuration']) : (prod ? 'production' : undefined); const sourcemaps = typeof options['source-map'] === 'boolean' ? Boolean(options['source-map']) : undefined; return Object.assign({}, baseOptions, { configuration, sourcemaps }); } platformToMode(platform) { if (platform === 'ios') { return 'ios'; } return 'md'; } modifyOpenURL(url, options) { return `${url}${options.browserOption ? options.browserOption : ''}${options.platform ? `?ionic:mode=${this.platformToMode(options.platform)}` : ''}`; } serveProject(options) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const [externalIP, availableInterfaces] = yield this.selectExternalIP(options); const port = options.port = yield utils_network_1.findClosestOpenPort(options.port); const ng = new AngularServeCLI(this.e); yield ng.serve(options); return { custom: ng.resolvedProgram !== ng.program, protocol: 'http', localAddress: 'localhost', externalAddress: externalIP, externalNetworkInterfaces: availableInterfaces, port, externallyAccessible: ![serve_1.BIND_ALL_ADDRESS, ...serve_1.LOCAL_ADDRESSES].includes(externalIP), }; }); } } exports.AngularServeRunner = AngularServeRunner; class AngularServeCLI extends serve_1.ServeCLI { constructor() { super(...arguments); this.name = 'Angular CLI'; this.pkg = '@angular/cli'; this.program = 'ng'; this.prefix = 'ng'; this.script = serve_1.SERVE_SCRIPT; } stdoutFilter(line) { if (this.resolvedProgram !== this.program) { return super.stdoutFilter(line); } if (line.includes('Development Server is listening')) { this.emit('ready'); return false; } return true; } buildArgs(options) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const { pkgManagerArgs } = yield Promise.resolve().then(() => require('../../utils/npm')); const args = yield this.serveOptionsToNgArgs(options); if (this.resolvedProgram === this.program) { return [...this.buildArchitectCommand(options), ...args]; } else { const [, ...pkgArgs] = yield pkgManagerArgs(this.e.config.get('npmClient'), { command: 'run', script: this.script, scriptArgs: [...args] }); return pkgArgs; } }); } serveOptionsToNgArgs(options) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const args = { _: [], host: options.address, port: String(options.port), 'source-map': options.sourcemaps !== false ? options.sourcemaps : 'false', }; if (options.engine === 'cordova') { const integration = yield this.e.project.getIntegration('cordova'); args.platform = options.platform; if (this.e.project.directory !== integration.root) { args.cordovaBasePath = integration.root; } } return [...cli_framework_1.unparseArgs(args), ...options['--']]; }); } buildArchitectCommand(options) { const cmd = options.engine === 'cordova' ? 'ionic-cordova-serve' : 'serve'; const project = options.project ? options.project : 'app'; return ['run', `${project}:${cmd}${options.configuration ? `:${options.configuration}` : ''}`]; } } exports.AngularServeCLI = AngularServeCLI;