Skip to content

Commit

Permalink
feat: don't inject generated files into hook command
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkanii committed Jan 26, 2024
1 parent ee5334f commit 4f6f352
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 12 deletions.
15 changes: 15 additions & 0 deletions docs/src/pages/reference/configuration/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,18 @@ module.exports = {
},
};
```

If you don't want to inject the generated files into the command, you can use `afterAllFilesWrite` with an object:

```js
module.exports = {
petstore: {
hooks: {
afterAllFilesWrite: {
command: 'prettier --write .',
injectGeneratedDirsAndFiles: false,
},
},
},
};
```
11 changes: 10 additions & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,16 @@ export type Hook = 'afterAllFilesWrite';

export type HookFunction = (...args: any[]) => void | Promise<void>;

export type HookCommand = string | HookFunction | (string | HookFunction)[];
export interface HookOption {
command: string | HookFunction;
injectGeneratedDirsAndFiles?: boolean;
}

export type HookCommand =
| string
| HookFunction
| HookOption
| (string | HookFunction | HookOption)[];

export type NormalizedHookCommand = HookCommand[];

Expand Down
38 changes: 29 additions & 9 deletions packages/orval/src/utils/executeHook.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {
Hook,
HookOption,
isFunction,
isObject,
isString,
log,
logError,
Expand All @@ -18,16 +20,34 @@ export const executeHook = async (
log(chalk.white(`Running ${name} hook...`));

for (const command of commands) {
if (isString(command)) {
const [cmd, ..._args] = [...parseArgsStringToArgv(command), ...args];

try {
await execa(cmd, _args);
} catch (e) {
logError(e, `Failed to run ${name} hook`);
try {
if (isString(command)) {
await executeCommand(command, args);
} else if (isFunction(command)) {
await command(args);
} else if (isObject(command)) {
await executeObjectCommand(command as HookOption, args);
}
} else if (isFunction(command)) {
await command(args);
} catch (e) {
logError(e, `Failed to run ${name} hook`);
}
}
};

async function executeCommand(command: string, args: string[]) {
const [cmd, ..._args] = [...parseArgsStringToArgv(command), ...args];

await execa(cmd, _args);
}

async function executeObjectCommand(command: HookOption, args: string[]) {
if (command.injectGeneratedDirsAndFiles === false) {
args = [];
}

if (isString(command.command)) {
await executeCommand(command.command, args);
} else if (isFunction(command.command)) {
await command.command();
}
}
10 changes: 8 additions & 2 deletions packages/orval/src/utils/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import {
GlobalOptions,
Hook,
HookFunction,
HookOption,
HooksOptions,
isBoolean,
isFunction,
isObject,
isString,
isUndefined,
isUrl,
mergeDeep,
Mutator,
Expand All @@ -23,9 +25,8 @@ import {
OutputMode,
QueryOptions,
RefComponentSuffix,
upath,
SwaggerParserOptions,
isUndefined,
upath,
} from '@orval/core';
import { DEFAULT_MOCK_OPTIONS } from '@orval/mock';
import chalk from 'chalk';
Expand Down Expand Up @@ -384,6 +385,11 @@ const normalizeHooks = (hooks: HooksOptions): NormalizedHookOptions => {
...acc,
[key]: [hooks[key]] as HookFunction[],
};
} else if (isObject(hooks[key])) {
return {
...acc,
[key]: [hooks[key]] as HookOption[],
};
}

return acc;
Expand Down

0 comments on commit 4f6f352

Please sign in to comment.