这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, header = true }: { path: PathLike; header?: boolean }) {
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
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/utils/write/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { getConfig } from '../config';
export const writeClientIndex = async (client: Client, outputPath: string): Promise<void> => {
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}`));
Expand Down
4 changes: 2 additions & 2 deletions packages/openapi-ts/src/utils/write/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ const processServiceTypes = (services: Service[], onNode: OnNode) => {
export const writeTypesAndEnums = async (openApi: OpenApi, outputPath: string, client: Client): Promise<void> => {
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) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/utils/write/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { getConfig } from '../config';
export const writeSchemas = async (openApi: OpenApi, outputPath: string): Promise<void> => {
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)}`;
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/utils/write/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const writeServices = async (
): Promise<void> => {
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[] = [];
Expand Down