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

refactoring: remove hooks reducer #8661

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/perfect-dolphins-fix.md
@@ -0,0 +1,6 @@
---
'@graphql-codegen/cli': patch
'@graphql-codegen/plugin-helpers': patch
---

refactor hook execution
39 changes: 4 additions & 35 deletions packages/graphql-codegen-cli/src/hooks.ts
Expand Up @@ -15,40 +15,6 @@ const DEFAULT_HOOKS: Types.LifecycleHooksDefinition = {
beforeAllFileWrite: [],
};

function normalizeHooks(_hooks: Partial<Types.LifecycleHooksDefinition>): {
[key in keyof Types.LifecycleHooksDefinition]: (string | Types.HookFunction)[];
} {
const keys = Object.keys({
...DEFAULT_HOOKS,
..._hooks,
});

return keys.reduce(
(prev: { [key in keyof Types.LifecycleHooksDefinition]: (string | Types.HookFunction)[] }, hookName: string) => {
if (typeof _hooks[hookName] === 'string') {
return {
...prev,
[hookName]: [_hooks[hookName]] as string[],
};
}
if (typeof _hooks[hookName] === 'function') {
return {
...prev,
[hookName]: [_hooks[hookName]],
};
}
if (Array.isArray(_hooks[hookName])) {
return {
...prev,
[hookName]: _hooks[hookName] as string[],
};
}
return prev;
},
{} as { [key in keyof Types.LifecycleHooksDefinition]: (string | Types.HookFunction)[] }
);
}

function execShellCommand(cmd: string): Promise<string> {
return new Promise((resolve, reject) => {
exec(
Expand Down Expand Up @@ -94,7 +60,10 @@ async function executeHooks(
}

export const lifecycleHooks = (_hooks: Partial<Types.LifecycleHooksDefinition> = {}) => {
const hooks = normalizeHooks(_hooks);
const hooks = {
...DEFAULT_HOOKS,
..._hooks,
};

return {
afterStart: async (): Promise<void> => executeHooks('afterStart', hooks.afterStart),
Expand Down