Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 additions & 2 deletions packages/@ionic/cli/src/commands/capacitor/sync.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { CommandMetadataOption } from '@ionic/cli-framework';
import { BaseError, CommandMetadataOption } from '@ionic/cli-framework';

import { CommandInstanceInfo, CommandLineInputs, CommandLineOptions, CommandMetadata, CommandPreRun } from '../../definitions';
import { CapacitorSyncHookName, CommandInstanceInfo, CommandLineInputs, CommandLineOptions, CommandMetadata, CommandPreRun } from '../../definitions';
import { input } from '../../lib/color';
import { FatalException } from '../../lib/errors';
import { Hook, HookDeps } from '../../lib/hooks';

import { CapacitorCommand } from './base';

Expand Down Expand Up @@ -53,6 +55,10 @@ ${input('ionic capacitor sync')} will do the following:
}

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

const [ platform ] = inputs;

if (options.build) {
Expand All @@ -66,5 +72,42 @@ ${input('ionic capacitor sync')} will do the following:
}

await this.runCapacitor(args);

const hookDeps: HookDeps = {
config: this.env.config,
project: this.project,
shell: this.env.shell,
};

await this.runCapacitorSyncHook('capacitor:sync:after', inputs, options, hookDeps);
}

private async runCapacitorSyncHook(name: CapacitorSyncHookName, inputs: CommandLineInputs, options: CommandLineOptions, e: HookDeps): Promise<void> {
const hook = new CapacitorSyncHook(name, e);
const buildRunner = await e.project.requireBuildRunner();

try {
await hook.run({
name: hook.name,
build: buildRunner.createOptionsFromCommandLine(inputs, options),
capacitor: this.createOptionsFromCommandLine(inputs, options),
});
} catch (e) {
if (e instanceof BaseError) {
throw new FatalException(e.message);
}

throw e;
}
}
}

class CapacitorSyncHook extends Hook {
readonly name: CapacitorSyncHookName;

constructor(name: CapacitorSyncHookName, e: HookDeps) {
super(e);

this.name = name;
}
}
10 changes: 8 additions & 2 deletions packages/@ionic/cli/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@ export interface Runner<T extends object, U> {
}

export type ProjectType = 'angular' | 'ionic-angular' | 'ionic1' | 'custom' | 'bare' | 'react' | 'vue';
export type HookName = 'build:before' | 'build:after' | 'serve:before' | 'serve:after' | 'capacitor:run:before' | 'capacitor:build:before';
export type HookName = 'build:before' | 'build:after' | 'serve:before' | 'serve:after' | 'capacitor:run:before' | 'capacitor:build:before' | 'capacitor:sync:after';

export type CapacitorRunHookName = 'capacitor:run:before';
export type CapacitorBuildHookName = 'capacitor:build:before';
export type CapacitorSyncHookName = 'capacitor:sync:after';

export interface BaseHookContext {
project: {
Expand All @@ -92,6 +93,11 @@ export interface BaseHookContext {
export type AnyServeOptions = ReactServeOptions | AngularServeOptions | IonicAngularServeOptions | Ionic1ServeOptions;
export type AnyBuildOptions = ReactBuildOptions | AngularBuildOptions | IonicAngularBuildOptions | Ionic1BuildOptions;

export interface CapacitorSyncHookInput {
readonly name: CapacitorSyncHookName;
readonly build?: AnyBuildOptions;
readonly capacitor: IonicCapacitorOptions;
}
export interface CapacitorRunHookInput {
readonly name: CapacitorRunHookName;
readonly serve?: AnyServeOptions;
Expand Down Expand Up @@ -120,7 +126,7 @@ export interface ServeAfterHookInput {
readonly serve: (AngularServeOptions | IonicAngularServeOptions | Ionic1ServeOptions) & ServeDetails;
}

export type HookInput = BuildHookInput | ServeBeforeHookInput | ServeAfterHookInput | CapacitorRunHookInput | CapacitorBuildHookInput;
export type HookInput = BuildHookInput | ServeBeforeHookInput | ServeAfterHookInput | CapacitorRunHookInput | CapacitorBuildHookInput | CapacitorSyncHookInput;
export type HookContext = BaseHookContext & HookInput;

export type HookFn = (ctx: HookContext) => Promise<void>;
Expand Down