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: adding cli options commands hooks #11898

Merged
merged 4 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions packages/amplify-cli-core/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@ export type HookEvent = {
// @public (undocumented)
export type HookExtensions = Record<string, {
runtime: string;
runtime_options?: string[];
runtime_windows?: string;
}>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,20 @@ describe('hooksExecutioner tests', () => {
test('should determine runtime from hooks-config', async () => {
stateManagerMock.getHooksConfigJson.mockReturnValueOnce({ extensions: { py: { runtime: 'python3' } } });
await executeHooks(HooksMeta.getInstance({ command: 'pull', plugin: 'core' }, 'pre'));
expect(execa).toHaveBeenCalledWith(pathToPython3Runtime, expect.anything(), expect.anything());
expect(execa).toHaveBeenCalledWith(pathToPython3Runtime, ['testProjectHooksDirPath/pre-pull.py'], expect.anything());
});

test('should determine runtime options from hooks-config', async () => {
stateManagerMock.getHooksConfigJson.mockReturnValueOnce({ extensions: { py: { runtime: 'python3' , runtime_options: ['mock1', 'mock2'] } } });
await executeHooks(HooksMeta.getInstance({ command: 'pull', plugin: 'core' }, 'pre'));
expect(execa).toHaveBeenCalledWith(pathToPython3Runtime, ['mock1','mock2', 'testProjectHooksDirPath/pre-pull.py'], expect.anything());
});

test('should determine empty array runtime options from hooks-config', async () => {
stateManagerMock.getHooksConfigJson.mockClear();
stateManagerMock.getHooksConfigJson.mockReturnValueOnce({ extensions: { py: { runtime: 'python3' , runtime_options: [] } } });
await executeHooks(HooksMeta.getInstance({ command: 'pull', plugin: 'core' }, 'pre'));
expect(execa).toHaveBeenCalledWith(pathToPython3Runtime, ['testProjectHooksDirPath/pre-pull.py'], expect.anything());
});

test('should determine windows runtime from hooks-config', async () => {
Expand Down
30 changes: 23 additions & 7 deletions packages/amplify-cli-core/src/hooks/hooksExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ import { pathManager, stateManager } from '../state-manager';

const logger = getLogger('amplify-cli-core', 'hooks/hooksExecutioner.ts');

/**
* runtime for hooks
*/
type HooksRuntime = { runtimePath: string, runtimeOptions?: string[] };


/**
* execute hooks present in the hooks directory
*/
Expand Down Expand Up @@ -46,16 +52,16 @@ export const executeHooks = async (hooksMetadata: HooksMeta): Promise<void> => {
if (!execFileMeta) {
continue;
}
const runtime = getRuntime(execFileMeta, hooksConfig);
if (!runtime) {
const hooksRuntime = getRuntime(execFileMeta, hooksConfig);
if (!hooksRuntime?.runtimePath) {
continue;
}
await execHelper(runtime, execFileMeta, hooksMetadata.getDataParameter(), hooksMetadata.getErrorParameter());
await execHelper(hooksRuntime, execFileMeta, hooksMetadata.getDataParameter(), hooksMetadata.getErrorParameter());
}
};

const execHelper = async (
runtime: string,
hooksRuntime: HooksRuntime,
execFileMeta: HookFileMeta,
dataParameter: DataParameter,
errorParameter?: ErrorParameter,
Expand All @@ -74,7 +80,9 @@ const execHelper = async (

try {
logger.info(`hooks file: ${execFileMeta.fileName} execution started`);
const childProcess = execa(runtime, [execFileMeta.filePath], {
// adding default if options arent defined
const runtimeArgs = (hooksRuntime.runtimeOptions ?? []).concat([execFileMeta.filePath])
Fixed Show fixed Hide fixed
const childProcess = execa(hooksRuntime.runtimePath, runtimeArgs, {
cwd: projectRoot,
env: { PATH: process.env.PATH },
input: JSON.stringify({
Expand Down Expand Up @@ -162,7 +170,7 @@ const splitFileName = (filename: string): HookFileMeta => {
return fileMeta;
};

const getRuntime = (fileMeta: HookFileMeta, hooksConfig: HooksConfig): string | undefined => {
const getRuntime = (fileMeta: HookFileMeta, hooksConfig: HooksConfig): HooksRuntime | undefined => {
const { extension } = fileMeta;
if (!extension) {
return undefined;
Expand All @@ -183,8 +191,16 @@ const getRuntime = (fileMeta: HookFileMeta, hooksConfig: HooksConfig): string |
if (!executablePath) {
throw new Error(String(`hooks runtime not found: ${runtime}`));
}
const hooksRuntime: HooksRuntime = {
runtimePath : executablePath,
};
// check runtime options
const runtimeOptions = extensionObj?.[extension]?.runtime_options;
if(Array.isArray(runtimeOptions) && runtimeOptions.length > 0){
hooksRuntime.runtimeOptions = extensionObj?.[extension]?.runtime_options;
}

return executablePath;
return hooksRuntime;
};

const getSupportedExtensions = (hooksConfig: HooksConfig): HookExtensions => ({ ...defaultSupportedExt, ...hooksConfig?.extensions });
2 changes: 1 addition & 1 deletion packages/amplify-cli-core/src/hooks/hooksTypes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type HookExtensions = Record<string, { runtime: string; runtime_windows?: string }>;
export type HookExtensions = Record<string, { runtime: string; runtime_options?: string[], runtime_windows?: string }>;

export type HooksConfig = {
extensions?: HookExtensions;
Expand Down