Skip to content

Commit 9f70b13

Browse files
committed
feat: ensure dir
1 parent 8518912 commit 9f70b13

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

.changeset/major-buses-relate.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"typed-openapi": patch
3+
---
4+
5+
Ensure dir is created before generating files

packages/typed-openapi/src/generate-client-files.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import SwaggerParser from "@apidevtools/swagger-parser";
22
import type { OpenAPIObject } from "openapi3-ts/oas31";
33
import { basename, join, dirname } from "pathe";
44
import { type } from "arktype";
5-
import { writeFile } from "fs/promises";
5+
import { mkdir, writeFile } from "fs/promises";
66
import { allowedRuntimes, generateFile } from "./generator.ts";
77
import { mapOpenApiEndpoints } from "./map-openapi-endpoints.ts";
88
import { generateTanstackQueryFile } from "./tanstack-query.generator.ts";
@@ -11,6 +11,14 @@ import { prettify } from "./format.ts";
1111
const cwd = process.cwd();
1212
const now = new Date();
1313

14+
async function ensureDir(dirPath: string): Promise<void> {
15+
try {
16+
await mkdir(dirPath, { recursive: true });
17+
} catch (error) {
18+
console.error(`Error ensuring directory: ${(error as Error).message}`);
19+
}
20+
}
21+
1422
export const optionsSchema = type({
1523
"output?": "string",
1624
runtime: allowedRuntimes,
@@ -35,6 +43,7 @@ export async function generateClientFiles(input: string, options: typeof options
3543
);
3644

3745
console.log("Generating client...", outputPath);
46+
await ensureDir(dirname(outputPath));
3847
await writeFile(outputPath, content);
3948

4049
if (options.tanstack) {
@@ -44,6 +53,7 @@ export async function generateClientFiles(input: string, options: typeof options
4453
});
4554
const tanstackOutputPath = join(dirname(outputPath), typeof options.tanstack === "string" ? options.tanstack : `tanstack.client.ts`);
4655
console.log("Generating tanstack client...", tanstackOutputPath);
56+
await ensureDir(dirname(tanstackOutputPath));
4757
await writeFile(tanstackOutputPath, tanstackContent);
4858
}
4959

0 commit comments

Comments
 (0)