nuxt-gin-tools 0.2.1 → 0.2.3
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/README.md +123 -11
- package/package.json +1 -1
- package/src/dev-go.d.ts +8 -1
- package/src/dev-go.js +34 -7
- package/src/develop.d.ts +7 -0
- package/src/develop.js +12 -18
package/README.md
CHANGED
|
@@ -1,23 +1,135 @@
|
|
|
1
1
|
# nuxt-gin-tools
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
`nuxt-gin-tools` 是 `nuxt-gin-starter` 的配套开发工具包,提供 Nuxt + Gin 项目的统一命令入口。
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
核心目标:
|
|
6
|
+
- 一条命令启动前后端开发环境
|
|
7
|
+
- 自动处理 Go 侧依赖和文件监听重启
|
|
8
|
+
- 提供 OpenAPI 代码生成与构建打包辅助
|
|
6
9
|
|
|
7
|
-
|
|
10
|
+
## 功能概览
|
|
8
11
|
|
|
9
|
-
|
|
12
|
+
- `nuxt-gin dev`
|
|
13
|
+
- 启动 Nuxt 开发服务
|
|
14
|
+
- 启动 Go 文件监听(基于 `chokidar`)
|
|
15
|
+
- Go 文件变化后自动重启 `go run main.go`
|
|
16
|
+
- `nuxt-gin install`
|
|
17
|
+
- 执行 Nuxt prepare
|
|
18
|
+
- 如果检测到 Go,则执行 `go mod download && go mod tidy`
|
|
19
|
+
- `nuxt-gin gen`
|
|
20
|
+
- 基于 `openapi.yaml` 生成 Go / TS API 代码
|
|
21
|
+
- `nuxt-gin build`
|
|
22
|
+
- 执行项目构建与打包流程
|
|
23
|
+
- `nuxt-gin cleanup`
|
|
24
|
+
- 清理开发产物
|
|
25
|
+
- `nuxt-gin update`
|
|
26
|
+
- 执行依赖更新流程
|
|
10
27
|
|
|
11
|
-
|
|
28
|
+
## 安装
|
|
12
29
|
|
|
13
|
-
|
|
30
|
+
在 `nuxt-gin-starter` 项目中作为依赖安装:
|
|
14
31
|
|
|
15
|
-
|
|
32
|
+
```bash
|
|
33
|
+
pnpm add nuxt-gin-tools
|
|
34
|
+
```
|
|
16
35
|
|
|
17
|
-
##
|
|
36
|
+
## 快速开始
|
|
18
37
|
|
|
19
|
-
|
|
38
|
+
```bash
|
|
39
|
+
# 初始化依赖(可选)
|
|
40
|
+
nuxt-gin install
|
|
20
41
|
|
|
21
|
-
|
|
42
|
+
# 启动开发
|
|
43
|
+
nuxt-gin dev
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## 命令说明
|
|
47
|
+
|
|
48
|
+
### `nuxt-gin dev`
|
|
49
|
+
|
|
50
|
+
开发模式下会并行运行:
|
|
51
|
+
- Nuxt:`npx nuxt dev --port=<nuxtPort> --host`
|
|
52
|
+
- Go:监听变更并运行 `go run main.go`
|
|
53
|
+
|
|
54
|
+
Go 监听规则来自 `.go-watch.json`(见下文)。
|
|
55
|
+
|
|
56
|
+
### `nuxt-gin install`
|
|
57
|
+
|
|
58
|
+
- 总是执行:`npx nuxt prepare`
|
|
59
|
+
- 检测到 Go 时额外执行:`go mod download && go mod tidy`
|
|
60
|
+
|
|
61
|
+
### `nuxt-gin gen`
|
|
62
|
+
|
|
63
|
+
依赖 `openapi-generator-cli`,默认会:
|
|
64
|
+
- 生成 Go Gin server 相关代码
|
|
65
|
+
- 生成 TypeScript axios 客户端代码
|
|
66
|
+
|
|
67
|
+
### `nuxt-gin build`
|
|
68
|
+
|
|
69
|
+
执行工具链内置的构建与打包逻辑。
|
|
70
|
+
|
|
71
|
+
### `nuxt-gin cleanup`
|
|
72
|
+
|
|
73
|
+
清理由工具链生成的临时目录和产物。
|
|
74
|
+
|
|
75
|
+
### `nuxt-gin update`
|
|
76
|
+
|
|
77
|
+
执行项目约定的更新逻辑。
|
|
78
|
+
|
|
79
|
+
## 配置
|
|
80
|
+
|
|
81
|
+
### 1) `server.config.json`
|
|
82
|
+
|
|
83
|
+
`dev` 命令会读取该文件,常用字段:
|
|
84
|
+
|
|
85
|
+
- `ginPort`: Gin 服务端口
|
|
86
|
+
- `nuxtPort`: Nuxt 开发端口
|
|
87
|
+
- `baseUrl`: Nuxt baseUrl
|
|
88
|
+
- `killPortBeforeDevelop`: 开发前是否释放端口(默认 `true`)
|
|
89
|
+
- `cleanupBeforeDevelop`: 开发前是否执行 cleanup(默认 `false`)
|
|
90
|
+
|
|
91
|
+
### 2) `.go-watch.json`
|
|
92
|
+
|
|
93
|
+
Go 监听配置文件,示例:
|
|
94
|
+
|
|
95
|
+
```json
|
|
96
|
+
{
|
|
97
|
+
"tmpDir": ".build/.server",
|
|
98
|
+
"testDataDir": "testdata",
|
|
99
|
+
"includeExt": ["go", "tpl", "html"],
|
|
100
|
+
"includeDir": [],
|
|
101
|
+
"includeFile": [],
|
|
102
|
+
"excludeDir": [
|
|
103
|
+
"assets",
|
|
104
|
+
".build/.server",
|
|
105
|
+
"vendor",
|
|
106
|
+
"testdata",
|
|
107
|
+
"node_modules",
|
|
108
|
+
"vue",
|
|
109
|
+
"api",
|
|
110
|
+
".vscode",
|
|
111
|
+
".git"
|
|
112
|
+
],
|
|
113
|
+
"excludeFile": [],
|
|
114
|
+
"excludeRegex": ["_test.go"]
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
也支持环境变量指定路径:
|
|
119
|
+
|
|
120
|
+
```bash
|
|
121
|
+
NUXT_GIN_WATCH_CONFIG=/path/to/.go-watch.json
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## 环境依赖
|
|
125
|
+
|
|
126
|
+
- Node.js
|
|
127
|
+
- pnpm
|
|
128
|
+
- Go(需要运行 Gin 侧开发与依赖下载时)
|
|
129
|
+
- `openapi-generator-cli`(仅 `nuxt-gin gen` 需要)
|
|
130
|
+
|
|
131
|
+
## 说明
|
|
132
|
+
|
|
133
|
+
- Go 侧热更新已不再依赖 Air。
|
|
134
|
+
- 当前方案为:`chokidar` 监听文件变化 + 重启 `go run main.go`。
|
|
22
135
|
|
|
23
|
-
- Cleanup script.
|
package/package.json
CHANGED
package/src/dev-go.d.ts
CHANGED
package/src/dev-go.js
CHANGED
|
@@ -12,17 +12,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
12
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.startGoDev = startGoDev;
|
|
15
16
|
const child_process_1 = require("child_process");
|
|
16
17
|
const child_process_2 = require("child_process");
|
|
17
18
|
const chokidar_1 = __importDefault(require("chokidar"));
|
|
18
19
|
const chalk_1 = __importDefault(require("chalk"));
|
|
19
20
|
const fs_extra_1 = require("fs-extra");
|
|
20
21
|
const os_1 = __importDefault(require("os"));
|
|
22
|
+
const readline_1 = require("readline");
|
|
21
23
|
const path_1 = require("path");
|
|
22
24
|
const cwd = process.cwd();
|
|
23
25
|
const RESTART_DEBOUNCE_MS = 150;
|
|
24
26
|
const SHUTDOWN_TIMEOUT_MS = 2000;
|
|
25
27
|
const LOG_TAG = "go-watch";
|
|
28
|
+
const GO_OUTPUT_TAG = chalk_1.default.blue("[go]");
|
|
26
29
|
const serverConfigPath = (0, path_1.join)(cwd, "server.config.json");
|
|
27
30
|
const ginPort = getGinPort();
|
|
28
31
|
function getGinPort() {
|
|
@@ -259,15 +262,29 @@ function quote(arg) {
|
|
|
259
262
|
}
|
|
260
263
|
return arg;
|
|
261
264
|
}
|
|
265
|
+
function forwardWithGoTag(input, output) {
|
|
266
|
+
if (!input) {
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
const rl = (0, readline_1.createInterface)({ input });
|
|
270
|
+
rl.on("line", (line) => {
|
|
271
|
+
output.write(`${GO_OUTPUT_TAG} ${line}\n`);
|
|
272
|
+
});
|
|
273
|
+
}
|
|
262
274
|
function runGoProcess() {
|
|
275
|
+
var _a, _b, _c;
|
|
263
276
|
const command = `go run ${quote("main.go")}`;
|
|
264
277
|
killGinPortIfNeeded();
|
|
265
278
|
console.log(chalk_1.default.green(`[${LOG_TAG}] start: ${command}`));
|
|
266
|
-
|
|
279
|
+
const proc = (0, child_process_1.spawn)(command, {
|
|
267
280
|
cwd,
|
|
268
281
|
shell: true,
|
|
269
|
-
|
|
282
|
+
env: Object.assign(Object.assign({}, process.env), { FORCE_COLOR: (_a = process.env.FORCE_COLOR) !== null && _a !== void 0 ? _a : "1", CLICOLOR: (_b = process.env.CLICOLOR) !== null && _b !== void 0 ? _b : "1", CLICOLOR_FORCE: (_c = process.env.CLICOLOR_FORCE) !== null && _c !== void 0 ? _c : "1" }),
|
|
283
|
+
stdio: ["inherit", "pipe", "pipe"],
|
|
270
284
|
});
|
|
285
|
+
forwardWithGoTag(proc.stdout, process.stdout);
|
|
286
|
+
forwardWithGoTag(proc.stderr, process.stderr);
|
|
287
|
+
return proc;
|
|
271
288
|
}
|
|
272
289
|
function stopGoProcess(proc) {
|
|
273
290
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -306,7 +323,14 @@ function stopGoProcess(proc) {
|
|
|
306
323
|
});
|
|
307
324
|
});
|
|
308
325
|
}
|
|
309
|
-
|
|
326
|
+
/**
|
|
327
|
+
* 启动 Go 开发监听流程:执行 `go run main.go`,并在文件变更后自动重启。
|
|
328
|
+
*
|
|
329
|
+
* 该函数通常由 `develop()` 调用;执行后会持续监听,直到收到退出信号。
|
|
330
|
+
*
|
|
331
|
+
* @returns {Promise<void>} 仅在监听流程被中断并完成清理后返回。
|
|
332
|
+
*/
|
|
333
|
+
function startGoDev() {
|
|
310
334
|
return __awaiter(this, void 0, void 0, function* () {
|
|
311
335
|
const watchConfig = loadWatchConfig();
|
|
312
336
|
const watchRoots = watchConfig.includeDir.length
|
|
@@ -368,7 +392,10 @@ function start() {
|
|
|
368
392
|
}));
|
|
369
393
|
});
|
|
370
394
|
}
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
});
|
|
395
|
+
if (require.main === module) {
|
|
396
|
+
// 兼容直接执行该文件(例如 node src/dev-go.js)。
|
|
397
|
+
startGoDev().catch((error) => {
|
|
398
|
+
console.error(chalk_1.default.red(`[${LOG_TAG}] failed to start: ${String(error)}`));
|
|
399
|
+
process.exit(1);
|
|
400
|
+
});
|
|
401
|
+
}
|
package/src/develop.d.ts
CHANGED
package/src/develop.js
CHANGED
|
@@ -21,20 +21,9 @@ const child_process_1 = require("child_process");
|
|
|
21
21
|
const chalk_1 = __importDefault(require("chalk"));
|
|
22
22
|
const cleanup_1 = __importDefault(require("./cleanup"));
|
|
23
23
|
const postinstall_1 = __importDefault(require("./postinstall"));
|
|
24
|
+
const dev_go_1 = require("./dev-go");
|
|
24
25
|
const cwd = process.cwd();
|
|
25
26
|
const serverConfig = (0, fs_extra_1.readJSONSync)((0, path_1.join)(cwd, "server.config.json"));
|
|
26
|
-
function getGoDevCommand() {
|
|
27
|
-
const scriptCandidates = [
|
|
28
|
-
(0, path_1.join)(__dirname, "dev-go.js"),
|
|
29
|
-
(0, path_1.join)(__dirname, "..", "dist", "src", "dev-go.js"),
|
|
30
|
-
(0, path_1.join)(process.cwd(), "node_modules", "nuxt-gin-tools", "dist", "src", "dev-go.js"),
|
|
31
|
-
];
|
|
32
|
-
const scriptPath = scriptCandidates.find((candidate) => (0, fs_extra_1.existsSync)(candidate));
|
|
33
|
-
if (!scriptPath) {
|
|
34
|
-
throw new Error("[nuxt-gin-tools] dev-go.js not found. Please reinstall nuxt-gin-tools.");
|
|
35
|
-
}
|
|
36
|
-
return `"${process.execPath}" "${scriptPath}"`;
|
|
37
|
-
}
|
|
38
27
|
function killPort(port) {
|
|
39
28
|
if (!Number.isInteger(port)) {
|
|
40
29
|
return;
|
|
@@ -129,6 +118,13 @@ function killPortsFromConfig() {
|
|
|
129
118
|
}
|
|
130
119
|
}
|
|
131
120
|
}
|
|
121
|
+
/**
|
|
122
|
+
* 启动本地开发环境。
|
|
123
|
+
*
|
|
124
|
+
* 行为包括:按配置执行预清理、释放开发端口、并行启动 Nuxt 与 Go 监听流程。
|
|
125
|
+
*
|
|
126
|
+
* @returns {Promise<void>} 仅在开发进程退出或出现异常时返回。
|
|
127
|
+
*/
|
|
132
128
|
function develop() {
|
|
133
129
|
return __awaiter(this, void 0, void 0, function* () {
|
|
134
130
|
const cleanupBeforeDevelop = serverConfig.cleanupBeforeDevelop === true;
|
|
@@ -150,18 +146,16 @@ function develop() {
|
|
|
150
146
|
killPortsFromConfig();
|
|
151
147
|
}
|
|
152
148
|
(0, fs_extra_1.ensureDirSync)((0, path_1.join)(cwd, ".build/.server"));
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
command: getGoDevCommand(),
|
|
156
|
-
name: "go",
|
|
157
|
-
prefixColor: "green",
|
|
158
|
-
},
|
|
149
|
+
// Nuxt 保持在 concurrently 中运行,统一复用 nuxt 标签输出。
|
|
150
|
+
const nuxtTask = (0, concurrently_1.default)([
|
|
159
151
|
{
|
|
160
152
|
command: `npx nuxt dev --port=${serverConfig.nuxtPort} --host`,
|
|
161
153
|
name: "nuxt",
|
|
162
154
|
prefixColor: "blue",
|
|
163
155
|
},
|
|
164
156
|
]).result;
|
|
157
|
+
// Go 侧直接调用本地 dev-go 逻辑,避免额外 shell 路径和引号问题。
|
|
158
|
+
yield Promise.all([(0, dev_go_1.startGoDev)(), nuxtTask]);
|
|
165
159
|
});
|
|
166
160
|
}
|
|
167
161
|
exports.default = develop;
|