@gopowerteam/request-generate 0.2.2 → 0.3.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/bin/download.mts +2 -2
- package/bin/generate.mts +1 -1
- package/bin/index.mts +2 -2
- package/dist/chunk-C0xms8kb.cjs +34 -0
- package/dist/index.cjs +1105 -0
- package/dist/index.d.cts +739 -0
- package/dist/index.d.mts +739 -0
- package/dist/index.mjs +937 -1040
- package/dist/templates/{partials → templates/partials}/export-service-namespace.hbs +1 -1
- package/dist/vite-plugin/index.cjs +238 -0
- package/dist/vite-plugin/index.d.cts +13 -0
- package/dist/vite-plugin/index.d.mts +14 -0
- package/dist/vite-plugin/index.mjs +118 -116
- package/package.json +29 -29
- package/dist/chunk-WF3XBEPN.mjs +0 -18
- package/dist/chunk-XXPGZHWZ.js +0 -10
- package/dist/index.d.ts +0 -165
- package/dist/index.js +0 -1203
- package/dist/vite-plugin/index.d.ts +0 -13
- package/dist/vite-plugin/index.js +0 -231
- /package/dist/templates/{export-model.hbs → templates/export-model.hbs} +0 -0
- /package/dist/templates/{export-service.hbs → templates/export-service.hbs} +0 -0
- /package/dist/templates/{partials → templates/partials}/export-description.hbs +0 -0
- /package/dist/templates/{partials → templates/partials}/export-header.hbs +0 -0
- /package/dist/templates/{partials → templates/partials}/export-model-field.hbs +0 -0
- /package/dist/templates/{partials → templates/partials}/export-model-import.hbs +0 -0
- /package/dist/templates/{partials → templates/partials}/export-model-type.hbs +0 -0
- /package/dist/templates/{partials → templates/partials}/export-operation-params-body.hbs +0 -0
- /package/dist/templates/{partials → templates/partials}/export-operation-params-path.hbs +0 -0
- /package/dist/templates/{partials → templates/partials}/export-operation-params-query.hbs +0 -0
- /package/dist/templates/{partials → templates/partials}/export-operation-response.hbs +0 -0
- /package/dist/templates/{partials → templates/partials}/export-schema-type.hbs +0 -0
- /package/dist/templates/{partials → templates/partials}/export-service-class.hbs +0 -0
- /package/dist/templates/{partials → templates/partials}/export-service-import.hbs +0 -0
- /package/dist/templates/{partials → templates/partials}/export-service-namespace-type.hbs +0 -0
- /package/dist/templates/{partials → templates/partials}/export-service-operation.hbs +0 -0
- /package/dist/templates/{partials → templates/partials}/is-required.hbs +0 -0
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
const require_chunk = require('../chunk-C0xms8kb.cjs');
|
|
2
|
+
let node_fs = require("node:fs");
|
|
3
|
+
node_fs = require_chunk.__toESM(node_fs);
|
|
4
|
+
let node_path = require("node:path");
|
|
5
|
+
node_path = require_chunk.__toESM(node_path);
|
|
6
|
+
let node_process = require("node:process");
|
|
7
|
+
node_process = require_chunk.__toESM(node_process);
|
|
8
|
+
let handlebars = require("handlebars");
|
|
9
|
+
handlebars = require_chunk.__toESM(handlebars);
|
|
10
|
+
let node_child_process = require("node:child_process");
|
|
11
|
+
|
|
12
|
+
//#region src/vite-plugin/templates.ts
|
|
13
|
+
const useRequestFunctionCodeTemplate = `
|
|
14
|
+
export function useRequest(
|
|
15
|
+
...selects
|
|
16
|
+
) {
|
|
17
|
+
const services = selects.map((select)=>{
|
|
18
|
+
const service = select(serviceMap)
|
|
19
|
+
return new service()
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
if(services.length === 1){
|
|
23
|
+
return services[0]
|
|
24
|
+
}else{
|
|
25
|
+
return services
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
`;
|
|
29
|
+
const useRequestFunctionTypeTemplate = `
|
|
30
|
+
export function useRequest<T1,T2,T3,T4,T5,T6>(
|
|
31
|
+
select1: ((services: typeof serviceMap) => { new (): T1 }),
|
|
32
|
+
select2: ((services: typeof serviceMap) => { new (): T2 }),
|
|
33
|
+
select3: ((services: typeof serviceMap) => { new (): T3 }),
|
|
34
|
+
select4: ((services: typeof serviceMap) => { new (): T4 }),
|
|
35
|
+
select5: ((services: typeof serviceMap) => { new (): T5 }),
|
|
36
|
+
select6: ((services: typeof serviceMap) => { new (): T6 }),
|
|
37
|
+
): [T1,T2,T3,T4,T5,T6]
|
|
38
|
+
export function useRequest<T1,T2,T3,T4,T5>(
|
|
39
|
+
select1: ((services: typeof serviceMap) => { new (): T1 }),
|
|
40
|
+
select2: ((services: typeof serviceMap) => { new (): T2 }),
|
|
41
|
+
select3: ((services: typeof serviceMap) => { new (): T3 }),
|
|
42
|
+
select4: ((services: typeof serviceMap) => { new (): T4 }),
|
|
43
|
+
select5: ((services: typeof serviceMap) => { new (): T5 }),
|
|
44
|
+
): [T1,T2,T3,T4,T5]
|
|
45
|
+
export function useRequest<T1,T2,T3,T4>(
|
|
46
|
+
select1: ((services: typeof serviceMap) => { new (): T1 }),
|
|
47
|
+
select2: ((services: typeof serviceMap) => { new (): T2 }),
|
|
48
|
+
select3: ((services: typeof serviceMap) => { new (): T3 }),
|
|
49
|
+
select4: ((services: typeof serviceMap) => { new (): T4 }),
|
|
50
|
+
): [T1,T2,T3,T4]
|
|
51
|
+
export function useRequest<T1,T2,T3>(
|
|
52
|
+
select1: ((services: typeof serviceMap) => { new (): T1 }),
|
|
53
|
+
select2: ((services: typeof serviceMap) => { new (): T2 }),
|
|
54
|
+
select3: ((services: typeof serviceMap) => { new (): T3 }),
|
|
55
|
+
): [T1,T2,T3]
|
|
56
|
+
export function useRequest<T1,T2>(
|
|
57
|
+
select1: ((services: typeof serviceMap) => { new (): T1 }),
|
|
58
|
+
select2: ((services: typeof serviceMap) => { new (): T2 }),
|
|
59
|
+
): [T1,T2]
|
|
60
|
+
export function useRequest<T>(
|
|
61
|
+
select: ((services: typeof serviceMap) => { new (): T })
|
|
62
|
+
): T
|
|
63
|
+
export function useRequest<T>(
|
|
64
|
+
...selects: ((services: typeof serviceMap) => { new (): T })[]
|
|
65
|
+
): T | T[]
|
|
66
|
+
`;
|
|
67
|
+
const importServiceTemplate = `
|
|
68
|
+
{{#if groups}}
|
|
69
|
+
{{#each groups}}
|
|
70
|
+
{{#each services}}
|
|
71
|
+
import { {{{name}}} as {{group}}_{{name}} } from '{{path}}'
|
|
72
|
+
{{/each}}
|
|
73
|
+
{{/each}}
|
|
74
|
+
{{else}}
|
|
75
|
+
{{#each services}}
|
|
76
|
+
import { {{{name}}} } from '{{path}}'
|
|
77
|
+
{{/each}}
|
|
78
|
+
{{/if}}
|
|
79
|
+
`;
|
|
80
|
+
const defineServiceMapTemplate = `
|
|
81
|
+
{{#if groups}}
|
|
82
|
+
const serviceMap = {
|
|
83
|
+
{{#each groups}}
|
|
84
|
+
{{{name}}}: {
|
|
85
|
+
{{#each services}}
|
|
86
|
+
{{{name}}}: {{group}}_{{name}},
|
|
87
|
+
{{/each}}
|
|
88
|
+
},
|
|
89
|
+
{{/each}}
|
|
90
|
+
}
|
|
91
|
+
{{else}}
|
|
92
|
+
const serviceMap = {
|
|
93
|
+
{{#each services}}
|
|
94
|
+
{{{name}}},
|
|
95
|
+
{{/each}}
|
|
96
|
+
}
|
|
97
|
+
{{/if}}
|
|
98
|
+
`;
|
|
99
|
+
const generateCodeTemplate = `
|
|
100
|
+
${importServiceTemplate.trim()}
|
|
101
|
+
|
|
102
|
+
${defineServiceMapTemplate.trim()}
|
|
103
|
+
|
|
104
|
+
${useRequestFunctionCodeTemplate.trim()}
|
|
105
|
+
`;
|
|
106
|
+
const generateDeclareTemplate = `declare module '{{{MODULE_ID}}}' {
|
|
107
|
+
${importServiceTemplate.trim()}
|
|
108
|
+
|
|
109
|
+
${defineServiceMapTemplate.trim()}
|
|
110
|
+
|
|
111
|
+
${useRequestFunctionTypeTemplate.trim()}
|
|
112
|
+
}
|
|
113
|
+
`;
|
|
114
|
+
|
|
115
|
+
//#endregion
|
|
116
|
+
//#region src/vite-plugin/index.ts
|
|
117
|
+
const MODULE_ID = "virtual:request";
|
|
118
|
+
const DECLARATION_FILE = "request.d.ts";
|
|
119
|
+
let viteConfig;
|
|
120
|
+
let GerneratedCodeStr = "";
|
|
121
|
+
function genretateDeclareAndCode(options) {
|
|
122
|
+
const services = getServiceItems(getServicePaths(options));
|
|
123
|
+
const groups = getServiceGroups(services);
|
|
124
|
+
if (services && services.length) {
|
|
125
|
+
generateCode(services, groups);
|
|
126
|
+
if (options.dts !== false) generateDeclare(services, groups, options);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
function generateRequestCode() {
|
|
130
|
+
(0, node_child_process.execSync)(`${node_path.resolve(viteConfig.root, "node_modules", ".bin", "request-generate")}`, { env: {
|
|
131
|
+
...node_process.default.env,
|
|
132
|
+
FORCE_COLOR: "1"
|
|
133
|
+
} });
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Request插件
|
|
137
|
+
*/
|
|
138
|
+
var vite_plugin_default = (options) => {
|
|
139
|
+
return {
|
|
140
|
+
name: "vite-plugin-vue-request",
|
|
141
|
+
enforce: "pre",
|
|
142
|
+
configResolved(config) {
|
|
143
|
+
viteConfig = config;
|
|
144
|
+
},
|
|
145
|
+
resolveId(id) {
|
|
146
|
+
return id === MODULE_ID ? MODULE_ID : void 0;
|
|
147
|
+
},
|
|
148
|
+
async buildStart() {
|
|
149
|
+
await generateRequestCode();
|
|
150
|
+
await genretateDeclareAndCode(options);
|
|
151
|
+
},
|
|
152
|
+
load(id) {
|
|
153
|
+
if (id !== MODULE_ID) return;
|
|
154
|
+
return GerneratedCodeStr;
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
};
|
|
158
|
+
/**
|
|
159
|
+
* 生成服务路径
|
|
160
|
+
* @param options PluginOptions
|
|
161
|
+
* @returns string[]
|
|
162
|
+
*/
|
|
163
|
+
function getServicePaths(options) {
|
|
164
|
+
const paths = [];
|
|
165
|
+
const walk = (dir) => {
|
|
166
|
+
node_fs.readdirSync(dir).forEach((file) => {
|
|
167
|
+
const fullpath = node_path.join(dir, file).replace(/\\/g, "/");
|
|
168
|
+
const stat = node_fs.statSync(fullpath);
|
|
169
|
+
if (stat.isFile() && fullpath.endsWith("Service.ts")) paths.push(fullpath);
|
|
170
|
+
else if (stat.isDirectory()) walk(node_path.join(dir, file));
|
|
171
|
+
});
|
|
172
|
+
};
|
|
173
|
+
const { replacement } = viteConfig.resolve.alias.find((alias) => alias.find === options.alias);
|
|
174
|
+
if (node_fs.existsSync(node_path.resolve(options.dir))) walk(node_path.resolve(options.dir));
|
|
175
|
+
return paths.map((filepath) => filepath.replace(replacement.replace(/\\/g, "/"), options.alias));
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* 生成服务项
|
|
179
|
+
* @param paths
|
|
180
|
+
* @returns ServiceItem[]
|
|
181
|
+
*/
|
|
182
|
+
function getServiceItems(paths) {
|
|
183
|
+
const toCaseString = (str = "") => str.replace(/-(\w)/g, (_, $1) => $1.toUpperCase()).replace(/^\S/, (s) => s.toUpperCase());
|
|
184
|
+
return paths.map((filePath) => {
|
|
185
|
+
const [name] = /[^\\]+(?=\.ts$)/.exec(toCaseString(node_path.basename(filePath))) || [];
|
|
186
|
+
const [group] = /(?<=^.\/http\/)(.*?)(?=\/.*?Service\.ts$)/.exec(filePath) || [];
|
|
187
|
+
return {
|
|
188
|
+
name: toCaseString(name),
|
|
189
|
+
group: group !== "services" ? toCaseString(group) : void 0,
|
|
190
|
+
path: filePath.replace(/\.ts$/g, "")
|
|
191
|
+
};
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* 获取分组列表
|
|
196
|
+
* @param services
|
|
197
|
+
* @returns GroupItem[]
|
|
198
|
+
*/
|
|
199
|
+
function getServiceGroups(services) {
|
|
200
|
+
return services.reduce((r, s) => {
|
|
201
|
+
if (!s.group) return r;
|
|
202
|
+
const group = r.find((x) => x.name === s.group) || {
|
|
203
|
+
name: s.group,
|
|
204
|
+
services: []
|
|
205
|
+
};
|
|
206
|
+
group.services.push(s);
|
|
207
|
+
if (!r.find((g) => g === group)) r.push(group);
|
|
208
|
+
return r;
|
|
209
|
+
}, []);
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* 生成代码
|
|
213
|
+
* @param services
|
|
214
|
+
* @param groups
|
|
215
|
+
*/
|
|
216
|
+
function generateCode(services, groups) {
|
|
217
|
+
GerneratedCodeStr = handlebars.default.compile(generateCodeTemplate)({
|
|
218
|
+
groups: groups.length ? groups : void 0,
|
|
219
|
+
services
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* 生成代码
|
|
224
|
+
* @param services
|
|
225
|
+
* @param groups
|
|
226
|
+
* @param options
|
|
227
|
+
*/
|
|
228
|
+
function generateDeclare(services, groups, options) {
|
|
229
|
+
const content = handlebars.default.compile(generateDeclareTemplate)({
|
|
230
|
+
groups: groups.length ? groups : void 0,
|
|
231
|
+
services,
|
|
232
|
+
MODULE_ID
|
|
233
|
+
});
|
|
234
|
+
node_fs.writeFileSync(node_path.resolve(viteConfig.root, typeof options.dts === "string" ? options.dts : DECLARATION_FILE), content.replace(/\r\n/g, "\n"), "utf-8");
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
//#endregion
|
|
238
|
+
module.exports = vite_plugin_default;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { PluginOption } from "vite";
|
|
2
|
+
|
|
3
|
+
//#region src/vite-plugin/index.d.ts
|
|
4
|
+
interface PluginOptions {
|
|
5
|
+
alias: string;
|
|
6
|
+
dir: string;
|
|
7
|
+
dts: string | boolean;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Request插件
|
|
11
|
+
*/
|
|
12
|
+
declare const _default: (options: PluginOptions) => PluginOption;
|
|
13
|
+
export = _default;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { PluginOption } from "vite";
|
|
2
|
+
|
|
3
|
+
//#region src/vite-plugin/index.d.ts
|
|
4
|
+
interface PluginOptions {
|
|
5
|
+
alias: string;
|
|
6
|
+
dir: string;
|
|
7
|
+
dts: string | boolean;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Request插件
|
|
11
|
+
*/
|
|
12
|
+
declare const _default: (options: PluginOptions) => PluginOption;
|
|
13
|
+
//#endregion
|
|
14
|
+
export { _default as default };
|
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
import "
|
|
2
|
-
|
|
3
|
-
// src/vite-plugin/index.ts
|
|
4
|
-
import { execSync } from "node:child_process";
|
|
5
|
-
import * as fs from "node:fs";
|
|
6
|
-
import * as path from "node:path";
|
|
1
|
+
import * as fs$1 from "node:fs";
|
|
2
|
+
import * as path$1 from "node:path";
|
|
7
3
|
import process from "node:process";
|
|
8
4
|
import Handlebars from "handlebars";
|
|
5
|
+
import { execSync } from "node:child_process";
|
|
9
6
|
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
//#region src/vite-plugin/templates.ts
|
|
8
|
+
const useRequestFunctionCodeTemplate = `
|
|
12
9
|
export function useRequest(
|
|
13
10
|
...selects
|
|
14
11
|
) {
|
|
@@ -24,7 +21,7 @@ export function useRequest(
|
|
|
24
21
|
}
|
|
25
22
|
}
|
|
26
23
|
`;
|
|
27
|
-
|
|
24
|
+
const useRequestFunctionTypeTemplate = `
|
|
28
25
|
export function useRequest<T1,T2,T3,T4,T5,T6>(
|
|
29
26
|
select1: ((services: typeof serviceMap) => { new (): T1 }),
|
|
30
27
|
select2: ((services: typeof serviceMap) => { new (): T2 }),
|
|
@@ -62,7 +59,7 @@ export function useRequest<T>(
|
|
|
62
59
|
...selects: ((services: typeof serviceMap) => { new (): T })[]
|
|
63
60
|
): T | T[]
|
|
64
61
|
`;
|
|
65
|
-
|
|
62
|
+
const importServiceTemplate = `
|
|
66
63
|
{{#if groups}}
|
|
67
64
|
{{#each groups}}
|
|
68
65
|
{{#each services}}
|
|
@@ -75,7 +72,7 @@ import { {{{name}}} } from '{{path}}'
|
|
|
75
72
|
{{/each}}
|
|
76
73
|
{{/if}}
|
|
77
74
|
`;
|
|
78
|
-
|
|
75
|
+
const defineServiceMapTemplate = `
|
|
79
76
|
{{#if groups}}
|
|
80
77
|
const serviceMap = {
|
|
81
78
|
{{#each groups}}
|
|
@@ -94,14 +91,14 @@ const serviceMap = {
|
|
|
94
91
|
}
|
|
95
92
|
{{/if}}
|
|
96
93
|
`;
|
|
97
|
-
|
|
94
|
+
const generateCodeTemplate = `
|
|
98
95
|
${importServiceTemplate.trim()}
|
|
99
96
|
|
|
100
97
|
${defineServiceMapTemplate.trim()}
|
|
101
98
|
|
|
102
99
|
${useRequestFunctionCodeTemplate.trim()}
|
|
103
100
|
`;
|
|
104
|
-
|
|
101
|
+
const generateDeclareTemplate = `declare module '{{{MODULE_ID}}}' {
|
|
105
102
|
${importServiceTemplate.trim()}
|
|
106
103
|
|
|
107
104
|
${defineServiceMapTemplate.trim()}
|
|
@@ -110,122 +107,127 @@ var generateDeclareTemplate = `declare module '{{{MODULE_ID}}}' {
|
|
|
110
107
|
}
|
|
111
108
|
`;
|
|
112
109
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
110
|
+
//#endregion
|
|
111
|
+
//#region src/vite-plugin/index.ts
|
|
112
|
+
const MODULE_ID = "virtual:request";
|
|
113
|
+
const DECLARATION_FILE = "request.d.ts";
|
|
114
|
+
let viteConfig;
|
|
115
|
+
let GerneratedCodeStr = "";
|
|
118
116
|
function genretateDeclareAndCode(options) {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
}
|
|
117
|
+
const services = getServiceItems(getServicePaths(options));
|
|
118
|
+
const groups = getServiceGroups(services);
|
|
119
|
+
if (services && services.length) {
|
|
120
|
+
generateCode(services, groups);
|
|
121
|
+
if (options.dts !== false) generateDeclare(services, groups, options);
|
|
122
|
+
}
|
|
126
123
|
}
|
|
127
124
|
function generateRequestCode() {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
FORCE_COLOR: "1"
|
|
133
|
-
}
|
|
134
|
-
});
|
|
125
|
+
execSync(`${path$1.resolve(viteConfig.root, "node_modules", ".bin", "request-generate")}`, { env: {
|
|
126
|
+
...process.env,
|
|
127
|
+
FORCE_COLOR: "1"
|
|
128
|
+
} });
|
|
135
129
|
}
|
|
130
|
+
/**
|
|
131
|
+
* Request插件
|
|
132
|
+
*/
|
|
136
133
|
var vite_plugin_default = (options) => {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
};
|
|
134
|
+
return {
|
|
135
|
+
name: "vite-plugin-vue-request",
|
|
136
|
+
enforce: "pre",
|
|
137
|
+
configResolved(config) {
|
|
138
|
+
viteConfig = config;
|
|
139
|
+
},
|
|
140
|
+
resolveId(id) {
|
|
141
|
+
return id === MODULE_ID ? MODULE_ID : void 0;
|
|
142
|
+
},
|
|
143
|
+
async buildStart() {
|
|
144
|
+
await generateRequestCode();
|
|
145
|
+
await genretateDeclareAndCode(options);
|
|
146
|
+
},
|
|
147
|
+
load(id) {
|
|
148
|
+
if (id !== MODULE_ID) return;
|
|
149
|
+
return GerneratedCodeStr;
|
|
150
|
+
}
|
|
151
|
+
};
|
|
156
152
|
};
|
|
153
|
+
/**
|
|
154
|
+
* 生成服务路径
|
|
155
|
+
* @param options PluginOptions
|
|
156
|
+
* @returns string[]
|
|
157
|
+
*/
|
|
157
158
|
function getServicePaths(options) {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
};
|
|
171
|
-
const { replacement } = viteConfig.resolve.alias.find(
|
|
172
|
-
(alias) => alias.find === options.alias
|
|
173
|
-
);
|
|
174
|
-
if (fs.existsSync(path.resolve(options.dir))) {
|
|
175
|
-
walk(path.resolve(options.dir));
|
|
176
|
-
}
|
|
177
|
-
return paths.map(
|
|
178
|
-
(filepath) => filepath.replace(replacement.replace(/\\/g, "/"), options.alias)
|
|
179
|
-
);
|
|
159
|
+
const paths = [];
|
|
160
|
+
const walk = (dir) => {
|
|
161
|
+
fs$1.readdirSync(dir).forEach((file) => {
|
|
162
|
+
const fullpath = path$1.join(dir, file).replace(/\\/g, "/");
|
|
163
|
+
const stat = fs$1.statSync(fullpath);
|
|
164
|
+
if (stat.isFile() && fullpath.endsWith("Service.ts")) paths.push(fullpath);
|
|
165
|
+
else if (stat.isDirectory()) walk(path$1.join(dir, file));
|
|
166
|
+
});
|
|
167
|
+
};
|
|
168
|
+
const { replacement } = viteConfig.resolve.alias.find((alias) => alias.find === options.alias);
|
|
169
|
+
if (fs$1.existsSync(path$1.resolve(options.dir))) walk(path$1.resolve(options.dir));
|
|
170
|
+
return paths.map((filepath) => filepath.replace(replacement.replace(/\\/g, "/"), options.alias));
|
|
180
171
|
}
|
|
172
|
+
/**
|
|
173
|
+
* 生成服务项
|
|
174
|
+
* @param paths
|
|
175
|
+
* @returns ServiceItem[]
|
|
176
|
+
*/
|
|
181
177
|
function getServiceItems(paths) {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
178
|
+
const toCaseString = (str = "") => str.replace(/-(\w)/g, (_, $1) => $1.toUpperCase()).replace(/^\S/, (s) => s.toUpperCase());
|
|
179
|
+
return paths.map((filePath) => {
|
|
180
|
+
const [name] = /[^\\]+(?=\.ts$)/.exec(toCaseString(path$1.basename(filePath))) || [];
|
|
181
|
+
const [group] = /(?<=^.\/http\/)(.*?)(?=\/.*?Service\.ts$)/.exec(filePath) || [];
|
|
182
|
+
return {
|
|
183
|
+
name: toCaseString(name),
|
|
184
|
+
group: group !== "services" ? toCaseString(group) : void 0,
|
|
185
|
+
path: filePath.replace(/\.ts$/g, "")
|
|
186
|
+
};
|
|
187
|
+
});
|
|
192
188
|
}
|
|
189
|
+
/**
|
|
190
|
+
* 获取分组列表
|
|
191
|
+
* @param services
|
|
192
|
+
* @returns GroupItem[]
|
|
193
|
+
*/
|
|
193
194
|
function getServiceGroups(services) {
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
r.push(group);
|
|
205
|
-
}
|
|
206
|
-
return r;
|
|
207
|
-
}, []);
|
|
195
|
+
return services.reduce((r, s) => {
|
|
196
|
+
if (!s.group) return r;
|
|
197
|
+
const group = r.find((x) => x.name === s.group) || {
|
|
198
|
+
name: s.group,
|
|
199
|
+
services: []
|
|
200
|
+
};
|
|
201
|
+
group.services.push(s);
|
|
202
|
+
if (!r.find((g) => g === group)) r.push(group);
|
|
203
|
+
return r;
|
|
204
|
+
}, []);
|
|
208
205
|
}
|
|
206
|
+
/**
|
|
207
|
+
* 生成代码
|
|
208
|
+
* @param services
|
|
209
|
+
* @param groups
|
|
210
|
+
*/
|
|
209
211
|
function generateCode(services, groups) {
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
});
|
|
212
|
+
GerneratedCodeStr = Handlebars.compile(generateCodeTemplate)({
|
|
213
|
+
groups: groups.length ? groups : void 0,
|
|
214
|
+
services
|
|
215
|
+
});
|
|
215
216
|
}
|
|
217
|
+
/**
|
|
218
|
+
* 生成代码
|
|
219
|
+
* @param services
|
|
220
|
+
* @param groups
|
|
221
|
+
* @param options
|
|
222
|
+
*/
|
|
216
223
|
function generateDeclare(services, groups, options) {
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
fs.writeFileSync(
|
|
224
|
-
path.resolve(viteConfig.root, options.dts ?? DECLARATION_FILE),
|
|
225
|
-
content.replace(/\r\n/g, "\n"),
|
|
226
|
-
"utf-8"
|
|
227
|
-
);
|
|
224
|
+
const content = Handlebars.compile(generateDeclareTemplate)({
|
|
225
|
+
groups: groups.length ? groups : void 0,
|
|
226
|
+
services,
|
|
227
|
+
MODULE_ID
|
|
228
|
+
});
|
|
229
|
+
fs$1.writeFileSync(path$1.resolve(viteConfig.root, typeof options.dts === "string" ? options.dts : DECLARATION_FILE), content.replace(/\r\n/g, "\n"), "utf-8");
|
|
228
230
|
}
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
};
|
|
231
|
+
|
|
232
|
+
//#endregion
|
|
233
|
+
export { vite_plugin_default as default };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gopowerteam/request-generate",
|
|
3
3
|
"type": "commonjs",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.3.0",
|
|
5
5
|
"private": false,
|
|
6
6
|
"keywords": [
|
|
7
7
|
"gopowerteam",
|
|
@@ -9,19 +9,19 @@
|
|
|
9
9
|
],
|
|
10
10
|
"exports": {
|
|
11
11
|
".": {
|
|
12
|
-
"types": "./dist/index.d.
|
|
12
|
+
"types": "./dist/index.d.mts",
|
|
13
13
|
"import": "./dist/index.mjs",
|
|
14
|
-
"require": "./dist/index.
|
|
14
|
+
"require": "./dist/index.cjs"
|
|
15
15
|
},
|
|
16
16
|
"./vite-plugin": {
|
|
17
|
-
"types": "./dist/vite-plugin/index.d.
|
|
17
|
+
"types": "./dist/vite-plugin/index.d.mts",
|
|
18
18
|
"import": "./dist/vite-plugin/index.mjs",
|
|
19
|
-
"require": "./dist/vite-plugin/index.
|
|
19
|
+
"require": "./dist/vite-plugin/index.cjs"
|
|
20
20
|
}
|
|
21
21
|
},
|
|
22
|
-
"main": "./dist/index.
|
|
22
|
+
"main": "./dist/index.cjs",
|
|
23
23
|
"module": "./dist/index.mjs",
|
|
24
|
-
"types": "./dist/index.d.
|
|
24
|
+
"types": "./dist/index.d.mts",
|
|
25
25
|
"bin": {
|
|
26
26
|
"request-download": "bin/download.mts",
|
|
27
27
|
"request-generate": "bin/generate.mts"
|
|
@@ -37,33 +37,33 @@
|
|
|
37
37
|
"access": "public"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@apidevtools/swagger-parser": "^
|
|
41
|
-
"chalk": "^5.
|
|
42
|
-
"commander": "^
|
|
40
|
+
"@apidevtools/swagger-parser": "^12.1.0",
|
|
41
|
+
"chalk": "^5.6.2",
|
|
42
|
+
"commander": "^14.0.3",
|
|
43
43
|
"handlebars": "^4.7.8",
|
|
44
|
-
"ora": "^
|
|
45
|
-
"qs": "^6.
|
|
46
|
-
"rimraf": "^
|
|
47
|
-
"rxjs": "^7.8.
|
|
48
|
-
"tsx": "^4.
|
|
44
|
+
"ora": "^9.3.0",
|
|
45
|
+
"qs": "^6.15.0",
|
|
46
|
+
"rimraf": "^6.1.3",
|
|
47
|
+
"rxjs": "^7.8.2",
|
|
48
|
+
"tsx": "^4.21.0"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
|
-
"@types/
|
|
52
|
-
"@types/
|
|
53
|
-
"@types/qs": "^6.9.7",
|
|
51
|
+
"@types/node": "24",
|
|
52
|
+
"@types/qs": "^6.14.0",
|
|
54
53
|
"@types/rimraf": "^3.0.2",
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
"vite": "^4.1.4",
|
|
62
|
-
"@gopowerteam/request": "0.1.20"
|
|
54
|
+
"openapi-types": "^12.1.3",
|
|
55
|
+
"tsdown": "0.21.0-beta.2",
|
|
56
|
+
"typescript": "^5.9.3",
|
|
57
|
+
"vite": "^7.3.1",
|
|
58
|
+
"vitest": "^3.0.0",
|
|
59
|
+
"@gopowerteam/request": "0.2.0"
|
|
63
60
|
},
|
|
64
61
|
"scripts": {
|
|
65
|
-
"dev": "
|
|
66
|
-
"build": "
|
|
67
|
-
"lint": "eslint . --fix"
|
|
62
|
+
"dev": "tsdown --watch",
|
|
63
|
+
"build": "tsdown",
|
|
64
|
+
"lint": "eslint . --fix",
|
|
65
|
+
"test": "vitest run",
|
|
66
|
+
"test:watch": "vitest",
|
|
67
|
+
"test:coverage": "vitest run --coverage"
|
|
68
68
|
}
|
|
69
69
|
}
|
package/dist/chunk-WF3XBEPN.mjs
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
-
var __publicField = (obj, key, value) => {
|
|
4
|
-
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
|
-
return value;
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
// ../../node_modules/.pnpm/tsup@6.6.3_postcss@8.5.3_ts-node@10.9.1_@types+node@20.16.5_typescript@4.9.5__typescript@4.9.5/node_modules/tsup/assets/esm_shims.js
|
|
9
|
-
import { fileURLToPath } from "url";
|
|
10
|
-
import path from "path";
|
|
11
|
-
var getFilename = () => fileURLToPath(import.meta.url);
|
|
12
|
-
var getDirname = () => path.dirname(getFilename());
|
|
13
|
-
var __dirname = /* @__PURE__ */ getDirname();
|
|
14
|
-
|
|
15
|
-
export {
|
|
16
|
-
__publicField,
|
|
17
|
-
__dirname
|
|
18
|
-
};
|