-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathcli.ts
More file actions
300 lines (272 loc) · 9.32 KB
/
cli.ts
File metadata and controls
300 lines (272 loc) · 9.32 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env node
import { command, run, option, string, optional, positional } from 'cmd-ts';
import * as E from 'fp-ts/Either';
import * as fs from 'fs';
import * as p from 'node:path';
import type { Expression } from '@swc/core';
import type { OpenAPIV3 } from 'openapi-types';
import { parseApiSpec, parseApiSpecComment } from './apiSpec';
import { getRefs } from './ref';
import { convertRoutesToOpenAPI } from './openapi';
import type { Route } from './route';
import type { Schema } from './ir';
import { getPackageJsonPath } from './packageInfo';
import { Project } from './project';
import { KNOWN_IMPORTS } from './knownImports';
import { findSymbolInitializer } from './resolveInit';
import { parseCodecInitializer } from './codec';
import { SourceFile } from './sourceFile';
import { logError, logInfo, logWarn } from './error';
const app = command({
name: 'api-ts',
args: {
input: positional({
type: string,
description: `API route definition file`,
displayName: 'file',
}),
name: option({
type: optional(string),
description: 'API name',
long: 'name',
short: 'n',
}),
version: option({
type: optional(string),
description: 'API version',
long: 'version',
short: 'v',
}),
description: option({
type: optional(string),
description: 'API description',
long: 'description',
short: 'd',
}),
codecFile: option({
type: optional(string),
description: 'Custom codec definition file',
long: 'codec-file',
short: 'c',
}),
},
handler: async ({
input,
name: nameParam,
version: versionParam,
description: descriptionParam,
codecFile: codecFileParam,
}) => {
const filePath = p.resolve(input);
const packageJsonPath = await getPackageJsonPath(filePath);
let packageJson: Record<string, any> = {};
if (packageJsonPath !== undefined) {
packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
}
const name = nameParam ?? packageJson['name'] ?? 'openapi-generator';
const version = versionParam ?? packageJson['version'] ?? '0.0.1';
const description =
descriptionParam ?? packageJson['description'] ?? `API spec for ${name}`;
let codecFile: string | undefined = codecFileParam;
if (
codecFileParam === undefined &&
packageJsonPath !== undefined &&
packageJson['openapi-generator']?.['codec-file'] !== undefined
) {
const relativeCodecFilePath = packageJson['openapi-generator']['codec-file'];
codecFile = p.join(p.dirname(packageJsonPath), relativeCodecFilePath);
}
let knownImports = KNOWN_IMPORTS;
if (codecFile !== undefined) {
const codecFilePath = p.resolve(codecFile);
const codecModule = await import(codecFilePath);
if (codecModule.default === undefined) {
logError(`Could not find default export in ${codecFilePath}`);
process.exit(1);
}
const customCodecs = codecModule.default(E);
knownImports = { ...knownImports, ...customCodecs };
}
const projectE = await new Project({}, knownImports).parseEntryPoint(filePath);
if (E.isLeft(projectE)) {
logError(`${projectE.left}`);
process.exit(1);
}
const project = projectE.right;
const entryPoint = project.get(filePath);
if (entryPoint === undefined) {
logError(`Could not find entry point ${filePath}`);
process.exit(1);
}
let apiSpec: Route[] = [];
let servers: OpenAPIV3.ServerObject[] = [];
for (const symbol of Object.values(entryPoint.symbols.declarations)) {
if (symbol.init === undefined) {
continue;
} else if (symbol.init.type !== 'CallExpression') {
continue;
} else if (symbol.init.arguments.length === 0) {
continue;
} else if (
symbol.init.callee.type === 'Super' ||
symbol.init.callee.type === 'Import'
) {
logWarn(`Skipping ${symbol.name} because it is a ${symbol.init.callee.type}`);
continue;
} else if (!isApiSpec(entryPoint, symbol.init.callee)) {
continue;
}
logInfo(`Found API spec in ${symbol.name}`);
const result = parseApiSpec(
project,
entryPoint,
symbol.init.arguments[0]!.expression,
);
if (E.isLeft(result)) {
logError(`Error when parsing ${symbol.name}: ${result.left}`);
process.exit(1);
}
const server = parseApiSpecComment(symbol.comment);
if (server !== undefined) {
servers.push(server);
}
apiSpec.push(...result.right);
}
if (apiSpec.length === 0) {
logError(`Could not find API spec in ${filePath}`);
process.exit(1);
}
const components: Record<string, Schema> = {};
const queue: Schema[] = apiSpec.flatMap((route) => {
return [
...route.parameters.map((p) => p.schema),
...(route.body !== undefined ? [route.body] : []),
...Object.values(route.response),
];
});
const componentLocations: Record<string, string> = {};
const componentCollisionCounters: Record<string, number> = {};
const componentNameMapping: Record<string, Record<string, string>> = {};
// Helper to generate unique component names (name, name1, name2, ...)
const getUniqueComponentName = (name: string): string => {
if (components[name] === undefined) {
return name;
}
const counter = (componentCollisionCounters[name] ?? 0) + 1;
componentCollisionCounters[name] = counter;
return `${name}${counter}`;
};
let schema: Schema | undefined;
while (((schema = queue.pop()), schema !== undefined)) {
const refs = getRefs(schema, project.getTypes());
for (const ref of refs) {
if (
components[ref.name] !== undefined &&
componentLocations[ref.name] === ref.location
) {
continue;
}
const componentName =
components[ref.name] !== undefined
? getUniqueComponentName(ref.name)
: ref.name;
// Track the mapping: location -> originalName -> finalComponentName
if (componentNameMapping[ref.location] === undefined) {
componentNameMapping[ref.location] = {};
}
componentNameMapping[ref.location]![ref.name] = componentName;
const sourceFile = project.get(ref.location);
if (sourceFile === undefined) {
logError(`Could not find '${ref.name}' from '${ref.location}'`);
process.exit(1);
}
const initE = findSymbolInitializer(project, sourceFile, ref.name);
if (E.isLeft(initE)) {
logError(
`Could not find symbol '${ref.name}' in '${ref.location}': ${initE.left}`,
);
process.exit(1);
}
const [newSourceFile, init, comment] = initE.right;
if (init === null) {
console.log({ ref });
let errorMessage = `Could not determine encode/decode types for codec '${ref.name}' in '${ref.location}'`;
if (ref.location.includes('/node_modules/io-ts-types/')) {
errorMessage += `
It looks like this codec comes from io-ts-types. Try importing directly from io-ts-types instead:
\`\`\`
import { ${ref.name} } from 'io-ts-types';
\`\`\`
`;
} else {
errorMessage += `
Consider defining a custom codec for this type.
https://github.com/BitGo/api-ts/tree/master/packages/openapi-generator#4-defining-custom-codecs
`;
}
logError(
errorMessage
.split('\n')
.map((line) => line.trimStart())
.join('\n'),
);
process.exit(1);
}
const codecE = parseCodecInitializer(project, newSourceFile, init);
if (E.isLeft(codecE)) {
logError(
`Could not parse codec '${ref.name}' in '${ref.location}': ${codecE.left}`,
);
process.exit(1);
}
if (comment !== undefined) {
codecE.right.comment = comment;
}
components[componentName] = codecE.right;
componentLocations[componentName] = ref.location;
queue.push(codecE.right);
}
}
const openapi = convertRoutesToOpenAPI(
{
title: name,
version,
description,
},
servers,
apiSpec,
components,
componentNameMapping,
);
console.log(JSON.stringify(openapi, null, 2));
},
});
function isApiSpec(entryPoint: SourceFile, init: Expression): boolean {
if (init.type === 'Identifier') {
return (
entryPoint.symbols.imports.find(
(imp) =>
imp.type === 'named' &&
imp.from === '@api-ts/io-ts-http' &&
imp.importedName === 'apiSpec' &&
imp.localName === init.value,
) !== undefined
);
} else if (init.type === 'MemberExpression') {
return (
entryPoint.symbols.imports.find(
(imp) =>
imp.type === 'star' &&
imp.from === '@api-ts/io-ts-http' &&
init.object.type === 'Identifier' &&
init.object.value === imp.localName &&
init.property.type === 'Identifier' &&
init.property.value === 'apiSpec',
) !== undefined
);
} else {
return false;
}
}
// parse arguments
run(app, process.argv.slice(2));