Skip to content

Commit

Permalink
feat(capacitor): perform Ionic for copy and sync
Browse files Browse the repository at this point in the history
resolves #3783
  • Loading branch information
imhoffd committed Feb 4, 2020
1 parent 5b97dd5 commit be42aee
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 29 deletions.
25 changes: 23 additions & 2 deletions packages/@ionic/cli/src/commands/capacitor/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { pathExists } from '@ionic/utils-fs';
import { ERROR_COMMAND_NOT_FOUND, ERROR_SIGNAL_EXIT, SubprocessError } from '@ionic/utils-subprocess';
import * as path from 'path';

import { CommandInstanceInfo, ProjectIntegration } from '../../definitions';
import { CommandInstanceInfo, CommandLineInputs, CommandLineOptions, ProjectIntegration } from '../../definitions';
import { input } from '../../lib/color';
import { Command } from '../../lib/command';
import { FatalException } from '../../lib/errors';
import { FatalException, RunnerException } from '../../lib/errors';
import { runCommand } from '../../lib/executor';
import { generateOptionsForCapacitorBuild } from '../../lib/integrations/capacitor/utils';

export abstract class CapacitorCommand extends Command {
private _integration?: Required<ProjectIntegration>;
Expand Down Expand Up @@ -69,6 +70,26 @@ export abstract class CapacitorCommand extends Command {
}
}

async runBuild(inputs: CommandLineInputs, options: CommandLineOptions): Promise<void> {
if (!this.project) {
throw new FatalException(`Cannot use Capacitor outside a project directory.`);
}

if (options['build']) {
try {
const runner = await this.project.requireBuildRunner();
const runnerOpts = runner.createOptionsFromCommandLine(inputs, generateOptionsForCapacitorBuild(inputs, options));
await runner.run(runnerOpts);
} catch (e) {
if (e instanceof RunnerException) {
throw new FatalException(e.message);
}

throw e;
}
}
}

async checkForPlatformInstallation(platform: string) {
if (!this.project) {
throw new FatalException('Cannot use Capacitor outside a project directory.');
Expand Down
29 changes: 27 additions & 2 deletions packages/@ionic/cli/src/commands/capacitor/copy.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,44 @@
import { CommandMetadataOption } from '@ionic/cli-framework';

import { CommandInstanceInfo, CommandLineInputs, CommandLineOptions, CommandMetadata, CommandPreRun } from '../../definitions';
import { input, strong } from '../../lib/color';
import { input } from '../../lib/color';

import { CapacitorCommand } from './base';

export class CopyCommand extends CapacitorCommand implements CommandPreRun {
async getMetadata(): Promise<CommandMetadata> {
const options: CommandMetadataOption[] = [
{
name: 'build',
summary: 'Do not invoke an Ionic build',
type: Boolean,
default: true,
},
];

const runner = this.project && await this.project.getBuildRunner();

if (runner) {
const libmetadata = await runner.getCommandMetadata();
options.push(...libmetadata.options || []);
}

return {
name: 'copy',
type: 'project',
summary: 'Copy web assets to native platforms',
description: `
${input('ionic capacitor copy')} will do the following:
- Copy the ${strong('www/')} directory into your native platforms.
- Perform an Ionic build, which compiles web assets
- Copy web assets to Capacitor native platform(s)
`,
inputs: [
{
name: 'platform',
summary: `The platform to copy (e.g. ${['android', 'ios', 'electron'].map(v => input(v)).join(', ')})`,
},
],
options,
};
}

Expand All @@ -32,6 +52,11 @@ ${input('ionic capacitor copy')} will do the following:

async run(inputs: CommandLineInputs, options: CommandLineOptions): Promise<void> {
const [ platform ] = inputs;

if (options.build) {
await this.runBuild(inputs, options);
}

const args = ['copy'];

if (platform) {
Expand Down
20 changes: 0 additions & 20 deletions packages/@ionic/cli/src/commands/capacitor/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,24 +204,4 @@ For Android and iOS, you can setup Remote Debugging on your device with browser

conf.setServerUrl(serverUrl);
}

async runBuild(inputs: CommandLineInputs, options: CommandLineOptions): Promise<void> {
if (!this.project) {
throw new FatalException(`Cannot run ${input('ionic capacitor run/emulate')} outside a project directory.`);
}

if (options['build']) {
try {
const runner = await this.project.requireBuildRunner();
const runnerOpts = runner.createOptionsFromCommandLine(inputs, generateOptionsForCapacitorBuild(inputs, options));
await runner.run(runnerOpts);
} catch (e) {
if (e instanceof RunnerException) {
throw new FatalException(e.message);
}

throw e;
}
}
}
}
31 changes: 28 additions & 3 deletions packages/@ionic/cli/src/commands/capacitor/sync.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,46 @@
import { CommandMetadataOption } from '@ionic/cli-framework';

import { CommandInstanceInfo, CommandLineInputs, CommandLineOptions, CommandMetadata, CommandPreRun } from '../../definitions';
import { input } from '../../lib/color';

import { CapacitorCommand } from './base';

export class SyncCommand extends CapacitorCommand implements CommandPreRun {
async getMetadata(): Promise<CommandMetadata> {
const options: CommandMetadataOption[] = [
{
name: 'build',
summary: 'Do not invoke an Ionic build',
type: Boolean,
default: true,
},
];

const runner = this.project && await this.project.getBuildRunner();

if (runner) {
const libmetadata = await runner.getCommandMetadata();
options.push(...libmetadata.options || []);
}

return {
name: 'sync',
type: 'project',
summary: 'Sync (copy + update) an Ionic project',
description: `
${input('ionic capacitor sync')} will do the following:
- Copy web assets to all Capacitor native platforms
- Update each Capacitor native platforms, such as any dependencies that need updating.
- Install any discovered Capacitor or Cordova plugins.
- Perform an Ionic build, which compiles web assets
- Copy web assets to Capacitor native platform(s)
- Update Capacitor native platform(s) and dependencies
- Install any discovered Capacitor or Cordova plugins
`,
inputs: [
{
name: 'platform',
summary: `The platform to sync (e.g. ${['android', 'ios', 'electron'].map(v => input(v)).join(', ')})`,
},
],
options,
};
}

Expand All @@ -34,6 +54,11 @@ ${input('ionic capacitor sync')} will do the following:

async run(inputs: CommandLineInputs, options: CommandLineOptions): Promise<void> {
const [ platform ] = inputs;

if (options.build) {
await this.runBuild(inputs, options);
}

const args = ['sync'];

if (platform) {
Expand Down
4 changes: 2 additions & 2 deletions packages/@ionic/cli/src/commands/capacitor/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export class UpdateCommand extends CapacitorCommand implements CommandPreRun {
summary: 'Update Capacitor native platforms, install Capacitor/Cordova plugins',
description: `
${input('ionic capacitor update')} will do the following:
- Update each Capacitor native project, such as any dependencies that need updating.
- Install any discovered Capacitor or Cordova plugins.
- Update Capacitor native platform(s) and dependencies
- Install any discovered Capacitor or Cordova plugins
`,
inputs: [
{
Expand Down

0 comments on commit be42aee

Please sign in to comment.