-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathpackage.js
102 lines (85 loc) · 3.29 KB
/
package.js
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import {execSync} from 'child_process';
import {existsSync, readdirSync, statSync} from 'fs';
import {join, basename, dirname, resolve, relative} from 'path';
import url from 'url';
// 要排除的目录列表
const EXCLUDE_DIRS = ['.git', '.idea', 'soft', 'pyTools', 'drop_code', 'jstest', 'local', 'logs', '对话1.txt', 'vod_cache'];
// 要排除的文件列表
const EXCLUDE_FILES = ['config/env.json', '.env', 'js/UC分享.js', 'js/百忙无果[官].js', 'json/UC分享.json', 'jx/奇奇.js', 'data/settings/link_data.json', 'index.json', 'custom.json'];
// 获取脚本所在目录
const getScriptDir = () => dirname(resolve(url.fileURLToPath(import.meta.url)));
// 筛选带 [密] 的文件
const filterGreenFiles = (scriptDir) => {
const jsDir = join(scriptDir, 'js');
const greenFiles = [];
if (existsSync(jsDir)) {
const stack = [jsDir];
while (stack.length) {
const currentDir = stack.pop();
const items = readdirSync(currentDir);
for (const item of items) {
const fullPath = join(currentDir, item);
const stats = statSync(fullPath);
if (stats.isDirectory()) {
stack.push(fullPath);
} else if (/\[密[^\]]*\]/.test(item)) {
greenFiles.push(relative(scriptDir, fullPath));
}
}
}
}
return greenFiles;
};
// 压缩目录
const compressDirectory = (scriptDir, green) => {
const currentDir = basename(scriptDir);
const currentTime = new Date().toLocaleDateString('zh-CN', {
year: 'numeric',
month: '2-digit',
day: '2-digit'
}).replace(/\//g, '');
const archiveSuffix = green ? '-green' : '';
const archiveName = `${currentDir}-${currentTime}${archiveSuffix}.7z`;
const parentDir = resolve(scriptDir, '..');
const archivePath = join(parentDir, archiveName);
// 构建 7z 命令
const excludeParams = [];
// 排除目录
for (const excludeDir of EXCLUDE_DIRS) {
excludeParams.push(`-xr!${excludeDir}`);
}
// 排除文件
for (const excludeFile of EXCLUDE_FILES) {
const excludeFilePath = join(scriptDir, excludeFile);
if (existsSync(excludeFilePath)) {
excludeParams.push(`-xr!${excludeFile}`);
} else {
console.warn(`警告: ${excludeFile} 不存在!`);
}
}
// 如果启用 green 筛选,排除不符合条件的文件
if (green) {
const greenFiles = filterGreenFiles(scriptDir);
for (const file of greenFiles) {
excludeParams.push(`-x!${file}`);
}
}
// 构建命令,打包目录内容而不包含目录本身
const command = `7z a "${archivePath}" "${join(scriptDir, '*')}" -r ${excludeParams.join(' ')}`;
console.log(`构建的 7z 命令: ${command}`);
try {
execSync(command, {stdio: 'inherit'});
console.log(`压缩完成: ${archivePath}`);
} catch (error) {
console.error(`压缩失败: ${error.message}`);
}
};
// 主程序入口
const main = () => {
const scriptDir = getScriptDir();
// 简单解析命令行参数
const args = process.argv.slice(2);
const green = args.includes('-g') || args.includes('--green');
compressDirectory(scriptDir, green);
};
main();