@jayfong/x-server 2.92.2 → 2.94.0
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/lib/_cjs/cli/api_generator.js +16 -5
- package/lib/_cjs/cli/cli.js +20 -1
- package/lib/cli/api_generator.d.ts +8 -1
- package/lib/cli/api_generator.js +16 -5
- package/lib/cli/cli.js +20 -1
- package/package.json +1 -1
|
@@ -17,8 +17,9 @@ var _http_method = require("../core/http_method");
|
|
|
17
17
|
/** 接口数据 */
|
|
18
18
|
|
|
19
19
|
class ApiGenerator {
|
|
20
|
-
constructor(
|
|
21
|
-
this.
|
|
20
|
+
constructor(options = {}) {
|
|
21
|
+
this.options = options;
|
|
22
|
+
this.cwd = void 0;
|
|
22
23
|
this.debug = (0, _debug.default)('api');
|
|
23
24
|
// 几种情况:
|
|
24
25
|
// /index.ts
|
|
@@ -27,6 +28,7 @@ class ApiGenerator {
|
|
|
27
28
|
// /user/list.ts
|
|
28
29
|
this.categoryUrlRe = /^.+\/src\/handlers\/(?:index\.ts|(.*)\/index\.ts|(.*)\.ts)$/;
|
|
29
30
|
this.checker = void 0;
|
|
31
|
+
this.cwd = this.options.cwd || process.cwd();
|
|
30
32
|
}
|
|
31
33
|
async start() {
|
|
32
34
|
this.debug('启动项目...');
|
|
@@ -62,6 +64,14 @@ class ApiGenerator {
|
|
|
62
64
|
// 忽略仅开发时生效的路由
|
|
63
65
|
if (/@dev[\/\.]/.test(categorySourceFilePath)) continue;
|
|
64
66
|
|
|
67
|
+
// 支持 include, exclude
|
|
68
|
+
if (this.options.include?.length) {
|
|
69
|
+
if (this.options.include.every(item => !categorySourceFilePath.includes(item))) continue;
|
|
70
|
+
}
|
|
71
|
+
if (this.options.exclude?.length) {
|
|
72
|
+
if (this.options.exclude.some(item => categorySourceFilePath.includes(item))) continue;
|
|
73
|
+
}
|
|
74
|
+
|
|
65
75
|
// 分类 URL
|
|
66
76
|
const categoryUrlMatch = categorySourceFilePath.match(this.categoryUrlRe);
|
|
67
77
|
let categoryUrl = categoryUrlMatch[1] ?? categoryUrlMatch[2] ?? '';
|
|
@@ -133,11 +143,12 @@ class ApiGenerator {
|
|
|
133
143
|
}
|
|
134
144
|
}
|
|
135
145
|
this.debug('写入文件...');
|
|
136
|
-
|
|
146
|
+
const prefix = this.options.name ? `${this.options.name}_` : '';
|
|
147
|
+
await Promise.all([_fsExtra.default.outputJSON(_path.default.join(this.cwd, `temp/${prefix}api.json`), apiData, {
|
|
137
148
|
spaces: 2
|
|
138
|
-
}), _fsExtra.default.outputJSON(_path.default.join(this.cwd,
|
|
149
|
+
}), _fsExtra.default.outputJSON(_path.default.join(this.cwd, `temp/${prefix}yapi.json`), this.apiDataToYApiData(apiData), {
|
|
139
150
|
spaces: 2
|
|
140
|
-
}), _fsExtra.default.outputJSON(_path.default.join(this.cwd,
|
|
151
|
+
}), _fsExtra.default.outputJSON(_path.default.join(this.cwd, `temp/${prefix}openapi31.json`), this.apiDataToOpenAPI31Data(apiData), {
|
|
141
152
|
spaces: 2
|
|
142
153
|
})]);
|
|
143
154
|
}
|
package/lib/_cjs/cli/cli.js
CHANGED
|
@@ -238,6 +238,20 @@ _yargs.default.command('dev', '开始开发', _ => _.positional('index', {
|
|
|
238
238
|
alias: 'h',
|
|
239
239
|
describe: '渠道',
|
|
240
240
|
type: 'string'
|
|
241
|
+
}).positional('include', {
|
|
242
|
+
alias: 'i',
|
|
243
|
+
describe: '包括',
|
|
244
|
+
type: 'string',
|
|
245
|
+
array: true
|
|
246
|
+
}).positional('exclude', {
|
|
247
|
+
alias: 'e',
|
|
248
|
+
describe: '排除',
|
|
249
|
+
type: 'string',
|
|
250
|
+
array: true
|
|
251
|
+
}).positional('name', {
|
|
252
|
+
alias: 'n',
|
|
253
|
+
describe: '名称',
|
|
254
|
+
type: 'string'
|
|
241
255
|
}), async argv => {
|
|
242
256
|
await _env_util.EnvUtil.withChannel({
|
|
243
257
|
cwd: process.cwd(),
|
|
@@ -250,7 +264,12 @@ _yargs.default.command('dev', '开始开发', _ => _.positional('index', {
|
|
|
250
264
|
file: envFiles,
|
|
251
265
|
channel: channel
|
|
252
266
|
});
|
|
253
|
-
await new _api_generator.ApiGenerator(
|
|
267
|
+
await new _api_generator.ApiGenerator({
|
|
268
|
+
cwd: process.cwd(),
|
|
269
|
+
include: argv.include?.flatMap(v => v.split(',')),
|
|
270
|
+
exclude: argv.exclude?.flatMap(v => v.split(',')),
|
|
271
|
+
name: argv.name
|
|
272
|
+
}).start();
|
|
254
273
|
}
|
|
255
274
|
});
|
|
256
275
|
}).command('prisma', 'prisma 代理,主要为了注入环境变量', _ => _.positional('production', {
|
|
@@ -31,9 +31,16 @@ type ApiData = {
|
|
|
31
31
|
requestDataJsonSchema: JSONSchema4;
|
|
32
32
|
responseDataJsonSchema: JSONSchema4;
|
|
33
33
|
};
|
|
34
|
+
export type ApiGeneratorOptions = {
|
|
35
|
+
cwd?: string;
|
|
36
|
+
include?: string[];
|
|
37
|
+
exclude?: string[];
|
|
38
|
+
name?: string;
|
|
39
|
+
};
|
|
34
40
|
export declare class ApiGenerator {
|
|
41
|
+
private options;
|
|
35
42
|
private cwd;
|
|
36
|
-
constructor(
|
|
43
|
+
constructor(options?: ApiGeneratorOptions);
|
|
37
44
|
debug: createDebug.Debugger;
|
|
38
45
|
categoryUrlRe: RegExp;
|
|
39
46
|
checker: ts.TypeChecker;
|
package/lib/cli/api_generator.js
CHANGED
|
@@ -12,8 +12,9 @@ import { HandlerMethodToHttpMethod } from "../core/http_method";
|
|
|
12
12
|
/** 接口数据 */
|
|
13
13
|
|
|
14
14
|
export class ApiGenerator {
|
|
15
|
-
constructor(
|
|
16
|
-
this.
|
|
15
|
+
constructor(options = {}) {
|
|
16
|
+
this.options = options;
|
|
17
|
+
this.cwd = void 0;
|
|
17
18
|
this.debug = createDebug('api');
|
|
18
19
|
// 几种情况:
|
|
19
20
|
// /index.ts
|
|
@@ -22,6 +23,7 @@ export class ApiGenerator {
|
|
|
22
23
|
// /user/list.ts
|
|
23
24
|
this.categoryUrlRe = /^.+\/src\/handlers\/(?:index\.ts|(.*)\/index\.ts|(.*)\.ts)$/;
|
|
24
25
|
this.checker = void 0;
|
|
26
|
+
this.cwd = this.options.cwd || process.cwd();
|
|
25
27
|
}
|
|
26
28
|
async start() {
|
|
27
29
|
this.debug('启动项目...');
|
|
@@ -57,6 +59,14 @@ export class ApiGenerator {
|
|
|
57
59
|
// 忽略仅开发时生效的路由
|
|
58
60
|
if (/@dev[\/\.]/.test(categorySourceFilePath)) continue;
|
|
59
61
|
|
|
62
|
+
// 支持 include, exclude
|
|
63
|
+
if (this.options.include?.length) {
|
|
64
|
+
if (this.options.include.every(item => !categorySourceFilePath.includes(item))) continue;
|
|
65
|
+
}
|
|
66
|
+
if (this.options.exclude?.length) {
|
|
67
|
+
if (this.options.exclude.some(item => categorySourceFilePath.includes(item))) continue;
|
|
68
|
+
}
|
|
69
|
+
|
|
60
70
|
// 分类 URL
|
|
61
71
|
const categoryUrlMatch = categorySourceFilePath.match(this.categoryUrlRe);
|
|
62
72
|
let categoryUrl = categoryUrlMatch[1] ?? categoryUrlMatch[2] ?? '';
|
|
@@ -128,11 +138,12 @@ export class ApiGenerator {
|
|
|
128
138
|
}
|
|
129
139
|
}
|
|
130
140
|
this.debug('写入文件...');
|
|
131
|
-
|
|
141
|
+
const prefix = this.options.name ? `${this.options.name}_` : '';
|
|
142
|
+
await Promise.all([fs.outputJSON(path.join(this.cwd, `temp/${prefix}api.json`), apiData, {
|
|
132
143
|
spaces: 2
|
|
133
|
-
}), fs.outputJSON(path.join(this.cwd,
|
|
144
|
+
}), fs.outputJSON(path.join(this.cwd, `temp/${prefix}yapi.json`), this.apiDataToYApiData(apiData), {
|
|
134
145
|
spaces: 2
|
|
135
|
-
}), fs.outputJSON(path.join(this.cwd,
|
|
146
|
+
}), fs.outputJSON(path.join(this.cwd, `temp/${prefix}openapi31.json`), this.apiDataToOpenAPI31Data(apiData), {
|
|
136
147
|
spaces: 2
|
|
137
148
|
})]);
|
|
138
149
|
}
|
package/lib/cli/cli.js
CHANGED
|
@@ -236,6 +236,20 @@ yargs.command('dev', '开始开发', _ => _.positional('index', {
|
|
|
236
236
|
alias: 'h',
|
|
237
237
|
describe: '渠道',
|
|
238
238
|
type: 'string'
|
|
239
|
+
}).positional('include', {
|
|
240
|
+
alias: 'i',
|
|
241
|
+
describe: '包括',
|
|
242
|
+
type: 'string',
|
|
243
|
+
array: true
|
|
244
|
+
}).positional('exclude', {
|
|
245
|
+
alias: 'e',
|
|
246
|
+
describe: '排除',
|
|
247
|
+
type: 'string',
|
|
248
|
+
array: true
|
|
249
|
+
}).positional('name', {
|
|
250
|
+
alias: 'n',
|
|
251
|
+
describe: '名称',
|
|
252
|
+
type: 'string'
|
|
239
253
|
}), async argv => {
|
|
240
254
|
await EnvUtil.withChannel({
|
|
241
255
|
cwd: process.cwd(),
|
|
@@ -248,7 +262,12 @@ yargs.command('dev', '开始开发', _ => _.positional('index', {
|
|
|
248
262
|
file: envFiles,
|
|
249
263
|
channel: channel
|
|
250
264
|
});
|
|
251
|
-
await new ApiGenerator(
|
|
265
|
+
await new ApiGenerator({
|
|
266
|
+
cwd: process.cwd(),
|
|
267
|
+
include: argv.include?.flatMap(v => v.split(',')),
|
|
268
|
+
exclude: argv.exclude?.flatMap(v => v.split(',')),
|
|
269
|
+
name: argv.name
|
|
270
|
+
}).start();
|
|
252
271
|
}
|
|
253
272
|
});
|
|
254
273
|
}).command('prisma', 'prisma 代理,主要为了注入环境变量', _ => _.positional('production', {
|