@fastcar/cli 0.0.7 → 0.0.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/README.md +201 -21
- package/bin/cli.js +121 -36
- package/package.json +3 -4
- package/src/init.js +615 -201
- package/src/pack.js +204 -0
- package/src/reverse.js +204 -0
- package/src/templates.json +32 -0
- package/src/utils.js +51 -66
package/src/pack.js
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const process = require("process");
|
|
4
|
+
const { execSync } = require("child_process");
|
|
5
|
+
const utils = require("./utils");
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 检测可用的包管理器
|
|
9
|
+
* @returns {string} 包管理器名称 (npm/yarn/pnpm)
|
|
10
|
+
*/
|
|
11
|
+
function detectPackageManager() {
|
|
12
|
+
// 检查是否有 lock 文件来确定使用的包管理器
|
|
13
|
+
const cwd = process.cwd();
|
|
14
|
+
|
|
15
|
+
if (fs.existsSync(path.join(cwd, "pnpm-lock.yaml"))) {
|
|
16
|
+
return "pnpm";
|
|
17
|
+
}
|
|
18
|
+
if (fs.existsSync(path.join(cwd, "yarn.lock"))) {
|
|
19
|
+
return "yarn";
|
|
20
|
+
}
|
|
21
|
+
// 默认使用 npm
|
|
22
|
+
return "npm";
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 获取包管理器的安装命令
|
|
27
|
+
* @param {string} pm - 包管理器名称
|
|
28
|
+
* @returns {string} 安装命令
|
|
29
|
+
*/
|
|
30
|
+
function getInstallCommand(pm) {
|
|
31
|
+
switch (pm) {
|
|
32
|
+
case "yarn":
|
|
33
|
+
return "yarn install --production --frozen-lockfile";
|
|
34
|
+
case "pnpm":
|
|
35
|
+
return "pnpm install --prod";
|
|
36
|
+
case "npm":
|
|
37
|
+
default:
|
|
38
|
+
return "npm install --production";
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* 检查包管理器是否已安装
|
|
44
|
+
* @param {string} pm - 包管理器名称
|
|
45
|
+
* @returns {boolean}
|
|
46
|
+
*/
|
|
47
|
+
function isPackageManagerInstalled(pm) {
|
|
48
|
+
if (pm === "npm") return true; // npm 随 Node.js 一起安装
|
|
49
|
+
|
|
50
|
+
try {
|
|
51
|
+
execSync(`${pm} --version`, { stdio: "pipe" });
|
|
52
|
+
return true;
|
|
53
|
+
} catch {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 打包项目,使用包管理器安装生产依赖
|
|
60
|
+
* @param {string} projectPath - 项目路径,默认为当前目录
|
|
61
|
+
* @param {string} outputPath - 输出路径,默认为项目目录下的 dist 文件夹
|
|
62
|
+
* @param {string} packageManager - 指定包管理器 (npm/yarn/pnpm),不传则自动检测
|
|
63
|
+
*/
|
|
64
|
+
async function packProject(projectPath, outputPath, packageManager) {
|
|
65
|
+
const cwd = projectPath || process.cwd();
|
|
66
|
+
|
|
67
|
+
// 检查项目目录
|
|
68
|
+
const packageJsonPath = path.join(cwd, "package.json");
|
|
69
|
+
if (!fs.existsSync(packageJsonPath)) {
|
|
70
|
+
console.log("❌ 未找到 package.json,请在项目根目录执行此命令");
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// 读取 package.json
|
|
75
|
+
let packageJson;
|
|
76
|
+
try {
|
|
77
|
+
packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"));
|
|
78
|
+
} catch (error) {
|
|
79
|
+
console.log("❌ 解析 package.json 失败:", error.message);
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// 确定包管理器
|
|
84
|
+
let pm = packageManager || detectPackageManager();
|
|
85
|
+
|
|
86
|
+
// 检查指定的包管理器是否可用
|
|
87
|
+
if (!isPackageManagerInstalled(pm)) {
|
|
88
|
+
console.log(`⚠️ 未找到 ${pm},尝试使用 npm...`);
|
|
89
|
+
pm = "npm";
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
console.log("📦 开始打包项目...");
|
|
93
|
+
console.log(` 项目路径: ${cwd}`);
|
|
94
|
+
console.log(` 包管理器: ${pm}`);
|
|
95
|
+
|
|
96
|
+
const deps = Object.keys(packageJson.dependencies || {});
|
|
97
|
+
console.log(` 生产依赖: ${deps.length} 个`);
|
|
98
|
+
|
|
99
|
+
// 确定输出路径
|
|
100
|
+
const projectName = packageJson.name || "project";
|
|
101
|
+
const version = packageJson.version || "0.0.0";
|
|
102
|
+
const folderName = path.basename(cwd);
|
|
103
|
+
const defaultOutputDir = outputPath || path.join(cwd, "dist");
|
|
104
|
+
const zipFileName = `${projectName}-${version}.zip`;
|
|
105
|
+
const zipFilePath = path.join(defaultOutputDir, zipFileName);
|
|
106
|
+
|
|
107
|
+
// 创建临时目录,使用项目文件夹名作为根目录名
|
|
108
|
+
const tempDir = path.join(cwd, `.pack-temp-${Date.now()}`);
|
|
109
|
+
const rootDir = path.join(tempDir, folderName);
|
|
110
|
+
|
|
111
|
+
try {
|
|
112
|
+
fs.mkdirSync(rootDir, { recursive: true });
|
|
113
|
+
fs.mkdirSync(defaultOutputDir, { recursive: true });
|
|
114
|
+
|
|
115
|
+
// 复制项目文件到临时目录(排除 node_modules)
|
|
116
|
+
console.log("📋 正在复制项目文件...");
|
|
117
|
+
await copyProjectFiles(cwd, rootDir);
|
|
118
|
+
|
|
119
|
+
// 复制 lock 文件(如果存在)
|
|
120
|
+
const lockFiles = ["package-lock.json", "yarn.lock", "pnpm-lock.yaml"];
|
|
121
|
+
for (const lockFile of lockFiles) {
|
|
122
|
+
const lockPath = path.join(cwd, lockFile);
|
|
123
|
+
if (fs.existsSync(lockPath)) {
|
|
124
|
+
fs.copyFileSync(lockPath, path.join(rootDir, lockFile));
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// 执行安装生产依赖
|
|
129
|
+
console.log(`🗜️ 正在使用 ${pm} 安装生产依赖...`);
|
|
130
|
+
const installCmd = getInstallCommand(pm);
|
|
131
|
+
try {
|
|
132
|
+
execSync(installCmd, {
|
|
133
|
+
cwd: rootDir,
|
|
134
|
+
stdio: "inherit"
|
|
135
|
+
});
|
|
136
|
+
} catch (error) {
|
|
137
|
+
console.log(`⚠️ ${pm} 安装失败,尝试使用 npm...`);
|
|
138
|
+
execSync(getInstallCommand("npm"), {
|
|
139
|
+
cwd: rootDir,
|
|
140
|
+
stdio: "inherit"
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// 打包
|
|
145
|
+
console.log("📦 正在生成压缩包...");
|
|
146
|
+
if (fs.existsSync(zipFilePath)) {
|
|
147
|
+
fs.rmSync(zipFilePath);
|
|
148
|
+
}
|
|
149
|
+
await utils.zipFile(rootDir, zipFilePath);
|
|
150
|
+
|
|
151
|
+
console.log("✅ 打包完成!");
|
|
152
|
+
console.log(` 输出文件: ${zipFilePath}`);
|
|
153
|
+
console.log(` 解压目录: ${folderName}/`);
|
|
154
|
+
|
|
155
|
+
// 计算文件大小
|
|
156
|
+
const stats = fs.statSync(zipFilePath);
|
|
157
|
+
const sizeMB = (stats.size / 1024 / 1024).toFixed(2);
|
|
158
|
+
console.log(` 文件大小: ${sizeMB} MB`);
|
|
159
|
+
} catch (error) {
|
|
160
|
+
console.log("❌ 打包失败:", error.message);
|
|
161
|
+
} finally {
|
|
162
|
+
// 清理临时目录
|
|
163
|
+
if (fs.existsSync(tempDir)) {
|
|
164
|
+
utils.delDirEctory(tempDir);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* 复制项目文件(排除 node_modules、dist、logs 和 .log 文件)
|
|
171
|
+
*/
|
|
172
|
+
async function copyProjectFiles(src, dest) {
|
|
173
|
+
const items = fs.readdirSync(src);
|
|
174
|
+
|
|
175
|
+
for (const item of items) {
|
|
176
|
+
// 跳过隐藏文件、node_modules、dist、logs 目录
|
|
177
|
+
if (item.startsWith(".") || item === "node_modules" || item === "dist" || item === "logs") {
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// 跳过 .log 文件
|
|
182
|
+
if (item.endsWith(".log")) {
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const srcPath = path.join(src, item);
|
|
187
|
+
const destPath = path.join(dest, item);
|
|
188
|
+
const stat = fs.statSync(srcPath);
|
|
189
|
+
|
|
190
|
+
if (stat.isDirectory()) {
|
|
191
|
+
// 递归复制目录
|
|
192
|
+
fs.mkdirSync(destPath, { recursive: true });
|
|
193
|
+
await copyProjectFiles(srcPath, destPath);
|
|
194
|
+
} else {
|
|
195
|
+
// 复制文件
|
|
196
|
+
fs.copyFileSync(srcPath, destPath);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
module.exports = {
|
|
202
|
+
packProject,
|
|
203
|
+
detectPackageManager,
|
|
204
|
+
};
|
package/src/reverse.js
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
const path = require("path");
|
|
2
|
+
const fs = require("fs");
|
|
3
|
+
const process = require("process");
|
|
4
|
+
|
|
5
|
+
// 默认配置文件模板
|
|
6
|
+
const defaultConfig = {
|
|
7
|
+
tables: ["test"],
|
|
8
|
+
modelDir: "",
|
|
9
|
+
mapperDir: "",
|
|
10
|
+
dbConfig: {
|
|
11
|
+
host: "localhost",
|
|
12
|
+
port: 3306,
|
|
13
|
+
user: "root",
|
|
14
|
+
password: "password",
|
|
15
|
+
database: "test_db",
|
|
16
|
+
},
|
|
17
|
+
style: {
|
|
18
|
+
tabWidth: 4,
|
|
19
|
+
printWidth: 200,
|
|
20
|
+
trailingComma: "es5",
|
|
21
|
+
useTabs: true,
|
|
22
|
+
parser: "typescript",
|
|
23
|
+
endOfLine: "crlf",
|
|
24
|
+
},
|
|
25
|
+
ignoreCamelcase: false,
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
// 生成默认配置文件
|
|
29
|
+
function initReverseConfig() {
|
|
30
|
+
const cwd = process.cwd();
|
|
31
|
+
const configPath = path.join(cwd, "reverse.config.json");
|
|
32
|
+
|
|
33
|
+
// 检查是否已存在
|
|
34
|
+
if (fs.existsSync(configPath)) {
|
|
35
|
+
console.log("⚠️ reverse.config.json 配置文件已存在");
|
|
36
|
+
console.log(" 如需重新生成,请先删除现有文件");
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// 填充默认路径
|
|
41
|
+
const config = {
|
|
42
|
+
...defaultConfig,
|
|
43
|
+
modelDir: path.join(cwd, "src", "model").replace(/\\/g, "/"),
|
|
44
|
+
mapperDir: path.join(cwd, "src", "mapper").replace(/\\/g, "/"),
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
try {
|
|
48
|
+
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
|
|
49
|
+
console.log("✅ 已生成 reverse.config.json 配置文件");
|
|
50
|
+
console.log("📁 文件路径:", configPath);
|
|
51
|
+
console.log("\n💡 请根据需要修改以下配置:");
|
|
52
|
+
console.log(" • tables: 要逆向生成的表名数组");
|
|
53
|
+
console.log(" • modelDir: Model 文件输出目录");
|
|
54
|
+
console.log(" • mapperDir: Mapper 文件输出目录");
|
|
55
|
+
console.log(" • dbConfig: 数据库连接配置");
|
|
56
|
+
} catch (error) {
|
|
57
|
+
console.log("❌ 生成配置文件失败:", error.message);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// 数据库表逆向生成
|
|
62
|
+
async function reverseGenerate(args = []) {
|
|
63
|
+
const cwd = process.cwd();
|
|
64
|
+
|
|
65
|
+
// 检查是否在项目目录下(通过检查 package.json)
|
|
66
|
+
const packagePath = path.join(cwd, "package.json");
|
|
67
|
+
const nodeModulesPath = path.join(cwd, "node_modules");
|
|
68
|
+
|
|
69
|
+
if (!fs.existsSync(packagePath)) {
|
|
70
|
+
console.log("❌ 请在项目根目录下执行此命令");
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// 读取配置文件
|
|
75
|
+
const configPath = path.join(cwd, "reverse.config.json");
|
|
76
|
+
if (!fs.existsSync(configPath)) {
|
|
77
|
+
console.log("❌ 未找到 reverse.config.json 配置文件");
|
|
78
|
+
console.log(" 请在项目根目录创建该文件,格式如下:");
|
|
79
|
+
console.log(`
|
|
80
|
+
{
|
|
81
|
+
"tables": ["test"],
|
|
82
|
+
"modelDir": "${cwd.replace(/\\/g, "/")}/src/models",
|
|
83
|
+
"mapperDir": "${cwd.replace(/\\/g, "/")}/src/mappers",
|
|
84
|
+
"dbConfig": {
|
|
85
|
+
"host": "localhost",
|
|
86
|
+
"port": 3306,
|
|
87
|
+
"user": "root",
|
|
88
|
+
"password": "password",
|
|
89
|
+
"database": "test_db"
|
|
90
|
+
},
|
|
91
|
+
"style": {
|
|
92
|
+
"tabWidth": 4,
|
|
93
|
+
"printWidth": 200,
|
|
94
|
+
"trailingComma": "es5",
|
|
95
|
+
"useTabs": true,
|
|
96
|
+
"parser": "typescript",
|
|
97
|
+
"endOfLine": "crlf"
|
|
98
|
+
},
|
|
99
|
+
"ignoreCamelcase": false
|
|
100
|
+
}
|
|
101
|
+
`);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
let config;
|
|
106
|
+
try {
|
|
107
|
+
config = JSON.parse(fs.readFileSync(configPath, "utf-8"));
|
|
108
|
+
} catch (error) {
|
|
109
|
+
console.log("❌ 配置文件解析失败:", error.message);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// 校验必填参数
|
|
114
|
+
const requiredFields = ["tables", "modelDir", "mapperDir", "dbConfig"];
|
|
115
|
+
const missingFields = requiredFields.filter((field) => !config[field]);
|
|
116
|
+
if (missingFields.length > 0) {
|
|
117
|
+
console.log("❌ 配置文件缺少必填字段:", missingFields.join(", "));
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// 尝试加载 @fastcar/mysql-tool 包
|
|
122
|
+
let mysqlTool;
|
|
123
|
+
try {
|
|
124
|
+
// 先尝试从项目本地加载
|
|
125
|
+
mysqlTool = require(path.join(nodeModulesPath, "@fastcar/mysql-tool"));
|
|
126
|
+
} catch (e) {
|
|
127
|
+
// 检查错误类型:是 @fastcar/mysql-tool 本身缺失,还是其依赖 @fastcar/mysql 缺失
|
|
128
|
+
if (e.code === "MODULE_NOT_FOUND") {
|
|
129
|
+
// 如果错误消息中包含 @fastcar/mysql-tool,说明是包本身未安装
|
|
130
|
+
if (e.message.includes("@fastcar/mysql-tool")) {
|
|
131
|
+
console.log("❌ 未找到 @fastcar/mysql-tool 包,请先安装:");
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
// 如果 Require stack 中有 mysql-tool,说明是 @fastcar/mysql 依赖缺失
|
|
135
|
+
if (
|
|
136
|
+
e.requireStack &&
|
|
137
|
+
e.requireStack.some((s) => s.includes("@fastcar/mysql-tool"))
|
|
138
|
+
) {
|
|
139
|
+
console.log("❌ 缺少依赖包 @fastcar/mysql,请先安装:");
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
try {
|
|
145
|
+
// 如果本地没有,尝试全局加载
|
|
146
|
+
mysqlTool = require("@fastcar/mysql-tool");
|
|
147
|
+
} catch (e2) {
|
|
148
|
+
if (e2.code === "MODULE_NOT_FOUND") {
|
|
149
|
+
// 同样区分是包本身缺失还是依赖缺失
|
|
150
|
+
if (e2.message.includes("@fastcar/mysql-tool")) {
|
|
151
|
+
console.log("❌ 未找到 @fastcar/mysql-tool 包,请先安装:");
|
|
152
|
+
console.log(" npm install @fastcar/mysql-tool");
|
|
153
|
+
console.log(" 或: yarn add @fastcar/mysql-tool");
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
if (e2.message.includes("@fastcar/mysql")) {
|
|
157
|
+
console.log("❌ 缺少依赖包 @fastcar/mysql,请先安装:");
|
|
158
|
+
console.log(" npm install @fastcar/mysql");
|
|
159
|
+
console.log(" 或: yarn add @fastcar/mysql");
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
console.log("❌ 加载 @fastcar/mysql-tool 失败:", e2.message);
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// 检查 ReverseGenerate.generator 方法是否存在
|
|
170
|
+
if (
|
|
171
|
+
!mysqlTool.ReverseGenerate ||
|
|
172
|
+
typeof mysqlTool.ReverseGenerate.generator !== "function"
|
|
173
|
+
) {
|
|
174
|
+
console.log(
|
|
175
|
+
"❌ @fastcar/mysql-tool 包中未找到 ReverseGenerate.generator 方法",
|
|
176
|
+
);
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
try {
|
|
181
|
+
console.log("🔄 开始执行数据库表逆向生成...");
|
|
182
|
+
console.log("📋 目标表:", config.tables.join(", "));
|
|
183
|
+
console.log("📁 Model 输出目录:", config.modelDir);
|
|
184
|
+
console.log("📁 Mapper 输出目录:", config.mapperDir);
|
|
185
|
+
await mysqlTool.ReverseGenerate.generator(config);
|
|
186
|
+
console.log("✅ 逆向生成完成");
|
|
187
|
+
} catch (error) {
|
|
188
|
+
// 检查是否是 @fastcar/mysql 依赖缺失
|
|
189
|
+
if (
|
|
190
|
+
error.code === "MODULE_NOT_FOUND" &&
|
|
191
|
+
error.message.includes("@fastcar/mysql")
|
|
192
|
+
) {
|
|
193
|
+
console.log("❌ 缺少依赖包 @fastcar/mysql,请先安装:");
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
console.log("❌ 逆向生成失败:", error.message);
|
|
197
|
+
process.exit(1);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
module.exports = {
|
|
202
|
+
reverseGenerate,
|
|
203
|
+
initReverseConfig,
|
|
204
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"web": {
|
|
3
|
+
"name": "web",
|
|
4
|
+
"description": "Web 应用模板 - 适用于构建 Web 服务端应用",
|
|
5
|
+
"package": "@fastcar/template-web",
|
|
6
|
+
"tags": ["web", "server", "http"]
|
|
7
|
+
},
|
|
8
|
+
"rpc": {
|
|
9
|
+
"name": "rpc",
|
|
10
|
+
"description": "RPC 服务模板 - 适用于构建微服务 RPC 节点",
|
|
11
|
+
"package": "@fastcar/template-rpc",
|
|
12
|
+
"tags": ["rpc", "microservice"]
|
|
13
|
+
},
|
|
14
|
+
"cos": {
|
|
15
|
+
"name": "cos",
|
|
16
|
+
"description": "COS 存储模板 - 适用于对象存储服务",
|
|
17
|
+
"package": "@fastcar/template-cos",
|
|
18
|
+
"tags": ["cos", "storage"]
|
|
19
|
+
},
|
|
20
|
+
"micro": {
|
|
21
|
+
"name": "micro",
|
|
22
|
+
"description": "微服务模板 - 适用于构建完整的微服务架构",
|
|
23
|
+
"package": "@fastcar/template-microservices",
|
|
24
|
+
"tags": ["microservices", "fullstack"]
|
|
25
|
+
},
|
|
26
|
+
"static": {
|
|
27
|
+
"name": "static",
|
|
28
|
+
"description": "静态资源模板 - 适用于静态网站或资源服务",
|
|
29
|
+
"package": "@fastcar/template-static",
|
|
30
|
+
"tags": ["static", "assets"]
|
|
31
|
+
}
|
|
32
|
+
}
|
package/src/utils.js
CHANGED
|
@@ -1,96 +1,81 @@
|
|
|
1
1
|
const fs = require("fs");
|
|
2
2
|
const path = require("path");
|
|
3
3
|
const compressing = require("compressing");
|
|
4
|
-
const util = require("util");
|
|
5
4
|
const yaml = require("yaml");
|
|
6
5
|
|
|
7
6
|
//复制文件
|
|
8
7
|
function copyDirectory(src, dest) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
8
|
+
if (!fs.existsSync(dest)) {
|
|
9
|
+
fs.mkdirSync(dest, { recursive: true });
|
|
10
|
+
}
|
|
11
|
+
if (!fs.existsSync(src)) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
let dirs = fs.readdirSync(src);
|
|
16
|
+
dirs.forEach((item) => {
|
|
17
|
+
let item_path = path.join(src, item);
|
|
18
|
+
let temp = fs.statSync(item_path);
|
|
19
|
+
|
|
20
|
+
if (temp.isFile()) {
|
|
21
|
+
fs.copyFileSync(item_path, path.join(dest, item));
|
|
22
|
+
} else if (temp.isDirectory()) {
|
|
23
|
+
copyDirectory(item_path, path.join(dest, item));
|
|
17
24
|
}
|
|
25
|
+
});
|
|
18
26
|
|
|
19
|
-
|
|
20
|
-
dirs.forEach((item) => {
|
|
21
|
-
|
|
22
|
-
let item_path = path.join(src, item);
|
|
23
|
-
let temp = fs.statSync(item_path);
|
|
24
|
-
|
|
25
|
-
if (temp.isFile()) {
|
|
26
|
-
|
|
27
|
-
fs.copyFileSync(item_path, path.join(dest, item));
|
|
28
|
-
} else if (temp.isDirectory()) {
|
|
29
|
-
|
|
30
|
-
copyDirectory(item_path, path.join(dest, item));
|
|
31
|
-
}
|
|
32
|
-
});
|
|
27
|
+
return true;
|
|
33
28
|
}
|
|
34
29
|
|
|
35
30
|
//递归删除文件夹
|
|
36
31
|
function delDirEctory(src) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
32
|
+
if (!fs.existsSync(src)) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
let srcstats = fs.statSync(src);
|
|
37
|
+
if (srcstats.isFile()) {
|
|
38
|
+
fs.rmSync(src);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
let dirs = fs.readdirSync(src);
|
|
43
|
+
dirs.forEach((item) => {
|
|
44
|
+
let item_path = path.join(src, item);
|
|
45
|
+
let temp = fs.statSync(item_path);
|
|
46
|
+
|
|
47
|
+
if (temp.isFile()) {
|
|
48
|
+
fs.rmSync(item_path);
|
|
49
|
+
} else if (temp.isDirectory()) {
|
|
50
|
+
delDirEctory(item_path);
|
|
41
51
|
}
|
|
52
|
+
});
|
|
42
53
|
|
|
43
|
-
|
|
44
|
-
if (srcstats.isFile()) {
|
|
45
|
-
|
|
46
|
-
fs.rmSync(src);
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
let dirs = fs.readdirSync(src);
|
|
51
|
-
dirs.forEach((item) => {
|
|
52
|
-
|
|
53
|
-
let item_path = path.join(src, item);
|
|
54
|
-
let temp = fs.statSync(item_path);
|
|
55
|
-
|
|
56
|
-
if (temp.isFile()) {
|
|
57
|
-
|
|
58
|
-
fs.rmSync(item_path);
|
|
59
|
-
} else if (temp.isDirectory()) {
|
|
60
|
-
|
|
61
|
-
delDirEctory(item_path);
|
|
62
|
-
}
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
fs.rmdirSync(src);
|
|
54
|
+
fs.rmdirSync(src);
|
|
66
55
|
}
|
|
67
56
|
|
|
68
57
|
async function unzipFile(src, dest) {
|
|
69
|
-
|
|
70
|
-
await compressing.zip.uncompress(src, dest);
|
|
58
|
+
await compressing.zip.uncompress(src, dest);
|
|
71
59
|
}
|
|
72
60
|
|
|
73
61
|
async function zipFile(src, dest) {
|
|
74
|
-
|
|
75
|
-
await compressing.zip.compressDir(src, dest);
|
|
62
|
+
await compressing.zip.compressDir(src, dest);
|
|
76
63
|
}
|
|
77
64
|
|
|
78
65
|
function readYaml(fp) {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
return yaml.parse(content);
|
|
66
|
+
let content = fs.readFileSync(fp, "utf-8");
|
|
67
|
+
return yaml.parse(content);
|
|
82
68
|
}
|
|
83
69
|
|
|
84
70
|
function writeYaml(fp, obj) {
|
|
85
|
-
|
|
86
|
-
fs.writeFileSync(fp, yaml.stringify(obj));
|
|
71
|
+
fs.writeFileSync(fp, yaml.stringify(obj));
|
|
87
72
|
}
|
|
88
73
|
|
|
89
74
|
module.exports = {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}
|
|
75
|
+
copyDirectory,
|
|
76
|
+
delDirEctory,
|
|
77
|
+
unzipFile,
|
|
78
|
+
zipFile,
|
|
79
|
+
readYaml,
|
|
80
|
+
writeYaml,
|
|
81
|
+
};
|