Skip to content

Commit 9e29235

Browse files
committed
feat(capacitor): install platform if missing
1 parent 8310ff6 commit 9e29235

6 files changed

Lines changed: 36 additions & 0 deletions

File tree

packages/ionic/src/commands/capacitor/base.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import chalk from 'chalk';
2+
import * as path from 'path';
23

34
import { ERROR_SHELL_COMMAND_NOT_FOUND, ShellCommandError } from '@ionic/cli-framework';
5+
import { pathExists } from '@ionic/cli-framework/utils/fs';
46
import { CommandInstanceInfo } from '@ionic/cli-utils';
57
import { Command } from '@ionic/cli-utils/lib/command';
68
import { FatalException } from '@ionic/cli-utils/lib/errors';
@@ -51,6 +53,24 @@ export abstract class CapacitorCommand extends Command {
5153
}
5254
}
5355

56+
async checkForPlatformInstallation(platform: string) {
57+
if (!this.project) {
58+
throw new FatalException('Cannot use Capacitor outside a project directory.');
59+
}
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): Promise<[string, boolean]> => [p, await pathExists(path.resolve(integrationRoot, p))])))
65+
.filter(([, e]) => e)
66+
.map(([p]) => p);
67+
68+
if (!platforms.includes(platform)) {
69+
await this._runCapacitor(['add', platform]);
70+
}
71+
}
72+
}
73+
5474
private async promptToInstallCapacitor(): Promise<boolean> {
5575
if (!this.project) {
5676
throw new FatalException(`Cannot use Capacitor outside a project directory.`);

packages/ionic/src/commands/capacitor/copy.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ ${chalk.green('ionic capacitor copy')} will do the following:
2727

2828
async preRun(inputs: CommandLineInputs, options: CommandLineOptions, runinfo: CommandInstanceInfo): Promise<void> {
2929
await this.preRunChecks(runinfo);
30+
31+
if (inputs[0]) {
32+
await this.checkForPlatformInstallation(inputs[0]);
33+
}
3034
}
3135

3236
async run(inputs: CommandLineInputs, options: CommandLineOptions): Promise<void> {

packages/ionic/src/commands/capacitor/open.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ ${chalk.green('ionic capacitor open')} will do the following:
3939

4040
inputs[0] = platform.trim();
4141
}
42+
43+
await this.checkForPlatformInstallation(inputs[0]);
4244
}
4345

4446
async run(inputs: CommandLineInputs, options: CommandLineOptions): Promise<void> {

packages/ionic/src/commands/capacitor/run.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ ${chalk.cyan('[1]')}: ${chalk.bold('https://ionicframework.com/docs/developer-re
9090

9191
inputs[0] = platform.trim();
9292
}
93+
94+
await this.checkForPlatformInstallation(inputs[0]);
9395
}
9496

9597
async run(inputs: CommandLineInputs, options: CommandLineOptions): Promise<void> {

packages/ionic/src/commands/capacitor/sync.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ ${chalk.green('ionic capacitor sync')} will do the following:
2929

3030
async preRun(inputs: CommandLineInputs, options: CommandLineOptions, runinfo: CommandInstanceInfo): Promise<void> {
3131
await this.preRunChecks(runinfo);
32+
33+
if (inputs[0]) {
34+
await this.checkForPlatformInstallation(inputs[0]);
35+
}
3236
}
3337

3438
async run(inputs: CommandLineInputs, options: CommandLineOptions): Promise<void> {

packages/ionic/src/commands/capacitor/update.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ ${chalk.green('ionic capacitor update')} will do the following:
2828

2929
async preRun(inputs: CommandLineInputs, options: CommandLineOptions, runinfo: CommandInstanceInfo): Promise<void> {
3030
await this.preRunChecks(runinfo);
31+
32+
if (inputs[0]) {
33+
await this.checkForPlatformInstallation(inputs[0]);
34+
}
3135
}
3236

3337
async run(inputs: CommandLineInputs, options: CommandLineOptions): Promise<void> {

0 commit comments

Comments
 (0)