path
This commit is contained in:
parent
158aad76c5
commit
d9e7c04453
@ -1,4 +1,4 @@
|
||||
const path = require('path');
|
||||
const path = require('path'); // 添加缺失的path模块导入
|
||||
const fs = require('fs');
|
||||
const { app } = require('electron');
|
||||
|
||||
@ -7,14 +7,24 @@ const isDev = process.env.NODE_ENV === 'development' || !app.isPackaged;
|
||||
|
||||
// 检测是否为便携模式
|
||||
let isPortable = false;
|
||||
const exePath = app.getPath('exe');
|
||||
const appDir = path.dirname(exePath);
|
||||
const portableFlagPath = path.join(appDir, 'portable.txt');
|
||||
let appDir;
|
||||
|
||||
// 如果应用目录中存在portable.txt文件,则启用便携模式
|
||||
if (!isDev && fs.existsSync(portableFlagPath)) {
|
||||
isPortable = true;
|
||||
console.log('启用便携模式');
|
||||
if (!isDev) {
|
||||
const exePath = app.getPath('exe');
|
||||
appDir = path.dirname(exePath);
|
||||
|
||||
// 检查应用目录下是否存在portable.txt文件
|
||||
const portableFlagPath = path.join(appDir, 'portable.txt');
|
||||
if (fs.existsSync(portableFlagPath)) {
|
||||
isPortable = true;
|
||||
console.log('启用便携模式');
|
||||
}
|
||||
|
||||
// 如果可执行文件在portable-app目录中,也认为是便携模式
|
||||
if (path.basename(appDir) === 'portable-app') {
|
||||
isPortable = true;
|
||||
console.log('检测到可执行文件在portable-app目录中,启用便携模式');
|
||||
}
|
||||
}
|
||||
|
||||
// 根据模式选择数据目录
|
||||
@ -23,8 +33,21 @@ if (isDev) {
|
||||
// 开发环境:数据存储在工程的data目录
|
||||
dataDir = path.join(process.cwd(), 'data');
|
||||
} else if (isPortable) {
|
||||
// 便携模式:数据存储在应用目录下的data文件夹
|
||||
dataDir = path.join(appDir, 'data');
|
||||
// 便携模式:
|
||||
// 如果可执行文件在portable-app目录中,则数据目录与portable-app目录同级
|
||||
if (path.basename(appDir) === 'portable-app') {
|
||||
dataDir = path.join(path.dirname(appDir), 'data');
|
||||
} else {
|
||||
// 否则数据存储在应用目录下的data文件夹
|
||||
dataDir = path.join(appDir, 'data');
|
||||
}
|
||||
|
||||
// 确保便携模式标记文件存在
|
||||
const portableFlagPath = path.join(path.dirname(dataDir), 'portable.txt');
|
||||
if (!fs.existsSync(portableFlagPath)) {
|
||||
fs.writeFileSync(portableFlagPath, '');
|
||||
console.log('创建便携模式标记文件');
|
||||
}
|
||||
} else {
|
||||
// 非便携模式:数据存储在应用同级的data目录
|
||||
dataDir = path.join(appDir, 'data');
|
||||
|
@ -76,6 +76,68 @@ function buildPortableApp() {
|
||||
|
||||
console.log('Windows 7便携应用构建完成!');
|
||||
console.log('构建产物位于 dist_electron 目录');
|
||||
|
||||
// ===== 添加文件复制逻辑,确保data目录与可执行文件同级 =====
|
||||
console.log('开始准备data目录结构...');
|
||||
|
||||
// 定义路径
|
||||
const projectRoot = __dirname;
|
||||
const buildDir = path.join(projectRoot, 'dist_electron');
|
||||
const portableAppDir = path.join(buildDir, 'portable-app');
|
||||
const destDataDir = path.join(buildDir, 'data'); // data目录与portable-app目录同级
|
||||
|
||||
// 确保目标data目录存在
|
||||
if (!fs.existsSync(destDataDir)) {
|
||||
fs.mkdirSync(destDataDir, { recursive: true });
|
||||
console.log(`创建目标data目录: ${destDataDir}`);
|
||||
}
|
||||
|
||||
// 复制.gitignore文件(如果存在)
|
||||
const gitignoreSrc = path.join(projectRoot, 'data', '.gitignore');
|
||||
const gitignoreDest = path.join(destDataDir, '.gitignore');
|
||||
if (fs.existsSync(gitignoreSrc)) {
|
||||
fs.copyFileSync(gitignoreSrc, gitignoreDest);
|
||||
console.log('已复制.gitignore文件');
|
||||
}
|
||||
|
||||
// 创建空的system.db和user.db文件(如果不存在)
|
||||
const systemDbPath = path.join(destDataDir, 'system.db');
|
||||
const userDbPath = path.join(destDataDir, 'user.db');
|
||||
|
||||
if (!fs.existsSync(systemDbPath)) {
|
||||
fs.writeFileSync(systemDbPath, '');
|
||||
console.log('已创建空的system.db文件');
|
||||
}
|
||||
|
||||
if (!fs.existsSync(userDbPath)) {
|
||||
fs.writeFileSync(userDbPath, '');
|
||||
console.log('已创建空的user.db文件');
|
||||
}
|
||||
|
||||
// 可选:如果项目中已有数据库文件,可以复制它们
|
||||
const projectSystemDb = path.join(projectRoot, 'data', 'system.db');
|
||||
const projectUserDb = path.join(projectRoot, 'data', 'user.db');
|
||||
|
||||
if (fs.existsSync(projectSystemDb)) {
|
||||
fs.copyFileSync(projectSystemDb, systemDbPath);
|
||||
console.log('已复制项目中的system.db文件');
|
||||
}
|
||||
|
||||
if (fs.existsSync(projectUserDb)) {
|
||||
fs.copyFileSync(projectUserDb, userDbPath);
|
||||
console.log('已复制项目中的user.db文件');
|
||||
}
|
||||
|
||||
console.log('data目录结构准备完成!');
|
||||
console.log('最终目录结构:');
|
||||
console.log(`- dist_electron/`);
|
||||
console.log(` - portable-app/`);
|
||||
console.log(` - StatExamPortable_${require('./package.json').version}_*.exe`);
|
||||
console.log(` - data/`);
|
||||
console.log(` - system.db`);
|
||||
console.log(` - user.db`);
|
||||
console.log(` - .gitignore`);
|
||||
|
||||
} catch (error) {
|
||||
console.error('构建失败:', error);
|
||||
process.exit(1);
|
||||
|
Loading…
Reference in New Issue
Block a user