142 lines
4.5 KiB
JavaScript
142 lines
4.5 KiB
JavaScript
const fs = require('fs'); // 添加缺失的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目录完成');
|
||
}
|
||
|
||
// 创建便携模式标识文件
|
||
const portableFlagPath = path.join(__dirname, 'portable.txt');
|
||
if (!fs.existsSync(portableFlagPath)) {
|
||
fs.writeFileSync(portableFlagPath, '');
|
||
console.log('创建便携模式标识文件完成');
|
||
}
|
||
|
||
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';
|
||
|
||
// 使用最基本的命令行参数
|
||
execSync(
|
||
'npm run electron:build -- --win portable --ia32 --x64 --publish never',
|
||
{ stdio: 'inherit' }
|
||
);
|
||
|
||
// 构建完成后,创建最终的便携应用结构
|
||
console.log('创建便携应用最终结构...');
|
||
createPortableStructure();
|
||
|
||
console.log('Windows 7便携应用构建完成!');
|
||
console.log('构建产物位于 dist_electron 目录下的 portable-app 文件夹');
|
||
} catch (error) {
|
||
console.error('构建失败:', error);
|
||
process.exit(1);
|
||
}
|
||
}
|
||
|
||
// 创建便携应用最终结构:可执行文件和data目录在同一级
|
||
function createPortableStructure() {
|
||
const distDir = path.join(__dirname, 'dist_electron');
|
||
const portableAppDir = path.join(distDir, 'portable-app');
|
||
|
||
// 创建目标目录
|
||
if (fs.existsSync(portableAppDir)) {
|
||
deleteFolderRecursive(portableAppDir);
|
||
}
|
||
fs.mkdirSync(portableAppDir, { recursive: true });
|
||
|
||
// 复制可执行文件
|
||
const exeFiles = fs.readdirSync(distDir).filter(file => file.endsWith('.exe'));
|
||
if (exeFiles.length > 0) {
|
||
const exeFile = exeFiles[0];
|
||
fs.copyFileSync(path.join(distDir, exeFile), path.join(portableAppDir, exeFile));
|
||
console.log(`已复制可执行文件: ${exeFile}`);
|
||
}
|
||
|
||
// 创建data目录并复制数据库文件
|
||
const srcDataDir = path.join(__dirname, 'data');
|
||
const destDataDir = path.join(portableAppDir, 'data');
|
||
fs.mkdirSync(destDataDir, { recursive: true });
|
||
|
||
// 复制data目录下的所有文件
|
||
if (fs.existsSync(srcDataDir)) {
|
||
const dataFiles = fs.readdirSync(srcDataDir);
|
||
dataFiles.forEach(file => {
|
||
const srcPath = path.join(srcDataDir, file);
|
||
const destPath = path.join(destDataDir, file);
|
||
fs.copyFileSync(srcPath, destPath);
|
||
console.log(`已复制数据文件: ${file}`);
|
||
});
|
||
}
|
||
|
||
// 确保portable.txt文件在可执行文件同级目录
|
||
fs.copyFileSync(path.join(__dirname, 'portable.txt'), path.join(portableAppDir, 'portable.txt'));
|
||
console.log('已复制便携模式标识文件');
|
||
}
|
||
|
||
// 主函数
|
||
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();
|