sync.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.SyncCommand = void 0;
  4. const tslib_1 = require("tslib");
  5. const cli_framework_1 = require("@ionic/cli-framework");
  6. const color_1 = require("../../lib/color");
  7. const errors_1 = require("../../lib/errors");
  8. const hooks_1 = require("../../lib/hooks");
  9. const base_1 = require("./base");
  10. const semver = tslib_1.__importStar(require("semver"));
  11. class SyncCommand extends base_1.CapacitorCommand {
  12. async getMetadata() {
  13. const options = [
  14. {
  15. name: 'build',
  16. summary: 'Do not invoke an Ionic build',
  17. type: Boolean,
  18. default: true,
  19. },
  20. {
  21. name: 'inline',
  22. summary: 'Use inline source maps (only available on capacitor 4.1.0+)',
  23. type: Boolean,
  24. default: false
  25. }
  26. ];
  27. const runner = this.project && await this.project.getBuildRunner();
  28. if (runner) {
  29. const libmetadata = await runner.getCommandMetadata();
  30. options.push(...libmetadata.options || []);
  31. }
  32. return {
  33. name: 'sync',
  34. type: 'project',
  35. summary: 'Sync (copy + update) an Ionic project',
  36. description: `
  37. ${(0, color_1.input)('ionic capacitor sync')} will do the following:
  38. - Perform an Ionic build, which compiles web assets
  39. - Copy web assets to Capacitor native platform(s)
  40. - Update Capacitor native platform(s) and dependencies
  41. - Install any discovered Capacitor or Cordova plugins
  42. `,
  43. inputs: [
  44. {
  45. name: 'platform',
  46. summary: `The platform to sync (e.g. ${['android', 'ios'].map(v => (0, color_1.input)(v)).join(', ')})`,
  47. },
  48. ],
  49. options,
  50. };
  51. }
  52. async preRun(inputs, options, runinfo) {
  53. await this.preRunChecks(runinfo);
  54. if (inputs[0]) {
  55. await this.checkForPlatformInstallation(inputs[0]);
  56. }
  57. }
  58. async run(inputs, options) {
  59. if (!this.project) {
  60. throw new errors_1.FatalException(`Cannot run ${(0, color_1.input)('ionic capacitor sync')} outside a project directory.`);
  61. }
  62. const [platform] = inputs;
  63. if (options.build) {
  64. await this.runBuild(inputs, options);
  65. }
  66. const args = ['sync'];
  67. const capVersion = await this.getCapacitorVersion();
  68. if (semver.gte(capVersion, "4.1.0") && options.inline) {
  69. args.push("--inline");
  70. }
  71. if (platform) {
  72. args.push(platform);
  73. }
  74. await this.runCapacitor(args);
  75. const hookDeps = {
  76. config: this.env.config,
  77. project: this.project,
  78. shell: this.env.shell,
  79. };
  80. await this.runCapacitorSyncHook('capacitor:sync:after', inputs, options, hookDeps);
  81. }
  82. async runCapacitorSyncHook(name, inputs, options, e) {
  83. const hook = new CapacitorSyncHook(name, e);
  84. const buildRunner = await e.project.requireBuildRunner();
  85. try {
  86. await hook.run({
  87. name: hook.name,
  88. build: buildRunner.createOptionsFromCommandLine(inputs, options),
  89. capacitor: await this.createOptionsFromCommandLine(inputs, options),
  90. });
  91. }
  92. catch (e) {
  93. if (e instanceof cli_framework_1.BaseError) {
  94. throw new errors_1.FatalException(e.message);
  95. }
  96. throw e;
  97. }
  98. }
  99. }
  100. exports.SyncCommand = SyncCommand;
  101. class CapacitorSyncHook extends hooks_1.Hook {
  102. constructor(name, e) {
  103. super(e);
  104. this.name = name;
  105. }
  106. }