-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathpackage-bundle.js
More file actions
84 lines (70 loc) · 2.61 KB
/
package-bundle.js
File metadata and controls
84 lines (70 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import {execSync} from 'child_process';
import {existsSync, statSync} from 'fs';
import {join, basename, dirname, resolve} from 'path';
import url from 'url';
// 要打包的文件/目录列表 (相对于 drpy-node-bundle 目录)
const INCLUDE_ITEMS = [
'libs',
'spider',
'localt5.js',
'localDsCoreTest.js'
];
// 获取脚本所在目录 (e:\gitwork\drpy-node)
const getScriptDir = () => dirname(resolve(url.fileURLToPath(import.meta.url)));
// 压缩 drpy-node-bundle 目录
const compressBundle = (scriptDir) => {
// drpy-node-bundle 目录路径
const bundleDir = join(scriptDir, 'drpy-node-bundle');
if (!existsSync(bundleDir)) {
console.error(`错误: 目录 ${bundleDir} 不存在!`);
return;
}
// 生成压缩包名称
const currentDirName = basename(bundleDir); // drpy-node-bundle
const currentTime = new Date().toLocaleDateString('zh-CN', {
year: 'numeric',
month: '2-digit',
day: '2-digit'
}).replace(/\//g, '');
const archiveName = `${currentDirName}-${currentTime}.7z`;
// 输出路径 (与 drpy-node 的打包输出位置一致,即项目根目录的上一级)
const parentDir = resolve(scriptDir, '..');
const archivePath = join(parentDir, archiveName);
// 构建包含参数
const includeParams = [];
for (const item of INCLUDE_ITEMS) {
const itemPath = join(bundleDir, item);
if (existsSync(itemPath)) {
// 使用相对路径,以便在 cwd 切换后正确引用
includeParams.push(`"${item}"`);
} else {
console.warn(`警告: ${item} 不存在于 ${bundleDir},将被跳过。`);
}
}
if (includeParams.length === 0) {
console.error('错误: 没有找到需要打包的文件或目录!');
return;
}
// 构建 7z 命令
// 使用 cd 切换到 bundleDir 目录,这样打包的内容就是相对路径(不包含 drpy-node-bundle 目录本身)
// 7z a "archivePath" item1 item2 ...
const command = `7z a -t7z "${archivePath}" ${includeParams.join(' ')}`;
console.log(`正在打包到: ${archivePath}`);
console.log(`执行命令 (在 ${bundleDir} 下): ${command}`);
try {
// 在 bundleDir 目录下执行命令
execSync(command, {
stdio: 'inherit',
cwd: bundleDir
});
console.log(`压缩完成: ${archivePath}`);
} catch (error) {
console.error(`压缩失败: ${error.message}`);
}
};
// 主程序入口
const main = () => {
const scriptDir = getScriptDir();
compressBundle(scriptDir);
};
main();