@karry.sun/yapi-gen 0.2.7 → 0.2.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/config/index.js +74 -0
- package/dist/cjs/config/merge-config.test.js +46 -0
- package/dist/cjs/config/schema.js +18 -0
- package/dist/cjs/index.cjs.js +11 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/resolve-config.cjs.js +124 -0
- package/dist/config/index.d.ts +2 -2
- package/dist/config/index.d.ts.map +1 -1
- package/dist/config/index.js +10 -4
- package/dist/config/merge-config.test.d.ts +2 -0
- package/dist/config/merge-config.test.d.ts.map +1 -0
- package/dist/config/merge-config.test.js +44 -0
- package/dist/index.cjs.d.ts +8 -0
- package/dist/index.cjs.d.ts.map +1 -0
- package/dist/index.cjs.js +5 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/resolve-config.cjs.d.ts +3 -0
- package/dist/resolve-config.cjs.d.ts.map +1 -0
- package/dist/resolve-config.cjs.js +121 -0
- package/dist/resolve-config.d.ts +1 -1
- package/dist/resolve-config.d.ts.map +1 -1
- package/dist/resolve-config.js +28 -11
- package/dist/run.d.ts.map +1 -1
- package/dist/run.js +61 -32
- package/package.json +6 -3
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.mergeConfig = mergeConfig;
|
|
4
|
+
exports.defineYapiGenConfig = defineYapiGenConfig;
|
|
5
|
+
const path_1 = require("path");
|
|
6
|
+
const schema_js_1 = require("./schema.js");
|
|
7
|
+
/** 默认 preproccessInterface:按 YTT_INTERFACE_IDS / YTT_CATEGORY_IDS 过滤,支持按接口/分类生成 */
|
|
8
|
+
function getDefaultPreproccessInterface(interfaceInfo) {
|
|
9
|
+
const ids = process.env.YTT_INTERFACE_IDS;
|
|
10
|
+
const catIds = process.env.YTT_CATEGORY_IDS;
|
|
11
|
+
if (ids) {
|
|
12
|
+
const allowList = ids.split(',').map((s) => parseInt(s.trim(), 10));
|
|
13
|
+
if (!allowList.includes(interfaceInfo._id))
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
else if (catIds) {
|
|
17
|
+
const allowList = catIds.split(',').map((s) => s.trim());
|
|
18
|
+
if (!allowList.includes(String(interfaceInfo.catid)))
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
return interfaceInfo;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* 默认 outputFilePath:按 outputDir + 分类映射生成,支持 YTT_INTERFACE_IDS / YTT_CAT_MAPPING
|
|
25
|
+
* 当 YAPI_GEN_PROJECT_ROOT 存在时使用绝对路径(临时目录模式),否则使用 '../' 相对路径
|
|
26
|
+
*/
|
|
27
|
+
function getDefaultOutputFilePath(outputDir) {
|
|
28
|
+
const projectRoot = process.env.YAPI_GEN_PROJECT_ROOT;
|
|
29
|
+
const base = projectRoot ? (0, path_1.join)(projectRoot, outputDir) : `../${outputDir}`;
|
|
30
|
+
return (interfaceInfo) => {
|
|
31
|
+
const ids = process.env.YTT_INTERFACE_IDS;
|
|
32
|
+
if (ids)
|
|
33
|
+
return projectRoot ? (0, path_1.join)(base, '.temp', `api_${interfaceInfo._id}.ts`) : `${base}/.temp/api_${interfaceInfo._id}.ts`;
|
|
34
|
+
const catId = interfaceInfo.catid ?? 0;
|
|
35
|
+
let dirName = `cat_${catId}`;
|
|
36
|
+
try {
|
|
37
|
+
const mapping = process.env.YTT_CAT_MAPPING
|
|
38
|
+
? JSON.parse(process.env.YTT_CAT_MAPPING)
|
|
39
|
+
: {};
|
|
40
|
+
if (mapping[String(catId)])
|
|
41
|
+
dirName = mapping[String(catId)];
|
|
42
|
+
}
|
|
43
|
+
catch (_) { }
|
|
44
|
+
return projectRoot ? (0, path_1.join)(base, dirName, 'index.ts') : `${base}/${dirName}/index.ts`;
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* 合并用户配置与默认配置,返回可直接用于 defineConfig([...]) 的配置对象
|
|
49
|
+
*/
|
|
50
|
+
function mergeConfig(user) {
|
|
51
|
+
const defaults = (0, schema_js_1.getDefaultConfig)();
|
|
52
|
+
const merged = {
|
|
53
|
+
...defaults,
|
|
54
|
+
...user,
|
|
55
|
+
reactHooks: user.reactHooks ?? defaults.reactHooks,
|
|
56
|
+
};
|
|
57
|
+
if (merged.outputFilePath == null) {
|
|
58
|
+
const outputDir = merged.outputDir ?? 'src/yapi';
|
|
59
|
+
merged.outputFilePath = getDefaultOutputFilePath(outputDir);
|
|
60
|
+
}
|
|
61
|
+
if (merged.preproccessInterface == null) {
|
|
62
|
+
merged.preproccessInterface = getDefaultPreproccessInterface;
|
|
63
|
+
}
|
|
64
|
+
return merged;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* 定义 yapi-gen 配置的辅助函数,带类型提示与默认值
|
|
68
|
+
* 在项目根目录的 yapi-gen.config.ts 中使用:
|
|
69
|
+
* import { defineYapiGenConfig } from 'yapi-gen';
|
|
70
|
+
* export default defineYapiGenConfig({ serverUrl: '...', ... });
|
|
71
|
+
*/
|
|
72
|
+
function defineYapiGenConfig(config) {
|
|
73
|
+
return mergeConfig(config);
|
|
74
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const vitest_1 = require("vitest");
|
|
4
|
+
const index_js_1 = require("./index.js");
|
|
5
|
+
(0, vitest_1.describe)('mergeConfig', () => {
|
|
6
|
+
(0, vitest_1.it)('应填充默认 outputFilePath', () => {
|
|
7
|
+
const config = (0, index_js_1.mergeConfig)({
|
|
8
|
+
serverUrl: 'https://yapi.example.com',
|
|
9
|
+
projects: [{ token: 'x', categories: [{ id: 0 }] }],
|
|
10
|
+
});
|
|
11
|
+
(0, vitest_1.expect)(config.outputFilePath).toBeDefined();
|
|
12
|
+
(0, vitest_1.expect)(typeof config.outputFilePath).toBe('function');
|
|
13
|
+
const path = config.outputFilePath({ _id: 1, catid: 401 }, {});
|
|
14
|
+
(0, vitest_1.expect)(path).toContain('src/yapi');
|
|
15
|
+
(0, vitest_1.expect)(path).toContain('cat_401');
|
|
16
|
+
});
|
|
17
|
+
(0, vitest_1.it)('应填充默认 preproccessInterface', () => {
|
|
18
|
+
const config = (0, index_js_1.mergeConfig)({
|
|
19
|
+
serverUrl: 'https://yapi.example.com',
|
|
20
|
+
projects: [{ token: 'x', categories: [{ id: 0 }] }],
|
|
21
|
+
});
|
|
22
|
+
(0, vitest_1.expect)(config.preproccessInterface).toBeDefined();
|
|
23
|
+
const result = config.preproccessInterface({ _id: 1, catid: 401 }, {});
|
|
24
|
+
(0, vitest_1.expect)(result).toEqual({ _id: 1, catid: 401 });
|
|
25
|
+
});
|
|
26
|
+
(0, vitest_1.it)('用户 outputFilePath 应覆盖默认', () => {
|
|
27
|
+
const customPath = 'custom/api.ts';
|
|
28
|
+
const config = (0, index_js_1.mergeConfig)({
|
|
29
|
+
serverUrl: 'https://yapi.example.com',
|
|
30
|
+
outputFilePath: customPath,
|
|
31
|
+
projects: [{ token: 'x', categories: [{ id: 0 }] }],
|
|
32
|
+
});
|
|
33
|
+
(0, vitest_1.expect)(config.outputFilePath).toBe(customPath);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
(0, vitest_1.describe)('defineYapiGenConfig', () => {
|
|
37
|
+
(0, vitest_1.it)('应返回合并后的配置', () => {
|
|
38
|
+
const config = (0, index_js_1.defineYapiGenConfig)({
|
|
39
|
+
serverUrl: 'https://yapi.example.com',
|
|
40
|
+
projects: [{ token: 'x', categories: [{ id: 0 }] }],
|
|
41
|
+
});
|
|
42
|
+
(0, vitest_1.expect)(config.serverUrl).toBe('https://yapi.example.com');
|
|
43
|
+
(0, vitest_1.expect)(config.serverType).toBe('yapi');
|
|
44
|
+
(0, vitest_1.expect)(config.outputFilePath).toBeDefined();
|
|
45
|
+
});
|
|
46
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getDefaultConfig = getDefaultConfig;
|
|
4
|
+
const DEFAULT_REACT_HOOKS = {
|
|
5
|
+
enabled: false,
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* 默认配置(与 ytt 常用默认一致)
|
|
9
|
+
*/
|
|
10
|
+
function getDefaultConfig() {
|
|
11
|
+
return {
|
|
12
|
+
serverType: 'yapi',
|
|
13
|
+
typesOnly: false,
|
|
14
|
+
target: 'typescript',
|
|
15
|
+
reactHooks: DEFAULT_REACT_HOOKS,
|
|
16
|
+
prodEnvName: 'production',
|
|
17
|
+
};
|
|
18
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resolveConfig = exports.mergeConfig = exports.defineYapiGenConfig = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* CJS 入口:供 require() 加载
|
|
6
|
+
*/
|
|
7
|
+
var index_js_1 = require("./config/index.js");
|
|
8
|
+
Object.defineProperty(exports, "defineYapiGenConfig", { enumerable: true, get: function () { return index_js_1.defineYapiGenConfig; } });
|
|
9
|
+
Object.defineProperty(exports, "mergeConfig", { enumerable: true, get: function () { return index_js_1.mergeConfig; } });
|
|
10
|
+
var resolve_config_cjs_js_1 = require("./resolve-config.cjs.js");
|
|
11
|
+
Object.defineProperty(exports, "resolveConfig", { enumerable: true, get: function () { return resolve_config_cjs_js_1.resolveConfig; } });
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resolveConfig = resolveConfig;
|
|
4
|
+
/**
|
|
5
|
+
* CJS 版本:供 require() 加载,无 import.meta
|
|
6
|
+
* YAPI_GEN_PROJECT_ROOT 存在时用绝对路径,否则用 '../' 相对路径
|
|
7
|
+
*/
|
|
8
|
+
function forYttCwd(p) {
|
|
9
|
+
if (p.startsWith('/') || p.startsWith('@'))
|
|
10
|
+
return p;
|
|
11
|
+
const root = process.env.YAPI_GEN_PROJECT_ROOT;
|
|
12
|
+
return root ? (0, path_1.join)(root, p) : `../${p}`;
|
|
13
|
+
}
|
|
14
|
+
const fs_1 = require("fs");
|
|
15
|
+
const path_1 = require("path");
|
|
16
|
+
function getPackageName(cwd) {
|
|
17
|
+
try {
|
|
18
|
+
const { createRequire } = require('module');
|
|
19
|
+
const req = createRequire((0, path_1.join)(cwd, 'package.json'));
|
|
20
|
+
for (const id of ['@karry.sun/yapi-gen/package.json', 'yapi-gen/package.json']) {
|
|
21
|
+
try {
|
|
22
|
+
const pkgPath = req.resolve(id);
|
|
23
|
+
const pkg = JSON.parse((0, fs_1.readFileSync)(pkgPath, 'utf-8'));
|
|
24
|
+
return (pkg && pkg.name) || 'yapi-gen';
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
/* fallthrough */
|
|
33
|
+
}
|
|
34
|
+
return 'yapi-gen';
|
|
35
|
+
}
|
|
36
|
+
const DEFAULT_REQUEST_FILE_PATH = 'src/yapi/request.ts';
|
|
37
|
+
const DEFAULT_AXIOS_INSTANCE_PATH = "/@/utils/http/axios";
|
|
38
|
+
const DEFAULT_NUXT_REQUEST_IMPORT_PATH = '#imports';
|
|
39
|
+
function normalizeRequestAdapter(adapter) {
|
|
40
|
+
if (adapter == null)
|
|
41
|
+
return { type: 'custom' };
|
|
42
|
+
if (adapter === 'axios') {
|
|
43
|
+
return { type: 'axios', axiosInstancePath: DEFAULT_AXIOS_INSTANCE_PATH, axiosExportName: 'defHttp' };
|
|
44
|
+
}
|
|
45
|
+
if (adapter === 'nuxt-bbm')
|
|
46
|
+
return { type: 'nuxt-bbm', requestImportPath: DEFAULT_NUXT_REQUEST_IMPORT_PATH };
|
|
47
|
+
if (adapter === 'custom')
|
|
48
|
+
return { type: 'custom' };
|
|
49
|
+
if (typeof adapter === 'object') {
|
|
50
|
+
if (adapter.type === 'axios') {
|
|
51
|
+
return {
|
|
52
|
+
type: 'axios',
|
|
53
|
+
axiosInstancePath: adapter.axiosInstancePath ?? DEFAULT_AXIOS_INSTANCE_PATH,
|
|
54
|
+
axiosExportName: adapter.axiosExportName ?? 'defHttp',
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
if (adapter.type === 'nuxt-bbm') {
|
|
58
|
+
return { type: 'nuxt-bbm', requestImportPath: adapter.requestImportPath ?? DEFAULT_NUXT_REQUEST_IMPORT_PATH };
|
|
59
|
+
}
|
|
60
|
+
if (adapter.type === 'custom') {
|
|
61
|
+
return { type: 'custom', requestFunctionFilePath: adapter.requestFunctionFilePath };
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return { type: 'custom' };
|
|
65
|
+
}
|
|
66
|
+
function getAxiosAdapterFileContent(packageName, axiosInstancePath, exportName) {
|
|
67
|
+
const importLine = exportName === 'default'
|
|
68
|
+
? `import defHttp from '${axiosInstancePath}';`
|
|
69
|
+
: `import { defHttp } from '${axiosInstancePath}';`;
|
|
70
|
+
return `/**
|
|
71
|
+
* 由 yapi-gen 预设 "axios" 自动生成,请勿直接修改。
|
|
72
|
+
*/
|
|
73
|
+
import { createAxiosAdapter } from '${packageName}/adapters/axios';
|
|
74
|
+
${importLine}
|
|
75
|
+
|
|
76
|
+
export default createAxiosAdapter(defHttp);
|
|
77
|
+
`;
|
|
78
|
+
}
|
|
79
|
+
function getNuxtBbmAdapterFileContent(packageName, requestImportPath) {
|
|
80
|
+
return `/**
|
|
81
|
+
* 由 yapi-gen 预设 "nuxt-bbm" 自动生成,请勿直接修改。
|
|
82
|
+
*/
|
|
83
|
+
import { createNuxtAdapter } from '${packageName}/adapters/nuxt-bbm';
|
|
84
|
+
import { request } from '${requestImportPath}';
|
|
85
|
+
|
|
86
|
+
export default createNuxtAdapter(request);
|
|
87
|
+
`;
|
|
88
|
+
}
|
|
89
|
+
function resolveConfig(config, cwd) {
|
|
90
|
+
const resolved = { ...config };
|
|
91
|
+
const adapter = normalizeRequestAdapter(resolved.requestAdapter);
|
|
92
|
+
const requestFilePath = resolved.requestFunctionFilePath ?? adapter.requestFunctionFilePath ?? DEFAULT_REQUEST_FILE_PATH;
|
|
93
|
+
const packageName = getPackageName(cwd);
|
|
94
|
+
if (adapter.type === 'axios') {
|
|
95
|
+
const content = getAxiosAdapterFileContent(packageName, adapter.axiosInstancePath, adapter.axiosExportName ?? 'defHttp');
|
|
96
|
+
const absolutePath = (0, path_1.resolve)(cwd, requestFilePath);
|
|
97
|
+
const dir = (0, path_1.dirname)(absolutePath);
|
|
98
|
+
if (!(0, fs_1.existsSync)(dir))
|
|
99
|
+
(0, fs_1.mkdirSync)(dir, { recursive: true });
|
|
100
|
+
(0, fs_1.writeFileSync)(absolutePath, content, 'utf-8');
|
|
101
|
+
resolved.requestFunctionFilePath = forYttCwd(requestFilePath);
|
|
102
|
+
delete resolved.requestAdapter;
|
|
103
|
+
return resolved;
|
|
104
|
+
}
|
|
105
|
+
if (adapter.type === 'nuxt-bbm') {
|
|
106
|
+
const content = getNuxtBbmAdapterFileContent(packageName, adapter.requestImportPath);
|
|
107
|
+
const absolutePath = (0, path_1.resolve)(cwd, requestFilePath);
|
|
108
|
+
const dir = (0, path_1.dirname)(absolutePath);
|
|
109
|
+
if (!(0, fs_1.existsSync)(dir))
|
|
110
|
+
(0, fs_1.mkdirSync)(dir, { recursive: true });
|
|
111
|
+
(0, fs_1.writeFileSync)(absolutePath, content, 'utf-8');
|
|
112
|
+
resolved.requestFunctionFilePath = forYttCwd(requestFilePath);
|
|
113
|
+
delete resolved.requestAdapter;
|
|
114
|
+
return resolved;
|
|
115
|
+
}
|
|
116
|
+
if (!resolved.requestFunctionFilePath && !adapter.requestFunctionFilePath) {
|
|
117
|
+
throw new Error('[yapi-gen] requestAdapter 为 custom 或未指定时,必须配置 requestFunctionFilePath');
|
|
118
|
+
}
|
|
119
|
+
if (adapter.requestFunctionFilePath) {
|
|
120
|
+
resolved.requestFunctionFilePath = forYttCwd(adapter.requestFunctionFilePath);
|
|
121
|
+
}
|
|
122
|
+
delete resolved.requestAdapter;
|
|
123
|
+
return resolved;
|
|
124
|
+
}
|
package/dist/config/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { YapiGenUserConfig } from './schema';
|
|
1
|
+
import type { YapiGenUserConfig } from './schema.js';
|
|
2
2
|
/**
|
|
3
3
|
* 合并用户配置与默认配置,返回可直接用于 defineConfig([...]) 的配置对象
|
|
4
4
|
*/
|
|
@@ -10,5 +10,5 @@ export declare function mergeConfig(user: YapiGenUserConfig): YapiGenUserConfig;
|
|
|
10
10
|
* export default defineYapiGenConfig({ serverUrl: '...', ... });
|
|
11
11
|
*/
|
|
12
12
|
export declare function defineYapiGenConfig(config: YapiGenUserConfig): YapiGenUserConfig;
|
|
13
|
-
export type { YapiGenUserConfig, YapiGenProjectConfig, YapiGenCategoryConfig, YapiGenReactHooksConfig, YapiGenRequestAdapter, } from './schema';
|
|
13
|
+
export type { YapiGenUserConfig, YapiGenProjectConfig, YapiGenCategoryConfig, YapiGenReactHooksConfig, YapiGenRequestAdapter, } from './schema.js';
|
|
14
14
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/config/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/config/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAuCrD;;GAEG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,iBAAiB,GAAG,iBAAiB,CAetE;AAED;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,iBAAiB,GAAG,iBAAiB,CAEhF;AAED,YAAY,EACV,iBAAiB,EACjB,oBAAoB,EACpB,qBAAqB,EACrB,uBAAuB,EACvB,qBAAqB,GACtB,MAAM,aAAa,CAAC"}
|
package/dist/config/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { join } from 'path';
|
|
2
|
+
import { getDefaultConfig } from './schema.js';
|
|
2
3
|
/** 默认 preproccessInterface:按 YTT_INTERFACE_IDS / YTT_CATEGORY_IDS 过滤,支持按接口/分类生成 */
|
|
3
4
|
function getDefaultPreproccessInterface(interfaceInfo) {
|
|
4
5
|
const ids = process.env.YTT_INTERFACE_IDS;
|
|
@@ -15,12 +16,17 @@ function getDefaultPreproccessInterface(interfaceInfo) {
|
|
|
15
16
|
}
|
|
16
17
|
return interfaceInfo;
|
|
17
18
|
}
|
|
18
|
-
/**
|
|
19
|
+
/**
|
|
20
|
+
* 默认 outputFilePath:按 outputDir + 分类映射生成,支持 YTT_INTERFACE_IDS / YTT_CAT_MAPPING
|
|
21
|
+
* 当 YAPI_GEN_PROJECT_ROOT 存在时使用绝对路径(临时目录模式),否则使用 '../' 相对路径
|
|
22
|
+
*/
|
|
19
23
|
function getDefaultOutputFilePath(outputDir) {
|
|
24
|
+
const projectRoot = process.env.YAPI_GEN_PROJECT_ROOT;
|
|
25
|
+
const base = projectRoot ? join(projectRoot, outputDir) : `../${outputDir}`;
|
|
20
26
|
return (interfaceInfo) => {
|
|
21
27
|
const ids = process.env.YTT_INTERFACE_IDS;
|
|
22
28
|
if (ids)
|
|
23
|
-
return `${
|
|
29
|
+
return projectRoot ? join(base, '.temp', `api_${interfaceInfo._id}.ts`) : `${base}/.temp/api_${interfaceInfo._id}.ts`;
|
|
24
30
|
const catId = interfaceInfo.catid ?? 0;
|
|
25
31
|
let dirName = `cat_${catId}`;
|
|
26
32
|
try {
|
|
@@ -31,7 +37,7 @@ function getDefaultOutputFilePath(outputDir) {
|
|
|
31
37
|
dirName = mapping[String(catId)];
|
|
32
38
|
}
|
|
33
39
|
catch (_) { }
|
|
34
|
-
return `${
|
|
40
|
+
return projectRoot ? join(base, dirName, 'index.ts') : `${base}/${dirName}/index.ts`;
|
|
35
41
|
};
|
|
36
42
|
}
|
|
37
43
|
/**
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"merge-config.test.d.ts","sourceRoot":"","sources":["../../src/config/merge-config.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { mergeConfig, defineYapiGenConfig } from './index.js';
|
|
3
|
+
describe('mergeConfig', () => {
|
|
4
|
+
it('应填充默认 outputFilePath', () => {
|
|
5
|
+
const config = mergeConfig({
|
|
6
|
+
serverUrl: 'https://yapi.example.com',
|
|
7
|
+
projects: [{ token: 'x', categories: [{ id: 0 }] }],
|
|
8
|
+
});
|
|
9
|
+
expect(config.outputFilePath).toBeDefined();
|
|
10
|
+
expect(typeof config.outputFilePath).toBe('function');
|
|
11
|
+
const path = config.outputFilePath({ _id: 1, catid: 401 }, {});
|
|
12
|
+
expect(path).toContain('src/yapi');
|
|
13
|
+
expect(path).toContain('cat_401');
|
|
14
|
+
});
|
|
15
|
+
it('应填充默认 preproccessInterface', () => {
|
|
16
|
+
const config = mergeConfig({
|
|
17
|
+
serverUrl: 'https://yapi.example.com',
|
|
18
|
+
projects: [{ token: 'x', categories: [{ id: 0 }] }],
|
|
19
|
+
});
|
|
20
|
+
expect(config.preproccessInterface).toBeDefined();
|
|
21
|
+
const result = config.preproccessInterface({ _id: 1, catid: 401 }, {});
|
|
22
|
+
expect(result).toEqual({ _id: 1, catid: 401 });
|
|
23
|
+
});
|
|
24
|
+
it('用户 outputFilePath 应覆盖默认', () => {
|
|
25
|
+
const customPath = 'custom/api.ts';
|
|
26
|
+
const config = mergeConfig({
|
|
27
|
+
serverUrl: 'https://yapi.example.com',
|
|
28
|
+
outputFilePath: customPath,
|
|
29
|
+
projects: [{ token: 'x', categories: [{ id: 0 }] }],
|
|
30
|
+
});
|
|
31
|
+
expect(config.outputFilePath).toBe(customPath);
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
describe('defineYapiGenConfig', () => {
|
|
35
|
+
it('应返回合并后的配置', () => {
|
|
36
|
+
const config = defineYapiGenConfig({
|
|
37
|
+
serverUrl: 'https://yapi.example.com',
|
|
38
|
+
projects: [{ token: 'x', categories: [{ id: 0 }] }],
|
|
39
|
+
});
|
|
40
|
+
expect(config.serverUrl).toBe('https://yapi.example.com');
|
|
41
|
+
expect(config.serverType).toBe('yapi');
|
|
42
|
+
expect(config.outputFilePath).toBeDefined();
|
|
43
|
+
});
|
|
44
|
+
});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CJS 入口:供 require() 加载
|
|
3
|
+
*/
|
|
4
|
+
export { defineYapiGenConfig, mergeConfig } from './config/index.js';
|
|
5
|
+
export { resolveConfig } from './resolve-config.cjs.js';
|
|
6
|
+
export type { YapiGenUserConfig, YapiGenProjectConfig, YapiGenCategoryConfig, YapiGenReactHooksConfig, YapiGenRequestAdapter, } from './config/schema.js';
|
|
7
|
+
export type { RequestFunctionParams } from 'yapi-to-typescript';
|
|
8
|
+
//# sourceMappingURL=index.cjs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs.d.ts","sourceRoot":"","sources":["../src/index.cjs.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,mBAAmB,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AACrE,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACxD,YAAY,EACV,iBAAiB,EACjB,oBAAoB,EACpB,qBAAqB,EACrB,uBAAuB,EACvB,qBAAqB,GACtB,MAAM,oBAAoB,CAAC;AAC5B,YAAY,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
* yapi-gen
|
|
3
3
|
* 基于 yapi-to-typescript 的 YApi 前端代码生成 CLI,支持可配置的请求适配器与多项目复用
|
|
4
4
|
*/
|
|
5
|
-
export { defineYapiGenConfig, mergeConfig } from './config';
|
|
6
|
-
export { resolveConfig } from './resolve-config';
|
|
7
|
-
export type { YapiGenUserConfig, YapiGenProjectConfig, YapiGenCategoryConfig, YapiGenReactHooksConfig, YapiGenRequestAdapter, } from './config/schema';
|
|
5
|
+
export { defineYapiGenConfig, mergeConfig } from './config/index.js';
|
|
6
|
+
export { resolveConfig } from './resolve-config.js';
|
|
7
|
+
export type { YapiGenUserConfig, YapiGenProjectConfig, YapiGenCategoryConfig, YapiGenReactHooksConfig, YapiGenRequestAdapter, } from './config/schema.js';
|
|
8
8
|
export type { RequestFunctionParams } from 'yapi-to-typescript';
|
|
9
9
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,mBAAmB,EAAE,WAAW,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,mBAAmB,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AACrE,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,YAAY,EACV,iBAAiB,EACjB,oBAAoB,EACpB,qBAAqB,EACrB,uBAAuB,EACvB,qBAAqB,GACtB,MAAM,oBAAoB,CAAC;AAG5B,YAAY,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -2,5 +2,5 @@
|
|
|
2
2
|
* yapi-gen
|
|
3
3
|
* 基于 yapi-to-typescript 的 YApi 前端代码生成 CLI,支持可配置的请求适配器与多项目复用
|
|
4
4
|
*/
|
|
5
|
-
export { defineYapiGenConfig, mergeConfig } from './config';
|
|
6
|
-
export { resolveConfig } from './resolve-config';
|
|
5
|
+
export { defineYapiGenConfig, mergeConfig } from './config/index.js';
|
|
6
|
+
export { resolveConfig } from './resolve-config.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolve-config.cjs.d.ts","sourceRoot":"","sources":["../src/resolve-config.cjs.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,iBAAiB,EAAyB,MAAM,oBAAoB,CAAC;AAiFnF,wBAAgB,aAAa,CAAC,MAAM,EAAE,iBAAiB,EAAE,GAAG,EAAE,MAAM,GAAG,iBAAiB,CAoCvF"}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CJS 版本:供 require() 加载,无 import.meta
|
|
3
|
+
* YAPI_GEN_PROJECT_ROOT 存在时用绝对路径,否则用 '../' 相对路径
|
|
4
|
+
*/
|
|
5
|
+
function forYttCwd(p) {
|
|
6
|
+
if (p.startsWith('/') || p.startsWith('@'))
|
|
7
|
+
return p;
|
|
8
|
+
const root = process.env.YAPI_GEN_PROJECT_ROOT;
|
|
9
|
+
return root ? join(root, p) : `../${p}`;
|
|
10
|
+
}
|
|
11
|
+
import { existsSync, mkdirSync, writeFileSync, readFileSync } from 'fs';
|
|
12
|
+
import { resolve, dirname, join } from 'path';
|
|
13
|
+
function getPackageName(cwd) {
|
|
14
|
+
try {
|
|
15
|
+
const { createRequire } = require('module');
|
|
16
|
+
const req = createRequire(join(cwd, 'package.json'));
|
|
17
|
+
for (const id of ['@karry.sun/yapi-gen/package.json', 'yapi-gen/package.json']) {
|
|
18
|
+
try {
|
|
19
|
+
const pkgPath = req.resolve(id);
|
|
20
|
+
const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
|
|
21
|
+
return (pkg && pkg.name) || 'yapi-gen';
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
/* fallthrough */
|
|
30
|
+
}
|
|
31
|
+
return 'yapi-gen';
|
|
32
|
+
}
|
|
33
|
+
const DEFAULT_REQUEST_FILE_PATH = 'src/yapi/request.ts';
|
|
34
|
+
const DEFAULT_AXIOS_INSTANCE_PATH = "/@/utils/http/axios";
|
|
35
|
+
const DEFAULT_NUXT_REQUEST_IMPORT_PATH = '#imports';
|
|
36
|
+
function normalizeRequestAdapter(adapter) {
|
|
37
|
+
if (adapter == null)
|
|
38
|
+
return { type: 'custom' };
|
|
39
|
+
if (adapter === 'axios') {
|
|
40
|
+
return { type: 'axios', axiosInstancePath: DEFAULT_AXIOS_INSTANCE_PATH, axiosExportName: 'defHttp' };
|
|
41
|
+
}
|
|
42
|
+
if (adapter === 'nuxt-bbm')
|
|
43
|
+
return { type: 'nuxt-bbm', requestImportPath: DEFAULT_NUXT_REQUEST_IMPORT_PATH };
|
|
44
|
+
if (adapter === 'custom')
|
|
45
|
+
return { type: 'custom' };
|
|
46
|
+
if (typeof adapter === 'object') {
|
|
47
|
+
if (adapter.type === 'axios') {
|
|
48
|
+
return {
|
|
49
|
+
type: 'axios',
|
|
50
|
+
axiosInstancePath: adapter.axiosInstancePath ?? DEFAULT_AXIOS_INSTANCE_PATH,
|
|
51
|
+
axiosExportName: adapter.axiosExportName ?? 'defHttp',
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
if (adapter.type === 'nuxt-bbm') {
|
|
55
|
+
return { type: 'nuxt-bbm', requestImportPath: adapter.requestImportPath ?? DEFAULT_NUXT_REQUEST_IMPORT_PATH };
|
|
56
|
+
}
|
|
57
|
+
if (adapter.type === 'custom') {
|
|
58
|
+
return { type: 'custom', requestFunctionFilePath: adapter.requestFunctionFilePath };
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return { type: 'custom' };
|
|
62
|
+
}
|
|
63
|
+
function getAxiosAdapterFileContent(packageName, axiosInstancePath, exportName) {
|
|
64
|
+
const importLine = exportName === 'default'
|
|
65
|
+
? `import defHttp from '${axiosInstancePath}';`
|
|
66
|
+
: `import { defHttp } from '${axiosInstancePath}';`;
|
|
67
|
+
return `/**
|
|
68
|
+
* 由 yapi-gen 预设 "axios" 自动生成,请勿直接修改。
|
|
69
|
+
*/
|
|
70
|
+
import { createAxiosAdapter } from '${packageName}/adapters/axios';
|
|
71
|
+
${importLine}
|
|
72
|
+
|
|
73
|
+
export default createAxiosAdapter(defHttp);
|
|
74
|
+
`;
|
|
75
|
+
}
|
|
76
|
+
function getNuxtBbmAdapterFileContent(packageName, requestImportPath) {
|
|
77
|
+
return `/**
|
|
78
|
+
* 由 yapi-gen 预设 "nuxt-bbm" 自动生成,请勿直接修改。
|
|
79
|
+
*/
|
|
80
|
+
import { createNuxtAdapter } from '${packageName}/adapters/nuxt-bbm';
|
|
81
|
+
import { request } from '${requestImportPath}';
|
|
82
|
+
|
|
83
|
+
export default createNuxtAdapter(request);
|
|
84
|
+
`;
|
|
85
|
+
}
|
|
86
|
+
export function resolveConfig(config, cwd) {
|
|
87
|
+
const resolved = { ...config };
|
|
88
|
+
const adapter = normalizeRequestAdapter(resolved.requestAdapter);
|
|
89
|
+
const requestFilePath = resolved.requestFunctionFilePath ?? adapter.requestFunctionFilePath ?? DEFAULT_REQUEST_FILE_PATH;
|
|
90
|
+
const packageName = getPackageName(cwd);
|
|
91
|
+
if (adapter.type === 'axios') {
|
|
92
|
+
const content = getAxiosAdapterFileContent(packageName, adapter.axiosInstancePath, adapter.axiosExportName ?? 'defHttp');
|
|
93
|
+
const absolutePath = resolve(cwd, requestFilePath);
|
|
94
|
+
const dir = dirname(absolutePath);
|
|
95
|
+
if (!existsSync(dir))
|
|
96
|
+
mkdirSync(dir, { recursive: true });
|
|
97
|
+
writeFileSync(absolutePath, content, 'utf-8');
|
|
98
|
+
resolved.requestFunctionFilePath = forYttCwd(requestFilePath);
|
|
99
|
+
delete resolved.requestAdapter;
|
|
100
|
+
return resolved;
|
|
101
|
+
}
|
|
102
|
+
if (adapter.type === 'nuxt-bbm') {
|
|
103
|
+
const content = getNuxtBbmAdapterFileContent(packageName, adapter.requestImportPath);
|
|
104
|
+
const absolutePath = resolve(cwd, requestFilePath);
|
|
105
|
+
const dir = dirname(absolutePath);
|
|
106
|
+
if (!existsSync(dir))
|
|
107
|
+
mkdirSync(dir, { recursive: true });
|
|
108
|
+
writeFileSync(absolutePath, content, 'utf-8');
|
|
109
|
+
resolved.requestFunctionFilePath = forYttCwd(requestFilePath);
|
|
110
|
+
delete resolved.requestAdapter;
|
|
111
|
+
return resolved;
|
|
112
|
+
}
|
|
113
|
+
if (!resolved.requestFunctionFilePath && !adapter.requestFunctionFilePath) {
|
|
114
|
+
throw new Error('[yapi-gen] requestAdapter 为 custom 或未指定时,必须配置 requestFunctionFilePath');
|
|
115
|
+
}
|
|
116
|
+
if (adapter.requestFunctionFilePath) {
|
|
117
|
+
resolved.requestFunctionFilePath = forYttCwd(adapter.requestFunctionFilePath);
|
|
118
|
+
}
|
|
119
|
+
delete resolved.requestAdapter;
|
|
120
|
+
return resolved;
|
|
121
|
+
}
|
package/dist/resolve-config.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolve-config.d.ts","sourceRoot":"","sources":["../src/resolve-config.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,iBAAiB,EAAyB,MAAM,
|
|
1
|
+
{"version":3,"file":"resolve-config.d.ts","sourceRoot":"","sources":["../src/resolve-config.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,iBAAiB,EAAyB,MAAM,oBAAoB,CAAC;AA4GnF;;;GAGG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,iBAAiB,EAAE,GAAG,EAAE,MAAM,GAAG,iBAAiB,CA2CvF"}
|
package/dist/resolve-config.js
CHANGED
|
@@ -3,17 +3,25 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import { existsSync, mkdirSync, writeFileSync, readFileSync } from 'fs';
|
|
5
5
|
import { resolve, dirname, join } from 'path';
|
|
6
|
-
import {
|
|
7
|
-
function getPackageName() {
|
|
6
|
+
import { createRequire } from 'module';
|
|
7
|
+
function getPackageName(cwd) {
|
|
8
8
|
try {
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
const req = createRequire(join(cwd, 'package.json'));
|
|
10
|
+
for (const id of ['@karry.sun/yapi-gen/package.json', 'yapi-gen/package.json']) {
|
|
11
|
+
try {
|
|
12
|
+
const pkgPath = req.resolve(id);
|
|
13
|
+
const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
|
|
14
|
+
return (pkg && pkg.name) || 'yapi-gen';
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
13
20
|
}
|
|
14
21
|
catch {
|
|
15
|
-
|
|
22
|
+
/* fallthrough */
|
|
16
23
|
}
|
|
24
|
+
return 'yapi-gen';
|
|
17
25
|
}
|
|
18
26
|
const DEFAULT_REQUEST_FILE_PATH = 'src/yapi/request.ts';
|
|
19
27
|
const DEFAULT_AXIOS_INSTANCE_PATH = "/@/utils/http/axios";
|
|
@@ -83,6 +91,15 @@ import { request } from '${requestImportPath}';
|
|
|
83
91
|
export default createNuxtAdapter(request);
|
|
84
92
|
`;
|
|
85
93
|
}
|
|
94
|
+
/**
|
|
95
|
+
* ytt 使用 -c 时 cwd 为配置所在目录;YAPI_GEN_PROJECT_ROOT 存在时用绝对路径,否则用 '../' 相对路径
|
|
96
|
+
*/
|
|
97
|
+
function forYttCwd(p) {
|
|
98
|
+
if (p.startsWith('/') || p.startsWith('@'))
|
|
99
|
+
return p;
|
|
100
|
+
const root = process.env.YAPI_GEN_PROJECT_ROOT;
|
|
101
|
+
return root ? join(root, p) : `../${p}`;
|
|
102
|
+
}
|
|
86
103
|
/**
|
|
87
104
|
* 解析配置:若为预设(axios / nuxt-bbm),则生成并写入 request 文件,并补全 requestFunctionFilePath;
|
|
88
105
|
* 返回去掉 requestAdapter 等字段、可供 ytt defineConfig 使用的配置。
|
|
@@ -91,7 +108,7 @@ export function resolveConfig(config, cwd) {
|
|
|
91
108
|
const resolved = { ...config };
|
|
92
109
|
const adapter = normalizeRequestAdapter(resolved.requestAdapter);
|
|
93
110
|
const requestFilePath = resolved.requestFunctionFilePath ?? adapter.requestFunctionFilePath ?? DEFAULT_REQUEST_FILE_PATH;
|
|
94
|
-
const packageName = getPackageName();
|
|
111
|
+
const packageName = getPackageName(cwd);
|
|
95
112
|
if (adapter.type === 'axios') {
|
|
96
113
|
const content = getAxiosAdapterFileContent(packageName, adapter.axiosInstancePath, adapter.axiosExportName ?? 'defHttp');
|
|
97
114
|
const absolutePath = resolve(cwd, requestFilePath);
|
|
@@ -100,7 +117,7 @@ export function resolveConfig(config, cwd) {
|
|
|
100
117
|
mkdirSync(dir, { recursive: true });
|
|
101
118
|
}
|
|
102
119
|
writeFileSync(absolutePath, content, 'utf-8');
|
|
103
|
-
resolved.requestFunctionFilePath = requestFilePath;
|
|
120
|
+
resolved.requestFunctionFilePath = forYttCwd(requestFilePath);
|
|
104
121
|
delete resolved.requestAdapter;
|
|
105
122
|
return resolved;
|
|
106
123
|
}
|
|
@@ -112,7 +129,7 @@ export function resolveConfig(config, cwd) {
|
|
|
112
129
|
mkdirSync(dir, { recursive: true });
|
|
113
130
|
}
|
|
114
131
|
writeFileSync(absolutePath, content, 'utf-8');
|
|
115
|
-
resolved.requestFunctionFilePath = requestFilePath;
|
|
132
|
+
resolved.requestFunctionFilePath = forYttCwd(requestFilePath);
|
|
116
133
|
delete resolved.requestAdapter;
|
|
117
134
|
return resolved;
|
|
118
135
|
}
|
|
@@ -121,7 +138,7 @@ export function resolveConfig(config, cwd) {
|
|
|
121
138
|
throw new Error('[yapi-gen] requestAdapter 为 custom 或未指定时,必须配置 requestFunctionFilePath');
|
|
122
139
|
}
|
|
123
140
|
if (adapter.requestFunctionFilePath) {
|
|
124
|
-
resolved.requestFunctionFilePath = adapter.requestFunctionFilePath;
|
|
141
|
+
resolved.requestFunctionFilePath = forYttCwd(adapter.requestFunctionFilePath);
|
|
125
142
|
}
|
|
126
143
|
delete resolved.requestAdapter;
|
|
127
144
|
return resolved;
|
package/dist/run.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"run.d.ts","sourceRoot":"","sources":["../src/run.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"run.d.ts","sourceRoot":"","sources":["../src/run.ts"],"names":[],"mappings":"AAgKA,wBAAgB,GAAG,CAAC,OAAO,GAAE;IAC3B,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;CACZ,GAAG,MAAM,CAyFd"}
|
package/dist/run.js
CHANGED
|
@@ -4,8 +4,9 @@
|
|
|
4
4
|
* 支持目录映射、按接口/分类生成、合并模式(对应 web-finance-admin gen-api.mjs)
|
|
5
5
|
*/
|
|
6
6
|
import { spawnSync } from 'child_process';
|
|
7
|
-
import { existsSync, mkdirSync, writeFileSync } from 'fs';
|
|
7
|
+
import { existsSync, mkdirSync, mkdtempSync, writeFileSync, rmSync } from 'fs';
|
|
8
8
|
import { resolve, dirname, join } from 'path';
|
|
9
|
+
import { tmpdir } from 'os';
|
|
9
10
|
import { fileURLToPath } from 'url';
|
|
10
11
|
import { createRequire } from 'module';
|
|
11
12
|
import { readFileSync } from 'fs';
|
|
@@ -34,7 +35,6 @@ const CONFIG_NAMES = [
|
|
|
34
35
|
'.yapi-genrc.js',
|
|
35
36
|
'.yapi-genrc.mjs',
|
|
36
37
|
];
|
|
37
|
-
const WRAPPER_DIR = '.yapi-gen';
|
|
38
38
|
const WRAPPER_FILENAME = 'ytt.config.ts';
|
|
39
39
|
function findConfig(cwd) {
|
|
40
40
|
for (const name of CONFIG_NAMES) {
|
|
@@ -52,16 +52,38 @@ function getWrapperContent(cwd, configPath) {
|
|
|
52
52
|
const packageName = getPackageName();
|
|
53
53
|
const absoluteConfig = resolve(cwd, configPath);
|
|
54
54
|
const configPathEscaped = JSON.stringify(absoluteConfig);
|
|
55
|
+
const projectPkgPath = resolve(cwd, 'package.json');
|
|
56
|
+
const projectPkgEscaped = JSON.stringify(projectPkgPath);
|
|
55
57
|
return `/* 由 yapi-gen 自动生成,请勿修改 */
|
|
56
|
-
const { defineConfig } = require('yapi-to-typescript');
|
|
57
|
-
const { resolveConfig } = require('${packageName}');
|
|
58
58
|
const { createRequire } = require('module');
|
|
59
59
|
|
|
60
|
-
const req = createRequire(
|
|
60
|
+
const req = createRequire(${projectPkgEscaped});
|
|
61
|
+
const { defineConfig } = req('yapi-to-typescript');
|
|
62
|
+
const { resolveConfig } = req('${packageName}');
|
|
63
|
+
|
|
61
64
|
const userConfig = req(${configPathEscaped});
|
|
62
65
|
const raw = userConfig.default ?? userConfig;
|
|
63
66
|
const configList = Array.isArray(raw) ? raw : [raw];
|
|
64
|
-
|
|
67
|
+
let resolvedList = configList.map((c) => resolveConfig(c, process.cwd()));
|
|
68
|
+
|
|
69
|
+
// 分类模式:注入 YTT_CATEGORY_IDS 到 categories,确保 ytt 拉取指定分类
|
|
70
|
+
const catIdsEnv = process.env.YTT_CATEGORY_IDS;
|
|
71
|
+
if (catIdsEnv && catIdsEnv.trim()) {
|
|
72
|
+
const catIds = catIdsEnv.split(',').map((s) => parseInt(s.trim(), 10)).filter((n) => !isNaN(n));
|
|
73
|
+
if (catIds.length > 0) {
|
|
74
|
+
resolvedList = resolvedList.map((cfg) => {
|
|
75
|
+
const out = { ...cfg };
|
|
76
|
+
if (out.projects) {
|
|
77
|
+
out.projects = out.projects.map((proj) => {
|
|
78
|
+
const base = proj.categories?.[0] || { id: 0 };
|
|
79
|
+
const categories = catIds.map((id) => ({ ...base, id }));
|
|
80
|
+
return { ...proj, categories };
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
return out;
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
65
87
|
|
|
66
88
|
module.exports = { default: defineConfig(resolvedList) };
|
|
67
89
|
`;
|
|
@@ -133,8 +155,9 @@ export function run(options = {}) {
|
|
|
133
155
|
console.error('[yapi-gen] 或使用 init 子命令:pnpm yapi-gen init');
|
|
134
156
|
return 1;
|
|
135
157
|
}
|
|
136
|
-
//
|
|
137
|
-
const
|
|
158
|
+
// 以配置文件所在目录为项目根,确保映射文件路径正确(支持 monorepo 等场景)
|
|
159
|
+
const projectRoot = dirname(configPath);
|
|
160
|
+
const args = parseArgs(argv, projectRoot);
|
|
138
161
|
const { interfaceIds, categoryIds, manualMapping, outputDir, mappingFilePath } = args;
|
|
139
162
|
const isInterfaceMode = interfaceIds.length > 0;
|
|
140
163
|
const isCategoryMode = categoryIds.length > 0;
|
|
@@ -142,7 +165,7 @@ export function run(options = {}) {
|
|
|
142
165
|
console.error('[yapi-gen] 接口模式与分类模式不能同时使用');
|
|
143
166
|
return 1;
|
|
144
167
|
}
|
|
145
|
-
const yapiDir = resolve(
|
|
168
|
+
const yapiDir = resolve(projectRoot, outputDir);
|
|
146
169
|
const tempDir = join(yapiDir, '.temp');
|
|
147
170
|
const fileMapping = loadMapping(mappingFilePath);
|
|
148
171
|
const mergedMapping = { ...fileMapping, ...manualMapping };
|
|
@@ -163,29 +186,35 @@ export function run(options = {}) {
|
|
|
163
186
|
else {
|
|
164
187
|
console.log('[yapi-gen] 全量生成');
|
|
165
188
|
}
|
|
166
|
-
const wrapperDir =
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
}
|
|
170
|
-
const wrapperPath = resolve(wrapperDir, WRAPPER_FILENAME);
|
|
171
|
-
const content = getWrapperContent(cwd, configPath);
|
|
189
|
+
const wrapperDir = mkdtempSync(join(tmpdir(), 'yapi-gen-'));
|
|
190
|
+
const wrapperPath = join(wrapperDir, WRAPPER_FILENAME);
|
|
191
|
+
const content = getWrapperContent(projectRoot, configPath);
|
|
172
192
|
writeFileSync(wrapperPath, content, 'utf-8');
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
193
|
+
try {
|
|
194
|
+
const yttCliPath = require.resolve('yapi-to-typescript/lib/cjs/cli.js');
|
|
195
|
+
const result = spawnSync(process.execPath, [yttCliPath, '-c', wrapperPath], {
|
|
196
|
+
cwd: projectRoot,
|
|
197
|
+
stdio: 'inherit',
|
|
198
|
+
env: {
|
|
199
|
+
...process.env,
|
|
200
|
+
YAPI_GEN_PROJECT_ROOT: projectRoot,
|
|
201
|
+
YTT_INTERFACE_IDS: process.env.YTT_INTERFACE_IDS,
|
|
202
|
+
YTT_CATEGORY_IDS: process.env.YTT_CATEGORY_IDS,
|
|
203
|
+
YTT_CAT_MAPPING: process.env.YTT_CAT_MAPPING,
|
|
204
|
+
},
|
|
205
|
+
});
|
|
206
|
+
if (result.status !== 0) {
|
|
207
|
+
return result.status ?? 1;
|
|
208
|
+
}
|
|
209
|
+
if (isInterfaceMode) {
|
|
210
|
+
mergeTempToTarget(yapiDir, tempDir, interfaceIds, fileMapping, manualMapping);
|
|
211
|
+
}
|
|
212
|
+
return 0;
|
|
213
|
+
}
|
|
214
|
+
finally {
|
|
215
|
+
try {
|
|
216
|
+
rmSync(wrapperDir, { recursive: true });
|
|
217
|
+
}
|
|
218
|
+
catch (_) { }
|
|
189
219
|
}
|
|
190
|
-
return 0;
|
|
191
220
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@karry.sun/yapi-gen",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.9",
|
|
4
4
|
"description": "基于 yapi-to-typescript 的 YApi 前端代码生成 CLI,支持可配置的请求适配器与多项目复用",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
".": {
|
|
10
10
|
"types": "./dist/index.d.ts",
|
|
11
11
|
"import": "./dist/index.js",
|
|
12
|
+
"require": "./dist/cjs/index.cjs.js",
|
|
12
13
|
"default": "./dist/index.js"
|
|
13
14
|
},
|
|
14
15
|
"./adapters/axios": {
|
|
@@ -35,7 +36,8 @@
|
|
|
35
36
|
"yapi-to-typescript": "^3.38.0"
|
|
36
37
|
},
|
|
37
38
|
"devDependencies": {
|
|
38
|
-
"typescript": "^5.0.0"
|
|
39
|
+
"typescript": "^5.0.0",
|
|
40
|
+
"vitest": "^4.0.18"
|
|
39
41
|
},
|
|
40
42
|
"keywords": [
|
|
41
43
|
"yapi",
|
|
@@ -46,6 +48,7 @@
|
|
|
46
48
|
],
|
|
47
49
|
"license": "MIT",
|
|
48
50
|
"scripts": {
|
|
49
|
-
"build": "tsc"
|
|
51
|
+
"build": "tsc && tsc -p tsconfig.cjs.json && node -e \"require('fs').writeFileSync('dist/cjs/package.json', '{\\\"type\\\":\\\"commonjs\\\"}\\n')\"",
|
|
52
|
+
"test": "vitest run"
|
|
50
53
|
}
|
|
51
54
|
}
|