Skip to content

Commit

Permalink
feat: don't inject generated files into hook command (#1181)
Browse files Browse the repository at this point in the history
* feat: don't inject generated files into hook command

* Update hooks.md

---------

Co-authored-by: Melloware <mellowaredev@gmail.com>
  • Loading branch information
Arkanii and melloware committed Jan 31, 2024
1 parent 0d05129 commit 2a77a04
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 14 deletions.
19 changes: 17 additions & 2 deletions docs/src/pages/reference/configuration/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ id: configuration-hooks
title: Hooks
---

### afterAllFilesWrite
### afterAllFilesWritten

Type: `String` or `String[]` or `Function`.

Expand All @@ -15,7 +15,22 @@ that are generated by orval.
module.exports = {
petstore: {
hooks: {
afterAllFilesWrite: 'prettier --write',
afterAllFilesWritten: 'prettier --write',
},
},
};
```

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

```js
module.exports = {
petstore: {
hooks: {
afterAllFilesWritten: {
command: 'prettier --write .',
injectGeneratedDirsAndFiles: false,
},
},
},
};
Expand Down
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 2a77a04

Please sign in to comment.