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: don't inject generated files into hook command #1181

Merged
merged 2 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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: {
melloware marked this conversation as resolved.
Show resolved Hide resolved
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