build-zip.cjs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. const { execSync } = require('child_process');
  2. const fs = require('fs');
  3. const path = require('path');
  4. // 获取当前时间戳
  5. const now = new Date();
  6. const timestamp = `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, '0')}${String(now.getDate()).padStart(2, '0')}_${String(now.getHours()).padStart(2, '0')}${String(now.getMinutes()).padStart(2, '0')}${String(now.getSeconds()).padStart(2, '0')}`;
  7. // 自定义文字
  8. const customText = '教师端';
  9. // 定义路径和文件名
  10. const distPath = path.join(__dirname, '../dist');
  11. const originalZipName = 'dist.zip';
  12. const renamedZipName = `dist_${timestamp}_${customText}.zip`;
  13. // 等待 dist 文件夹存在
  14. function waitForDistFolder() {
  15. return new Promise((resolve) => {
  16. const checkDistExists = () => {
  17. if (fs.existsSync(distPath)) {
  18. console.log('📁 检测到 dist 文件夹');
  19. resolve(true);
  20. } else {
  21. console.log('⏳ 未检测到 dist 文件夹,5秒后重新检测...');
  22. setTimeout(checkDistExists, 5000);
  23. }
  24. };
  25. checkDistExists();
  26. });
  27. }
  28. // 主函数
  29. async function main() {
  30. try {
  31. // 等待 dist 文件夹出现
  32. await waitForDistFolder();
  33. // 使用 bestzip 打包 dist 文件夹为原始名称的 zip 文件
  34. execSync(`npx bestzip "${originalZipName}" "dist"`, {
  35. cwd: path.join(__dirname, '..'),
  36. stdio: 'inherit'
  37. });
  38. // 重命名 zip 文件
  39. fs.renameSync(originalZipName, renamedZipName);
  40. // 获取并打印文件详细信息
  41. const stats = fs.statSync(renamedZipName);
  42. console.log(`✅ 成功创建压缩包: ${renamedZipName}`);
  43. console.log(`📄 文件详细信息:`);
  44. console.log(` 大小: ${stats.size} 字节`);
  45. console.log(` 创建时间: ${stats.birthtime.toLocaleString()}`);
  46. console.log(` 修改时间: ${stats.mtime.toLocaleString()}`);
  47. } catch (error) {
  48. // 清理可能生成的临时文件
  49. if (fs.existsSync(originalZipName)) {
  50. fs.unlinkSync(originalZipName);
  51. }
  52. console.error('❌ 压缩失败:', error.message);
  53. }
  54. }
  55. // 启动主函数
  56. main();