-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathindex.ts
More file actions
81 lines (74 loc) · 2.56 KB
/
index.ts
File metadata and controls
81 lines (74 loc) · 2.56 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import fs from 'node:fs';
import { createRequire } from 'node:module';
import path from 'node:path';
import { generateApi } from './generate';
import type { CommonOptions, ConfigFile, GenerationOptions, OutputFileOptions } from './types';
import { isValidUrl, prettify } from './utils';
export type { ConfigFile } from './types';
const require = createRequire(__filename);
export async function generateEndpoints(options: GenerationOptions): Promise<string | void> {
const schemaLocation = options.schemaFile;
const schemaAbsPath = isValidUrl(options.schemaFile)
? options.schemaFile
: path.resolve(process.cwd(), schemaLocation);
const sourceCode = await enforceOazapftsTsVersion(async () => {
return generateApi(schemaAbsPath, options);
});
const { outputFile, prettierConfigFile } = options;
if (outputFile) {
fs.writeFileSync(
path.resolve(process.cwd(), outputFile),
await prettify(outputFile, sourceCode, prettierConfigFile)
);
} else {
return await prettify(null, sourceCode, prettierConfigFile);
}
}
export function parseConfig(fullConfig: ConfigFile) {
const outFiles: (CommonOptions & OutputFileOptions)[] = [];
if ('outputFiles' in fullConfig) {
const { outputFiles, ...commonConfig } = fullConfig;
for (const [outputFile, specificConfig] of Object.entries(outputFiles)) {
outFiles.push({
...commonConfig,
...specificConfig,
outputFile,
});
}
} else {
outFiles.push(fullConfig);
}
return outFiles;
}
/**
* Enforces `oazapfts` to use the same TypeScript version as this module itself uses.
* That should prevent enums from running out of sync if both libraries use different TS versions.
*
* In oazapfts v7, TypeScript is a peerDependency so this is typically a no-op,
* but we keep it for safety in environments with complex dependency resolution.
*/
function enforceOazapftsTsVersion<T>(cb: () => T): T {
let ozTsPath: string;
try {
ozTsPath = require.resolve('typescript', { paths: [require.resolve('oazapfts')] });
} catch {
// In oazapfts v7+, TypeScript is a peerDependency and may resolve to the
// same path. If resolution fails, just run the callback directly.
return cb();
}
const tsPath = require.resolve('typescript');
if (ozTsPath === tsPath) {
return cb();
}
const originalEntry = require.cache[ozTsPath];
try {
require.cache[ozTsPath] = require.cache[tsPath];
return cb();
} finally {
if (originalEntry) {
require.cache[ozTsPath] = originalEntry;
} else {
delete require.cache[ozTsPath];
}
}
}