nuxt-gin-tools 0.1.16 → 0.1.17

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.16",
3
+ "version": "0.1.17",
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"
@@ -16,6 +16,7 @@
16
16
  "7zip-min": "^2.1.0",
17
17
  "chalk": "^5.4.1",
18
18
  "concurrently": "^9.2.0",
19
+ "fast-glob": "^3.3.3",
19
20
  "fs-extra": "^11.3.0"
20
21
  },
21
22
  "devDependencies": {
@@ -25,4 +26,4 @@
25
26
  "ts-node": "^10.9.2",
26
27
  "typescript": "^5.8.3"
27
28
  }
28
- }
29
+ }
package/src/pack.d.ts CHANGED
@@ -1,3 +1,55 @@
1
+ export interface PackConfig {
2
+ /**
3
+ * 额外需要打包的文件映射
4
+ * key: 源文件路径(相对于项目根目录或绝对路径)
5
+ * value: 打包后对应位置(相对于服务器构建目录或绝对路径)
6
+ */
7
+ extraFiles?: Record<string, string>;
8
+ /**
9
+ * 额外需要打包的文件 Glob(相对于项目根目录)
10
+ */
11
+ extraFilesGlobs?: string[];
12
+ /**
13
+ * 排除文件/目录 Glob(相对于项目根目录)
14
+ */
15
+ exclude?: string[];
16
+ /**
17
+ * 打包输出 zip 名称(相对于默认 zip 目录)
18
+ */
19
+ zipName?: string;
20
+ /**
21
+ * 打包输出 zip 路径(相对于项目根目录或绝对路径)
22
+ */
23
+ zipPath?: string;
24
+ /**
25
+ * 服务器构建输出目录(相对于项目根目录或绝对路径)
26
+ */
27
+ serverPath?: string;
28
+ /**
29
+ * 打包前钩子
30
+ */
31
+ beforePack?: () => Promise<void> | void;
32
+ /**
33
+ * 打包后钩子
34
+ */
35
+ afterPack?: (zipPath: string) => Promise<void> | void;
36
+ /**
37
+ * 是否清理 dist
38
+ */
39
+ cleanDist?: boolean;
40
+ /**
41
+ * 是否写入启动脚本和 package.json
42
+ */
43
+ writeScripts?: boolean;
44
+ /**
45
+ * 写入/覆盖 package.json 内容
46
+ */
47
+ packageJson?: Record<string, unknown>;
48
+ /**
49
+ * 复制时是否覆盖同名文件
50
+ */
51
+ overwrite?: boolean;
52
+ }
1
53
  /**
2
54
  * 生成相对于服务器构建目录的绝对路径
3
55
  * @param relativePath - 相对路径
@@ -7,6 +59,7 @@ export declare function builtPath(relativePath: string): string;
7
59
  export declare const ZIP_PATH: string;
8
60
  export declare const SERVER_PATH: string;
9
61
  export declare const ORIGINAL_DIST_PATH: string;
62
+ export declare const PACK_CONFIG_PATH: string;
10
63
  export declare const SERVER_EXECUTABLE: string;
11
64
  export declare const PACKAGE_JSON_CONTENT: {
12
65
  private: boolean;
@@ -15,9 +68,7 @@ export declare const PACKAGE_JSON_CONTENT: {
15
68
  };
16
69
  };
17
70
  export declare const FILES_TO_COPY: {
18
- "vue/.output": string;
19
- ".build/.server/production.exe": string;
20
- "server.config.json": string;
71
+ [k: string]: string;
21
72
  };
22
73
  /**
23
74
  * 构建并打包项目的主函数
@@ -27,5 +78,5 @@ export declare const FILES_TO_COPY: {
27
78
  * 4. 打包文件为 7z 格式
28
79
  * 5. 清理原始 dist 目录
29
80
  */
30
- export declare function buildAndPack(): Promise<void>;
81
+ export declare function buildAndPack(config?: PackConfig): Promise<void>;
31
82
  export default buildAndPack;
package/src/pack.js CHANGED
@@ -45,7 +45,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
45
45
  return (mod && mod.__esModule) ? mod : { "default": mod };
46
46
  };
47
47
  Object.defineProperty(exports, "__esModule", { value: true });
48
- exports.FILES_TO_COPY = exports.PACKAGE_JSON_CONTENT = exports.SERVER_EXECUTABLE = exports.ORIGINAL_DIST_PATH = exports.SERVER_PATH = exports.ZIP_PATH = void 0;
48
+ exports.FILES_TO_COPY = exports.PACKAGE_JSON_CONTENT = exports.SERVER_EXECUTABLE = exports.PACK_CONFIG_PATH = exports.ORIGINAL_DIST_PATH = exports.SERVER_PATH = exports.ZIP_PATH = void 0;
49
49
  exports.builtPath = builtPath;
