From a3496e4bee15201645d43b6908c1791da380735f Mon Sep 17 00:00:00 2001 From: EinfachHans Date: Fri, 18 Jun 2021 14:16:01 +0200 Subject: [PATCH 1/2] feat(cli): Add ability to hook into cap commands closes #3593 --- cli/src/common.ts | 2 ++ cli/src/tasks/add.ts | 2 +- cli/src/tasks/copy.ts | 5 ++++- cli/src/tasks/open.ts | 2 +- cli/src/tasks/run.ts | 2 +- cli/src/tasks/sync.ts | 6 +++++- cli/src/tasks/update.ts | 6 +++++- 7 files changed, 19 insertions(+), 6 deletions(-) diff --git a/cli/src/common.ts b/cli/src/common.ts index 47bedce790..54ebd5e566 100644 --- a/cli/src/common.ts +++ b/cli/src/common.ts @@ -158,6 +158,7 @@ export async function wait(time: number): Promise { export async function runPlatformHook( config: Config, + platformName: string, platformDir: string, hook: string, ): Promise { @@ -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, }, }); diff --git a/cli/src/tasks/add.ts b/cli/src/tasks/add.ts index 3ae1eea6d0..f6f857d48d 100644 --- a/cli/src/tasks/add.ts +++ b/cli/src/tasks/add.ts @@ -40,7 +40,7 @@ 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.`; diff --git a/cli/src/tasks/copy.ts b/cli/src/tasks/copy.ts index a87ff05980..6edc1a60ed 100644 --- a/cli/src/tasks/copy.ts +++ b/cli/src/tasks/copy.ts @@ -28,7 +28,7 @@ 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.`); } @@ -58,6 +58,8 @@ 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); @@ -75,6 +77,7 @@ 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) { diff --git a/cli/src/tasks/open.ts b/cli/src/tasks/open.ts index 9c4bd2dc13..d3d9a79b86 100644 --- a/cli/src/tasks/open.ts +++ b/cli/src/tasks/open.ts @@ -20,7 +20,7 @@ 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.`); } diff --git a/cli/src/tasks/run.ts b/cli/src/tasks/run.ts index fc99256273..ca80c19fd5 100644 --- a/cli/src/tasks/run.ts +++ b/cli/src/tasks/run.ts @@ -32,7 +32,7 @@ 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.`); } diff --git a/cli/src/tasks/sync.ts b/cli/src/tasks/sync.ts index db9606d72f..8ca6292236 100644 --- a/cli/src/tasks/sync.ts +++ b/cli/src/tasks/sync.ts @@ -3,7 +3,7 @@ import { checkPackage, checkWebDir, selectPlatforms, - isValidPlatform, + isValidPlatform, runPlatformHook } from '../common'; import type { Config } from '../definitions'; import { fatal, isFatal } from '../errors'; @@ -60,10 +60,14 @@ export async function sync( platformName: string, deployment: boolean, ): Promise { + 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'); } diff --git a/cli/src/tasks/update.ts b/cli/src/tasks/update.ts index c8e1f91c61..20c7731e6a 100644 --- a/cli/src/tasks/update.ts +++ b/cli/src/tasks/update.ts @@ -25,7 +25,7 @@ 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.`); } @@ -79,10 +79,14 @@ export async function update( deployment: boolean, ): Promise { 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'); }); } From 5e02d4138c525ed5393254e8cdf4079747110c9d Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Thu, 24 Jun 2021 13:25:10 +0200 Subject: [PATCH 2/2] fmt --- cli/src/tasks/add.ts | 7 ++++++- cli/src/tasks/copy.ts | 21 ++++++++++++++++++--- cli/src/tasks/open.ts | 7 ++++++- cli/src/tasks/run.ts | 7 ++++++- cli/src/tasks/sync.ts | 17 ++++++++++++++--- cli/src/tasks/update.ts | 21 ++++++++++++++++++--- 6 files changed, 68 insertions(+), 12 deletions(-) diff --git a/cli/src/tasks/add.ts b/cli/src/tasks/add.ts index f6f857d48d..24d40a5bb9 100644 --- a/cli/src/tasks/add.ts +++ b/cli/src/tasks/add.ts @@ -40,7 +40,12 @@ export async function addCommand( if (selectedPlatformName && !(await isValidPlatform(selectedPlatformName))) { const platformDir = resolvePlatform(config, selectedPlatformName); if (platformDir) { - await runPlatformHook(config, selectedPlatformName, platformDir, 'capacitor:add'); + await runPlatformHook( + config, + selectedPlatformName, + platformDir, + 'capacitor:add', + ); } else { let msg = `Platform ${c.input(selectedPlatformName)} not found.`; diff --git a/cli/src/tasks/copy.ts b/cli/src/tasks/copy.ts index 6edc1a60ed..8129cb8b40 100644 --- a/cli/src/tasks/copy.ts +++ b/cli/src/tasks/copy.ts @@ -28,7 +28,12 @@ export async function copyCommand( if (selectedPlatformName && !(await isValidPlatform(selectedPlatformName))) { const platformDir = resolvePlatform(config, selectedPlatformName); if (platformDir) { - await runPlatformHook(config, selectedPlatformName, platformDir, 'capacitor:copy'); + await runPlatformHook( + config, + selectedPlatformName, + platformDir, + 'capacitor:copy', + ); } else { logger.error(`Platform ${c.input(selectedPlatformName)} not found.`); } @@ -58,7 +63,12 @@ export async function copy( throw result; } - await runPlatformHook(config, platformName, config.app.rootDir, 'capacitor:copy:before'); + await runPlatformHook( + config, + platformName, + config.app.rootDir, + 'capacitor:copy:before', + ); if (platformName === config.ios.name) { await copyWebDir(config, await config.ios.webDirAbs); @@ -77,7 +87,12 @@ export async function copy( throw `Platform ${platformName} is not valid.`; } }); - await runPlatformHook(config, platformName, config.app.rootDir, 'capacitor:copy:after'); + await runPlatformHook( + config, + platformName, + config.app.rootDir, + 'capacitor:copy:after', + ); } async function copyCapacitorConfig(config: Config, nativeAbsDir: string) { diff --git a/cli/src/tasks/open.ts b/cli/src/tasks/open.ts index d3d9a79b86..3b905a6c74 100644 --- a/cli/src/tasks/open.ts +++ b/cli/src/tasks/open.ts @@ -20,7 +20,12 @@ export async function openCommand( if (selectedPlatformName && !(await isValidPlatform(selectedPlatformName))) { const platformDir = resolvePlatform(config, selectedPlatformName); if (platformDir) { - await runPlatformHook(config, selectedPlatformName, platformDir, 'capacitor:open'); + await runPlatformHook( + config, + selectedPlatformName, + platformDir, + 'capacitor:open', + ); } else { logger.error(`Platform ${c.input(selectedPlatformName)} not found.`); } diff --git a/cli/src/tasks/run.ts b/cli/src/tasks/run.ts index ca80c19fd5..6644e93681 100644 --- a/cli/src/tasks/run.ts +++ b/cli/src/tasks/run.ts @@ -32,7 +32,12 @@ export async function runCommand( if (selectedPlatformName && !(await isValidPlatform(selectedPlatformName))) { const platformDir = resolvePlatform(config, selectedPlatformName); if (platformDir) { - await runPlatformHook(config, selectedPlatformName, platformDir, 'capacitor:run'); + await runPlatformHook( + config, + selectedPlatformName, + platformDir, + 'capacitor:run', + ); } else { logger.error(`Platform ${c.input(selectedPlatformName)} not found.`); } diff --git a/cli/src/tasks/sync.ts b/cli/src/tasks/sync.ts index 8ca6292236..9ce3cd9cbc 100644 --- a/cli/src/tasks/sync.ts +++ b/cli/src/tasks/sync.ts @@ -3,7 +3,8 @@ import { checkPackage, checkWebDir, selectPlatforms, - isValidPlatform, runPlatformHook + isValidPlatform, + runPlatformHook, } from '../common'; import type { Config } from '../definitions'; import { fatal, isFatal } from '../errors'; @@ -60,7 +61,12 @@ export async function sync( platformName: string, deployment: boolean, ): Promise { - await runPlatformHook(config, platformName, config.app.rootDir, 'capacitor:sync:before'); + await runPlatformHook( + config, + platformName, + config.app.rootDir, + 'capacitor:sync:before', + ); try { await copy(config, platformName); @@ -69,5 +75,10 @@ export async function sync( } await update(config, platformName, deployment); - await runPlatformHook(config, platformName, config.app.rootDir, 'capacitor:sync:after'); + await runPlatformHook( + config, + platformName, + config.app.rootDir, + 'capacitor:sync:after', + ); } diff --git a/cli/src/tasks/update.ts b/cli/src/tasks/update.ts index 20c7731e6a..c58f960da1 100644 --- a/cli/src/tasks/update.ts +++ b/cli/src/tasks/update.ts @@ -25,7 +25,12 @@ export async function updateCommand( if (selectedPlatformName && !(await isValidPlatform(selectedPlatformName))) { const platformDir = resolvePlatform(config, selectedPlatformName); if (platformDir) { - await runPlatformHook(config, selectedPlatformName, platformDir, 'capacitor:update'); + await runPlatformHook( + config, + selectedPlatformName, + platformDir, + 'capacitor:update', + ); } else { logger.error(`Platform ${c.input(selectedPlatformName)} not found.`); } @@ -79,7 +84,12 @@ export async function update( deployment: boolean, ): Promise { await runTask(c.success(c.strong(`update ${platformName}`)), async () => { - await runPlatformHook(config, platformName, config.app.rootDir, 'capacitor:update:before'); + await runPlatformHook( + config, + platformName, + config.app.rootDir, + 'capacitor:update:before', + ); if (platformName === config.ios.name) { await updateIOS(config, deployment); @@ -87,6 +97,11 @@ export async function update( await updateAndroid(config); } - await runPlatformHook(config, platformName, config.app.rootDir, 'capacitor:update:after'); + await runPlatformHook( + config, + platformName, + config.app.rootDir, + 'capacitor:update:after', + ); }); }