@kubb/cli
Version:
Command-line interface for Kubb, enabling easy generation of TypeScript, React-Query, Zod, and other code from OpenAPI specifications.
34 lines (26 loc) • 1.01 kB
text/typescript
import { createHash } from 'node:crypto'
import { styleText } from 'node:util'
import type { Config, KubbEvents } from '@kubb/core'
import type { AsyncEventEmitter } from '@kubb/core/utils'
import { tokenize } from '@kubb/core/utils'
type ExecutingHooksProps = {
hooks: NonNullable<Config['hooks']>
events: AsyncEventEmitter<KubbEvents>
}
export async function executeHooks({ hooks, events }: ExecutingHooksProps): Promise<void> {
const commands = Array.isArray(hooks.done) ? hooks.done : [hooks.done].filter(Boolean)
for (const command of commands) {
const [cmd, ...args] = tokenize(command)
if (!cmd) {
continue
}
const hookId = createHash('sha256').update(command).digest('hex')
await events.emit('hook:start', { id: hookId, command: cmd, args })
await events.onOnce('hook:end', async ({ success, error }) => {
if (!success) {
throw error
}
await events.emit('success', `${styleText('dim', command)} successfully executed`)
})
}
}