import archiver from 'archiver'; import fs from 'fs'; import path from 'path'; import * as pkgs from './package.json'; const appName = 'router-template' /** * 创建 ZIP 压缩包 * @param outputPath - 输出的 zip 文件路径 * @param files - 要压缩的文件列表 * @param directories - 要压缩的目录列表 */ export async function createZip( outputPath: string, files: string[] = [], directories: string[] = [] ): Promise { return new Promise((resolve, reject) => { // 创建输出流 const output = fs.createWriteStream(outputPath); const archive = archiver('zip', { zlib: { level: 9 } // 最高压缩级别 }); // 监听完成事件 output.on('close', () => { console.log(`✅ 压缩完成: ${archive.pointer()} bytes`); console.log(`📦 输出文件: ${outputPath}`); resolve(); }); // 监听警告 archive.on('warning', (err) => { if (err.code === 'ENOENT') { console.warn('⚠️ 警告:', err); } else { reject(err); } }); // 监听错误 archive.on('error', (err) => { reject(err); }); // 连接输出流 archive.pipe(output); // 添加文件 for (const file of files) { if (fs.existsSync(file)) { archive.file(file, { name: path.basename(file) }); console.log(`📄 添加文件: ${file}`); } else { console.warn(`⚠️ 文件不存在: ${file}`); } } // 添加目录 for (const dir of directories) { if (fs.existsSync(dir)) { archive.directory(dir, path.basename(dir)); console.log(`📁 添加目录: ${dir}`); } else { console.warn(`⚠️ 目录不存在: ${dir}`); } } // 完成归档 archive.finalize(); }); } /** * 为项目创建发布包 */ export async function createReleaseZip(): Promise { const files = [ `${appName}.exe`, 'readme.md' ]; const directories = [ 'public' ]; await createZip(`${appName}-${pkgs.version}.zip`, files, directories); } // 如果直接运行此文件 if (import.meta.main) { createReleaseZip() .then(() => console.log('🎉 打包成功!')) .catch((err) => console.error('❌ 打包失败:', err)); }