Skip to content

Commit

Permalink
fix: await all writes
Browse files Browse the repository at this point in the history
  • Loading branch information
invakid404 committed Nov 21, 2024
1 parent deada02 commit dead9d9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
8 changes: 4 additions & 4 deletions src/generator/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ export const generateSchemas = async ({
const schemaName = toValidIdentifier(`${mapName}_${path}`);
const zodSchema = schemaToZod(schema);

write(`const ${schemaName} = lazyObject(() => ${zodSchema});`);
await write(`const ${schemaName} = lazyObject(() => ${zodSchema});`);
pathToSchemaMap.set(path, schemaName);
}

write(`const ${mapName} = lazyObject(() => ({`);
await write(`const ${mapName} = lazyObject(() => ({`);
for (const [scriptPath, schemaName] of pathToSchemaMap) {
write(`${JSON.stringify(scriptPath)}: ${schemaName},`);
await write(`${JSON.stringify(scriptPath)}: ${schemaName},`);
}
write("} as const))");
await write("} as const))");
};

export type SchemaToZodOptions = {
Expand Down
3 changes: 2 additions & 1 deletion src/generator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import { runWithBuffer } from "./common.js";

export const generate = async (output: Writable) => {
const allResourceTypes = await listResourceTypes();

return run(output, allResourceTypes, async () => {
writePreamble();
await writePreamble();

const results = await Promise.all(
[generateResources, generateScripts, generateFlows].map((fn) =>
Expand Down
4 changes: 2 additions & 2 deletions src/generator/preamble.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ const preamble = dedent`
}
`;

export const writePreamble = () => {
export const writePreamble = async () => {
const { write } = getContext()!;

write(preamble);
await write(preamble);
};
20 changes: 11 additions & 9 deletions src/generator/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const preamble = dedent`
export const generateResources = async () => {
const { write, allResourceTypes } = getContext()!;

write(preamble);
await write(preamble);

const resourcesByType = new Map<string, string[]>();
for await (const {
Expand All @@ -45,35 +45,37 @@ export const generateResources = async () => {
resourceTypeToSchema: resourceTypeSchemaName,
});

write(`const ${typeSchemaName} = lazyObject(() => ${resourceTypeSchema});`);
await write(
`const ${typeSchemaName} = lazyObject(() => ${resourceTypeSchema});`,
);

const referencesSchemaName = resourceReferencesSchemaName(
resourceType.name,
);
const referencesSchema = schemaToZod(makeReferencesSchema(paths));

write(
await write(
`const ${referencesSchemaName} = lazyObject(() => ${referencesSchema});`,
);
}

write(`const ${resourceToTypeMap} = lazyObject(() => ({`);
await write(`const ${resourceToTypeMap} = lazyObject(() => ({`);
for (const [resourceTypeName, paths] of resourcesByType) {
const typeSchemaName = resourceTypeSchemaName(resourceTypeName);
for (const path of paths) {
write(`${JSON.stringify(path)}: ${typeSchemaName},`);
await write(`${JSON.stringify(path)}: ${typeSchemaName},`);
}
}
write(`} as const));`);
await write(`} as const));`);

write("export type ResourceTypes = {");
await write("export type ResourceTypes = {");
for (const resourceTypeName of [...resourcesByType.keys()].toSorted()) {
const typeSchemaName = resourceTypeSchemaName(resourceTypeName);
write(
await write(
`${JSON.stringify(resourceTypeName)}: z.infer<typeof ${typeSchemaName}>,`,
);
}
write("}");
await write("}");
};

export const resourceReferencesSchemaName = (resourceType: string) =>
Expand Down

0 comments on commit dead9d9

Please sign in to comment.