Skip to content

Commit 641233f

Browse files
committed
Update gen-schemas.ts
1 parent 43a6acb commit 641233f

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

scripts/gen-schemas.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@ import { convertToZod, SchemaSchema } from "./shared";
33
import { readdirSync } from "node:fs";
44
import path from "node:path";
55
import * as fs from "node:fs/promises";
6+
import { prettifyError } from "zod/mini";
67

78
async function processFile(filePath: string) {
8-
filePath = filePath.replaceAll("\\", "/");
9-
const fileName = filePath.split("/").pop()!.replace(".yaml", "");
10-
console.log({ filePath, fileName });
11-
const yamlStr = await Bun.file(filePath).text();
9+
const normalizedFilePath = filePath.replaceAll("\\", "/");
10+
const fileName = normalizedFilePath.split("/").pop()!.replace(".yaml", "");
11+
const yamlStr = await Bun.file(normalizedFilePath).text();
1212
const yamlContent = Bun.YAML.parse(yamlStr);
13-
const parsedSchema = SchemaSchema.parse(yamlContent);
14-
const { zodSchema, refs: uniqueRefs } = convertToZod(parsedSchema);
13+
const parsedSchema = SchemaSchema.safeParse(yamlContent);
14+
if (parsedSchema.error) {
15+
console.error(`Error while parsing ${normalizedFilePath}`);
16+
console.error(prettifyError(parsedSchema.error));
17+
throw parsedSchema.error;
18+
}
19+
const { zodSchema, refs: uniqueRefs } = convertToZod(parsedSchema.data);
1520

1621
uniqueRefs.sort();
1722
const refImports = uniqueRefs
@@ -37,6 +42,7 @@ export { ${fileName} };
3742
}
3843

3944
async function main() {
45+
console.log("Generating schemas...");
4046
const outDir = "src/schemas" as const;
4147
await fs.rm(outDir, { recursive: true, force: true });
4248
const schemasDir = "specs/schemas" as const;
@@ -56,6 +62,7 @@ async function main() {
5662
const content = importLines.join("\n");
5763
const outFile = `${outDir}/index.ts` as const;
5864
await Bun.write(outFile, content);
65+
console.log("Schemas generated");
5966
}
6067

6168
await main();

0 commit comments

Comments
 (0)