diff --git a/.changeset/fuzzy-vans-leave.md b/.changeset/fuzzy-vans-leave.md new file mode 100644 index 0000000000..8715888221 --- /dev/null +++ b/.changeset/fuzzy-vans-leave.md @@ -0,0 +1,5 @@ +--- +"@hey-api/openapi-ts": patch +--- + +fix: only delete generated files instead of whole output directory diff --git a/packages/openapi-ts/src/compiler/index.ts b/packages/openapi-ts/src/compiler/index.ts index 331696d966..b6064a2405 100644 --- a/packages/openapi-ts/src/compiler/index.ts +++ b/packages/openapi-ts/src/compiler/index.ts @@ -1,4 +1,4 @@ -import { writeFileSync } from 'node:fs'; +import { PathLike, writeFileSync } from 'node:fs'; import ts from 'typescript'; @@ -15,9 +15,9 @@ export class TypeScriptFile { private _headers: Array = []; private _imports: Array = []; private _items: Array = []; - private _path: string; + private _path: PathLike; - public constructor(path: string, header: boolean = true) { + public constructor({ path, header = true }: { path: PathLike; header?: boolean }) { this._path = path; if (header) { const text = 'This file is auto-generated by @hey-api/openapi-ts'; diff --git a/packages/openapi-ts/src/utils/write/client.ts b/packages/openapi-ts/src/utils/write/client.ts index 8a2c4ecb63..82766438f6 100644 --- a/packages/openapi-ts/src/utils/write/client.ts +++ b/packages/openapi-ts/src/utils/write/client.ts @@ -1,4 +1,4 @@ -import { mkdirSync, rmSync } from 'node:fs'; +import { existsSync, mkdirSync } from 'node:fs'; import path from 'node:path'; import type { OpenApi } from '../../openApi'; @@ -20,10 +20,6 @@ import { writeServices } from './services'; */ export const writeClient = async (openApi: OpenApi, client: Client, templates: Templates): Promise => { const config = getConfig(); - await rmSync(config.output, { - force: true, - recursive: true, - }); if (typeof config.exportServices === 'string') { const regexp = new RegExp(config.exportServices); @@ -35,6 +31,10 @@ export const writeClient = async (openApi: OpenApi, client: Client, templates: T client.models = client.models.filter(model => regexp.test(model.name)); } + if (!existsSync(path.resolve(config.output))) { + mkdirSync(path.resolve(config.output), { recursive: true }); + } + const sections = [ { dir: 'core', @@ -60,15 +60,6 @@ export const writeClient = async (openApi: OpenApi, client: Client, templates: T for (const section of sections) { const sectionPath = path.resolve(config.output, section.dir); - if (section.dir) { - await rmSync(sectionPath, { - force: true, - recursive: true, - }); - } - await mkdirSync(sectionPath, { - recursive: true, - }); await section.fn(openApi, sectionPath, client, templates); } diff --git a/packages/openapi-ts/src/utils/write/core.ts b/packages/openapi-ts/src/utils/write/core.ts index bc3ba33263..4cf4867ebd 100644 --- a/packages/openapi-ts/src/utils/write/core.ts +++ b/packages/openapi-ts/src/utils/write/core.ts @@ -1,4 +1,4 @@ -import { copyFileSync, existsSync, writeFileSync } from 'node:fs'; +import { copyFileSync, existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs'; import path from 'node:path'; import type { OpenApi } from '../../openApi'; @@ -28,6 +28,14 @@ export const writeCore = async ( version: client.version, }; + rmSync(path.resolve(outputPath), { + force: true, + recursive: true, + }); + mkdirSync(path.resolve(outputPath), { + recursive: true, + }); + if (config.exportCore) { await writeFileSync( path.resolve(outputPath, 'OpenAPI.ts'), diff --git a/packages/openapi-ts/src/utils/write/index.ts b/packages/openapi-ts/src/utils/write/index.ts index 8f3ff43bec..3ed4a132c3 100644 --- a/packages/openapi-ts/src/utils/write/index.ts +++ b/packages/openapi-ts/src/utils/write/index.ts @@ -13,7 +13,7 @@ import { getConfig } from '../config'; export const writeClientIndex = async (client: Client, outputPath: string): Promise => { const config = getConfig(); - const fileIndex = new TypeScriptFile(path.resolve(outputPath, 'index.ts')); + const fileIndex = new TypeScriptFile({ path: path.resolve(outputPath, 'index.ts') }); if (config.name) { fileIndex.add(compiler.export.named([config.name], `./${config.name}`)); diff --git a/packages/openapi-ts/src/utils/write/models.ts b/packages/openapi-ts/src/utils/write/models.ts index 71c9885421..ba8bb0773b 100644 --- a/packages/openapi-ts/src/utils/write/models.ts +++ b/packages/openapi-ts/src/utils/write/models.ts @@ -225,8 +225,8 @@ const processServiceTypes = (services: Service[], onNode: OnNode) => { export const writeTypesAndEnums = async (openApi: OpenApi, outputPath: string, client: Client): Promise => { const config = getConfig(); - const fileEnums = new TypeScriptFile(path.resolve(outputPath, 'enums.gen.ts')); - const fileModels = new TypeScriptFile(path.resolve(outputPath, 'models.ts')); + const fileEnums = new TypeScriptFile({ path: path.resolve(outputPath, 'enums.gen.ts') }); + const fileModels = new TypeScriptFile({ path: path.resolve(outputPath, 'models.ts') }); for (const model of client.models) { processModel(client, model, (node, type) => { diff --git a/packages/openapi-ts/src/utils/write/schemas.ts b/packages/openapi-ts/src/utils/write/schemas.ts index d4fcf63832..512c4e5742 100644 --- a/packages/openapi-ts/src/utils/write/schemas.ts +++ b/packages/openapi-ts/src/utils/write/schemas.ts @@ -13,7 +13,7 @@ import { getConfig } from '../config'; export const writeSchemas = async (openApi: OpenApi, outputPath: string): Promise => { const config = getConfig(); - const fileSchemas = new TypeScriptFile(path.resolve(outputPath, 'schemas.ts')); + const fileSchemas = new TypeScriptFile({ path: path.resolve(outputPath, 'schemas.ts') }); const addSchema = (name: string, obj: any) => { const validName = `$${ensureValidTypeScriptJavaScriptIdentifier(name)}`; diff --git a/packages/openapi-ts/src/utils/write/services.ts b/packages/openapi-ts/src/utils/write/services.ts index 7e97a8e9b2..20959fab48 100644 --- a/packages/openapi-ts/src/utils/write/services.ts +++ b/packages/openapi-ts/src/utils/write/services.ts @@ -22,7 +22,7 @@ export const writeServices = async ( ): Promise => { const config = getConfig(); - const fileServices = new TypeScriptFile(path.resolve(outputPath, 'services.ts')); + const fileServices = new TypeScriptFile({ path: path.resolve(outputPath, 'services.ts') }); let imports: string[] = []; let results: string[] = [];