nuxt-gin-tools 0.0.6 → 0.0.7
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/index.js +2 -2
- package/package.json +1 -1
- package/src/api-generate.d.ts +11 -0
- package/src/api-generate.js +121 -0
- package/src/pack.js +12 -10
package/index.js
CHANGED
|
@@ -10,7 +10,7 @@ const pack_1 = __importDefault(require("./src/pack"));
|
|
|
10
10
|
// 导入开发模式功能
|
|
11
11
|
const develop_1 = __importDefault(require("./src/develop"));
|
|
12
12
|
// 导入API生成功能
|
|
13
|
-
const
|
|
13
|
+
const api_generate_1 = __importDefault(require("./src/api-generate"));
|
|
14
14
|
// 导入安装后处理功能
|
|
15
15
|
const postinstall_1 = __importDefault(require("./src/postinstall"));
|
|
16
16
|
// 获取命令行参数(去除前两个默认参数)
|
|
@@ -32,7 +32,7 @@ switch (args[0]) {
|
|
|
32
32
|
break;
|
|
33
33
|
case "gen":
|
|
34
34
|
// 生成API代码(注:此处命令可能拼写错误,应为generate)
|
|
35
|
-
(0,
|
|
35
|
+
(0, api_generate_1.default)();
|
|
36
36
|
break;
|
|
37
37
|
case "install":
|
|
38
38
|
// 执行安装后的初始化操作
|
package/package.json
CHANGED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* 警告:本文件是API生成框架的一部分,请勿手动修改!
|
|
4
|
+
* 任何修改可能会被自动化流程覆盖。
|
|
5
|
+
* 如需调整生成逻辑,请修改相关配置文件或脚本。
|
|
6
|
+
*/
|
|
7
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
8
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
9
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
10
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
11
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
12
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
13
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.apiGenerate = apiGenerate;
|
|
21
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
22
|
+
const concurrently_1 = __importDefault(require("concurrently")); // 用于并发执行命令的工具
|
|
23
|
+
const fs_extra_1 = __importDefault(require("fs-extra")); // 文件系统操作工具,提供更便捷的API
|
|
24
|
+
const path_1 = __importDefault(require("path")); // 处理和转换文件路径的工具
|
|
25
|
+
const cwd = process.cwd(); // 获取当前工作目录
|
|
26
|
+
/**
|
|
27
|
+
* 要执行的命令列表
|
|
28
|
+
* 包含OpenAPI代码生成命令:
|
|
29
|
+
* 1. 生成Go Gin服务器代码
|
|
30
|
+
* 2. 生成TypeScript Axios客户端代码
|
|
31
|
+
*/
|
|
32
|
+
let commands = [
|
|
33
|
+
{
|
|
34
|
+
command: "openapi-generator-cli generate -i openapi.yaml -g go-gin-server -c node_modules/nuxt-gin-tools/src/go-gin-server.json -o .",
|
|
35
|
+
name: "go",
|
|
36
|
+
prefixColor: "green",
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
command: "openapi-generator-cli generate -i openapi.yaml -g typescript-axios -o vue/composables/api ",
|
|
40
|
+
name: "vue",
|
|
41
|
+
prefixColor: "blue",
|
|
42
|
+
},
|
|
43
|
+
];
|
|
44
|
+
/**
|
|
45
|
+
* 执行完成后需要删除的路径列表
|
|
46
|
+
*/
|
|
47
|
+
const pathsToDelete = ["api"];
|
|
48
|
+
/**
|
|
49
|
+
* 设置Vue API客户端的基础URL
|
|
50
|
+
* 修改TypeScript axios生成的base.ts文件,将BASE_PATH设置为相对路径
|
|
51
|
+
* @returns {Promise<string>} 返回原始文件内容的Promise
|
|
52
|
+
*/
|
|
53
|
+
function setVueBaseUrl() {
|
|
54
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
55
|
+
try {
|
|
56
|
+
// 构建Vue API运行时配置文件的完整路径
|
|
57
|
+
const VUE_API_RUNTIME_PATH = path_1.default.join(cwd, "vue/composables/api/base.ts");
|
|
58
|
+
// 读取原始文件内容
|
|
59
|
+
const originalContent = yield fs_extra_1.default.readFile(VUE_API_RUNTIME_PATH, "utf-8");
|
|
60
|
+
// 使用正则表达式替换BASE_PATH常量,移除协议和域名部分,使其成为相对路径
|
|
61
|
+
// 匹配类似 "export const BASE_PATH = "https://example.com"" 这样的行
|
|
62
|
+
const updatedContent = originalContent.replace(/export\s+const\s+BASE_PATH = "https?:\/\/[^/]+/, `export const BASE_PATH = "`);
|
|
63
|
+
// 将修改后的内容写回文件
|
|
64
|
+
yield fs_extra_1.default.outputFile(VUE_API_RUNTIME_PATH, updatedContent, "utf-8");
|
|
65
|
+
console.log("成功更新Vue API基础URL为相对路径");
|
|
66
|
+
return originalContent;
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
console.error("更新Vue API基础URL失败:", error);
|
|
70
|
+
throw error; // 将错误继续抛出,以便上层处理
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* 删除指定的路径
|
|
76
|
+
* 用于清理生成过程中产生的临时或不需要的文件和目录
|
|
77
|
+
*/
|
|
78
|
+
function removePaths() {
|
|
79
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
80
|
+
try {
|
|
81
|
+
// 遍历要删除的路径列表
|
|
82
|
+
for (const path of pathsToDelete) {
|
|
83
|
+
// 构建完整路径
|
|
84
|
+
const fullPath = path_1.default.join(cwd, path);
|
|
85
|
+
// 删除路径(文件或目录)
|
|
86
|
+
yield fs_extra_1.default.remove(fullPath);
|
|
87
|
+
console.log(`成功删除路径: ${fullPath}`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
console.error("删除路径失败:", error);
|
|
92
|
+
throw error; // 将错误继续抛出
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* 主函数
|
|
98
|
+
* 协调执行所有任务:生成代码、删除路径、配置Vue API
|
|
99
|
+
*/
|
|
100
|
+
function apiGenerate() {
|
|
101
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
102
|
+
try {
|
|
103
|
+
// 输出开始信息
|
|
104
|
+
console.log(chalk_1.default.bgGreen("开始生成API代码..."));
|
|
105
|
+
// 并发执行命令列表中的所有命令,等待所有命令完成
|
|
106
|
+
yield (0, concurrently_1.default)(commands).result;
|
|
107
|
+
// 按顺序执行清理和配置任务
|
|
108
|
+
yield removePaths();
|
|
109
|
+
yield setVueBaseUrl();
|
|
110
|
+
console.log(chalk_1.default.bgGreen("API代码生成和配置完成!"));
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
// 捕获并处理任何阶段发生的错误
|
|
114
|
+
console.error(chalk_1.default.red("执行过程中发生错误:"), error);
|
|
115
|
+
// 以错误码1退出进程,表示执行失败
|
|
116
|
+
process.exit(1);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
// 执行主函数
|
|
121
|
+
exports.default = apiGenerate;
|
package/src/pack.js
CHANGED
|
@@ -27,13 +27,13 @@ function p(...names) {
|
|
|
27
27
|
return path_1.default.join(cwd, ...names);
|
|
28
28
|
}
|
|
29
29
|
/** 打包文件夹相对于Workspace位置 */
|
|
30
|
-
const
|
|
30
|
+
const BUILD_DEST = ".build/server";
|
|
31
31
|
/** 需要打包的文件相对位置列表 */
|
|
32
32
|
const FILE_LIST = ["vue/.output", "tmp/production.exe", "ecosystem.config.js", "server.config.json"];
|
|
33
33
|
/** package.json 文件夹相对于Workspace位置 */
|
|
34
|
-
const PACKAGE_JSON =
|
|
34
|
+
const PACKAGE_JSON = BUILD_DEST + "/package.json";
|
|
35
35
|
/** 打包package.7z位置 */
|
|
36
|
-
const _7Z_PATH =
|
|
36
|
+
const _7Z_PATH = BUILD_DEST + "/../package.7z";
|
|
37
37
|
/** package.json 内容 */
|
|
38
38
|
const PACKAGE_JSON_CONTENT = {
|
|
39
39
|
private: true,
|
|
@@ -46,26 +46,28 @@ const PACKAGE_JSON_CONTENT = {
|
|
|
46
46
|
*/
|
|
47
47
|
function copyFiles() {
|
|
48
48
|
for (const path of FILE_LIST) {
|
|
49
|
-
|
|
49
|
+
const pathFrom = p(path);
|
|
50
|
+
const pathTo = p(BUILD_DEST, path);
|
|
51
|
+
fs_extra_1.default.copySync(pathFrom, pathTo);
|
|
50
52
|
}
|
|
51
53
|
}
|
|
52
54
|
function writeScriptFiles() {
|
|
53
|
-
fs_extra_1.default.outputFileSync(p(
|
|
54
|
-
fs_extra_1.default.outputFileSync(p(
|
|
55
|
-
fs_extra_1.default.outputFileSync(p(
|
|
55
|
+
fs_extra_1.default.outputFileSync(p(BUILD_DEST, "start.bat"), `powershell -ExecutionPolicy ByPass -File ./start.ps1`);
|
|
56
|
+
fs_extra_1.default.outputFileSync(p(BUILD_DEST, "start.ps1"), `./tmp/production.exe`);
|
|
57
|
+
fs_extra_1.default.outputFileSync(p(BUILD_DEST, "start.sh"), `./tmp/production.exe`);
|
|
56
58
|
}
|
|
57
59
|
/**
|
|
58
60
|
* 打包文件为7z格式
|
|
59
61
|
*/
|
|
60
62
|
function pack() {
|
|
61
63
|
return __awaiter(this, void 0, void 0, function* () {
|
|
62
|
-
fs_extra_1.default.removeSync(p(
|
|
64
|
+
fs_extra_1.default.removeSync(p(BUILD_DEST));
|
|
63
65
|
fs_extra_1.default.removeSync(p(_7Z_PATH));
|
|
64
66
|
copyFiles();
|
|
65
67
|
writeScriptFiles();
|
|
66
68
|
fs_extra_1.default.outputJSONSync(p(PACKAGE_JSON), PACKAGE_JSON_CONTENT, { spaces: 2 });
|
|
67
|
-
_7zip_min_1.default.pack(p(
|
|
68
|
-
fs_extra_1.default.removeSync(p(
|
|
69
|
+
_7zip_min_1.default.pack(p(BUILD_DEST), p(_7Z_PATH), () => {
|
|
70
|
+
fs_extra_1.default.removeSync(p(BUILD_DEST));
|
|
69
71
|
});
|
|
70
72
|
});
|
|
71
73
|
}
|