Skip to content

Commit 345e4b2

Browse files
committed
fix: 修复指定导入文件后未正确输出
1 parent 168cf33 commit 345e4b2

File tree

9 files changed

+53
-12
lines changed

9 files changed

+53
-12
lines changed

src/generator/Generator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export class Generator extends Emitter<GeneratorEmits> {
8686
// 3. 输出
8787
this.emit('process', makePayload('printing'));
8888
const printer = new Printer(openAPIV3Document, printerOptions);
89-
const code = printer.print({ module, file });
89+
const code = printer.print({ module, cwd, file });
9090

9191
// 4. 写入
9292
this.emit('process', makePayload('writing'));

src/printer/index.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { PrinterConfigs, PrinterOptions } from './types';
22
import { type OpenAPILatest, OpenAPIVersion } from '../types/openapi';
3-
import { toRelative } from '../utils/path';
3+
import { toImportPath } from '../utils/path';
44
import { isString, isUndefined } from '../utils/type-is';
55
import { Arg } from './Arg';
66
import { Args } from './Args';
@@ -188,14 +188,16 @@ export class Printer {
188188
private _printImports() {
189189
const {
190190
axiosImportName = '',
191-
axiosImportFile = AXIOS_IMPORT_FILE,
192-
axiosTypeImportFile = AXIOS_TYPE_IMPORT_FILE,
191+
axiosImportFile,
192+
axiosTypeImportFile,
193193
axiosRequestConfigTypeName = AXIOS_QUEST_CONFIG_TYPE_NAME,
194194
axiosResponseTypeName = AXIOS_PROMISE_TYPE_NAME,
195195
} = this.options || {};
196-
const { file } = this.configs;
197-
const importPath = toRelative(axiosImportFile, file);
198-
const importTypePath = toRelative(axiosTypeImportFile, file);
196+
const { cwd = '/', file } = this.configs;
197+
const axiosImportFile2 = axiosImportFile || AXIOS_IMPORT_FILE;
198+
const importPath = toImportPath(axiosImportFile2, cwd, file);
199+
const axiosTypeImportFile2 = axiosTypeImportFile || axiosImportFile || AXIOS_TYPE_IMPORT_FILE;
200+
const importTypePath = toImportPath(axiosTypeImportFile2, cwd, file);
199201

200202
return [
201203
toImportString(AXIOS_IMPORT_NAME, axiosImportName, importPath),

src/printer/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ export interface PrinterConfigs {
118118
*/
119119
module?: string;
120120

121+
/**
122+
* file cwd
123+
*/
124+
cwd?: string;
125+
121126
/**
122127
* file path
123128
*/

src/utils/path.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import path from 'node:path';
22

3+
export function isRelative(toFile: string) {
4+
return /^\.{1,2}[/\\]/.test(toFile);
5+
}
6+
37
export function toRelative(toFile: string, fromFile?: string) {
48
if (!fromFile)
59
return toFile;
@@ -10,3 +14,18 @@ export function toRelative(toFile: string, fromFile?: string) {
1014
const relative = path.relative(path.dirname(fromFile), toFile);
1115
return relative.startsWith('.') ? relative : `./${relative}`;
1216
}
17+
18+
export function toImportPath(toFile: string, cwd: string, fromFile?: string) {
19+
// /a/b/c, /cwd
20+
if (path.isAbsolute(toFile)) {
21+
return fromFile ? toRelative(toFile, fromFile) : toFile;
22+
}
23+
24+
// ./a/b/c, /cwd
25+
if (isRelative(toFile)) {
26+
return toImportPath(path.join(cwd, toFile), cwd, fromFile);
27+
}
28+
29+
// a/b/c, /cwd
30+
return toFile;
31+
}

test/example-dest/2.0/pet-store.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* @description This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters.
66
*/
77

8-
import {axios as axios} from "axios";
8+
import axios from "axios";
99
import {type AxiosRequestConfig as AxiosRequestConfig} from "axios";
1010
import {type AxiosPromise as AxiosPromise} from "axios";
1111

test/example-dest/3.0/pet-store.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* - [The source API definition for the Pet Store](https://github.com/swagger-api/swagger-petstore/blob/master/src/main/resources/openapi.yaml)
1313
*/
1414

15-
import {axios as axios} from "axios";
15+
import axios from "axios";
1616
import {type AxiosRequestConfig as AxiosRequestConfig} from "axios";
1717
import {type AxiosPromise as AxiosPromise} from "axios";
1818

test/example-dest/3.1/pet-store.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @see {@link http://swagger.io Find out more about Swagger}
1010
*/
1111

12-
import {axios as axios} from "axios";
12+
import axios from "axios";
1313
import {type AxiosRequestConfig as AxiosRequestConfig} from "axios";
1414
import {type AxiosPromise as AxiosPromise} from "axios";
1515

test/printer/options.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ it('axios 模块导入名称默认', () => {
1818
hideInfo: true,
1919
hideHelpers: true,
2020
})).toMatchInlineSnapshot(`
21-
"import {axios as axios} from "axios";
21+
"import axios from "axios";
2222
import {type AxiosRequestConfig as AxiosRequestConfig} from "axios";
2323
import {type AxiosPromise as AxiosPromise} from "axios";
2424

test/utils/path.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { toRelative } from '../../src/utils/path';
1+
import { isRelative, toImportPath, toRelative } from '../../src/utils/path';
2+
3+
it('isRelative', () => {
4+
expect(isRelative('./a/b/c')).toBe(true);
5+
expect(isRelative('/a/b/c')).toBe(false);
6+
expect(isRelative('a/b/c')).toBe(false);
7+
});
28

39
it('toRelative', () => {
410
expect(toRelative('./a/b/c')).toBe('./a/b/c');
@@ -9,3 +15,12 @@ it('toRelative', () => {
915
expect(toRelative('/a/b/c', '/a/b/e')).toBe('./c');
1016
expect(toRelative('/a/b/c', '/a/d/e')).toBe('../b/c');
1117
});
18+
19+
it('toImportPath', () => {
20+
expect(toImportPath('./a/b/c', '/cwd')).toBe('/cwd/a/b/c');
21+
expect(toImportPath('a/b/c', '/cwd')).toBe('a/b/c');
22+
expect(toImportPath('/a/b/c', '/cwd')).toBe('/a/b/c');
23+
expect(toImportPath('./a/b/c', '/cwd', '/cwd/d/e/f')).toBe('../../a/b/c');
24+
expect(toImportPath('a/b/c', '/cwd', '/cwd/d/e/f')).toBe('a/b/c');
25+
expect(toImportPath('/a/b/c', '/cwd', '/cwd/d/e/f')).toBe('../../../a/b/c');
26+
});

0 commit comments

Comments
 (0)