62 lines
2.1 KiB
JavaScript
62 lines
2.1 KiB
JavaScript
/**
|
|
* 修复 winCodeSign 解压时的符号链接问题
|
|
* 在 electron-builder 尝试解压之前,手动解压并跳过 darwin 目录
|
|
*/
|
|
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');
|
|
|
|
// 查找所有 .7z 文件
|
|
function findAndFixArchives() {
|
|
if (!fs.existsSync(cacheDir)) {
|
|
return;
|
|
}
|
|
|
|
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', ''));
|
|
|
|
// 如果已经解压过,跳过
|
|
if (fs.existsSync(extractDir)) {
|
|
continue;
|
|
}
|
|
|
|
try {
|
|
console.log(`正在解压 ${archive},跳过符号链接...`);
|
|
// 使用 -snl 跳过符号链接,-xr!darwin 排除 darwin 目录
|
|
const cmd = `"${sevenZip}" x "${archivePath}" -o"${extractDir}" -snl -xr!darwin -y`;
|
|
execSync(cmd, { stdio: 'inherit', cwd: cacheDir });
|
|
console.log(`成功解压 ${archive}`);
|
|
} catch (error) {
|
|
// 如果失败,尝试只跳过符号链接
|
|
try {
|
|
console.log(`尝试使用 -snl 选项重新解压...`);
|
|
const cmd = `"${sevenZip}" x "${archivePath}" -o"${extractDir}" -snl -y`;
|
|
execSync(cmd, { stdio: 'inherit', cwd: cacheDir });
|
|
// 删除 darwin 目录
|
|
const darwinPath = path.join(extractDir, 'darwin');
|
|
if (fs.existsSync(darwinPath)) {
|
|
fs.rmSync(darwinPath, { recursive: true, force: true });
|
|
}
|
|
console.log(`成功解压 ${archive}`);
|
|
} catch (e) {
|
|
console.warn(`解压 ${archive} 时出错:`, e.message);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 在构建前运行
|
|
if (require.main === module) {
|
|
findAndFixArchives();
|
|
}
|
|
|
|
module.exports = { findAndFixArchives };
|
|
|