50
50
  exports.buildAndPack = buildAndPack;
51
51
  // 导入项目构建工具,用于执行 Nuxt 项目的构建流程
@@ -57,6 +57,7 @@ const FS = __importStar(require("fs-extra"));
57
57
  // 导入路径处理模块,用于规范化和解析文件路径
58
58
  const Path = __importStar(require("path"));
59
59
  const os = __importStar(require("os"));
60
+ const fast_glob_1 = __importDefault(require("fast-glob"));
60
61
  /**
61
62
  * 生成相对于服务器构建目录的绝对路径
62
63
  * @param relativePath - 相对路径
@@ -71,6 +72,7 @@ exports.ZIP_PATH = Path.resolve(process.cwd(), ".build/production/server.7z");
71
72
  exports.SERVER_PATH = Path.resolve(process.cwd(), ".build/production/server");
72
73
  // 定义原始 dist 目录路径,用于清理操作
73
74
  exports.ORIGINAL_DIST_PATH = Path.resolve(process.cwd(), "dist");
75
+ exports.PACK_CONFIG_PATH = Path.resolve(process.cwd(), "pack.config.json");
74
76
  exports.SERVER_EXECUTABLE = os.platform() === "win32" ? "server-production.exe" : "server-production"; // 根据操作系统选择可执行文件名
75
77
  // 定义打包后项目的 package.json 内容
76
78
  exports.PACKAGE_JSON_CONTENT = {
@@ -79,54 +81,119 @@ exports.PACKAGE_JSON_CONTENT = {
79
81
  start: "./server-production.exe", // 定义启动命令
80
82
  },
81
83
  };
82
- // 定义需要复制到构建目录的文件映射关系
83
- exports.FILES_TO_COPY = {
84
- "vue/.output": builtPath("vue/.output"), // Vue 应用构建输出
85
- [`.build/.server/production.exe`]: builtPath("server-production.exe"), // 生产环境可执行文件
86
- "server.config.json": builtPath("server.config.json"), // 服务器配置文件
84
+ // 定义需要复制到构建目录的文件映射关系(目标为构建目录下的相对路径)
85
+ const DEFAULT_FILES_TO_COPY = {
86
+ "vue/.output": "vue/.output", // Vue 应用构建输出
87
+ [`.build/.server/production.exe`]: "server-production.exe", // 生产环境可执行文件
88
+ "server.config.json": "server.config.json", // 服务器配置文件
87
89
  };
90
+ // 兼容旧导出:默认构建目录下的绝对目标路径
91
+ exports.FILES_TO_COPY = Object.fromEntries(Object.entries(DEFAULT_FILES_TO_COPY).map(([src, dest]) => [src, builtPath(dest)]));
88
92
  /**
89
93
  * 写入启动脚本和 package.json 文件到构建目录
90
94
  */
