fork of hey-api/openapi-ts because I need some additional things
1import { getInput } from '@hey-api/shared';
2import colors from 'ansi-colors';
3
4import type { UserConfig } from './types';
5
6export interface Job {
7 config: UserConfig;
8 index: number;
9}
10
11export function expandToJobs(configs: ReadonlyArray<UserConfig>): ReadonlyArray<Job> {
12 const jobs: Array<Job> = [];
13 let jobIndex = 0;
14
15 for (const config of configs) {
16 const inputs = getInput(config);
17 const outputs = config.output instanceof Array ? config.output : [config.output];
18
19 if (outputs.length === 1) {
20 jobs.push({
21 config: {
22 ...config,
23 input: inputs,
24 output: outputs[0]!, // output array with single item
25 },
26 index: jobIndex++,
27 });
28 } else if (outputs.length > 1 && inputs.length !== outputs.length) {
29 // Warn and create job per output (all with same inputs)
30 console.warn(
31 `⚙️ ${colors.yellow('Warning:')} You provided ${colors.cyan(String(inputs.length))} ${colors.cyan(inputs.length === 1 ? 'input' : 'inputs')} and ${colors.yellow(String(outputs.length))} ${colors.yellow('outputs')}. This will produce identical output in multiple locations. You likely want to provide a single output or the same number of outputs as inputs.`,
32 );
33 for (const output of outputs) {
34 jobs.push({
35 config: { ...config, input: inputs, output },
36 index: jobIndex++,
37 });
38 }
39 } else if (outputs.length > 1) {
40 // Pair inputs with outputs by index
41 outputs.forEach((output, index) => {
42 jobs.push({
43 config: { ...config, input: inputs[index]!, output },
44 index: jobIndex++,
45 });
46 });
47 }
48 }
49
50 return jobs;
51}