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