Skip to content

Commit af65620

Browse files
committed
feat: get compilerOptions from file
1 parent 1d2f10b commit af65620

File tree

6 files changed

+153
-42
lines changed

6 files changed

+153
-42
lines changed

samples/freee.json

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25584,9 +25584,11 @@
2558425584
"components": {
2558525585
"schemas": {
2558625586
"URLSearchParams": {
25587+
"description": "`URLSearchParams` class is a global reference for `import { URLSearchParams } from 'node:url'`\nhttps://nodejs.org/api/url.html#class-urlsearchparams",
2558725588
"type": "object",
2558825589
"properties": {
2558925590
"size": {
25591+
"description": "The total number of parameter entries.",
2559025592
"type": "number"
2559125593
}
2559225594
},
@@ -25595,12 +25597,15 @@
2559525597
]
2559625598
},
2559725599
"Blob": {
25600+
"description": "`Blob` class is a global reference for `import { Blob } from 'node:buffer'`\nhttps://nodejs.org/api/buffer.html#class-blob",
2559825601
"type": "object",
2559925602
"properties": {
2560025603
"size": {
25604+
"description": "The total size of the `Blob` in bytes.",
2560125605
"type": "number"
2560225606
},
2560325607
"type": {
25608+
"description": "The content-type of the `Blob`.",
2560425609
"type": "string"
2560525610
}
2560625611
},
@@ -25610,33 +25615,67 @@
2561025615
]
2561125616
},
2561225617
"FormData": {
25613-
"type": "object"
25618+
"type": "object",
25619+
"properties": {
25620+
"forEach": {
25621+
"description": "Executes given callback function for each field of the FormData instance",
25622+
"type": "object"
25623+
},
25624+
"keys": {
25625+
"description": "Returns an [`iterator`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols) allowing to go through all keys contained in this `FormData` object.\nEach key is a `string`.",
25626+
"type": "object"
25627+
},
25628+
"values": {
25629+
"description": "Returns an [`iterator`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols) allowing to go through all values contained in this object `FormData` object.\nEach value is a [`FormDataValue`](https://developer.mozilla.org/en-US/docs/Web/API/FormDataEntryValue).",
25630+
"type": "object"
25631+
},
25632+
"entries": {
25633+
"description": "Returns an [`iterator`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols) allowing to go through the `FormData` key/value pairs.\nThe key of each pair is a string; the value is a [`FormDataValue`](https://developer.mozilla.org/en-US/docs/Web/API/FormDataEntryValue).",
25634+
"type": "object"
25635+
},
25636+
"__@iterator@11618": {
25637+
"description": "An alias for FormData#entries()",
25638+
"type": "object"
25639+
},
25640+
"__@toStringTag@11624": {
25641+
"type": "string"
25642+
}
25643+
},
25644+
"required": [
25645+
"__@iterator@11618",
25646+
"__@toStringTag@11624",
25647+
"entries",
25648+
"forEach",
25649+
"keys",
25650+
"values"
25651+
]
2561425652
},
2561525653
"File": {
25654+
"description": "`File` class is a global reference for `import { File } from 'node:buffer'`\nhttps://nodejs.org/api/buffer.html#class-file",
2561625655
"type": "object",
2561725656
"properties": {
25618-
"lastModified": {
25619-
"type": "number"
25620-
},
2562125657
"name": {
25658+
"description": "The name of the `File`.",
2562225659
"type": "string"
2562325660
},
25624-
"webkitRelativePath": {
25625-
"type": "string"
25661+
"lastModified": {
25662+
"description": "The last modified date of the `File`.",
25663+
"type": "number"
2562625664
},
2562725665
"size": {
25666+
"description": "The total size of the `Blob` in bytes.",
2562825667
"type": "number"
2562925668
},
2563025669
"type": {
25670+
"description": "The content-type of the `Blob`.",
2563125671
"type": "string"
2563225672
}
2563325673
},
2563425674
"required": [
2563525675
"lastModified",
2563625676
"name",
2563725677
"size",
25638-
"type",
25639-
"webkitRelativePath"
25678+
"type"
2564025679
]
2564125680
}
2564225681
}

