nuxt-gin-tools 0.2.5 → 0.2.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 CHANGED
@@ -1,5 +1,38 @@
1
1
  #!/usr/bin/env node
2
2
  "use strict";
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || (function () {
20
+ var ownKeys = function(o) {
21
+ ownKeys = Object.getOwnPropertyNames || function (o) {
22
+ var ar = [];
23
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
24
+ return ar;
25
+ };
26
+ return ownKeys(o);
27
+ };
28
+ return function (mod) {
29
+ if (mod && mod.__esModule) return mod;
30
+ var result = {};
31
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
32
+ __setModuleDefault(result, mod);
33
+ return result;
34
+ };
35
+ })();
3
36
  var __importDefault = (this && this.__importDefault) || function (mod) {
4
37
  return (mod && mod.__esModule) ? mod : { "default": mod };
5
38
  };
@@ -8,7 +41,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
8
41
  // 导入构建和打包功能
9
42
  const pack_1 = __importDefault(require("./src/pack"));
10
43
  // 导入开发模式功能
11
- const develop_1 = __importDefault(require("./src/develop"));
44
+ const develop_1 = __importStar(require("./src/develop"));
12
45
  // 导入API生成功能
13
46
  const api_generate_1 = __importDefault(require("./src/api-generate"));
14
47
  // 导入安装后处理功能
