nuxt-gin-tools 0.2.11 → 0.2.13
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/commands/api-generate.d.ts +11 -0
- package/commands/api-generate.js +133 -0
- package/commands/builder.d.ts +2 -0
- package/commands/builder.js +26 -0
- package/commands/cleanup.d.ts +8 -0
- package/commands/cleanup.js +97 -0
- package/commands/dev-go.d.ts +8 -0
- package/commands/dev-go.js +306 -0
- package/commands/develop.d.ts +21 -0
- package/commands/develop.js +106 -0
- package/commands/pack.d.ts +82 -0
- package/commands/pack.js +230 -0
- package/commands/postinstall.d.ts +2 -0
- package/commands/postinstall.js +32 -0
- package/commands/update.d.ts +2 -0
- package/commands/update.js +22 -0
- package/index.js +6 -6
- package/package.json +1 -1
- package/src/commands/api-generate.d.ts +11 -0
- package/src/commands/api-generate.js +133 -0
- package/src/commands/builder.d.ts +2 -0
- package/src/commands/builder.js +26 -0
- package/src/commands/cleanup.d.ts +8 -0
- package/src/commands/cleanup.js +97 -0
- package/src/commands/dev-go.d.ts +8 -0
- package/src/commands/dev-go.js +306 -0
- package/src/commands/develop.d.ts +21 -0
- package/src/commands/develop.js +106 -0
- package/src/commands/pack.d.ts +82 -0
- package/src/commands/pack.js +230 -0
- package/src/commands/postinstall.d.ts +2 -0
- package/src/commands/postinstall.js +32 -0
- package/src/commands/update.d.ts +2 -0
- package/src/commands/update.js +22 -0
- package/src/nuxt-config.d.ts +9 -43
- package/src/nuxt-config.js +52 -65
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.develop = develop;
|
|
16
|
+
exports.developNuxt = developNuxt;
|
|
17
|
+
exports.developGo = developGo;
|
|
18
|
+
const concurrently_1 = __importDefault(require("concurrently"));
|
|
19
|
+
const fs_extra_1 = require("fs-extra");
|
|
20
|
+
const path_1 = require("path");
|
|
21
|
+
const cleanup_1 = __importDefault(require("./cleanup"));
|
|
22
|
+
const postinstall_1 = __importDefault(require("./postinstall"));
|
|
23
|
+
const dev_go_1 = require("./dev-go");
|
|
24
|
+
const utils_1 = require("../src/utils");
|
|
25
|
+
const cwd = process.cwd();
|
|
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
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* 启动本地开发环境。
|
|
60
|
+
*
|
|
61
|
+
* 行为包括:按配置执行预清理、释放开发端口、并行启动 Nuxt 与 Go 监听流程。
|
|
62
|
+
*
|
|
63
|
+
* @returns {Promise<void>} 仅在开发进程退出或出现异常时返回。
|
|
64
|
+
*/
|
|
65
|
+
function develop() {
|
|
66
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
67
|
+
const killPortBeforeDevelop = serverConfig.killPortBeforeDevelop !== false;
|
|
68
|
+
yield prepareDevelop();
|
|
69
|
+
// 在开发前确保占用端口被释放
|
|
70
|
+
if (killPortBeforeDevelop) {
|
|
71
|
+
(0, utils_1.killPorts)([serverConfig.ginPort, serverConfig.nuxtPort]);
|
|
72
|
+
}
|
|
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();
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
exports.default = develop;
|
|
@@ -0,0 +1,82 @@
|
|
|
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
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* 生成相对于服务器构建目录的绝对路径
|
|
55
|
+
* @param relativePath - 相对路径
|
|
56
|
+
* @returns 解析后的绝对路径
|
|
57
|
+
*/
|
|
58
|
+
export declare function builtPath(relativePath: string): string;
|
|
59
|
+
export declare const ZIP_PATH: string;
|
|
60
|
+
export declare const SERVER_PATH: string;
|
|
61
|
+
export declare const ORIGINAL_DIST_PATH: string;
|
|
62
|
+
export declare const PACK_CONFIG_PATH: string;
|
|
63
|
+
export declare const SERVER_EXECUTABLE: string;
|
|
64
|
+
export declare const PACKAGE_JSON_CONTENT: {
|
|
65
|
+
private: boolean;
|
|
66
|
+
scripts: {
|
|
67
|
+
start: string;
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
export declare const FILES_TO_COPY: {
|
|
71
|
+
[k: string]: string;
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* 构建并打包项目的主函数
|
|
75
|
+
* 1. 执行项目构建
|
|
76
|
+
* 2. 复制生成的文件
|
|
77
|
+
* 3. 写入启动脚本
|
|
78
|
+
* 4. 打包文件为 7z 格式
|
|
79
|
+
* 5. 清理原始 dist 目录
|
|
80
|
+
*/
|
|
81
|
+
export declare function buildAndPack(config?: PackConfig): Promise<void>;
|
|
82
|
+
export default buildAndPack;
|
package/commands/pack.js
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
36
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
37
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
38
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
39
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
40
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
41
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
45
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
46
|
+
};
|
|
47
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
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
|
+
exports.builtPath = builtPath;
|
|
50
|
+
exports.buildAndPack = buildAndPack;
|
|
51
|
+
// 导入项目构建工具,用于执行 Nuxt 项目的构建流程
|
|
52
|
+
const builder_1 = __importDefault(require("./builder"));
|
|
53
|
+
// 导入 7zip 压缩工具库,用于将构建后的文件打包为 7z 格式
|
|
54
|
+
const Zip = __importStar(require("7zip-min"));
|
|
55
|
+
// 导入增强版的文件系统模块,提供更丰富的文件操作 API
|
|
56
|
+
const FS = __importStar(require("fs-extra"));
|
|
57
|
+
// 导入路径处理模块,用于规范化和解析文件路径
|
|
58
|
+
const Path = __importStar(require("path"));
|
|
59
|
+
const os = __importStar(require("os"));
|
|
60
|
+
const fast_glob_1 = __importDefault(require("fast-glob"));
|
|
61
|
+
/**
|
|
62
|
+
* 生成相对于服务器构建目录的绝对路径
|
|
63
|
+
* @param relativePath - 相对路径
|
|
64
|
+
* @returns 解析后的绝对路径
|
|
65
|
+
*/
|
|
66
|
+
function builtPath(relativePath) {
|
|
67
|
+
return Path.resolve(exports.SERVER_PATH, relativePath);
|
|
68
|
+
}
|
|
69
|
+
// 定义打包后的 7z 文件输出路径
|
|
70
|
+
exports.ZIP_PATH = Path.resolve(process.cwd(), ".build/production/server.7z");
|
|
71
|
+
// 定义服务器构建文件的输出目录
|
|
72
|
+
exports.SERVER_PATH = Path.resolve(process.cwd(), ".build/production/server");
|
|
73
|
+
// 定义原始 dist 目录路径,用于清理操作
|
|
74
|
+
exports.ORIGINAL_DIST_PATH = Path.resolve(process.cwd(), "dist");
|
|
75
|
+
exports.PACK_CONFIG_PATH = Path.resolve(process.cwd(), "pack.config.json");
|
|
76
|
+
exports.SERVER_EXECUTABLE = os.platform() === "win32" ? "server-production.exe" : "server-production"; // 根据操作系统选择可执行文件名
|
|
77
|
+
// 定义打包后项目的 package.json 内容
|
|
78
|
+
exports.PACKAGE_JSON_CONTENT = {
|
|
79
|
+
private: true,
|
|
80
|
+
scripts: {
|
|
81
|
+
start: "./server-production.exe", // 定义启动命令
|
|
82
|
+
},
|
|
83
|
+
};
|
|
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", // 服务器配置文件
|
|
89
|
+
};
|
|
90
|
+
// 兼容旧导出:默认构建目录下的绝对目标路径
|
|
91
|
+
exports.FILES_TO_COPY = Object.fromEntries(Object.entries(DEFAULT_FILES_TO_COPY).map(([src, dest]) => [src, builtPath(dest)]));
|
|
92
|
+
/**
|
|
93
|
+
* 写入启动脚本和 package.json 文件到构建目录
|
|
94
|
+
*/
|
|
95
|
+
function writeScriptFiles(serverPath, config) {
|
|
96
|
+
// 写入 Windows 批处理启动脚本
|
|
97
|
+
FS.outputFileSync(Path.resolve(serverPath, "start.bat"), `powershell -ExecutionPolicy ByPass -File ./start.ps1`);
|
|
98
|
+
// 写入 PowerShell 启动脚本
|
|
99
|
+
FS.outputFileSync(Path.resolve(serverPath, "start.ps1"), `./server-production.exe`);
|
|
100
|
+
// 写入 Linux/macOS 启动脚本
|
|
101
|
+
FS.outputFileSync(Path.resolve(serverPath, "start.sh"), `./server-production.exe`);
|
|
102
|
+
// 写入 package.json 文件,使用 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 });
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* 将配置的源文件复制到构建目录
|
|
108
|
+
*/
|
|
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
|
+
};
|
|
115
|
+
// 遍历文件映射,将每个源文件复制到目标位置
|
|
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;
|
|
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;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* 打包文件为7z格式
|
|
176
|
+
*/
|
|
177
|
+
function makeZip(serverPath, zipPath) {
|
|
178
|
+
return new Promise((resolve, reject) => {
|
|
179
|
+
// 使用 7zip 将服务器构建目录打包为 7z 文件
|
|
180
|
+
Zip.pack(serverPath, zipPath, (error) => {
|
|
181
|
+
if (error) {
|
|
182
|
+
console.error("打包失败:", error);
|
|
183
|
+
reject(error);
|
|
184
|
+
}
|
|
185
|
+
console.log("打包成功:", zipPath);
|
|
186
|
+
resolve(zipPath);
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* 清理原始 dist 目录
|
|
192
|
+
*/
|
|
193
|
+
function cleanUp(config) {
|
|
194
|
+
if ((config === null || config === void 0 ? void 0 : config.cleanDist) === false) {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
// 检查原始 dist 目录是否存在,存在则删除
|
|
198
|
+
if (FS.existsSync(exports.ORIGINAL_DIST_PATH)) {
|
|
199
|
+
FS.removeSync(exports.ORIGINAL_DIST_PATH);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* 构建并打包项目的主函数
|
|
204
|
+
* 1. 执行项目构建
|
|
205
|
+
* 2. 复制生成的文件
|
|
206
|
+
* 3. 写入启动脚本
|
|
207
|
+
* 4. 打包文件为 7z 格式
|
|
208
|
+
* 5. 清理原始 dist 目录
|
|
209
|
+
*/
|
|
210
|
+
function buildAndPack(config) {
|
|
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);
|
|
215
|
+
yield (0, builder_1.default)(); // 执行项目构建
|
|
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); // 清理临时文件
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
exports.default = buildAndPack;
|
|
@@ -0,0 +1,32 @@
|
|
|
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
|
+
exports.postInstall = postInstall;
|
|
7
|
+
const concurrently_1 = __importDefault(require("concurrently"));
|
|
8
|
+
const node_child_process_1 = require("node:child_process");
|
|
9
|
+
function postInstall() {
|
|
10
|
+
const hasGo = (0, node_child_process_1.spawnSync)("go", ["version"], { stdio: "ignore", shell: true }).status ===
|
|
11
|
+
0;
|
|
12
|
+
const commands = [
|
|
13
|
+
{
|
|
14
|
+
command: "npx nuxt prepare",
|
|
15
|
+
name: "nuxt",
|
|
16
|
+
prefixColor: "blue",
|
|
17
|
+
},
|
|
18
|
+
];
|
|
19
|
+
if (hasGo) {
|
|
20
|
+
commands.unshift({
|
|
21
|
+
command: "go mod download && go mod tidy",
|
|
22
|
+
name: "go",
|
|
23
|
+
prefixColor: "green",
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
console.warn("[nuxt-gin-tools] 未检测到 Go,已跳过 Go 相关安装。请先安装 Go 后再重新运行相关命令。");
|
|
28
|
+
}
|
|
29
|
+
// 执行并发命令
|
|
30
|
+
return (0, concurrently_1.default)(commands).result;
|
|
31
|
+
}
|
|
32
|
+
exports.default = postInstall;
|
|
@@ -0,0 +1,22 @@
|
|
|
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
|
+
exports.update = update;
|
|
7
|
+
const concurrently_1 = __importDefault(require("concurrently"));
|
|
8
|
+
function update() {
|
|
9
|
+
(0, concurrently_1.default)([
|
|
10
|
+
{
|
|
11
|
+
command: "pnpm update --latest",
|
|
12
|
+
name: "pnpm",
|
|
13
|
+
prefixColor: "magenta",
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
command: "go get -u && go mod tidy",
|
|
17
|
+
name: "go",
|
|
18
|
+
prefixColor: "green",
|
|
19
|
+
},
|
|
20
|
+
]);
|
|
21
|
+
}
|
|
22
|
+
exports.default = update;
|
package/index.js
CHANGED
|
@@ -39,17 +39,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
39
39
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
40
40
|
// index.ts - 入口文件,用于处理命令行参数并调用相应的功能模块
|
|
41
41
|
// 导入构建和打包功能
|
|
42
|
-
const pack_1 = __importDefault(require("./
|
|
42
|
+
const pack_1 = __importDefault(require("./commands/pack"));
|
|
43
43
|
// 导入开发模式功能
|
|
44
|
-
const develop_1 = __importStar(require("./
|
|
44
|
+
const develop_1 = __importStar(require("./commands/develop"));
|
|
45
45
|
// 导入API生成功能
|
|
46
|
-
const api_generate_1 = __importDefault(require("./
|
|
46
|
+
const api_generate_1 = __importDefault(require("./commands/api-generate"));
|
|
47
47
|
// 导入安装后处理功能
|
|
48
|
-
const postinstall_1 = __importDefault(require("./
|
|
48
|
+
const postinstall_1 = __importDefault(require("./commands/postinstall"));
|
|
49
49
|
// 导入清理功能
|
|
50
|
-
const cleanup_1 = __importDefault(require("./
|
|
50
|
+
const cleanup_1 = __importDefault(require("./commands/cleanup"));
|
|
51
51
|
// 导入更新功能
|
|
52
|
-
const update_1 = __importDefault(require("./
|
|
52
|
+
const update_1 = __importDefault(require("./commands/update"));
|
|
53
53
|
// 获取命令行参数(去除前两个默认参数)
|
|
54
54
|
const args = process.argv.slice(2);
|
|
55
55
|
// 检查是否提供了命令
|
package/package.json
CHANGED
|
@@ -0,0 +1,133 @@
|
|
|
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;
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
console.error("更新Vue API基础URL失败:", error);
|
|
70
|
+
throw error; // 将错误继续抛出,以便上层处理
|
|
71
|
+
}
|
|
72
|
+
});
|
|
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
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* 删除指定的路径
|
|
85
|
+
* 用于清理生成过程中产生的临时或不需要的文件和目录
|
|
86
|
+
*/
|
|
87
|
+
function removePaths() {
|
|
88
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
89
|
+
try {
|
|
90
|
+
// 遍历要删除的路径列表
|
|
91
|
+
for (const path of pathsToDelete) {
|
|
92
|
+
// 构建完整路径
|
|
93
|
+
const fullPath = path_1.default.join(cwd, path);
|
|
94
|
+
// 删除路径(文件或目录)
|
|
95
|
+
yield fs_extra_1.default.remove(fullPath);
|
|
96
|
+
console.log(`成功删除路径: ${fullPath}`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
catch (error) {
|
|
100
|
+
console.error("删除路径失败:", error);
|
|
101
|
+
throw error; // 将错误继续抛出
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* 主函数
|
|
107
|
+
* 协调执行所有任务:生成代码、删除路径、配置Vue API
|
|
108
|
+
*/
|
|
109
|
+
function apiGenerate() {
|
|
110
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
111
|
+
try {
|
|
112
|
+
// 输出开始信息
|
|
113
|
+
console.log(chalk_1.default.bgGreen("开始生成API代码..."));
|
|
114
|
+
// 并发执行命令列表中的所有命令,等待所有命令完成
|
|
115
|
+
yield (0, concurrently_1.default)(commands).result;
|
|
116
|
+
// 按顺序执行清理和配置任务
|
|
117
|
+
let tasks = [];
|
|
118
|
+
tasks.push(removePaths());
|
|
119
|
+
tasks.push(setVueBaseUrl());
|
|
120
|
+
tasks.push(setGoRoutes());
|
|
121
|
+
yield Promise.all(tasks);
|
|
122
|
+
console.log(chalk_1.default.bgGreen("API代码生成和配置完成!"));
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
// 捕获并处理任何阶段发生的错误
|
|
126
|
+
console.error(chalk_1.default.red("执行过程中发生错误:"), error);
|
|
127
|
+
// 以错误码1退出进程,表示执行失败
|
|
128
|
+
process.exit(1);
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
// 执行主函数
|
|
133
|
+
exports.default = apiGenerate;
|