package.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import {execSync} from 'child_process';
  2. import {existsSync, readdirSync, statSync} from 'fs';
  3. import {join, basename, dirname, resolve, relative} from 'path';
  4. import url from 'url';
  5. // 要排除的目录列表
  6. const EXCLUDE_DIRS = ['.git', '.idea', 'soft', 'pyTools', 'drop_code', 'jstest', 'local', 'logs', '对话1.txt', 'vod_cache', 'data/mv'];
  7. // 要排除的文件列表
  8. const EXCLUDE_FILES = ['config/env.json', '.env', 'js/UC分享.js', 'js/百忙无果[官].js', 'json/UC分享.json', 'jx/奇奇.js', 'jx/芒果关姐.js', 'data/settings/link_data.json', 'index.json', 'custom.json'];
  9. // 获取脚本所在目录
  10. const getScriptDir = () => dirname(resolve(url.fileURLToPath(import.meta.url)));
  11. // 筛选带 [密] 的文件
  12. const filterGreenFiles = (scriptDir) => {
  13. const jsDir = join(scriptDir, 'js');
  14. const greenFiles = [];
  15. if (existsSync(jsDir)) {
  16. const stack = [jsDir];
  17. while (stack.length) {
  18. const currentDir = stack.pop();
  19. const items = readdirSync(currentDir);
  20. for (const item of items) {
  21. const fullPath = join(currentDir, item);
  22. const stats = statSync(fullPath);
  23. if (stats.isDirectory()) {
  24. stack.push(fullPath);
  25. } else if (/\[密[^\]]*\]/.test(item)) {
  26. greenFiles.push(relative(scriptDir, fullPath));
  27. }
  28. }
  29. }
  30. }
  31. return greenFiles;
  32. };
  33. // 压缩目录
  34. const compressDirectory = (scriptDir, green) => {
  35. const currentDir = basename(scriptDir);
  36. const currentTime = new Date().toLocaleDateString('zh-CN', {
  37. year: 'numeric',
  38. month: '2-digit',
  39. day: '2-digit'
  40. }).replace(/\//g, '');
  41. const archiveSuffix = green ? '-green' : '';
  42. const archiveName = `${currentDir}-${currentTime}${archiveSuffix}.7z`;
  43. const parentDir = resolve(scriptDir, '..');
  44. const archivePath = join(parentDir, archiveName);
  45. // 构建 7z 命令
  46. const excludeParams = [];
  47. // 排除目录
  48. for (const excludeDir of EXCLUDE_DIRS) {
  49. excludeParams.push(`-xr!${excludeDir}`);
  50. }
  51. // 排除文件
  52. for (const excludeFile of EXCLUDE_FILES) {
  53. const excludeFilePath = join(scriptDir, excludeFile);
  54. if (existsSync(excludeFilePath)) {
  55. excludeParams.push(`-xr!${excludeFile}`);
  56. } else {
  57. console.warn(`警告: ${excludeFile} 不存在!`);
  58. }
  59. }
  60. // 如果启用 green 筛选,排除不符合条件的文件
  61. if (green) {
  62. const greenFiles = filterGreenFiles(scriptDir);
  63. for (const file of greenFiles) {
  64. excludeParams.push(`-x!${file}`);
  65. }
  66. }
  67. // 构建命令,打包目录内容而不包含目录本身
  68. const command = `7z a "${archivePath}" "${join(scriptDir, '*')}" -r ${excludeParams.join(' ')}`;
  69. console.log(`构建的 7z 命令: ${command}`);
  70. try {
  71. execSync(command, {stdio: 'inherit'});
  72. console.log(`压缩完成: ${archivePath}`);
  73. } catch (error) {
  74. console.error(`压缩失败: ${error.message}`);
  75. }
  76. };
  77. // 主程序入口
  78. const main = () => {
  79. const scriptDir = getScriptDir();
  80. // 简单解析命令行参数
  81. const args = process.argv.slice(2);
  82. const green = args.includes('-g') || args.includes('--green');
  83. compressDirectory(scriptDir, green);
  84. };
  85. main();