Skip to content

Commit

Permalink
fix: only delete generated files instead of whole output directory
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanshatford committed Apr 12, 2024
1 parent a4b0c22 commit b29a9e6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/fuzzy-vans-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": patch
---

fix: only delete generated files instead of whole output directory
6 changes: 3 additions & 3 deletions packages/openapi-ts/src/compiler/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { writeFileSync } from 'node:fs';
import { PathLike, writeFileSync } from 'node:fs';

import ts from 'typescript';

Expand All @@ -15,9 +15,9 @@ export class TypeScriptFile {
private _headers: Array<string> = [];
private _imports: Array<ts.Node> = [];
private _items: Array<ts.Node | string> = [];
private _path: string;
private _path: PathLike;

public constructor(path: string, header: boolean = true) {
public constructor(path: PathLike, header: boolean = true) {
this._path = path;
if (header) {
const text = 'This file is auto-generated by @hey-api/openapi-ts';
Expand Down
19 changes: 5 additions & 14 deletions packages/openapi-ts/src/utils/write/client.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -20,10 +20,6 @@ import { writeServices } from './services';
*/
export const writeClient = async (openApi: OpenApi, client: Client, templates: Templates): Promise<void> => {
const config = getConfig();
await rmSync(config.output, {
force: true,
recursive: true,
});

if (typeof config.exportServices === 'string') {
const regexp = new RegExp(config.exportServices);
Expand All @@ -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',
Expand All @@ -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);
}

Expand Down
10 changes: 9 additions & 1 deletion packages/openapi-ts/src/utils/write/core.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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'),
Expand Down

0 comments on commit b29a9e6

Please sign in to comment.