118 lines
3.4 KiB
JavaScript
118 lines
3.4 KiB
JavaScript
/**
|
|
* 构建脚本包装器
|
|
* 在 electron-builder 运行后自动修复 winCodeSign 解压问题
|
|
*/
|
|
const { spawn } = require('child_process');
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
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 fixWinCodeSign() {
|
|
if (!fs.existsSync(cacheDir) || !fs.existsSync(sevenZip)) {
|
|
return;
|
|
}
|
|
|
|
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 darwinPath = path.join(extractDir, 'darwin');
|
|
const needsFix = !fs.existsSync(extractDir) ||
|
|
(fs.existsSync(extractDir) && fs.existsSync(darwinPath));
|
|
|
|
if (needsFix) {
|
|
try {
|
|
console.log(`修复 winCodeSign 归档: ${archive}`);
|
|
// 删除旧的解压目录
|
|
if (fs.existsSync(extractDir)) {
|
|
try {
|
|
fs.rmSync(extractDir, { recursive: true, force: true });
|
|
} catch (e) {
|
|
// 忽略删除错误
|
|
}
|
|
}
|
|
// 重新解压,跳过符号链接和 darwin 目录
|
|
const cmd = `"${sevenZip}" x "${archivePath}" -o"${extractDir}" -snl -xr!darwin -y`;
|
|
execSync(cmd, { stdio: 'pipe', cwd: cacheDir });
|
|
console.log(`成功修复: ${archive}`);
|
|
} catch (error) {
|
|
// 如果失败,尝试只跳过符号链接
|
|
try {
|
|
const cmd = `"${sevenZip}" x "${archivePath}" -o"${extractDir}" -snl -y`;
|
|
execSync(cmd, { stdio: 'pipe', cwd: cacheDir });
|
|
if (fs.existsSync(darwinPath)) {
|
|
fs.rmSync(darwinPath, { recursive: true, force: true });
|
|
}
|
|
console.log(`成功修复: ${archive}`);
|
|
} catch (e) {
|
|
console.warn(`修复 ${archive} 失败:`, e.message);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} catch (error) {
|
|
// 忽略错误
|
|
}
|
|
}
|
|
|
|
// 运行 electron-builder
|
|
const electronBuilder = spawn('electron-builder', process.argv.slice(2), {
|
|
stdio: 'inherit',
|
|
shell: true,
|
|
env: {
|
|
...process.env,
|
|
CSC_IDENTITY_AUTO_DISCOVERY: 'false'
|
|
}
|
|
});
|
|
|
|
// 监听 electron-builder 的输出,当检测到解压错误时尝试修复
|
|
let outputBuffer = '';
|
|
electronBuilder.stdout?.on('data', (data) => {
|
|
const text = data.toString();
|
|
outputBuffer += text;
|
|
|
|
// 检测到解压错误
|
|
if (text.includes('Cannot create symbolic link') ||
|
|
text.includes('Sub items Errors')) {
|
|
setTimeout(() => {
|
|
fixWinCodeSign();
|
|
}, 1000);
|
|
}
|
|
});
|
|
|
|
electronBuilder.stderr?.on('data', (data) => {
|
|
const text = data.toString();
|
|
outputBuffer += text;
|
|
|
|
// 检测到解压错误
|
|
if (text.includes('Cannot create symbolic link') ||
|
|
text.includes('Sub items Errors')) {
|
|
setTimeout(() => {
|
|
fixWinCodeSign();
|
|
}, 1000);
|
|
}
|
|
});
|
|
|
|
electronBuilder.on('close', (code) => {
|
|
// 构建完成后再次尝试修复
|
|
fixWinCodeSign();
|
|
process.exit(code);
|
|
});
|
|
|