@@ -2,7 +2,7 @@ import SwaggerParser from "@apidevtools/swagger-parser";
22import type { OpenAPIObject } from "openapi3-ts/oas31" ;
33import { basename , join , dirname } from "pathe" ;
44import { type } from "arktype" ;
5- import { writeFile } from "fs/promises" ;
5+ import { mkdir , writeFile } from "fs/promises" ;
66import { allowedRuntimes , generateFile } from "./generator.ts" ;
77import { mapOpenApiEndpoints } from "./map-openapi-endpoints.ts" ;
88import { generateTanstackQueryFile } from "./tanstack-query.generator.ts" ;
@@ -11,6 +11,14 @@ import { prettify } from "./format.ts";
1111const cwd = process . cwd ( ) ;
1212const 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+
1422export 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