samples/openapi.json

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5548,33 +5548,67 @@
55485548
]
55495549
},
55505550
"FormData": {
5551-
"type": "object"
5551+
"type": "object",
5552+
"properties": {
5553+
"forEach": {
5554+
"description": "Executes given callback function for each field of the FormData instance",
5555+
"type": "object"
5556+
},
5557+
"keys": {
5558+
"description": "Returns an [`iterator`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols) allowing to go through all keys contained in this `FormData` object.\nEach key is a `string`.",
5559+
"type": "object"
5560+
},
5561+
"values": {
5562+
"description": "Returns an [`iterator`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols) allowing to go through all values contained in this object `FormData` object.\nEach value is a [`FormDataValue`](https://developer.mozilla.org/en-US/docs/Web/API/FormDataEntryValue).",
5563+
"type": "object"
5564+
},
5565+
"entries": {
5566+
"description": "Returns an [`iterator`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols) allowing to go through the `FormData` key/value pairs.\nThe key of each pair is a string; the value is a [`FormDataValue`](https://developer.mozilla.org/en-US/docs/Web/API/FormDataEntryValue).",
5567+
"type": "object"
5568+
},
5569+
"__@iterator@15354": {
5570+
"description": "An alias for FormData#entries()",
5571+
"type": "object"
5572+
},
5573+
"__@toStringTag@15391": {
5574+
"type": "string"
5575+
}
5576+
},
5577+
"required": [
5578+
"__@iterator@15354",
5579+
"__@toStringTag@15391",
5580+
"entries",
5581+
"forEach",
5582+
"keys",
5583+
"values"
5584+
]
55525585
},
55535586
"File": {
5587+
"description": "`File` class is a global reference for `import { File } from 'node:buffer'`\nhttps://nodejs.org/api/buffer.html#class-file",
55545588
"type": "object",
55555589
"properties": {
5556-
"lastModified": {
5557-
"type": "number"
5558-
},
55595590
"name": {
5591+
"description": "The name of the `File`.",
55605592
"type": "string"
55615593
},
5562-
"webkitRelativePath": {
5563-
"type": "string"
5594+
"lastModified": {
5595+
"description": "The last modified date of the `File`.",
5596+
"type": "number"
55645597
},
55655598
"size": {
5599+
"description": "The total size of the `Blob` in bytes.",
55665600
"type": "number"
55675601
},
55685602
"type": {
5603+
"description": "The content-type of the `Blob`.",
55695604
"type": "string"
55705605
}
55715606
},
55725607
"required": [
55735608
"lastModified",
55745609
"name",
55755610
"size",
5576-
"type",
5577-
"webkitRelativePath"
5611+
"type"
55785612
]
55795613
}
55805614
}

