diff --git a/apps/app-builder/src/__tests__/__snapshots__/exports.spec.ts.snap b/apps/app-builder/src/__tests__/__snapshots__/exports.spec.ts.snap index 5d10d5b97..6020555b7 100644 --- a/apps/app-builder/src/__tests__/__snapshots__/exports.spec.ts.snap +++ b/apps/app-builder/src/__tests__/__snapshots__/exports.spec.ts.snap @@ -4,5 +4,8 @@ exports[`exports > exports from index.ts 1`] = ` [ "PdkPlatformName", "defineConfig", + "executeCommand", + "executePromises", + "getPlatformDistPath", ] `; diff --git a/apps/app-builder/src/constants.ts b/apps/app-builder/src/constants.ts index ae5bd0caa..84b9e9bd4 100644 --- a/apps/app-builder/src/constants.ts +++ b/apps/app-builder/src/constants.ts @@ -24,6 +24,20 @@ export const COMMAND_UPGRADE_NAME = 'upgrade'; export const COMMAND_ZIP_NAME = 'zip'; +export const ALL_COMMAND_NAMES = [ + COMMAND_CLEAN_NAME, + COMMAND_COPY_NAME, + COMMAND_INCREMENT_NAME, + COMMAND_INIT_NAME, + COMMAND_RENAME_NAME, + COMMAND_TRANSFORM_NAME, + COMMAND_TRANSLATIONS_NAME, + COMMAND_UPGRADE_NAME, + COMMAND_ZIP_NAME, +] as const; + +export type CommandName = (typeof ALL_COMMAND_NAMES)[number]; + export enum VerbosityLevel { Verbose = 1, VeryVerbose = 2, diff --git a/apps/app-builder/src/index.ts b/apps/app-builder/src/index.ts index f3f794d88..bf14c8553 100644 --- a/apps/app-builder/src/index.ts +++ b/apps/app-builder/src/index.ts @@ -9,3 +9,5 @@ export type {PdkBuilderConfig}; export {PdkPlatformName} from './types'; export const defineConfig = PdkBuilderConfig)>(config: C): C => config; + +export {executeCommand, executePromises, getPlatformDistPath} from './utils'; diff --git a/apps/app-builder/src/types/config.ts b/apps/app-builder/src/types/config.ts index 0e7a5c405..e4fddbbb3 100644 --- a/apps/app-builder/src/types/config.ts +++ b/apps/app-builder/src/types/config.ts @@ -1,8 +1,19 @@ import {type OneOrMore} from '@myparcel/ts-utils'; import {type VersionSource} from '../increment'; +import {type CommandName} from '../constants'; import {type NodePackageManager} from '../commands/upgrade/types'; import {type PdkPlatformName, type StringGenerator} from './common'; -import {type CommandDefinition} from './command'; +import { + type CommandDefinition, + type PdkBuilderCommand, + type PdkBuilderCommandWithoutConfig, + type PdkBuilderContext, +} from './command'; + +type CommandHooks = Record< + CommandName, + (params: {command: PdkBuilderCommandWithoutConfig | PdkBuilderCommand; context: PdkBuilderContext}) => Promise +>; export type PdkBuilderConfig = { /** @@ -97,6 +108,11 @@ export type PdkBuilderConfig = { }; additionalCommands?: CommandDefinition[]; + + hooks: { + before?: CommandHooks; + after?: CommandHooks; + }; }; export type ResolvedPdkBuilderConfig = Required> & { diff --git a/apps/app-builder/src/utils/createWithConfig.ts b/apps/app-builder/src/utils/createWithConfig.ts index d27937d04..622fbfccd 100644 --- a/apps/app-builder/src/utils/createWithConfig.ts +++ b/apps/app-builder/src/utils/createWithConfig.ts @@ -1,4 +1,5 @@ -import {type CreateHook, type WithConfigParams} from '../types'; +import {type CreateHook, type PdkBuilderContext, type WithConfigParams} from '../types'; +import {type CommandName, VerbosityLevel} from '../constants'; import {resolveConfig} from './resolveConfig'; import {parseCommandInput} from './parseCommandInput'; import {mergeDefaultConfig} from './mergeDefaultConfig'; @@ -9,10 +10,24 @@ export const createWithConfig: CreateHook = (env) => { const config = await resolveConfig(env); const {command, context} = await parseCommandInput(callback, args, env); - await command({ + const commandName = command.name as CommandName; + + const mergedContext: PdkBuilderContext = { ...context, config: mergeDefaultConfig(config), - }); + }; + + if (config.hooks.before?.[commandName] && context.args.verbose >= VerbosityLevel.Verbose) { + context.debug('Running before hook'); + await config.hooks.before?.[commandName]?.({command, context: mergedContext}); + } + + await command(mergedContext); + + if (config.hooks.after?.[commandName] && context.args.verbose >= VerbosityLevel.Verbose) { + context.debug('Running after hook'); + await config.hooks.after?.[commandName]?.({command, context: mergedContext}); + } context.debug.logTimeTaken(); };