fork of hey-api/openapi-ts because I need some additional things
at feat/skip-token 65 lines 1.5 kB view raw
1import colors from 'ansi-colors'; 2import { sync } from 'cross-spawn'; 3 4type Output = { 5 /** 6 * The absolute path to the output folder. 7 */ 8 path: string; 9 /** 10 * Post-processing commands to run on the output folder, executed in order. 11 */ 12 postProcess: ReadonlyArray<string | UserPostProcessor>; 13}; 14 15export type UserPostProcessor = { 16 /** 17 * Arguments to pass to the command. Use `{{path}}` as a placeholder 18 * for the output directory path. 19 * 20 * @example ['format', '--write', '{{path}}'] 21 */ 22 args: ReadonlyArray<string>; 23 /** 24 * The command to run (e.g., 'biome', 'prettier', 'eslint'). 25 */ 26 command: string; 27 /** 28 * Display name for logging. Defaults to the command name. 29 */ 30 name?: string; 31}; 32 33export type PostProcessor = { 34 /** 35 * Arguments to pass to the command. 36 */ 37 args: ReadonlyArray<string>; 38 /** 39 * The command to run. 40 */ 41 command: string; 42 /** 43 * Display name for logging. 44 */ 45 name: string; 46}; 47 48export function postprocessOutput( 49 config: Output, 50 postProcessors: Record<string, PostProcessor>, 51 jobPrefix: string, 52): void { 53 for (const processor of config.postProcess) { 54 const resolved = typeof processor === 'string' ? postProcessors[processor] : processor; 55 56 // TODO: show warning 57 if (!resolved) continue; 58 59 const name = resolved.name ?? resolved.command; 60 const args = resolved.args.map((arg) => arg.replace('{{path}}', config.path)); 61 62 console.log(`${jobPrefix}🧹 Running ${colors.cyanBright(name)}`); 63 sync(resolved.command, args); 64 } 65}