nuxt-gin-tools 0.1.7 → 0.1.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-gin-tools",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "description": "This project is used as a dependency for [nuxt-gin-starter](https://github.com/RapboyGao/nuxt-gin-starter.git)",
5
5
  "bin": {
6
6
  "nuxt-gin": "index.js"
@@ -63,7 +63,7 @@ function setVueBaseUrl() {
63
63
  // 将修改后的内容写回文件
64
64
  yield fs_extra_1.default.outputFile(VUE_API_RUNTIME_PATH, updatedContent, "utf-8");
65
65
  console.log("成功更新Vue API基础URL为相对路径");
66
- return originalContent;
66
+ return;
67
67
  }
68
68
  catch (error) {
69
69
  console.error("更新Vue API基础URL失败:", error);
@@ -71,6 +71,15 @@ function setVueBaseUrl() {
71
71
  }
72
72
  });
73
73
  }
74
+ function setGoRoutes() {
75
+ return __awaiter(this, void 0, void 0, function* () {
76
+ const GO_API_RUNTIME_PATH = path_1.default.join(cwd, "server/api/routers.go");
77
+ const originalContent = yield fs_extra_1.default.readFile(GO_API_RUNTIME_PATH, "utf-8");
78
+ const updatedContent = originalContent.replace(/getRoutes/g, "GetRoutes");
79
+ // overwrite
80
+ yield fs_extra_1.default.outputFile(GO_API_RUNTIME_PATH, updatedContent, "utf-8");
81
+ });
82
+ }
74
83
  /**
75
84
  * 删除指定的路径
76
85
  * 用于清理生成过程中产生的临时或不需要的文件和目录
@@ -105,8 +114,11 @@ function apiGenerate() {
105
114
  // 并发执行命令列表中的所有命令,等待所有命令完成
106
115
  yield (0, concurrently_1.default)(commands).result;
107
116
  // 按顺序执行清理和配置任务
108
- yield removePaths();
109
- yield setVueBaseUrl();
117
+ let tasks = [];
118
+ tasks.push(removePaths());
119
+ tasks.push(setVueBaseUrl());
120
+ tasks.push(setGoRoutes());
121
+ yield Promise.all(tasks);
110
122
  console.log(chalk_1.default.bgGreen("API代码生成和配置完成!"));
111
123
  }
112
124
  catch (error) {
package/src/generate.d.ts DELETED
@@ -1,11 +0,0 @@
1
- /**
2
- * 警告:本文件是API生成框架的一部分,请勿手动修改!
3
- * 任何修改可能会被自动化流程覆盖。
4
- * 如需调整生成逻辑,请修改相关配置文件或脚本。
5
- */
6
- /**
7
- * 主函数
8
- * 协调执行所有任务:生成代码、删除路径、配置Vue API
9
- */
10
- export declare function apiGenerate(): Promise<void>;
11
- export default apiGenerate;
package/src/generate.js DELETED
@@ -1,121 +0,0 @@
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;