91
- function writeScriptFiles() {
95
+ function writeScriptFiles(serverPath, config) {
92
96
  // 写入 Windows 批处理启动脚本
93
- FS.outputFileSync(builtPath("start.bat"), `powershell -ExecutionPolicy ByPass -File ./start.ps1`);
97
+ FS.outputFileSync(Path.resolve(serverPath, "start.bat"), `powershell -ExecutionPolicy ByPass -File ./start.ps1`);
94
98
  // 写入 PowerShell 启动脚本
95
- FS.outputFileSync(builtPath("start.ps1"), `./server-production.exe`);
99
+ FS.outputFileSync(Path.resolve(serverPath, "start.ps1"), `./server-production.exe`);
96
100
  // 写入 Linux/macOS 启动脚本
97
- FS.outputFileSync(builtPath("start.sh"), `./server-production.exe`);
101
+ FS.outputFileSync(Path.resolve(serverPath, "start.sh"), `./server-production.exe`);
98
102
  // 写入 package.json 文件,使用 2 个空格缩进
99
- FS.outputJSONSync(builtPath("package.json"), exports.PACKAGE_JSON_CONTENT, { spaces: 2 });
103
+ const mergedPackageJson = mergePackageJson(exports.PACKAGE_JSON_CONTENT, config === null || config === void 0 ? void 0 : config.packageJson);
104
+ FS.outputJSONSync(Path.resolve(serverPath, "package.json"), mergedPackageJson, { spaces: 2 });
100
105
  }
101
106
  /**
102
107
  * 将配置的源文件复制到构建目录
103
108
  */
104
- function copyGeneratedFiles() {
109
+ function copyGeneratedFiles(serverPath, config) {
110
+ var _a;
111
+ const copyOptions = {
112
+ overwrite: (config === null || config === void 0 ? void 0 : config.overwrite) !== false,
113
+ errorOnExist: (config === null || config === void 0 ? void 0 : config.overwrite) === false,
114
+ };
105
115
  // 遍历文件映射,将每个源文件复制到目标位置
106
- for (const [src, dest] of Object.entries(exports.FILES_TO_COPY)) {
107
- FS.copySync(Path.resolve(process.cwd(), src), dest);
116
+ for (const [src, dest] of Object.entries(DEFAULT_FILES_TO_COPY)) {
117
+ const resolvedDest = Path.resolve(serverPath, dest);
118
+ FS.copySync(Path.resolve(process.cwd(), src), resolvedDest, copyOptions);
119
+ }
120
+ if (config === null || config === void 0 ? void 0 : config.extraFiles) {
121
+ for (const [src, dest] of Object.entries(config.extraFiles)) {
122
+ const resolvedSrc = Path.resolve(process.cwd(), src);
123
+ const resolvedDest = Path.isAbsolute(dest) ? dest : Path.resolve(serverPath, dest);
124
+ FS.copySync(resolvedSrc, resolvedDest, copyOptions);
125
+ }
126
+ }
127
+ if ((_a = config === null || config === void 0 ? void 0 : config.extraFilesGlobs) === null || _a === void 0 ? void 0 : _a.length) {
128
+ const matchedFiles = fast_glob_1.default.sync(config.extraFilesGlobs, {
129
+ cwd: process.cwd(),
130
+ onlyFiles: true,
131
+ dot: true,
132
+ ignore: config.exclude,
133
+ });
134
+ for (const file of matchedFiles) {
135
+ const resolvedSrc = Path.resolve(process.cwd(), file);
136
+ const resolvedDest = Path.resolve(serverPath, file);
137
+ FS.copySync(resolvedSrc, resolvedDest, copyOptions);
138
+ }
139
+ }
140
+ }
141
+ function mergePackageJson(base, override) {
142
+ if (!override) {
143
+ return base;
144
+ }
145
+ const baseScripts = typeof base.scripts === "object" && base.scripts ? base.scripts : {};
146
+ const overrideScripts = typeof override.scripts === "object" && override.scripts ? override.scripts : {};
147
+ return Object.assign(Object.assign(Object.assign({}, base), override), { scripts: Object.assign(Object.assign({}, baseScripts), overrideScripts) });
148
+ }
149
+ function readPackConfigFromCwd() {
150
+ if (!FS.existsSync(exports.PACK_CONFIG_PATH)) {
151
+ return undefined;
152
+ }
153
+ return FS.readJSONSync(exports.PACK_CONFIG_PATH);
154
+ }
155
+ function resolveServerPath(config) {
156
+ if (!(config === null || config === void 0 ? void 0 : config.serverPath)) {
157
+ return exports.SERVER_PATH;
108
158
  }
159
+ return Path.isAbsolute(config.serverPath)
160
+ ? config.serverPath
161
+ : Path.resolve(process.cwd(), config.serverPath);
162
+ }
163
+ function resolveZipPath(config) {
164
+ if (config === null || config === void 0 ? void 0 : config.zipPath) {
165
+ return Path.isAbsolute(config.zipPath)
166
+ ? config.zipPath
167
+ : Path.resolve(process.cwd(), config.zipPath);
168
+ }
169
+ if (config === null || config === void 0 ? void 0 : config.zipName) {
170
+ return Path.resolve(Path.dirname(exports.ZIP_PATH), config.zipName);
171
+ }
172
+ return exports.ZIP_PATH;
109
173
  }
110
174
  /**
111
175
  * 打包文件为7z格式
112
176
  */
113
- function makeZip() {
177
+ function makeZip(serverPath, zipPath) {
114
178
  return new Promise((resolve, reject) => {
115
179
  // 使用 7zip 将服务器构建目录打包为 7z 文件
116
- Zip.pack(exports.SERVER_PATH, exports.ZIP_PATH, (error) => {
180
+ Zip.pack(serverPath, zipPath, (error) => {
117
181
  if (error) {
118
182
  console.error("打包失败:", error);
119
183
  reject(error);
120
184
  }
121
- console.log("打包成功:", exports.ZIP_PATH);
122
- resolve(exports.ZIP_PATH);
185
+ console.log("打包成功:", zipPath);
186
+ resolve(zipPath);
123
187
  });
124
188
  });
125
189
  }
126
190
  /**
127
191
  * 清理原始 dist 目录
128
192
  */
129
- function cleanUp() {
193
+ function cleanUp(config) {
194
+ if ((config === null || config === void 0 ? void 0 : config.cleanDist) === false) {
195
+ return;
196
+ }
130
197
  // 检查原始 dist 目录是否存在,存在则删除
131
198
  if (FS.existsSync(exports.ORIGINAL_DIST_PATH)) {
132
199
  FS.removeSync(exports.ORIGINAL_DIST_PATH);
@@ -140,13 +207,24 @@ function cleanUp() {
140
207
  * 4. 打包文件为 7z 格式
141
208
  * 5. 清理原始 dist 目录
142
209
  */
143
- function buildAndPack() {
210
+ function buildAndPack(config) {
144
211
  return __awaiter(this, void 0, void 0, function* () {
212
+ const resolvedConfig = config !== null && config !== void 0 ? config : readPackConfigFromCwd();
213
+ const serverPath = resolveServerPath(resolvedConfig);
214
+ const zipPath = resolveZipPath(resolvedConfig);
145
215
  yield (0, builder_1.default)(); // 执行项目构建
146
- copyGeneratedFiles(); // 复制相关文件
147
- writeScriptFiles(); // 写入脚本文件
148
- yield makeZip(); // 打包文件
149
- cleanUp(); // 清理临时文件
216
+ if (resolvedConfig === null || resolvedConfig === void 0 ? void 0 : resolvedConfig.beforePack) {
217
+ yield resolvedConfig.beforePack();
218
+ }
219
+ copyGeneratedFiles(serverPath, resolvedConfig); // 复制相关文件
220
+ if ((resolvedConfig === null || resolvedConfig === void 0 ? void 0 : resolvedConfig.writeScripts) !== false) {
221
+ writeScriptFiles(serverPath, resolvedConfig); // 写入脚本文件
222
+ }
223
+ yield makeZip(serverPath, zipPath); // 打包文件
224
+ if (resolvedConfig === null || resolvedConfig === void 0 ? void 0 : resolvedConfig.afterPack) {
225
+ yield resolvedConfig.afterPack(zipPath);
226
+ }
227
+ cleanUp(resolvedConfig); // 清理临时文件
150
228
  });
151
229
  }
152
230
  exports.default = buildAndPack;
@@ -9,7 +9,7 @@ function postInstall() {
9
9
  // 执行并发命令
10
10
  return (0, concurrently_1.default)([
11
11
  {
12
- command: "go mod download && go mod tidy",
12
+ command: "go mod download && go mod tidy && go install github.com/cosmtrek/air@latest",
13
13
  name: "go",
14
14
  prefixColor: "green",
15
15
  },
package/dev.d.ts DELETED
@@ -1 +0,0 @@
1
- export {};
package/dev.go DELETED
@@ -1,32 +0,0 @@
1
- package main
2
-
3
- import (
4
- "log"
5
- "os"
6
- "os/exec"
7
- "runtime"
8
- )
9
-
10
- func main() {
11
- // 构建命令
12
- cmd := exec.Command("~/go/bin/air", "-c", "node_modules/nuxt-gin-tools/.air.toml")
13
-
14
- // 如果是windows,运行air
15
- if runtime.GOOS == "windows" {
16
- cmd = exec.Command("air", "-c", "node_modules/nuxt-gin-tools/.air.toml")
17
- }
18
-
19
- // 设置标准输出和错误输出
20
- cmd.Stdout = os.Stdout
21
- cmd.Stderr = os.Stderr
22
-
23
- // 启动命令
24
- if err := cmd.Start(); err != nil {
25
- log.Fatalf("Failed to start air: %v", err)
26
- }
27
-
28
- // 等待命令完成
29
- if err := cmd.Wait(); err != nil {
30
- log.Fatalf("Air command finished with error: %v", err)
31
- }
32
- }
package/dev.js DELETED
@@ -1,17 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const concurrently_1 = __importDefault(require("concurrently"));
7
- const fs_extra_1 = require("fs-extra");
8
- const path_1 = require("path");
9
- const cwd = process.cwd();
10
- const serverConfig = (0, fs_extra_1.readJSONSync)((0, path_1.join)(cwd, "server.config.json"));
11
- (0, concurrently_1.default)([
12
- {
13
- command: `nuxt dev --port ${serverConfig.port}`,
14
- name: "nuxt",
15
- prefixColor: "yellow",
16
- },
17
- ]);
package/go.mod DELETED
@@ -1,3 +0,0 @@
1
- module nuxt-gin
2
-
3
- go 1.24.3