109 lines
3.2 KiB
JavaScript
109 lines
3.2 KiB
JavaScript
const fs = require('fs');
|
||
const path = require('path');
|
||
const { execSync } = require('child_process');
|
||
|
||
// 创建一个兼容Node.js 12的删除文件夹函数
|
||
function deleteFolderRecursive(dir) {
|
||
if (fs.existsSync(dir)) {
|
||
const files = fs.readdirSync(dir);
|
||
for (let i = 0; i < files.length; i++) {
|
||
const file = files[i];
|
||
const curPath = path.join(dir, file);
|
||
if (fs.lstatSync(curPath).isDirectory()) {
|
||
// 递归删除子文件夹
|
||
deleteFolderRecursive(curPath);
|
||
} else {
|
||
// 删除文件
|
||
fs.unlinkSync(curPath);
|
||
}
|
||
}
|
||
// 删除空文件夹
|
||
fs.rmdirSync(dir);
|
||
}
|
||
}
|
||
|
||
// 创建打包前的准备工作
|
||
function prepareForBuild() {
|
||
console.log('开始准备Windows 7便携应用打包...');
|
||
|
||
try {
|
||
// 清理之前的构建文件 - 使用兼容Node.js 12的方法
|
||
const buildDir = path.join(__dirname, 'dist_electron');
|
||
if (fs.existsSync(buildDir)) {
|
||
console.log('清理之前的构建文件...');
|
||
deleteFolderRecursive(buildDir);
|
||
}
|
||
|
||
// 确保data目录存在(便携应用需要)
|
||
const dataDir = path.join(__dirname, 'data');
|
||
if (!fs.existsSync(dataDir)) {
|
||
fs.mkdirSync(dataDir);
|
||
fs.writeFileSync(path.join(dataDir, '.gitignore'), '*\n!.gitignore');
|
||
console.log('创建data目录完成');
|
||
}
|
||
|
||
// 确保portable-app目录存在
|
||
const portableDir = path.join(buildDir, 'portable-app');
|
||
if (!fs.existsSync(portableDir)) {
|
||
fs.mkdirSync(portableDir, { recursive: true });
|
||
console.log('创建portable-app目录完成');
|
||
}
|
||
|
||
console.log('准备工作完成');
|
||
} catch (error) {
|
||
console.error('准备工作失败:', error);
|
||
process.exit(1);
|
||
}
|
||
}
|
||
|
||
// 执行打包命令 - 移除不支持的--no-sign参数
|
||
function buildPortableApp() {
|
||
console.log('开始构建Windows 7便携应用...');
|
||
|
||
try {
|
||
// 完全删除签名环境变量
|
||
delete process.env.WIN_CSC_LINK;
|
||
delete process.env.WIN_CSC_KEY_PASSWORD;
|
||
// 设置环境变量明确禁用签名相关功能
|
||
process.env.ELECTRON_BUILDER_SKIP_NOTARIZATION = 'true';
|
||
process.env.ELECTRON_SKIP_NOTARIZE = 'true';
|
||
|
||
// 使用最基本的命令行参数,移除不支持的--no-sign
|
||
execSync(
|
||
'npm run electron:build -- --win portable --ia32 --x64 --publish never',
|
||
{ stdio: 'inherit' }
|
||
);
|
||
|
||
console.log('Windows 7便携应用构建完成!');
|
||
console.log('构建产物位于 dist_electron 目录');
|
||
|
||
// ===== 移除data目录复制逻辑 =====
|
||
|
||
// 构建完成后的其他处理(如果有)
|
||
console.log('便携应用构建流程完成');
|
||
console.log('data目录结构准备完成!');
|
||
console.log('最终目录结构:');
|
||
console.log(`- dist_electron/`);
|
||
console.log(` - portable-app/`);
|
||
console.log(` - StatExamPortable_${require('./package.json').version}_*.exe`);
|
||
|
||
} catch (error) {
|
||
console.error('构建失败:', error);
|
||
process.exit(1);
|
||
}
|
||
}
|
||
|
||
// 主函数
|
||
function main() {
|
||
console.log('==== Windows 7便携应用打包工具 ====');
|
||
console.log('Node.js版本:', process.version);
|
||
console.log('Electron版本: 11.5.0');
|
||
console.log('Vue版本: 2.6.11');
|
||
|
||
prepareForBuild();
|
||
buildPortableApp();
|
||
}
|
||
|
||
// 执行主函数
|
||
main();
|