samples/simple-openapi.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -315,30 +315,31 @@
315315
"components": {
316316
"schemas": {
317317
"File": {
318+
"description": "`File` class is a global reference for `import { File } from 'node:buffer'`\nhttps://nodejs.org/api/buffer.html#class-file",
318319
"type": "object",
319320
"properties": {
320-
"lastModified": {
321-
"type": "number"
322-
},
323321
"name": {
322+
"description": "The name of the `File`.",
324323
"type": "string"
325324
},
326-
"webkitRelativePath": {
327-
"type": "string"
325+
"lastModified": {
326+
"description": "The last modified date of the `File`.",
327+
"type": "number"
328328
},
329329
"size": {
330+
"description": "The total size of the `Blob` in bytes.",
330331
"type": "number"
331332
},
332333
"type": {
334+
"description": "The content-type of the `Blob`.",
333335
"type": "string"
334336
}
335337
},
336338
"required": [
337339
"lastModified",
338340
"name",
339341
"size",
340-
"type",
341-
"webkitRelativePath"
342+
"type"
342343
]
343344
}
344345
}

samples/swagger.json

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -873,9 +873,11 @@
873873
"components": {
874874
"schemas": {
875875
"URLSearchParams": {
876+
"description": "`URLSearchParams` class is a global reference for `import { URLSearchParams } from 'node:url'`\nhttps://nodejs.org/api/url.html#class-urlsearchparams",
876877
"type": "object",
877878
"properties": {
878879
"size": {
880+
"description": "The total number of parameter entries.",
879881
"type": "number"
880882
}
881883
},
@@ -884,33 +886,67 @@
884886
]
885887
},
886888
"FormData": {
887-
"type": "object"
889+
"type": "object",
890+
"properties": {
891+
"forEach": {
892+
"description": "Executes given callback function for each field of the FormData instance",
893+
"type": "object"
894+
},
895+
"keys": {
896+
"description": "Returns an [`iterator`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols) allowing to go through all keys contained in this `FormData` object.\nEach key is a `string`.",
897+
"type": "object"
898+
},
899+
"values": {
900+
"description": "Returns an [`iterator`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols) allowing to go through all values contained in this object `FormData` object.\nEach value is a [`FormDataValue`](https://developer.mozilla.org/en-US/docs/Web/API/FormDataEntryValue).",
901+
"type": "object"
902+
},
903+
"entries": {
904+
"description": "Returns an [`iterator`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols) allowing to go through the `FormData` key/value pairs.\nThe key of each pair is a string; the value is a [`FormDataValue`](https://developer.mozilla.org/en-US/docs/Web/API/FormDataEntryValue).",
905+
"type": "object"
906+
},
907+
"__@iterator@129": {
908+
"description": "An alias for FormData#entries()",
909+
"type": "object"
910+
},
911+
"__@toStringTag@135": {
912+
"type": "string"
913+
}
914+
},
915+
"required": [
916+
"__@iterator@129",
917+
"__@toStringTag@135",
918+
"entries",
919+
"forEach",
920+
"keys",
921+
"values"
922+
]
888923
},
889924
"File": {
925+
"description": "`File` class is a global reference for `import { File } from 'node:buffer'`\nhttps://nodejs.org/api/buffer.html#class-file",
890926
"type": "object",
891927
"properties": {
892-
"lastModified": {
893-
"type": "number"
894-
},
895928
"name": {
929+
"description": "The name of the `File`.",
896930
"type": "string"
897931
},
898-
"webkitRelativePath": {
899-
"type": "string"
932+
"lastModified": {
933+
"description": "The last modified date of the `File`.",
934+
"type": "number"
900935
},
901936
"size": {
937+
"description": "The total size of the `Blob` in bytes.",
902938
"type": "number"
903939
},
904940
"type": {
941+
"description": "The content-type of the `Blob`.",
905942
"type": "string"
906943
}
907944
},
908945
"required": [
909946
"lastModified",
910947
"name",
911948
"size",
912-
"type",
913-
"webkitRelativePath"
949+
"type"
914950
]
915951
}
916952
}

src/generate.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { getDirentTree } from 'aspida/dist/cjs/getDirentTree';
44
import { existsSync, readFileSync, unlinkSync, writeFileSync } from 'fs';
55
import type { OpenAPIV3_1 } from 'openapi-types';
66
import { join } from 'path';
7+
import ts from 'typescript';
78
import * as TJS from 'typescript-json-schema';
89
import type { PartialConfig } from './getConfig';
910
import { getConfig } from './getConfig';
@@ -29,7 +30,7 @@ export const generate = (configs?: PartialConfig) =>
2930
console.log(`${config.output} was built successfully.`);
3031
});
3132

32-
const toOpenAPI = (params: { input: string; template?: OpenAPIV3_1.Document | string }): string => {
33+
const toOpenAPI = (params: { input: string; template: OpenAPIV3_1.Document | string }): string => {
3334
const tree = getDirentTree(params.input);
3435

3536
const createFilePaths = (tree: DirentTree): string[] => {
@@ -50,15 +51,16 @@ type AllMethods = [${paths.map((_, i) => `Methods${i}`).join(', ')}]`;
5051

5152
writeFileSync(typeFilePath, typeFile, 'utf8');
5253

53-
const compilerOptions: TJS.CompilerOptions = {
54-
strictNullChecks: true,
55-
rootDir: process.cwd(),
56-
baseUrl: process.cwd(),
57-
// @ts-expect-error dont match ScriptTarget
58-
target: 'ES2022',
59-
};
54+
const configFileName = ts.findConfigFile(params.input, ts.sys.fileExists);
55+
const compilerOptions = configFileName
56+
? ts.parseJsonConfigFileContent(
57+
ts.readConfigFile(configFileName, ts.sys.readFile).config,
58+
ts.sys,
59+
params.input,
60+
)
61+
: undefined;
6062

61-
const program = TJS.getProgramFromFiles([typeFilePath], compilerOptions);
63+
const program = TJS.getProgramFromFiles([typeFilePath], compilerOptions?.options);
6264
const schema = TJS.generateSchema(program, 'AllMethods', { required: true });
6365
const doc: OpenAPIV3_1.Document = {
6466
...(typeof params.template === 'string'

tsconfig.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
"declaration": true,
44
"declarationMap": true,
55
"downlevelIteration": true,
6-
"experimentalDecorators": true,
76
"esModuleInterop": true,
8-
"lib": ["esnext", "dom", "dom.iterable"],
7+
"lib": ["esnext"],
98
"module": "commonjs",
109
"moduleResolution": "node",
1110
"outDir": "dist",

0 commit comments

Comments
 (0)