Skip to content

Commit 558ef68

Browse files
committed
fix: 修正 axiosImportFile 导入路径
1 parent 694ac7f commit 558ef68

File tree

8 files changed

+41
-12
lines changed

8 files changed

+41
-12
lines changed

src/generators/Generator.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class Generator extends Emitter<GeneratorEmits> {
5757
const { cwd, dest, ...globalPrinter } = generatorOptions;
5858
const { document, ...scopePrinter } = openAPIOptions;
5959
const fileName = `${module}.ts`;
60-
const filePath = path.join(cwd, dest, fileName);
60+
const file = path.join(cwd, dest, fileName);
6161

6262
// 1. 参数合并
6363
const printerOptions = Object.assign({}, globalPrinter, scopePrinter);
@@ -73,7 +73,7 @@ export class Generator extends Emitter<GeneratorEmits> {
7373
module,
7474
stage: step,
7575
options,
76-
filePath,
76+
file,
7777
});
7878

7979
// 2. 读取
@@ -85,12 +85,12 @@ export class Generator extends Emitter<GeneratorEmits> {
8585
// 3. 输出
8686
this.emit('process', makePayload('printing'));
8787
const printer = new Printer(openAPIV3Document, printerOptions);
88-
const code = printer.print({ module });
88+
const code = printer.print({ module, file: file });
8989

9090
// 4. 写入
9191
this.emit('process', makePayload('writing'));
92-
fs.mkdirSync(path.dirname(filePath), { recursive: true });
93-
fs.writeFileSync(filePath, await formatTsCode(code), 'utf8');
92+
fs.mkdirSync(path.dirname(file), { recursive: true });
93+
fs.writeFileSync(file, await formatTsCode(code), 'utf8');
9494

9595
this.emit('process', makePayload('generated'));
9696
}

src/generators/Logger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class Logger {
1616
chalk.yellowBright(`${step}/${payload.count}`),
1717
payload.module,
1818
payload.stage,
19-
payload.stage === 'generated' ? path.relative(payload.options.cwd, payload.filePath) : '',
19+
payload.stage === 'generated' ? path.relative(payload.options.cwd, payload.file) : '',
2020
);
2121
}
2222

src/generators/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export interface GeneratingPayload {
4646
module: string;
4747
stage: GeneratingStage;
4848
options: GeneratingOptions;
49-
filePath: string;
49+
file: string;
5050
}
5151

5252
export type GeneratorEmits = {

src/printer/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from 'node:path';
12
import { pkgName } from '../const';
23
import type { OpenApi3 } from '../types/openapi';
34
import { never } from '../utils/func';
@@ -24,6 +25,7 @@ import { JsDoc } from './JsDoc';
2425
import { Named } from './Named';
2526
import { Schemata } from './Schemata';
2627
import type { PrinterOptions, PrinterConfigs, RequestStatusCodeMatch } from './types';
28+
import { toRelative } from '../utils/path';
2729

2830
const allowMethods = ['get', 'put', 'post', 'delete', 'options', 'head', 'patch', 'trace'];
2931

@@ -95,7 +97,7 @@ export class Printer {
9597
private _printImports() {
9698
const {
9799
axiosImportName = AXIOS_IMPORT_NAME,
98-
axiosNamedImport: axiosNamedImport,
100+
axiosNamedImport,
99101
axiosImportFile = AXIOS_IMPORT_FILE,
100102
axiosRequestConfigTypeName = AXIOS_QUEST_CONFIG_TYPE_NAME,
101103
axiosResponseTypeName = AXIOS_PROMISE_TYPE_NAME,
@@ -104,15 +106,17 @@ export class Printer {
104106
const firstServer = this.document.servers?.[0];
105107
const defaultBaseURL = firstServer?.url || '/';
106108
const BASE_URL = isString(baseURL) ? baseURL : baseURL?.(this.document) || defaultBaseURL;
109+
const { file } = this.configs;
110+
const importPath = toRelative(axiosImportFile, file);
107111

108112
return [
109113
//
110114
axiosNamedImport
111115
? // 具名导入
112-
`import {${axiosImportName}} from "${axiosImportFile}";`
116+
`import {${axiosImportName}} from "${importPath}";`
113117
: // 默认导入
114-
`import ${axiosImportName} from "${axiosImportFile}";`,
115-
`import type {${axiosRequestConfigTypeName}, ${axiosResponseTypeName}} from "${axiosImportFile}";`,
118+
`import ${axiosImportName} from "${importPath}";`,
119+
`import type {${axiosRequestConfigTypeName}, ${axiosResponseTypeName}} from "${importPath}";`,
116120
`import {resolveURL} from "${pkgName}/client";`,
117121
`import type {OneOf} from "${pkgName}/client";`,
118122
'',

src/printer/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ export type PrinterConfigs = {
114114
*/
115115
module?: string;
116116

117+
/**
118+
* file path
119+
*/
120+
file?: string;
121+
117122
hideInfo?: boolean;
118123
hideImports?: boolean;
119124
hideComponents?: boolean;

src/utils/path.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import path from 'node:path';
2+
3+
export function toRelative(to: string, from?: string) {
4+
if (!from) return to;
5+
if (!path.isAbsolute(to)) return to;
6+
7+
const relative = path.relative(from, to);
8+
return relative.startsWith('.') ? relative : `./${relative}`;
9+
}

test/generators/Logger.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ test('Logger', async () => {
1414
count: 99,
1515
module: 'test',
1616
stage: 'generated',
17-
filePath: '/a/b/c/d/e/f',
17+
file: '/a/b/c/d/e/f',
1818
options: {
1919
cwd: '/a/b/c',
2020
dest: '',

test/utils/path.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { toRelative } from '../../src/utils/path';
2+
3+
test('toRelative', () => {
4+
expect(toRelative('./a/b/c')).toBe('./a/b/c');
5+
expect(toRelative('/a/b/c')).toBe('/a/b/c');
6+
expect(toRelative('./a/b/c', '/a')).toBe('./a/b/c');
7+
expect(toRelative('/a/b/c', '/a')).toBe('./b/c');
8+
expect(toRelative('/a/b/c', '/a/b')).toBe('./c');
9+
expect(toRelative('/a/b/c', '/a/b/e')).toBe('../c');
10+
expect(toRelative('/a/b/c', '/a/d/e')).toBe('../../b/c');
11+
});

0 commit comments

Comments
 (0)