Skip to content

Commit ce7f4d9

Browse files
author
Louis Varin
authored
Merge pull request #664 from bitgopatmcl/less-silent-errors
Only attempt to parse `apiSpec`s, error out if parsing fails
2 parents 38f7af7 + eb6c15b commit ce7f4d9

File tree

1 file changed

+42
-1
lines changed
  • packages/openapi-generator/src

1 file changed

+42
-1
lines changed

packages/openapi-generator/src/cli.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
import * as E from 'fp-ts/Either';
1414
import * as fs from 'fs';
1515
import * as p from 'path';
16+
import type { Expression } from '@swc/core';
1617

1718
import { parseApiSpec } from './apiSpec';
1819
import { getRefs } from './ref';
@@ -24,6 +25,7 @@ import { Project } from './project';
2425
import { KNOWN_IMPORTS } from './knownImports';
2526
import { findSymbolInitializer } from './resolveInit';
2627
import { parseCodecInitializer } from './codec';
28+
import { SourceFile } from './sourceFile';
2729

2830
const app = command({
2931
name: 'api-ts',
@@ -118,7 +120,18 @@ const app = command({
118120
continue;
119121
} else if (symbol.init.arguments.length === 0) {
120122
continue;
123+
} else if (
124+
symbol.init.callee.type === 'Super' ||
125+
symbol.init.callee.type === 'Import'
126+
) {
127+
console.error(
128+
`Skipping ${symbol.name} because it is a ${symbol.init.callee.type}`,
129+
);
130+
continue;
131+
} else if (!isApiSpec(entryPoint, symbol.init.callee)) {
132+
continue;
121133
}
134+
console.error(`Found API spec in ${symbol.name}`);
122135

123136
const result = parseApiSpec(
124137
project.right,
@@ -127,7 +140,7 @@ const app = command({
127140
);
128141
if (E.isLeft(result)) {
129142
console.error(`Error parsing ${symbol.name}: ${result.left}`);
130-
continue;
143+
process.exit(1);
131144
}
132145

133146
apiSpec.push(...result.right);
@@ -190,5 +203,33 @@ const app = command({
190203
},
191204
});
192205

206+
function isApiSpec(entryPoint: SourceFile, init: Expression): boolean {
207+
if (init.type === 'Identifier') {
208+
return (
209+
entryPoint.symbols.imports.find(
210+
(imp) =>
211+
imp.type === 'named' &&
212+
imp.from === '@api-ts/io-ts-http' &&
213+
imp.importedName === 'apiSpec' &&
214+
imp.localName === init.value,
215+
) !== undefined
216+
);
217+
} else if (init.type === 'MemberExpression') {
218+
return (
219+
entryPoint.symbols.imports.find(
220+
(imp) =>
221+
imp.type === 'star' &&
222+
imp.from === '@api-ts/io-ts-http' &&
223+
init.object.type === 'Identifier' &&
224+
init.object.value === imp.localName &&
225+
init.property.type === 'Identifier' &&
226+
init.property.value === 'apiSpec',
227+
) !== undefined
228+
);
229+
} else {
230+
return false;
231+
}
232+
}
233+
193234
// parse arguments
194235
run(app, process.argv.slice(2));

0 commit comments

Comments
 (0)