forked from orval-labs/orval
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.ts
52 lines (44 loc) · 1.31 KB
/
generate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { pathExists } from 'fs-extra';
import { dirname, resolve } from 'upath';
import { importSpecs } from './core/importers/specs';
import { writeSpecs } from './core/writers/specs';
import { ExternalConfigFile, Options } from './types';
import { catchError } from './utils/errors';
import { dynamicImport } from './utils/imports';
export const generateSpec = async (
workspace: string,
options: Options,
projectName?: string,
) => {
try {
const writeSpecProps = await importSpecs(workspace, options);
await writeSpecs(writeSpecProps, workspace, options, projectName);
} catch (e) {
catchError(e);
}
};
export const generateConfig = async (
path: string = './orval.config.js',
projectName?: string,
) => {
const fullPath = resolve(process.cwd(), path);
if (!(await pathExists(fullPath))) {
catchError('orval config not found');
}
const config = await dynamicImport<ExternalConfigFile>(fullPath);
const workspace = dirname(fullPath);
if (projectName) {
const project = config[projectName];
if (project) {
generateSpec(workspace, project, projectName);
} else {
catchError('Project not found');
}
return;
}
return Promise.all(
Object.entries(config).map(([projectName, options]) =>
generateSpec(workspace, options, projectName),
),
);
};