fix: 修复release.js 默认没有清理临时文件的问题

This commit is contained in:
2025-12-23 09:52:12 +08:00
parent a59ce3c718
commit 688c4b952e

View File

@@ -211,12 +211,13 @@ function createZipArchive(sourceDir, outputPath) {
try {
// 根据操作系统选择命令
if (process.platform === 'win32') {
// Windows系统使用PowerShell命令
const command = `Compress-Archive -Path "${sourceDir}\*" -DestinationPath "${outputPath}" -Force`;
execSync(command, { shell: 'powershell.exe' });
// Windows系统使用PowerShell命令,排除文件名后缀为 -mp-weixin.zip 的文件
// 使用PowerShell的管道功能和Where-Object来排除特定文件
const command = `PowerShell.exe -Command "Get-ChildItem -Path \"${sourceDir}\" | Where-Object { $_.Name -notlike '*\-mp\-weixin\.zip' } | Compress-Archive -DestinationPath \"${outputPath}\" -Force"`;
execSync(command, { shell: 'cmd.exe' });
} else {
// Linux/Mac系统使用zip命令
const command = `zip -r "${outputPath}" * -d "${sourceDir}"`;
// Linux/Mac系统使用zip命令,排除文件名后缀为 -mp-weixin.zip 的文件
const command = `zip -r "${outputPath}" * -x "*-mp-weixin.zip"`;
execSync(command, { cwd: sourceDir });
}
console.log(`Zip archive created: ${outputPath}`);
@@ -226,6 +227,31 @@ function createZipArchive(sourceDir, outputPath) {
}
}
function cleanDistDir(distDir) {
if (fs.existsSync(distDir)) {
// 清理dist目录下除**-mp-weixin.zip文件外的所有内容
const files = fs.readdirSync(distDir);
files.forEach(file => {
const filePath = path.join(distDir, file);
const stats = fs.statSync(filePath);
// 只保留符合特定命名模式的zip文件**-mp-weixin.zip
if (stats.isFile() && path.extname(file) === '.zip' && file.endsWith('-mp-weixin.zip')) {
console.log(`Keeping zip file: ${file}`);
} else {
if (stats.isDirectory()) {
fs.rmSync(filePath, { recursive: true, force: true });
console.log(`Removed directory: ${file}`);
} else {
fs.unlinkSync(filePath);
console.log(`Removed file: ${file}`);
}
}
});
console.log('Cleaned dist directory (excluding -mp-weixin.zip files)');
}
}
// 主函数
function main() {
try {
@@ -235,30 +261,7 @@ function main() {
// 清理dist目录如果存在且不保留
if (fs.existsSync(distDir)) {
if (keepDist) {
console.log('Keeping existing dist directory contents');
} else {
// 清理dist目录下除zip文件外的所有内容
const files = fs.readdirSync(distDir);
files.forEach(file => {
const filePath = path.join(distDir, file);
const stats = fs.statSync(filePath);
// 只保留zip文件
if (stats.isFile() && path.extname(file) === '.zip') {
console.log(`Keeping zip file: ${file}`);
} else {
if (stats.isDirectory()) {
fs.rmSync(filePath, { recursive: true, force: true });
console.log(`Removed directory: ${file}`);
} else {
fs.unlinkSync(filePath);
console.log(`Removed file: ${file}`);
}
}
});
console.log('Cleaned dist directory (excluding zip files)');
}
cleanDistDir(distDir);
} else {
// 创建dist目录
fs.mkdirSync(distDir, { recursive: true });
@@ -278,6 +281,11 @@ function main() {
// 创建zip压缩包
console.log('Creating zip archive...');
createZipArchive(distDir, zipPath);
// 清理dist目录如果不保留
if (!keepDist) {
cleanDistDir(distDir);
}
console.log('\nAll tasks completed successfully!');
console.log(`Dist directory: ${distDir}`);