Format generated schema files with Prettier

This commit is contained in:
Koen Vlaswinkel
2023-09-25 15:35:02 +02:00
parent c55e87c64b
commit 9392fb75c8
2 changed files with 28 additions and 21 deletions

View File

@@ -1,6 +1,7 @@
import { createGenerator } from "ts-json-schema-generator";
import { join, resolve } from "path";
import { outputJSON } from "fs-extra";
import { outputFile } from "fs-extra";
import { format, resolveConfig } from "prettier";
const extensionDirectory = resolve(__dirname, "..");
@@ -37,21 +38,32 @@ const schemas = [
},
];
async function generateSchemas() {
for (const schemaDefinition of schemas) {
const schema = createGenerator({
path: schemaDefinition.path,
tsconfig: resolve(extensionDirectory, "tsconfig.json"),
type: schemaDefinition.type,
skipTypeCheck: true,
topRef: true,
additionalProperties: true,
}).createSchema(schemaDefinition.type);
async function generateSchema(
schemaDefinition: (typeof schemas)[number],
): Promise<void> {
const schema = createGenerator({
path: schemaDefinition.path,
tsconfig: resolve(extensionDirectory, "tsconfig.json"),
type: schemaDefinition.type,
skipTypeCheck: true,
topRef: true,
additionalProperties: true,
}).createSchema(schemaDefinition.type);
await outputJSON(schemaDefinition.schemaPath, schema, {
spaces: 2,
});
}
const schemaJson = JSON.stringify(schema, null, 2);
const prettierOptions = await resolveConfig(schemaDefinition.schemaPath);
const formattedSchemaJson = await format(schemaJson, {
...prettierOptions,
filepath: schemaDefinition.schemaPath,
});
await outputFile(schemaDefinition.schemaPath, formattedSchemaJson);
}
async function generateSchemas() {
await Promise.all(schemas.map(generateSchema));
}
generateSchemas().catch((e: unknown) => {

View File

@@ -31,12 +31,7 @@
}
}
},
"required": [
"name",
"version",
"dataExtensions",
"extensionTargets"
]
"required": ["name", "version", "dataExtensions", "extensionTargets"]
}
}
}