| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- const { execSync } = require('child_process');
- const fs = require('fs');
- const path = require('path');
- // 获取当前时间戳
- const now = new Date();
- 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')}`;
- // 自定义文字
- const customText = '后台端';
- // 定义路径和文件名
- const distPath = path.join(__dirname, '../dist');
- const originalZipName = 'dist.zip';
- const renamedZipName = `dist_${timestamp}_${customText}.zip`;
- // 等待 dist 文件夹存在
- function waitForDistFolder() {
- return new Promise((resolve) => {
- const checkDistExists = () => {
- if (fs.existsSync(distPath)) {
- console.log('📁 检测到 dist 文件夹');
- resolve(true);
- } else {
- console.log('⏳ 未检测到 dist 文件夹,5秒后重新检测...');
- setTimeout(checkDistExists, 5000);
- }
- };
- checkDistExists();
- });
- }
- // 主函数
- async function main() {
- try {
- // 等待 dist 文件夹出现
- await waitForDistFolder();
- // 使用 bestzip 打包 dist 文件夹为原始名称的 zip 文件
- execSync(`npx bestzip "${originalZipName}" "dist"`, {
- cwd: path.join(__dirname, '..'),
- stdio: 'inherit'
- });
- // 重命名 zip 文件
- fs.renameSync(originalZipName, renamedZipName);
- // 获取并打印文件详细信息
- const stats = fs.statSync(renamedZipName);
- console.log(`✅ 成功创建压缩包: ${renamedZipName}`);
- console.log(`📄 文件详细信息:`);
- console.log(` 大小: ${stats.size} 字节`);
- console.log(` 创建时间: ${stats.birthtime.toLocaleString()}`);
- console.log(` 修改时间: ${stats.mtime.toLocaleString()}`);
- } catch (error) {
- // 清理可能生成的临时文件
- if (fs.existsSync(originalZipName)) {
- fs.unlinkSync(originalZipName);
- }
- console.error('❌ 压缩失败:', error.message);
- }
- }
- // 启动主函数
- main();
|