UNPKG

4.95 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const utils_process_1 = require("@ionic/utils-process");
4const utils_subprocess_1 = require("@ionic/utils-subprocess");
5const color_1 = require("./color");
6const errors_1 = require("./errors");
7const logger_1 = require("./utils/logger");
8const npm_1 = require("./utils/npm");
9exports.SUPPORTED_PLATFORMS = ['ios', 'android'];
10function createNativeRunArgs({ packagePath, platform, forwardedPorts = [] }, options) {
11 const opts = [platform, '--app', packagePath];
12 const target = options['target'] ? String(options['target']) : undefined;
13 if (target) {
14 opts.push('--target', target);
15 }
16 else if (options['emulator']) {
17 opts.push('--virtual');
18 }
19 else if (options['device']) {
20 opts.push('--device');
21 }
22 if (options['connect']) {
23 opts.push('--connect');
24 }
25 for (const port of forwardedPorts) {
26 opts.push('--forward', `${port}:${port}`);
27 }
28 if (options['json']) {
29 opts.push('--json');
30 }
31 if (options['verbose']) {
32 opts.push('--verbose');
33 }
34 return opts;
35}
36exports.createNativeRunArgs = createNativeRunArgs;
37function createNativeRunListArgs(inputs, options) {
38 const args = [];
39 if (inputs[0]) {
40 args.push(inputs[0]);
41 }
42 args.push('--list');
43 if (options['json']) {
44 args.push('--json');
45 }
46 if (options['device']) {
47 args.push('--device');
48 }
49 if (options['emulator']) {
50 args.push('--virtual');
51 }
52 if (options['json']) {
53 args.push('--json');
54 }
55 return args;
56}
57exports.createNativeRunListArgs = createNativeRunListArgs;
58async function runNativeRun({ config, log, shell }, args, options = {}) {
59 const connect = args.includes('--connect');
60 const stream = logger_1.createPrefixedWriteStream(log, color_1.weak(`[native-run]`));
61 try {
62 await shell.run('native-run', args, { showCommand: !args.includes('--json'), fatalOnNotFound: false, stream, ...options });
63 }
64 catch (e) {
65 if (e instanceof utils_subprocess_1.SubprocessError && e.code === utils_subprocess_1.ERROR_COMMAND_NOT_FOUND) {
66 throw createNativeRunNotFoundError(config.get('npmClient'));
67 }
68 throw e;
69 }
70 // If we connect the `native-run` process to the running app, then we
71 // should also connect the Ionic CLI with the running `native-run` process.
72 // This will exit the Ionic CLI when `native-run` exits.
73 if (connect) {
74 utils_process_1.processExit(0); // tslint:disable-line:no-floating-promises
75 }
76}
77exports.runNativeRun = runNativeRun;
78async function checkNativeRun({ config }) {
79 const p = await findNativeRun();
80 if (!p) {
81 throw await createNativeRunNotFoundError(config.get('npmClient'));
82 }
83}
84exports.checkNativeRun = checkNativeRun;
85async function findNativeRun() {
86 try {
87 return await utils_subprocess_1.which('native-run');
88 }
89 catch (e) {
90 if (e.code !== 'ENOENT') {
91 throw e;
92 }
93 }
94}
95exports.findNativeRun = findNativeRun;
96async function createNativeRunNotFoundError(npmClient) {
97 const installArgs = await npm_1.pkgManagerArgs(npmClient, { command: 'install', pkg: 'native-run', global: true });
98 return new errors_1.FatalException(`${color_1.input('native-run')} was not found on your PATH. Please install it globally:\n` +
99 `${color_1.input(installArgs.join(' '))}\n`);
100}
101async function getNativeTargets({ log, shell }, platform) {
102 try {
103 const proc = await shell.createSubprocess('native-run', [platform, '--list', '--json']);
104 const output = await proc.output();
105 return JSON.parse(output);
106 }
107 catch (e) {
108 if (e instanceof utils_subprocess_1.SubprocessError && e.code === utils_subprocess_1.ERROR_NON_ZERO_EXIT) {
109 const output = e.output ? JSON.parse(e.output) : {};
110 throw new errors_1.FatalException(`Error while getting native targets for ${color_1.input(platform)}: ${output.error || output.code}\n` +
111 (platform === 'android' && output.code === 'ERR_UNSUITABLE_API_INSTALLATION' ?
112 (`\n${color_1.input('native-run')} needs a fully installed SDK Platform to run your app.\n` +
113 `- Run ${color_1.input('native-run android --sdk-info')} to see missing packages for each API level.\n` +
114 `- Install missing packages in Android Studio by opening the SDK manager.\n`) : '') +
115 `\nThis error occurred while using ${color_1.input('native-run')}. You can try running this command with ${color_1.input('--no-native-run')}, which will revert to using Cordova.\n`);
116 }
117 log.warn(`Error while getting native targets for ${color_1.input(platform)}:\n${e.stack ? e.stack : e}`);
118 }
119 return { devices: [], virtualDevices: [] };
120}
121exports.getNativeTargets = getNativeTargets;