Skip to content

Commit

Permalink
feat(app-builder): add before and after hooks for commands
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Oct 9, 2023
1 parent c1de135 commit 7895f16
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ exports[`exports > exports from index.ts 1`] = `
[
"PdkPlatformName",
"defineConfig",
"executeCommand",
"executePromises",
"getPlatformDistPath",
]
`;
14 changes: 14 additions & 0 deletions apps/app-builder/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions apps/app-builder/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ export type {PdkBuilderConfig};
export {PdkPlatformName} from './types';

export const defineConfig = <C extends PdkBuilderConfig | (() => PdkBuilderConfig)>(config: C): C => config;

export {executeCommand, executePromises, getPlatformDistPath} from './utils';
18 changes: 17 additions & 1 deletion apps/app-builder/src/types/config.ts
Original file line number Diff line number Diff line change
@@ -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<void>
>;

export type PdkBuilderConfig = {
/**
Expand Down Expand Up @@ -97,6 +108,11 @@ export type PdkBuilderConfig = {
};

additionalCommands?: CommandDefinition[];

hooks: {
before?: CommandHooks;
after?: CommandHooks;
};
};

export type ResolvedPdkBuilderConfig = Required<Omit<PdkBuilderConfig, 'translations'>> & {
Expand Down
21 changes: 18 additions & 3 deletions apps/app-builder/src/utils/createWithConfig.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -9,10 +10,24 @@ export const createWithConfig: CreateHook<WithConfigParams> = (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();
};
Expand Down

0 comments on commit 7895f16

Please sign in to comment.