build-zip.cjs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  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 newDistName = `dist_${timestamp}_${customText}`;
  11. const distPath = path.join(__dirname, '../dist');
  12. const newDistPath = path.join(__dirname, `../${newDistName}`);
  13. const zipFileName = `${newDistName}.zip`;
  14. // 重命名 dist 文件夹
  15. if (fs.existsSync(distPath)) {
  16. fs.renameSync(distPath, newDistPath);
  17. console.log(`📁 文件夹已重命名为: ${newDistName}`);
  18. }
  19. // 使用 bestzip 打包为 zip 文件
  20. try {
  21. execSync(`npx bestzip "${zipFileName}" "${newDistName}"`, {
  22. cwd: path.join(__dirname, '..'),
  23. stdio: 'inherit'
  24. });
  25. fs.rmdirSync(newDistPath, { recursive: true });
  26. console.log(`✅ 成功创建压缩包: ${zipFileName}`);
  27. } catch (error) {
  28. fs.rmdirSync(newDistPath, { recursive: true });
  29. console.error('❌ 压缩失败:', error.message);
  30. }