|
1 | 1 | import { createGenerator } from "ts-json-schema-generator"; |
2 | 2 | import { join, resolve } from "path"; |
3 | | -import { outputJSON } from "fs-extra"; |
| 3 | +import { outputFile } from "fs-extra"; |
| 4 | +import { format, resolveConfig } from "prettier"; |
4 | 5 |
|
5 | 6 | const extensionDirectory = resolve(__dirname, ".."); |
6 | 7 |
|
@@ -37,21 +38,32 @@ const schemas = [ |
37 | 38 | }, |
38 | 39 | ]; |
39 | 40 |
|
| 41 | +async function generateSchema( |
| 42 | + schemaDefinition: (typeof schemas)[number], |
| 43 | +): Promise<void> { |
| 44 | + const schema = createGenerator({ |
| 45 | + path: schemaDefinition.path, |
| 46 | + tsconfig: resolve(extensionDirectory, "tsconfig.json"), |
| 47 | + type: schemaDefinition.type, |
| 48 | + skipTypeCheck: true, |
| 49 | + topRef: true, |
| 50 | + additionalProperties: true, |
| 51 | + }).createSchema(schemaDefinition.type); |
| 52 | + |
| 53 | + const schemaJson = JSON.stringify(schema, null, 2); |
| 54 | + |
| 55 | + const prettierOptions = await resolveConfig(schemaDefinition.schemaPath); |
| 56 | + |
| 57 | + const formattedSchemaJson = await format(schemaJson, { |
| 58 | + ...prettierOptions, |
| 59 | + filepath: schemaDefinition.schemaPath, |
| 60 | + }); |
| 61 | + |
| 62 | + await outputFile(schemaDefinition.schemaPath, formattedSchemaJson); |
| 63 | +} |
| 64 | + |
40 | 65 | async function generateSchemas() { |
41 | | - for (const schemaDefinition of schemas) { |
42 | | - const schema = createGenerator({ |
43 | | - path: schemaDefinition.path, |
44 | | - tsconfig: resolve(extensionDirectory, "tsconfig.json"), |
45 | | - type: schemaDefinition.type, |
46 | | - skipTypeCheck: true, |
47 | | - topRef: true, |
48 | | - additionalProperties: true, |
49 | | - }).createSchema(schemaDefinition.type); |
50 | | - |
51 | | - await outputJSON(schemaDefinition.schemaPath, schema, { |
52 | | - spaces: 2, |
53 | | - }); |
54 | | - } |
| 66 | + await Promise.all(schemas.map(generateSchema)); |
55 | 67 | } |
56 | 68 |
|
57 | 69 | generateSchemas().catch((e: unknown) => { |
|
0 commit comments