1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const utils_fs_1 = require("@ionic/utils-fs");
|
4 | const utils_subprocess_1 = require("@ionic/utils-subprocess");
|
5 | const path = require("path");
|
6 | const color_1 = require("../../lib/color");
|
7 | const command_1 = require("../../lib/command");
|
8 | const errors_1 = require("../../lib/errors");
|
9 | const executor_1 = require("../../lib/executor");
|
10 | class CapacitorCommand extends command_1.Command {
|
11 | get integration() {
|
12 | if (!this.project) {
|
13 | throw new errors_1.FatalException(`Cannot use Capacitor outside a project directory.`);
|
14 | }
|
15 | if (!this._integration) {
|
16 | this._integration = this.project.requireIntegration('capacitor');
|
17 | }
|
18 | return this._integration;
|
19 | }
|
20 | async checkCapacitor(runinfo) {
|
21 | if (!this.project) {
|
22 | throw new errors_1.FatalException(`Cannot use Capacitor outside a project directory.`);
|
23 | }
|
24 | const capacitor = this.project.getIntegration('capacitor');
|
25 | if (!capacitor) {
|
26 | await executor_1.runCommand(runinfo, ['integrations', 'enable', 'capacitor']);
|
27 | }
|
28 | }
|
29 | async preRunChecks(runinfo) {
|
30 | await this.checkCapacitor(runinfo);
|
31 | }
|
32 | async runCapacitor(argList) {
|
33 | try {
|
34 | return await this._runCapacitor(argList);
|
35 | }
|
36 | catch (e) {
|
37 | if (e instanceof utils_subprocess_1.SubprocessError) {
|
38 | if (e.code === utils_subprocess_1.ERROR_COMMAND_NOT_FOUND) {
|
39 | const pkg = '@capacitor/cli';
|
40 | const requiredMsg = `The Capacitor CLI is required for Capacitor projects.`;
|
41 | this.env.log.nl();
|
42 | this.env.log.info(`Looks like ${color_1.input(pkg)} isn't installed in this project.\n` + requiredMsg);
|
43 | this.env.log.nl();
|
44 | const installed = await this.promptToInstallCapacitor();
|
45 | if (!installed) {
|
46 | throw new errors_1.FatalException(`${color_1.input(pkg)} is required for Capacitor projects.`);
|
47 | }
|
48 | return this.runCapacitor(argList);
|
49 | }
|
50 | if (e.code === utils_subprocess_1.ERROR_SIGNAL_EXIT) {
|
51 | return;
|
52 | }
|
53 | }
|
54 | throw e;
|
55 | }
|
56 | }
|
57 | async checkForPlatformInstallation(platform) {
|
58 | if (!this.project) {
|
59 | throw new errors_1.FatalException('Cannot use Capacitor outside a project directory.');
|
60 | }
|
61 | if (platform) {
|
62 | const integrationRoot = this.project.directory;
|
63 | const platformsToCheck = ['android', 'ios', 'electron'];
|
64 | const platforms = (await Promise.all(platformsToCheck.map(async (p) => [p, await utils_fs_1.pathExists(path.resolve(integrationRoot, p))])))
|
65 | .filter(([, e]) => e)
|
66 | .map(([p]) => p);
|
67 | if (!platforms.includes(platform)) {
|
68 | await this._runCapacitor(['add', platform]);
|
69 | }
|
70 | }
|
71 | }
|
72 | async promptToInstallCapacitor() {
|
73 | if (!this.project) {
|
74 | throw new errors_1.FatalException(`Cannot use Capacitor outside a project directory.`);
|
75 | }
|
76 | const { pkgManagerArgs } = await Promise.resolve().then(() => require('../../lib/utils/npm'));
|
77 | const pkg = '@capacitor/cli';
|
78 | const [manager, ...managerArgs] = await pkgManagerArgs(this.env.config.get('npmClient'), { pkg, command: 'install', saveDev: true });
|
79 | const confirm = await this.env.prompt({
|
80 | name: 'confirm',
|
81 | message: `Install ${color_1.input(pkg)}?`,
|
82 | type: 'confirm',
|
83 | });
|
84 | if (!confirm) {
|
85 | this.env.log.warn(`Not installing--here's how to install manually: ${color_1.input(`${manager} ${managerArgs.join(' ')}`)}`);
|
86 | return false;
|
87 | }
|
88 | await this.env.shell.run(manager, managerArgs, { cwd: this.project.directory });
|
89 | return true;
|
90 | }
|
91 | async _runCapacitor(argList) {
|
92 | if (!this.project) {
|
93 | throw new errors_1.FatalException(`Cannot use Capacitor outside a project directory.`);
|
94 | }
|
95 | await this.env.shell.run('capacitor', argList, { fatalOnNotFound: false, truncateErrorOutput: 5000, stdio: 'inherit', cwd: this.integration.root });
|
96 | }
|
97 | }
|
98 | exports.CapacitorCommand = CapacitorCommand;
|