@@ -30,6 +63,14 @@ switch (args[0]) {
30
63
  // 启动开发模式
31
64
  (0, develop_1.default)();
32
65
  break;
66
+ case "dev:nuxt":
67
+ // 仅启动 Nuxt 开发模式
68
+ (0, develop_1.developNuxt)();
69
+ break;
70
+ case "dev:go":
71
+ // 仅启动 Go 开发模式
72
+ (0, develop_1.developGo)();
73
+ break;
33
74
  case "build":
34
75
  // 执行构建和打包操作
35
76
  (0, pack_1.default)();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-gin-tools",
3
- "version": "0.2.5",
3
+ "version": "0.2.7",
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"
package/src/dev-go.js CHANGED
@@ -23,6 +23,7 @@ const cwd = process.cwd();
23
23
  const RESTART_DEBOUNCE_MS = 150;
24
24
  const SHUTDOWN_TIMEOUT_MS = 2000;
25
25
  const LOG_TAG = "go-watch";
26
+ const GO_WATCH_PREFIX = chalk_1.default.bgMagenta.white(`[${LOG_TAG}]`);
26
27
  const serverConfigPath = (0, path_1.join)(cwd, "server.config.json");
27
28
  const ginPort = getGinPort();
28
29
  function getGinPort() {
@@ -96,7 +97,7 @@ function loadWatchConfig() {
96
97
  parsedConfig = JSON.parse((0, fs_extra_1.readFileSync)(configPath, "utf-8"));
97
98
  }
98
99
  catch (_j) {
99
- console.warn(chalk_1.default.green(`[${LOG_TAG}] invalid watch config JSON, fallback to defaults: ${configPath}`));
100
+ console.warn(`${GO_WATCH_PREFIX} invalid watch config JSON, fallback to defaults: ${configPath}`);
100
101
  return defaultConfig;
101
102
  }
102
103
  const includeExt = toStringArray((_a = parsedConfig.includeExt) !== null && _a !== void 0 ? _a : parsedConfig.include_ext)
@@ -183,7 +184,7 @@ function quote(arg) {
183
184
  function runGoProcess() {
184
185
  const command = `go run ${quote("main.go")}`;
185
186
  killGinPortIfNeeded();
186
- console.log(chalk_1.default.green(`[${LOG_TAG}] start: ${command}`));
187
+ console.log(`${GO_WATCH_PREFIX} start: ${command}`);
187
188
  return (0, child_process_1.spawn)(command, {
188
189
  cwd,
189
190
  shell: true,
@@ -240,7 +241,7 @@ function startGoDev() {
240
241
  const watchRoots = watchConfig.includeDir.length
241
242
  ? watchConfig.includeDir.map((dir) => (0, path_1.join)(cwd, dir))
242
243
  : [cwd];
243
- console.log(chalk_1.default.green(`[${LOG_TAG}] watching: ${watchRoots.map((item) => toProjectRelative(item)).join(", ")}`));
244
+ console.log(`${GO_WATCH_PREFIX} watching: ${watchRoots.map((item) => toProjectRelative(item)).join(", ")}`);
244
245
  let restarting = false;
245
246
  let goProc = runGoProcess();
246
247
  let restartTimer = null;
@@ -265,7 +266,7 @@ function startGoDev() {
265
266
  return;
266
267
  }
267
268
  restarting = true;
268
- console.log(chalk_1.default.green(`[${LOG_TAG}] ${eventName}: ${relPath}, restarting...`));
269
+ console.log(`${GO_WATCH_PREFIX} ${eventName}: ${relPath}, restarting...`);
269
270
  yield stopGoProcess(goProc);
270
271
  goProc = runGoProcess();
271
272
  restarting = false;
@@ -276,7 +277,7 @@ function startGoDev() {
276
277
  .on("change", (filePath) => triggerRestart("change", filePath))
277
278
  .on("unlink", (filePath) => triggerRestart("unlink", filePath))
278
279
  .on("error", (error) => {
279
- console.error(chalk_1.default.green(`[${LOG_TAG}] watcher error: ${String(error)}`));
280
+ console.error(`${GO_WATCH_PREFIX} watcher error: ${String(error)}`);
280
281
  });
281
282
  const shutdown = () => __awaiter(this, void 0, void 0, function* () {
282
283
  if (restartTimer) {
@@ -299,7 +300,7 @@ function startGoDev() {
299
300
  if (require.main === module) {
300
301
  // 兼容直接执行该文件(例如 node src/dev-go.js)。
301
302
  startGoDev().catch((error) => {
302
- console.error(chalk_1.default.green(`[${LOG_TAG}] failed to start: ${String(error)}`));
303
+ console.error(`${GO_WATCH_PREFIX} failed to start: ${String(error)}`);
303
304
  process.exit(1);
304
305
  });
305
306
  }
package/src/develop.d.ts CHANGED
@@ -6,4 +6,16 @@
6
6
  * @returns {Promise<void>} 仅在开发进程退出或出现异常时返回。
7
7
  */
8
8
  export declare function develop(): Promise<void>;
9
+ /**
10
+ * 仅启动 Nuxt 开发服务(带 nuxt 标签输出)。
11
+ *
12
+ * @returns {Promise<void>} 仅在开发进程退出或出现异常时返回。
13
+ */
14
+ export declare function developNuxt(): Promise<void>;
15
+ /**
16
+ * 仅启动 Go 开发监听流程。
17
+ *
18
+ * @returns {Promise<void>} 仅在开发进程退出或出现异常时返回。
19
+ */
20
+ export declare function developGo(): Promise<void>;
9
21
  export default develop;
package/src/develop.js CHANGED
@@ -13,6 +13,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.develop = develop;
16
+ exports.developNuxt = developNuxt;
17
+ exports.developGo = developGo;
16
18
  const concurrently_1 = __importDefault(require("concurrently"));
17
19
  const fs_extra_1 = require("fs-extra");
18
20
  const path_1 = require("path");
@@ -22,6 +24,37 @@ const dev_go_1 = require("./dev-go");
22
24
  const utils_1 = require("./utils");
23
25
  const cwd = process.cwd();
24
26
  const serverConfig = (0, fs_extra_1.readJSONSync)((0, path_1.join)(cwd, "server.config.json"));
27
+ function prepareDevelop() {
28
+ return __awaiter(this, void 0, void 0, function* () {
29
+ const cleanupBeforeDevelop = serverConfig.cleanupBeforeDevelop === true;
30
+ if (cleanupBeforeDevelop) {
31
+ yield (0, cleanup_1.default)();
32
+ yield (0, postinstall_1.default)();
33
+ return;
34
+ }
35
+ if (!(0, fs_extra_1.existsSync)((0, path_1.join)(cwd, "vue/.nuxt")) || !(0, fs_extra_1.existsSync)((0, path_1.join)(cwd, "go.sum"))) {
36
+ yield (0, cleanup_1.default)();
37
+ yield (0, postinstall_1.default)();
38
+ }
39
+ });
40
+ }
41
+ function runNuxtDev() {
42
+ return __awaiter(this, void 0, void 0, function* () {
43
+ yield (0, concurrently_1.default)([
44
+ {
45
+ command: `npx nuxt dev --port=${serverConfig.nuxtPort} --host`,
46
+ name: "nuxt",
47
+ prefixColor: "blue",
48
+ },
49
+ ]).result;
50
+ });
51
+ }
52
+ function runGoDev() {
53
+ return __awaiter(this, void 0, void 0, function* () {
54
+ (0, fs_extra_1.ensureDirSync)((0, path_1.join)(cwd, ".build/.server"));
55
+ yield (0, dev_go_1.startGoDev)();
56
+ });
57
+ }
25
58
  /**
26
59
  * 启动本地开发环境。
27
60
  *
@@ -31,35 +64,43 @@ const serverConfig = (0, fs_extra_1.readJSONSync)((0, path_1.join)(cwd, "server.
31
64
  */
32
65
  function develop() {
33
66
  return __awaiter(this, void 0, void 0, function* () {
34
- const cleanupBeforeDevelop = serverConfig.cleanupBeforeDevelop === true;
35
67
  const killPortBeforeDevelop = serverConfig.killPortBeforeDevelop !== false;
36
- // 如果配置为开发前清理,则直接执行清理和安装后处理
37
- if (cleanupBeforeDevelop) {
38
- yield (0, cleanup_1.default)();
39
- yield (0, postinstall_1.default)();
40
- }
41
- else {
42
- // 否则仅在关键依赖缺失时执行
43
- if (!(0, fs_extra_1.existsSync)((0, path_1.join)(cwd, "vue/.nuxt")) || !(0, fs_extra_1.existsSync)((0, path_1.join)(cwd, "go.sum"))) {
44
- yield (0, cleanup_1.default)();
45
- yield (0, postinstall_1.default)();
46
- }
47
- }
68
+ yield prepareDevelop();
48
69
  // 在开发前确保占用端口被释放
49
70
  if (killPortBeforeDevelop) {
50
71
  (0, utils_1.killPorts)([serverConfig.ginPort, serverConfig.nuxtPort]);
51
72
  }
52
- (0, fs_extra_1.ensureDirSync)((0, path_1.join)(cwd, ".build/.server"));
53
- // Nuxt 保持在 concurrently 中运行,统一复用 nuxt 标签输出。
54
- const nuxtTask = (0, concurrently_1.default)([
55
- {
56
- command: `npx nuxt dev --port=${serverConfig.nuxtPort} --host`,
57
- name: "nuxt",
58
- prefixColor: "blue",
59
- },
60
- ]).result;
61
- // Go 侧直接调用本地 dev-go 逻辑,避免额外 shell 路径和引号问题。
62
- yield Promise.all([(0, dev_go_1.startGoDev)(), nuxtTask]);
73
+ yield Promise.all([runGoDev(), runNuxtDev()]);
74
+ });
75
+ }
76
+ /**
77
+ * 仅启动 Nuxt 开发服务(带 nuxt 标签输出)。
78
+ *
79
+ * @returns {Promise<void>} 仅在开发进程退出或出现异常时返回。
80
+ */
81
+ function developNuxt() {
82
+ return __awaiter(this, void 0, void 0, function* () {
83
+ const killPortBeforeDevelop = serverConfig.killPortBeforeDevelop !== false;
84
+ yield prepareDevelop();
85
+ if (killPortBeforeDevelop) {
86
+ (0, utils_1.killPorts)([serverConfig.nuxtPort]);
87
+ }
88
+ yield runNuxtDev();
89
+ });
90
+ }
91
+ /**
92
+ * 仅启动 Go 开发监听流程。
93
+ *
94
+ * @returns {Promise<void>} 仅在开发进程退出或出现异常时返回。
95
+ */
96
+ function developGo() {
97
+ return __awaiter(this, void 0, void 0, function* () {
98
+ const killPortBeforeDevelop = serverConfig.killPortBeforeDevelop !== false;
99
+ yield prepareDevelop();
100
+ if (killPortBeforeDevelop) {
101
+ (0, utils_1.killPorts)([serverConfig.ginPort]);
102
+ }
103
+ yield runGoDev();
63
104
  });
64
105
  }
65
106
  exports.default = develop;
@@ -30,13 +30,16 @@ function createDefaultConfig({ serverConfig, apiBasePath }) {
30
30
  * 例如,将 "https://example.com/api-go" 转换为 "/api-go"
31
31
  */
32
32
  const thisBasePath = apiBasePath.replace(/^https?:[/]{2}[^/]+/, "");
33
+ const normalizedProxyBasePath = thisBasePath.endsWith("/")
34
+ ? thisBasePath.slice(0, -1)
35
+ : thisBasePath;
33
36
  /**
34
37
  * 目标服务器的 URL
35
38
  * 格式为:http://localhost:ginPort/serverBasePath
36
39
  * 其中,ginPort 是从 serverConfig 中获取的 Gin 服务器的端口号
37
40
  * serverBasePath 是从 MyNuxtConfig 中获取的服务器基础路径
38
41
  */
39
- const target = `http://localhost:${serverConfig.ginPort}${thisBasePath}`;
42
+ const target = `http://localhost:${serverConfig.ginPort}${normalizedProxyBasePath}`;
40
43
  return {
41
44
  buildDir: "vue/.nuxt", // 设置构建目录为 "vue/.nuxt",表示 Nuxt 项目的构建输出将存放在该目录下
42
45
  srcDir: "vue", // 设置源代码目录为 "vue",表示 Nuxt 项目的源代码将存放在该目录下
@@ -85,15 +88,26 @@ function createDefaultConfig({ serverConfig, apiBasePath }) {
85
88
  },
86
89
  devProxy: {
87
90
  // 定义代理规则,将匹配 thisBasePath 的请求代理到目标服务器
88
- [thisBasePath]: {
91
+ [normalizedProxyBasePath]: {
89
92
  // 目标服务器的 URL
90
93
  target: target,
91
94
  // 是否改变请求的源,设置为 true 可以避免跨域问题
92
95
  changeOrigin: true,
96
+ // 启用 WebSocket 代理
97
+ ws: true,
93
98
  },
94
99
  },
95
100
  },
96
101
  vite: {
102
+ server: {
103
+ proxy: {
104
+ [normalizedProxyBasePath]: {
105
+ target,
106
+ changeOrigin: true,
107
+ ws: true,
108
+ },
109
+ },
110
+ },
97
111
  // 配置 Vite 插件
98
112
  plugins: [],
99
113
  // 配置 esbuild 编译器的选项