Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): Add hooks to call into npm scripts when capacitor commands run #4739

Merged
merged 3 commits into from
Jun 24, 2021
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
2 changes: 2 additions & 0 deletions cli/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ export async function wait(time: number): Promise<void> {

export async function runPlatformHook(
config: Config,
platformName: string,
platformDir: string,
hook: string,
): Promise<void> {
Expand All @@ -179,6 +180,7 @@ export async function runPlatformHook(
CAPACITOR_ROOT_DIR: config.app.rootDir,
CAPACITOR_WEB_DIR: config.app.webDirAbs,
CAPACITOR_CONFIG: JSON.stringify(config.app.extConfig),
CAPACITOR_PLATFORM_NAME: platformName,
...process.env,
},
});
Expand Down
7 changes: 6 additions & 1 deletion cli/src/tasks/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ export async function addCommand(
if (selectedPlatformName && !(await isValidPlatform(selectedPlatformName))) {
const platformDir = resolvePlatform(config, selectedPlatformName);
if (platformDir) {
await runPlatformHook(config, platformDir, 'capacitor:add');
await runPlatformHook(
config,
selectedPlatformName,
platformDir,
'capacitor:add',
);
} else {
let msg = `Platform ${c.input(selectedPlatformName)} not found.`;

Expand Down
20 changes: 19 additions & 1 deletion cli/src/tasks/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ export async function copyCommand(
if (selectedPlatformName && !(await isValidPlatform(selectedPlatformName))) {
const platformDir = resolvePlatform(config, selectedPlatformName);
if (platformDir) {
await runPlatformHook(config, platformDir, 'capacitor:copy');
await runPlatformHook(
config,
selectedPlatformName,
platformDir,
'capacitor:copy',
);
} else {
logger.error(`Platform ${c.input(selectedPlatformName)} not found.`);
}
Expand Down Expand Up @@ -58,6 +63,13 @@ export async function copy(
throw result;
}

await runPlatformHook(
config,
platformName,
config.app.rootDir,
'capacitor:copy:before',
);

if (platformName === config.ios.name) {
await copyWebDir(config, await config.ios.webDirAbs);
await copyCapacitorConfig(config, config.ios.nativeTargetDirAbs);
Expand All @@ -75,6 +87,12 @@ export async function copy(
throw `Platform ${platformName} is not valid.`;
}
});
await runPlatformHook(
config,
platformName,
config.app.rootDir,
'capacitor:copy:after',
);
}

async function copyCapacitorConfig(config: Config, nativeAbsDir: string) {
Expand Down
7 changes: 6 additions & 1 deletion cli/src/tasks/open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ export async function openCommand(
if (selectedPlatformName && !(await isValidPlatform(selectedPlatformName))) {
const platformDir = resolvePlatform(config, selectedPlatformName);
if (platformDir) {
await runPlatformHook(config, platformDir, 'capacitor:open');
await runPlatformHook(
config,
selectedPlatformName,
platformDir,
'capacitor:open',
);
} else {
logger.error(`Platform ${c.input(selectedPlatformName)} not found.`);
}
Expand Down
7 changes: 6 additions & 1 deletion cli/src/tasks/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ export async function runCommand(
if (selectedPlatformName && !(await isValidPlatform(selectedPlatformName))) {
const platformDir = resolvePlatform(config, selectedPlatformName);
if (platformDir) {
await runPlatformHook(config, platformDir, 'capacitor:run');
await runPlatformHook(
config,
selectedPlatformName,
platformDir,
'capacitor:run',
);
} else {
logger.error(`Platform ${c.input(selectedPlatformName)} not found.`);
}
Expand Down
15 changes: 15 additions & 0 deletions cli/src/tasks/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
checkWebDir,
selectPlatforms,
isValidPlatform,
runPlatformHook,
} from '../common';
import type { Config } from '../definitions';
import { fatal, isFatal } from '../errors';
Expand Down Expand Up @@ -60,10 +61,24 @@ export async function sync(
platformName: string,
deployment: boolean,
): Promise<void> {
await runPlatformHook(
config,
platformName,
config.app.rootDir,
'capacitor:sync:before',
);

try {
await copy(config, platformName);
} catch (e) {
logger.error(e.stack ?? e);
}
await update(config, platformName, deployment);

await runPlatformHook(
config,
platformName,
config.app.rootDir,
'capacitor:sync:after',
);
}
21 changes: 20 additions & 1 deletion cli/src/tasks/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ export async function updateCommand(
if (selectedPlatformName && !(await isValidPlatform(selectedPlatformName))) {
const platformDir = resolvePlatform(config, selectedPlatformName);
if (platformDir) {
await runPlatformHook(config, platformDir, 'capacitor:update');
await runPlatformHook(
config,
selectedPlatformName,
platformDir,
'capacitor:update',
);
} else {
logger.error(`Platform ${c.input(selectedPlatformName)} not found.`);
}
Expand Down Expand Up @@ -79,10 +84,24 @@ export async function update(
deployment: boolean,
): Promise<void> {
await runTask(c.success(c.strong(`update ${platformName}`)), async () => {
await runPlatformHook(
config,
platformName,
config.app.rootDir,
'capacitor:update:before',
);

if (platformName === config.ios.name) {
await updateIOS(config, deployment);
} else if (platformName === config.android.name) {
await updateAndroid(config);
}

await runPlatformHook(
config,
platformName,
config.app.rootDir,
'capacitor:update:after',
);
});
}