Files
cstb-next/scripts/rename-build-artifacts.js

143 lines
4.1 KiB
JavaScript
Raw Normal View History

#!/usr/bin/env node
/**
* 批量重命名构建产物将文件名中的 "CS工具箱" 改为 "CS_Toolbox"
* 用于解决 GitHub releases 不支持中文文件名的问题
*
* 使用方法:
* node scripts/rename-build-artifacts.js [options]
*
* 选项:
* --target <target> 构建类型可选默认 release可选值debugreleasefast-release
* --dry-run 仅显示将要重命名的文件不实际执行
*/
const fs = require('fs');
const path = require('path');
// 解析命令行参数
const args = process.argv.slice(2);
const getArg = (name, defaultValue = null) => {
const index = args.indexOf(name);
if (index !== -1 && args[index + 1]) {
return args[index + 1];
}
return defaultValue;
};
const target = getArg('--target', 'release');
const dryRun = args.includes('--dry-run');
// 验证 target 参数
const validTargets = ['debug', 'release', 'fast-release'];
if (!validTargets.includes(target)) {
console.error(`错误: --target 必须是以下值之一: ${validTargets.join(', ')}`);
process.exit(1);
}
// 根据 target 确定构建产物目录
const bundleDir = path.join(__dirname, '../src-tauri/target', target, 'bundle');
console.log(`使用构建类型: ${target}`);
console.log(`构建产物目录: ${bundleDir}`);
console.log(dryRun ? '(仅预览模式,不会实际重命名)' : '');
if (!fs.existsSync(bundleDir)) {
console.error(`错误: 构建产物目录不存在: ${bundleDir}`);
console.error('请先构建应用');
process.exit(1);
}
// 需要重命名的文件扩展名
const extensions = ['.exe', '.dmg', '.AppImage', '.deb', '.rpm', '.app.tar.gz', '.sig'];
// 递归查找并重命名文件
function renameFilesRecursive(dir, maxDepth = 3, currentDepth = 0) {
if (!fs.existsSync(dir) || currentDepth > maxDepth) {
return [];
}
const renamedFiles = [];
try {
const items = fs.readdirSync(dir);
for (const item of items) {
const itemPath = path.join(dir, item);
const stat = fs.statSync(itemPath);
if (stat.isFile()) {
// 检查文件名是否包含 "CS工具箱"
if (item.includes('CS工具箱')) {
const newName = item.replace(/CS工具箱/g, 'CS_Toolbox');
const newPath = path.join(dir, newName);
// 检查新文件名是否已存在
if (fs.existsSync(newPath) && itemPath !== newPath) {
console.warn(` 警告: 目标文件已存在,跳过: ${newName}`);
continue;
}
renamedFiles.push({
oldPath: itemPath,
newPath: newPath,
oldName: item,
newName: newName
});
}
} else if (stat.isDirectory() && currentDepth < maxDepth) {
// 递归查找子目录
const subRenamed = renameFilesRecursive(itemPath, maxDepth, currentDepth + 1);
renamedFiles.push(...subRenamed);
}
}
} catch (err) {
console.error(`读取目录时出错 ${dir}:`, err.message);
}
return renamedFiles;
}
// 查找所有需要重命名的文件
console.log('\n查找需要重命名的文件...');
const filesToRename = renameFilesRecursive(bundleDir);
if (filesToRename.length === 0) {
console.log('✓ 未找到需要重命名的文件');
process.exit(0);
}
// 显示将要重命名的文件
console.log(`\n找到 ${filesToRename.length} 个文件需要重命名:\n`);
filesToRename.forEach(({ oldName, newName }) => {
console.log(` ${oldName}`);
console.log(`${newName}\n`);
});
if (dryRun) {
console.log('(预览模式,未实际执行重命名)');
process.exit(0);
}
// 执行重命名
console.log('执行重命名...');
let successCount = 0;
let errorCount = 0;
for (const { oldPath, newPath, oldName, newName } of filesToRename) {
try {
fs.renameSync(oldPath, newPath);
console.log(`${oldName}${newName}`);
successCount++;
} catch (err) {
console.error(`✗ 重命名失败 ${oldName}:`, err.message);
errorCount++;
}
}
console.log(`\n完成: 成功 ${successCount} 个,失败 ${errorCount}`);
if (errorCount > 0) {
process.exit(1);
}