native-run.js 5.0 KB

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