@gopowerteam/request-generate 0.2.0 → 0.2.2
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 +14 -12
- package/bin/generate.mts +18 -14
- package/dist/index.d.ts +1 -1
- package/dist/index.js +21 -13
- package/dist/index.mjs +21 -13
- package/dist/vite-plugin/index.js +7 -1
- package/dist/vite-plugin/index.mjs +7 -1
- package/package.json +1 -1
package/bin/download.mts
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
#!/usr/bin/env tsx
|
|
2
2
|
|
|
3
|
-
import path from 'node:path'
|
|
4
3
|
import fs from 'node:fs'
|
|
4
|
+
import path from 'node:path'
|
|
5
|
+
import process from 'node:process'
|
|
6
|
+
import { fileURLToPath } from 'node:url'
|
|
5
7
|
import { program } from 'commander'
|
|
6
|
-
import { fileURLToPath } from 'node:url';
|
|
7
8
|
|
|
8
|
-
const __filename = fileURLToPath(import.meta.url)
|
|
9
|
-
const __dirname = path.dirname(__filename)
|
|
9
|
+
const __filename = fileURLToPath(import.meta.url)
|
|
10
|
+
const __dirname = path.dirname(__filename)
|
|
10
11
|
const RequestGenerate = await import(`file://${path.resolve(
|
|
11
12
|
__dirname,
|
|
12
13
|
'..',
|
|
13
14
|
'dist',
|
|
14
|
-
'index.mjs'
|
|
15
|
+
'index.mjs',
|
|
15
16
|
)}`)
|
|
16
17
|
|
|
17
18
|
const params = program
|
|
@@ -24,30 +25,30 @@ const params = program
|
|
|
24
25
|
const configFilePaths = [
|
|
25
26
|
'request.config.ts',
|
|
26
27
|
'request-generate.config.cjs',
|
|
27
|
-
'request-generate.config.js'
|
|
28
|
+
'request-generate.config.js',
|
|
28
29
|
]
|
|
29
30
|
|
|
30
31
|
/**
|
|
31
32
|
* 加载配置文件
|
|
32
33
|
* @param {*} filePath
|
|
33
|
-
* @returns
|
|
34
34
|
*/
|
|
35
35
|
async function loadConfigFile(filePath) {
|
|
36
36
|
if (filePath) {
|
|
37
37
|
configFilePaths.unshift(filePath)
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
const configFilePath = configFilePaths.find(
|
|
41
|
-
fs.existsSync(path.resolve(process.cwd(), file))
|
|
40
|
+
const configFilePath = configFilePaths.find(file =>
|
|
41
|
+
fs.existsSync(path.resolve(process.cwd(), file)),
|
|
42
42
|
)
|
|
43
43
|
|
|
44
|
-
if(!configFilePath){
|
|
45
|
-
throw new Error(
|
|
44
|
+
if (!configFilePath) {
|
|
45
|
+
throw new Error('Not Found Config File.')
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
if (configFilePath.endsWith('.js') || configFilePath.endsWith('.ts')) {
|
|
49
49
|
return import(`file://${path.resolve(process.cwd(), configFilePath)}`)
|
|
50
|
-
}
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
51
52
|
throw new Error('无法找到RequestGenerate配置文件')
|
|
52
53
|
}
|
|
53
54
|
}
|
|
@@ -57,6 +58,7 @@ if (RequestGenerate) {
|
|
|
57
58
|
|
|
58
59
|
RequestGenerate.download(config)
|
|
59
60
|
.then(() => {
|
|
61
|
+
// eslint-disable-next-line no-console
|
|
60
62
|
console.log('接口文件更新完成')
|
|
61
63
|
process.exit(0)
|
|
62
64
|
})
|
package/bin/generate.mts
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
#!/usr/bin/env tsx
|
|
2
2
|
|
|
3
|
-
import path from 'node:path'
|
|
4
3
|
import fs from 'node:fs'
|
|
4
|
+
import path from 'node:path'
|
|
5
|
+
import process from 'node:process'
|
|
6
|
+
import { fileURLToPath } from 'node:url'
|
|
5
7
|
import { program } from 'commander'
|
|
6
|
-
import { fileURLToPath } from 'node:url';
|
|
7
8
|
|
|
8
|
-
const __filename = fileURLToPath(import.meta.url)
|
|
9
|
-
const __dirname = path.dirname(__filename)
|
|
9
|
+
const __filename = fileURLToPath(import.meta.url)
|
|
10
|
+
const __dirname = path.dirname(__filename)
|
|
10
11
|
const RequestGenerate = await import(`file://${path.resolve(
|
|
11
12
|
__dirname,
|
|
12
13
|
'..',
|
|
13
14
|
'dist',
|
|
14
|
-
'index.mjs'
|
|
15
|
+
'index.mjs',
|
|
15
16
|
)}`)
|
|
16
17
|
|
|
17
18
|
const params = program
|
|
@@ -24,32 +25,33 @@ const params = program
|
|
|
24
25
|
const configFilePaths = [
|
|
25
26
|
'request.config.ts',
|
|
26
27
|
'request-generate.config.cjs',
|
|
27
|
-
'request-generate.config.js'
|
|
28
|
+
'request-generate.config.js',
|
|
28
29
|
]
|
|
29
30
|
|
|
30
31
|
/**
|
|
31
32
|
* 加载配置文件
|
|
32
33
|
* @param {*} filePath
|
|
33
|
-
* @returns
|
|
34
34
|
*/
|
|
35
35
|
async function loadConfigFile(filePath) {
|
|
36
36
|
if (filePath) {
|
|
37
37
|
configFilePaths.unshift(filePath)
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
const configFilePath = configFilePaths.find(
|
|
41
|
-
fs.existsSync(path.resolve(process.cwd(), file))
|
|
40
|
+
const configFilePath = configFilePaths.find(file =>
|
|
41
|
+
fs.existsSync(path.resolve(process.cwd(), file)),
|
|
42
42
|
)
|
|
43
43
|
|
|
44
|
-
if(!configFilePath){
|
|
45
|
-
throw new Error(
|
|
44
|
+
if (!configFilePath) {
|
|
45
|
+
throw new Error('Not Find Config File.')
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
if (configFilePath.endsWith('js')) {
|
|
49
49
|
return import(`file://${path.resolve(process.cwd(), configFilePath)}`)
|
|
50
|
-
}
|
|
50
|
+
}
|
|
51
|
+
else if (configFilePath.endsWith('ts')) {
|
|
51
52
|
return import(`file://${path.resolve(process.cwd(), configFilePath)}`)
|
|
52
|
-
}
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
53
55
|
throw new Error('无法找到RequestGenerate配置文件')
|
|
54
56
|
}
|
|
55
57
|
}
|
|
@@ -59,9 +61,11 @@ if (RequestGenerate) {
|
|
|
59
61
|
|
|
60
62
|
try {
|
|
61
63
|
await RequestGenerate.generate(config)
|
|
64
|
+
// eslint-disable-next-line no-console
|
|
62
65
|
console.log('接口文件生成完成')
|
|
63
66
|
process.exit(0)
|
|
64
|
-
}
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
65
69
|
console.error(error)
|
|
66
70
|
process.exit(1)
|
|
67
71
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -137,7 +137,7 @@ declare class Generate {
|
|
|
137
137
|
* @returns Promise<void>
|
|
138
138
|
*/
|
|
139
139
|
static startup(options: GenerateOptions): Promise<void>;
|
|
140
|
-
static getApiDocument(
|
|
140
|
+
static getApiDocument(input: string): Promise<UnkownVersionDocument>;
|
|
141
141
|
/**
|
|
142
142
|
* 生成应用
|
|
143
143
|
*/
|
package/dist/index.js
CHANGED
|
@@ -982,11 +982,11 @@ var _crypto = require('crypto'); var _crypto2 = _interopRequireDefault(_crypto);
|
|
|
982
982
|
|
|
983
983
|
var ConfigFileName = ".config.json";
|
|
984
984
|
var ConfigFilePath = path2.default.resolve(".request", ConfigFileName);
|
|
985
|
-
function updateOptionsFromLocalConfig(options) {
|
|
985
|
+
function updateOptionsFromLocalConfig(options, output) {
|
|
986
986
|
const config = readLocalConfig();
|
|
987
987
|
const toUpdateOptions = /* @__PURE__ */ new Map();
|
|
988
988
|
options = options.filter((item) => {
|
|
989
|
-
const md5 =
|
|
989
|
+
const md5 = isNeedUpdate(item.name, config, output);
|
|
990
990
|
if (md5) {
|
|
991
991
|
toUpdateOptions.set(item.name, md5);
|
|
992
992
|
return true;
|
|
@@ -994,18 +994,28 @@ function updateOptionsFromLocalConfig(options) {
|
|
|
994
994
|
return false;
|
|
995
995
|
}
|
|
996
996
|
});
|
|
997
|
+
options.forEach((item) => {
|
|
998
|
+
const file = path2.default.resolve(".request", `${item.name}.json`);
|
|
999
|
+
item.input = file;
|
|
1000
|
+
});
|
|
997
1001
|
writeLocalConfig(toUpdateOptions, config);
|
|
998
1002
|
return options;
|
|
999
1003
|
}
|
|
1000
|
-
function
|
|
1004
|
+
function isNeedUpdate(name, config, output) {
|
|
1001
1005
|
const file = path2.default.resolve(".request", `${name}.json`);
|
|
1006
|
+
const outputDir = path2.default.resolve(output);
|
|
1002
1007
|
if (!fs.default.existsSync(file)) {
|
|
1003
1008
|
throw new Error(`\u672A\u627E\u5230\u76F8\u5E94\u7684\u914D\u7F6E\u6587\u4EF6: ${name}.json`);
|
|
1004
1009
|
}
|
|
1005
1010
|
const data = fs.default.readFileSync(file);
|
|
1006
1011
|
const md5 = _crypto2.default.createHash("md5").update(data.toString()).digest("hex");
|
|
1012
|
+
if (!fs.default.existsSync(path2.default.join(outputDir, name))) {
|
|
1013
|
+
return md5;
|
|
1014
|
+
}
|
|
1007
1015
|
const configItem = config.find((x) => x.name === name);
|
|
1008
|
-
|
|
1016
|
+
if (!configItem || configItem.md5 !== md5) {
|
|
1017
|
+
return md5;
|
|
1018
|
+
}
|
|
1009
1019
|
}
|
|
1010
1020
|
function readLocalConfig() {
|
|
1011
1021
|
if (!fs.default.existsSync(ConfigFilePath)) {
|
|
@@ -1113,7 +1123,7 @@ var _Generate = class {
|
|
|
1113
1123
|
_Generate.options = options;
|
|
1114
1124
|
registerHandlebarTemplates();
|
|
1115
1125
|
let applicationOptions = generateServiceOptions(options);
|
|
1116
|
-
applicationOptions = updateOptionsFromLocalConfig(applicationOptions);
|
|
1126
|
+
applicationOptions = updateOptionsFromLocalConfig(applicationOptions, options.output);
|
|
1117
1127
|
const applications = [];
|
|
1118
1128
|
for (const applicationOption of applicationOptions) {
|
|
1119
1129
|
const client = await _Generate.generateApplicationClient(applicationOption);
|
|
@@ -1127,17 +1137,15 @@ var _Generate = class {
|
|
|
1127
1137
|
_Generate.writeClient(application.client, application.options);
|
|
1128
1138
|
});
|
|
1129
1139
|
}
|
|
1130
|
-
static async getApiDocument(
|
|
1131
|
-
if (retry >= 3) {
|
|
1132
|
-
console.error(`\u8BF7\u6C42[${url}]\u5931\u8D25,\u8BF7\u7A0D\u540E\u91CD\u8BD5.`);
|
|
1133
|
-
_process2.default.exit(0);
|
|
1134
|
-
}
|
|
1140
|
+
static async getApiDocument(input) {
|
|
1135
1141
|
try {
|
|
1136
|
-
|
|
1137
|
-
|
|
1142
|
+
const document = await getOpenApiDocument(
|
|
1143
|
+
input
|
|
1138
1144
|
);
|
|
1145
|
+
return document;
|
|
1139
1146
|
} catch (e) {
|
|
1140
|
-
|
|
1147
|
+
console.error(`\u8BF7\u6C42\u6587\u4EF6[${input}]\u5931\u8D25,\u8BF7\u7A0D\u540E\u91CD\u8BD5.`);
|
|
1148
|
+
_process2.default.exit(0);
|
|
1141
1149
|
}
|
|
1142
1150
|
}
|
|
1143
1151
|
/**
|
package/dist/index.mjs
CHANGED
|
@@ -983,11 +983,11 @@ import fs3 from "node:fs";
|
|
|
983
983
|
import path4 from "node:path";
|
|
984
984
|
var ConfigFileName = ".config.json";
|
|
985
985
|
var ConfigFilePath = path4.resolve(".request", ConfigFileName);
|
|
986
|
-
function updateOptionsFromLocalConfig(options) {
|
|
986
|
+
function updateOptionsFromLocalConfig(options, output) {
|
|
987
987
|
const config = readLocalConfig();
|
|
988
988
|
const toUpdateOptions = /* @__PURE__ */ new Map();
|
|
989
989
|
options = options.filter((item) => {
|
|
990
|
-
const md5 =
|
|
990
|
+
const md5 = isNeedUpdate(item.name, config, output);
|
|
991
991
|
if (md5) {
|
|
992
992
|
toUpdateOptions.set(item.name, md5);
|
|
993
993
|
return true;
|
|
@@ -995,18 +995,28 @@ function updateOptionsFromLocalConfig(options) {
|
|
|
995
995
|
return false;
|
|
996
996
|
}
|
|
997
997
|
});
|
|
998
|
+
options.forEach((item) => {
|
|
999
|
+
const file = path4.resolve(".request", `${item.name}.json`);
|
|
1000
|
+
item.input = file;
|
|
1001
|
+
});
|
|
998
1002
|
writeLocalConfig(toUpdateOptions, config);
|
|
999
1003
|
return options;
|
|
1000
1004
|
}
|
|
1001
|
-
function
|
|
1005
|
+
function isNeedUpdate(name, config, output) {
|
|
1002
1006
|
const file = path4.resolve(".request", `${name}.json`);
|
|
1007
|
+
const outputDir = path4.resolve(output);
|
|
1003
1008
|
if (!fs3.existsSync(file)) {
|
|
1004
1009
|
throw new Error(`\u672A\u627E\u5230\u76F8\u5E94\u7684\u914D\u7F6E\u6587\u4EF6: ${name}.json`);
|
|
1005
1010
|
}
|
|
1006
1011
|
const data = fs3.readFileSync(file);
|
|
1007
1012
|
const md5 = crypto.createHash("md5").update(data.toString()).digest("hex");
|
|
1013
|
+
if (!fs3.existsSync(path4.join(outputDir, name))) {
|
|
1014
|
+
return md5;
|
|
1015
|
+
}
|
|
1008
1016
|
const configItem = config.find((x) => x.name === name);
|
|
1009
|
-
|
|
1017
|
+
if (!configItem || configItem.md5 !== md5) {
|
|
1018
|
+
return md5;
|
|
1019
|
+
}
|
|
1010
1020
|
}
|
|
1011
1021
|
function readLocalConfig() {
|
|
1012
1022
|
if (!fs3.existsSync(ConfigFilePath)) {
|
|
@@ -1114,7 +1124,7 @@ var _Generate = class {
|
|
|
1114
1124
|
_Generate.options = options;
|
|
1115
1125
|
registerHandlebarTemplates();
|
|
1116
1126
|
let applicationOptions = generateServiceOptions(options);
|
|
1117
|
-
applicationOptions = updateOptionsFromLocalConfig(applicationOptions);
|
|
1127
|
+
applicationOptions = updateOptionsFromLocalConfig(applicationOptions, options.output);
|
|
1118
1128
|
const applications = [];
|
|
1119
1129
|
for (const applicationOption of applicationOptions) {
|
|
1120
1130
|
const client = await _Generate.generateApplicationClient(applicationOption);
|
|
@@ -1128,17 +1138,15 @@ var _Generate = class {
|
|
|
1128
1138
|
_Generate.writeClient(application.client, application.options);
|
|
1129
1139
|
});
|
|
1130
1140
|
}
|
|
1131
|
-
static async getApiDocument(
|
|
1132
|
-
if (retry >= 3) {
|
|
1133
|
-
console.error(`\u8BF7\u6C42[${url}]\u5931\u8D25,\u8BF7\u7A0D\u540E\u91CD\u8BD5.`);
|
|
1134
|
-
process.exit(0);
|
|
1135
|
-
}
|
|
1141
|
+
static async getApiDocument(input) {
|
|
1136
1142
|
try {
|
|
1137
|
-
|
|
1138
|
-
|
|
1143
|
+
const document = await getOpenApiDocument(
|
|
1144
|
+
input
|
|
1139
1145
|
);
|
|
1146
|
+
return document;
|
|
1140
1147
|
} catch {
|
|
1141
|
-
|
|
1148
|
+
console.error(`\u8BF7\u6C42\u6587\u4EF6[${input}]\u5931\u8D25,\u8BF7\u7A0D\u540E\u91CD\u8BD5.`);
|
|
1149
|
+
process.exit(0);
|
|
1142
1150
|
}
|
|
1143
1151
|
}
|
|
1144
1152
|
/**
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
var _child_process = require('child_process');
|
|
5
5
|
var _fs = require('fs'); var fs = _interopRequireWildcard(_fs);
|
|
6
6
|
var _path = require('path'); var path = _interopRequireWildcard(_path);
|
|
7
|
+
var _process = require('process'); var _process2 = _interopRequireDefault(_process);
|
|
7
8
|
var _handlebars = require('handlebars'); var _handlebars2 = _interopRequireDefault(_handlebars);
|
|
8
9
|
|
|
9
10
|
// src/vite-plugin/templates.ts
|
|
@@ -125,7 +126,12 @@ function genretateDeclareAndCode(options) {
|
|
|
125
126
|
}
|
|
126
127
|
function generateRequestCode() {
|
|
127
128
|
const generateScript = path.resolve(viteConfig.root, "node_modules", ".bin", "request-generate");
|
|
128
|
-
_child_process.execSync.call(void 0, `${generateScript}
|
|
129
|
+
_child_process.execSync.call(void 0, `${generateScript}`, {
|
|
130
|
+
env: {
|
|
131
|
+
..._process2.default.env,
|
|
132
|
+
FORCE_COLOR: "1"
|
|
133
|
+
}
|
|
134
|
+
});
|
|
129
135
|
}
|
|
130
136
|
var vite_plugin_default = (options) => {
|
|
131
137
|
return {
|
|
@@ -4,6 +4,7 @@ import "../chunk-WF3XBEPN.mjs";
|
|
|
4
4
|
import { execSync } from "node:child_process";
|
|
5
5
|
import * as fs from "node:fs";
|
|
6
6
|
import * as path from "node:path";
|
|
7
|
+
import process from "node:process";
|
|
7
8
|
import Handlebars from "handlebars";
|
|
8
9
|
|
|
9
10
|
// src/vite-plugin/templates.ts
|
|
@@ -125,7 +126,12 @@ function genretateDeclareAndCode(options) {
|
|
|
125
126
|
}
|
|
126
127
|
function generateRequestCode() {
|
|
127
128
|
const generateScript = path.resolve(viteConfig.root, "node_modules", ".bin", "request-generate");
|
|
128
|
-
execSync(`${generateScript}
|
|
129
|
+
execSync(`${generateScript}`, {
|
|
130
|
+
env: {
|
|
131
|
+
...process.env,
|
|
132
|
+
FORCE_COLOR: "1"
|
|
133
|
+
}
|
|
134
|
+
});
|
|
129
135
|
}
|
|
130
136
|
var vite_plugin_default = (options) => {
|
|
131
137
|
return {
|