AssetPro/electron-builder.config.js

131 lines
3.7 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* electron-builder 配置文件
* 处理 winCodeSign 解压时的符号链接问题
*/
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
// 修复 winCodeSign 解压问题
function fixWinCodeSign() {
const cacheDir = path.join(
process.env.LOCALAPPDATA || process.env.HOME,
'electron-builder',
'Cache',
'winCodeSign'
);
if (!fs.existsSync(cacheDir)) {
return;
}
const sevenZip = path.join(__dirname, 'node_modules', '7zip-bin', 'win', 'x64', '7za.exe');
if (!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', ''));
// 如果已经解压过,检查是否有 darwin 目录,如果有则删除
if (fs.existsSync(extractDir)) {
const darwinPath = path.join(extractDir, 'darwin');
if (fs.existsSync(darwinPath)) {
try {
fs.rmSync(darwinPath, { recursive: true, force: true });
console.log(`已删除 darwin 目录: ${darwinPath}`);
} catch (e) {
// 忽略删除错误
}
}
continue;
}
try {
console.log(`正在解压 ${archive},跳过符号链接...`);
// 使用 -snl 跳过符号链接,-xr!darwin 排除 darwin 目录
const cmd = `"${sevenZip}" x "${archivePath}" -o"${extractDir}" -snl -xr!darwin -y`;
execSync(cmd, { stdio: 'pipe', cwd: cacheDir });
console.log(`成功解压 ${archive}`);
} catch (error) {
// 如果解压失败,尝试只跳过符号链接
try {
console.log(`尝试使用 -snl 选项重新解压 ${archive}...`);
const cmd = `"${sevenZip}" x "${archivePath}" -o"${extractDir}" -snl -y`;
execSync(cmd, { stdio: 'pipe', cwd: cacheDir });
// 删除 darwin 目录Windows 上不需要)
const darwinPath = path.join(extractDir, 'darwin');
if (fs.existsSync(darwinPath)) {
try {
fs.rmSync(darwinPath, { recursive: true, force: true });
console.log(`已删除 darwin 目录`);
} catch (e) {
// 忽略删除错误
}
}
console.log(`成功解压 ${archive}`);
} catch (e) {
console.warn(`解压 ${archive} 时出错:`, e.message);
}
}
}
} catch (error) {
// 忽略错误,继续构建
console.warn('修复 winCodeSign 时出错:', error.message);
}
}
// 在配置加载时运行修复
fixWinCodeSign();
module.exports = {
appId: 'com.assetpro.app',
productName: 'AssetPro',
directories: {
output: 'dist',
buildResources: 'build'
},
files: [
'dist-electron/**/*',
'dist-renderer/**/*',
'package.json'
],
afterSign: null,
win: {
target: [
{
target: 'nsis',
arch: ['x64']
}
],
sign: null,
signingHashAlgorithms: [],
signDlls: false,
forceCodeSigning: false,
verifyUpdateCodeSignature: false
},
nsis: {
oneClick: false,
allowToChangeInstallationDirectory: true
},
beforePack: async (context) => {
// 在打包前再次尝试修复
fixWinCodeSign();
// 确保 dist-renderer 存在
const distRendererPath = path.join(__dirname, 'dist-renderer');
if (!fs.existsSync(distRendererPath)) {
console.warn('警告: dist-renderer 目录不存在,请先运行 vite build');
} else {
console.log('dist-renderer 目录存在,将被复制到 resources/dist');
}
}
};