105 lines
2.9 KiB
JavaScript
105 lines
2.9 KiB
JavaScript
/**
|
|
* 监控并修复 winCodeSign 解压问题
|
|
* 在 electron-builder 运行期间持续检查并修复
|
|
*/
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const { execSync } = require('child_process');
|
|
|
|
const cacheDir = path.join(
|
|
process.env.LOCALAPPDATA || process.env.HOME,
|
|
'electron-builder',
|
|
'Cache',
|
|
'winCodeSign'
|
|
);
|
|
|
|
const sevenZip = path.join(__dirname, '..', 'node_modules', '7zip-bin', 'win', 'x64', '7za.exe');
|
|
|
|
function fixArchive(archivePath, extractDir) {
|
|
if (!fs.existsSync(sevenZip)) {
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
// 首先尝试跳过符号链接和 darwin 目录
|
|
const cmd = `"${sevenZip}" x "${archivePath}" -o"${extractDir}" -snl -xr!darwin -y`;
|
|
execSync(cmd, { stdio: 'pipe', cwd: path.dirname(archivePath) });
|
|
return true;
|
|
} catch (error) {
|
|
try {
|
|
// 如果失败,只跳过符号链接
|
|
const cmd = `"${sevenZip}" x "${archivePath}" -o"${extractDir}" -snl -y`;
|
|
execSync(cmd, { stdio: 'pipe', cwd: path.dirname(archivePath) });
|
|
// 删除 darwin 目录
|
|
const darwinPath = path.join(extractDir, 'darwin');
|
|
if (fs.existsSync(darwinPath)) {
|
|
try {
|
|
fs.rmSync(darwinPath, { recursive: true, force: true });
|
|
} catch (e) {
|
|
// 忽略删除错误
|
|
}
|
|
}
|
|
return true;
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
function watchAndFix() {
|
|
if (!fs.existsSync(cacheDir)) {
|
|
return;
|
|
}
|
|
|
|
const checkInterval = setInterval(() => {
|
|
try {
|
|
const files = fs.readdirSync(cacheDir);
|
|
const archives = files.filter(f => f.endsWith('.7z'));
|
|
|
|
for (const archive of archives) {
|
|
const archivePath = path.join(cacheDir, archive);
|
|
const extractDir = path.join(cacheDir, archive.replace('.7z', ''));
|
|
|
|
// 如果 .7z 文件存在但解压目录不存在或损坏,尝试修复
|
|
if (fs.existsSync(archivePath)) {
|
|
const stats = fs.statSync(archivePath);
|
|
const archiveTime = stats.mtime.getTime();
|
|
|
|
// 检查解压目录是否存在且完整
|
|
let needsFix = false;
|
|
if (!fs.existsSync(extractDir)) {
|
|
needsFix = true;
|
|
} else {
|
|
// 检查是否有 darwin 目录(可能导致问题)
|
|
const darwinPath = path.join(extractDir, 'darwin');
|
|
if (fs.existsSync(darwinPath)) {
|
|
needsFix = true;
|
|
}
|
|
}
|
|
|
|
if (needsFix) {
|
|
console.log(`检测到需要修复的归档: ${archive}`);
|
|
if (fixArchive(archivePath, extractDir)) {
|
|
console.log(`成功修复: ${archive}`);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} catch (error) {
|
|
// 忽略错误,继续监控
|
|
}
|
|
}, 1000); // 每秒检查一次
|
|
|
|
// 30 秒后停止监控(构建应该已经完成或失败)
|
|
setTimeout(() => {
|
|
clearInterval(checkInterval);
|
|
}, 30000);
|
|
}
|
|
|
|
if (require.main === module) {
|
|
watchAndFix();
|
|
}
|
|
|
|
module.exports = { watchAndFix, fixArchive };
|
|
|