index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.runProgram = exports.run = void 0;
  4. const tslib_1 = require("tslib");
  5. const commander_1 = require("commander");
  6. const path_1 = require("path");
  7. const colors_1 = tslib_1.__importDefault(require("./colors"));
  8. const config_1 = require("./config");
  9. const errors_1 = require("./errors");
  10. const ipc_1 = require("./ipc");
  11. const log_1 = require("./log");
  12. const telemetry_1 = require("./telemetry");
  13. const cli_1 = require("./util/cli");
  14. const emoji_1 = require("./util/emoji");
  15. process.on('unhandledRejection', error => {
  16. console.error(colors_1.default.failure('[fatal]'), error);
  17. });
  18. process.on('message', ipc_1.receive);
  19. async function run() {
  20. try {
  21. const config = await (0, config_1.loadConfig)();
  22. runProgram(config);
  23. }
  24. catch (e) {
  25. process.exitCode = (0, errors_1.isFatal)(e) ? e.exitCode : 1;
  26. log_1.logger.error(e.message ? e.message : String(e));
  27. }
  28. }
  29. exports.run = run;
  30. function runProgram(config) {
  31. commander_1.program.version(config.cli.package.version);
  32. commander_1.program
  33. .command('config', { hidden: true })
  34. .description(`print evaluated Capacitor config`)
  35. .option('--json', 'Print in JSON format')
  36. .action((0, cli_1.wrapAction)(async ({ json }) => {
  37. const { configCommand } = await Promise.resolve().then(() => tslib_1.__importStar(require('./tasks/config')));
  38. await configCommand(config, json);
  39. }));
  40. commander_1.program
  41. .command('create [directory] [name] [id]', { hidden: true })
  42. .description('Creates a new Capacitor project')
  43. .action((0, cli_1.wrapAction)(async () => {
  44. const { createCommand } = await Promise.resolve().then(() => tslib_1.__importStar(require('./tasks/create')));
  45. await createCommand();
  46. }));
  47. commander_1.program
  48. .command('init [appName] [appId]')
  49. .description(`Initialize Capacitor configuration`)
  50. .option('--web-dir <value>', 'Optional: Directory of your projects built web assets')
  51. .action((0, cli_1.wrapAction)((0, telemetry_1.telemetryAction)(config, async (appName, appId, { webDir }) => {
  52. const { initCommand } = await Promise.resolve().then(() => tslib_1.__importStar(require('./tasks/init')));
  53. await initCommand(config, appName, appId, webDir);
  54. })));
  55. commander_1.program
  56. .command('serve', { hidden: true })
  57. .description('Serves a Capacitor Progressive Web App in the browser')
  58. .action((0, cli_1.wrapAction)(async () => {
  59. const { serveCommand } = await Promise.resolve().then(() => tslib_1.__importStar(require('./tasks/serve')));
  60. await serveCommand();
  61. }));
  62. commander_1.program
  63. .command('sync [platform]')
  64. .description(`${colors_1.default.input('copy')} + ${colors_1.default.input('update')}`)
  65. .option('--deployment', 'Optional: if provided, pod install will use --deployment option')
  66. .option('--inline', 'Optional: if true, all source maps will be inlined for easier debugging on mobile devices', false)
  67. .action((0, cli_1.wrapAction)((0, telemetry_1.telemetryAction)(config, async (platform, { deployment, inline }) => {
  68. (0, config_1.checkExternalConfig)(config.app);
  69. const { syncCommand } = await Promise.resolve().then(() => tslib_1.__importStar(require('./tasks/sync')));
  70. await syncCommand(config, platform, deployment, inline);
  71. })));
  72. commander_1.program
  73. .command('update [platform]')
  74. .description(`updates the native plugins and dependencies based on ${colors_1.default.strong('package.json')}`)
  75. .option('--deployment', 'Optional: if provided, pod install will use --deployment option')
  76. .action((0, cli_1.wrapAction)((0, telemetry_1.telemetryAction)(config, async (platform, { deployment }) => {
  77. (0, config_1.checkExternalConfig)(config.app);
  78. const { updateCommand } = await Promise.resolve().then(() => tslib_1.__importStar(require('./tasks/update')));
  79. await updateCommand(config, platform, deployment);
  80. })));
  81. commander_1.program
  82. .command('copy [platform]')
  83. .description('copies the web app build into the native app')
  84. .option('--inline', 'Optional: if true, all source maps will be inlined for easier debugging on mobile devices', false)
  85. .action((0, cli_1.wrapAction)((0, telemetry_1.telemetryAction)(config, async (platform, { inline }) => {
  86. (0, config_1.checkExternalConfig)(config.app);
  87. const { copyCommand } = await Promise.resolve().then(() => tslib_1.__importStar(require('./tasks/copy')));
  88. await copyCommand(config, platform, inline);
  89. })));
  90. commander_1.program
  91. .command('build <platform>')
  92. .description('builds the release version of the selected platform')
  93. .option('--scheme <schemeToBuild>', 'iOS Scheme to build')
  94. .option('--flavor <flavorToBuild>', 'Android Flavor to build')
  95. .option('--keystorepath <keystorePath>', 'Path to the keystore')
  96. .option('--keystorepass <keystorePass>', 'Password to the keystore')
  97. .option('--keystorealias <keystoreAlias>', 'Key Alias in the keystore')
  98. .option('--configuration <name>', 'Configuration name of the iOS Scheme')
  99. .option('--keystorealiaspass <keystoreAliasPass>', 'Password for the Key Alias')
  100. .addOption(new commander_1.Option('--androidreleasetype <androidreleasetype>', 'Android release type; APK or AAB').choices(['AAB', 'APK']))
  101. .addOption(new commander_1.Option('--signing-type <signingtype>', 'Program used to sign apps (default: jarsigner)').choices(['apksigner', 'jarsigner']))
  102. .action((0, cli_1.wrapAction)((0, telemetry_1.telemetryAction)(config, async (platform, { scheme, flavor, keystorepath, keystorepass, keystorealias, keystorealiaspass, androidreleasetype, signingType, configuration, }) => {
  103. const { buildCommand } = await Promise.resolve().then(() => tslib_1.__importStar(require('./tasks/build')));
  104. await buildCommand(config, platform, {
  105. scheme,
  106. flavor,
  107. keystorepath,
  108. keystorepass,
  109. keystorealias,
  110. keystorealiaspass,
  111. androidreleasetype,
  112. signingtype: signingType,
  113. configuration,
  114. });
  115. })));
  116. commander_1.program
  117. .command(`run [platform]`)
  118. .description(`runs ${colors_1.default.input('sync')}, then builds and deploys the native app`)
  119. .option('--scheme <schemeName>', 'set the scheme of the iOS project')
  120. .option('--flavor <flavorName>', 'set the flavor of the Android project (flavor dimensions not yet supported)')
  121. .option('--list', 'list targets, then quit')
  122. // TODO: remove once --json is a hidden option (https://github.com/tj/commander.js/issues/1106)
  123. .allowUnknownOption(true)
  124. .option('--target <id>', 'use a specific target')
  125. .option('--no-sync', `do not run ${colors_1.default.input('sync')}`)
  126. .option('--forwardPorts <port:port>', 'Automatically run "adb reverse" for better live-reloading support')
  127. .option('-l, --live-reload', 'Enable Live Reload')
  128. .option('--host <host>', 'Host used for live reload')
  129. .option('--port <port>', 'Port used for live reload')
  130. .option('--configuration <name>', 'Configuration name of the iOS Scheme')
  131. .action((0, cli_1.wrapAction)((0, telemetry_1.telemetryAction)(config, async (platform, { scheme, flavor, list, target, sync, forwardPorts, liveReload, host, port, configuration, }) => {
  132. const { runCommand } = await Promise.resolve().then(() => tslib_1.__importStar(require('./tasks/run')));
  133. await runCommand(config, platform, {
  134. scheme,
  135. flavor,
  136. list,
  137. target,
  138. sync,
  139. forwardPorts,
  140. liveReload,
  141. host,
  142. port,
  143. configuration,
  144. });
  145. })));
  146. commander_1.program
  147. .command('open [platform]')
  148. .description('opens the native project workspace (Xcode for iOS)')
  149. .action((0, cli_1.wrapAction)((0, telemetry_1.telemetryAction)(config, async (platform) => {
  150. const { openCommand } = await Promise.resolve().then(() => tslib_1.__importStar(require('./tasks/open')));
  151. await openCommand(config, platform);
  152. })));
  153. commander_1.program
  154. .command('add [platform]')
  155. .description('add a native platform project')
  156. .option('--packagemanager <packageManager>', 'The package manager to use for dependency installs (Cocoapods, SPM **experimental**)')
  157. .action((0, cli_1.wrapAction)((0, telemetry_1.telemetryAction)(config, async (platform, { packagemanager }) => {
  158. (0, config_1.checkExternalConfig)(config.app);
  159. const { addCommand } = await Promise.resolve().then(() => tslib_1.__importStar(require('./tasks/add')));
  160. const configWritable = config;
  161. if (packagemanager === 'SPM') {
  162. configWritable.cli.assets.ios.platformTemplateArchive =
  163. 'ios-spm-template.tar.gz';
  164. configWritable.cli.assets.ios.platformTemplateArchiveAbs = (0, path_1.resolve)(configWritable.cli.assetsDirAbs, configWritable.cli.assets.ios.platformTemplateArchive);
  165. }
  166. await addCommand(configWritable, platform);
  167. })));
  168. commander_1.program
  169. .command('ls [platform]')
  170. .description('list installed Cordova and Capacitor plugins')
  171. .action((0, cli_1.wrapAction)((0, telemetry_1.telemetryAction)(config, async (platform) => {
  172. (0, config_1.checkExternalConfig)(config.app);
  173. const { listCommand } = await Promise.resolve().then(() => tslib_1.__importStar(require('./tasks/list')));
  174. await listCommand(config, platform);
  175. })));
  176. commander_1.program
  177. .command('doctor [platform]')
  178. .description('checks the current setup for common errors')
  179. .action((0, cli_1.wrapAction)((0, telemetry_1.telemetryAction)(config, async (platform) => {
  180. (0, config_1.checkExternalConfig)(config.app);
  181. const { doctorCommand } = await Promise.resolve().then(() => tslib_1.__importStar(require('./tasks/doctor')));
  182. await doctorCommand(config, platform);
  183. })));
  184. commander_1.program
  185. .command('telemetry [on|off]', { hidden: true })
  186. .description('enable or disable telemetry')
  187. .action((0, cli_1.wrapAction)(async (onOrOff) => {
  188. const { telemetryCommand } = await Promise.resolve().then(() => tslib_1.__importStar(require('./tasks/telemetry')));
  189. await telemetryCommand(onOrOff);
  190. }));
  191. commander_1.program
  192. .command('📡', { hidden: true })
  193. .description('IPC receiver command')
  194. .action(() => {
  195. // no-op: IPC messages are received via `process.on('message')`
  196. });
  197. commander_1.program
  198. .command('plugin:generate', { hidden: true })
  199. .description('start a new Capacitor plugin')
  200. .action((0, cli_1.wrapAction)(async () => {
  201. const { newPluginCommand } = await Promise.resolve().then(() => tslib_1.__importStar(require('./tasks/new-plugin')));
  202. await newPluginCommand();
  203. }));
  204. commander_1.program
  205. .command('migrate')
  206. .option('--noprompt', 'do not prompt for confirmation')
  207. .option('--packagemanager <packageManager>', 'The package manager to use for dependency installs (npm, pnpm, yarn)')
  208. .description('Migrate your current Capacitor app to the latest major version of Capacitor.')
  209. .action((0, cli_1.wrapAction)(async ({ noprompt, packagemanager }) => {
  210. const { migrateCommand } = await Promise.resolve().then(() => tslib_1.__importStar(require('./tasks/migrate')));
  211. await migrateCommand(config, noprompt, packagemanager);
  212. }));
  213. commander_1.program.arguments('[command]').action((0, cli_1.wrapAction)(async (cmd) => {
  214. if (typeof cmd === 'undefined') {
  215. log_1.output.write(`\n ${(0, emoji_1.emoji)('⚡️', '--')} ${colors_1.default.strong('Capacitor - Cross-Platform apps with JavaScript and the Web')} ${(0, emoji_1.emoji)('⚡️', '--')}\n\n`);
  216. commander_1.program.outputHelp();
  217. }
  218. else {
  219. (0, errors_1.fatal)(`Unknown command: ${colors_1.default.input(cmd)}`);
  220. }
  221. }));
  222. commander_1.program.parse(process.argv);
  223. }
  224. exports.runProgram = runProgram;