109 lines
3.9 KiB
JavaScript
109 lines
3.9 KiB
JavaScript
const path = require('path');
|
||
const fs = require('fs');
|
||
const { app } = require('electron');
|
||
|
||
// 判断是否为开发环境
|
||
const isDev = process.env.NODE_ENV === 'development' || !app.isPackaged;
|
||
|
||
// 检测是否为便携模式
|
||
let isPortable = false;
|
||
let dataDir;
|
||
|
||
if (isDev) {
|
||
// 开发环境:数据存储在工程的data目录
|
||
dataDir = path.join(process.cwd(), 'data');
|
||
} else {
|
||
try {
|
||
// 关键修改:对于打包后的应用,尤其是便携模式,我们需要更智能地确定数据目录位置
|
||
// 1. 获取进程可执行文件路径
|
||
const execPath = process.execPath;
|
||
// 2. 获取可执行文件所在目录
|
||
let appDir = path.dirname(execPath);
|
||
|
||
console.log('进程可执行文件路径:', execPath);
|
||
console.log('进程目录:', appDir);
|
||
|
||
// 3. 检测是否存在portable.txt文件(标记文件)
|
||
const portableFlagPath = path.join(appDir, 'portable.txt');
|
||
if (fs.existsSync(portableFlagPath)) {
|
||
isPortable = true;
|
||
console.log('检测到便携模式标记文件');
|
||
}
|
||
|
||
// 4. 检查是否在临时目录中运行(常见于便携应用解压运行时)
|
||
const isTempDir = appDir.includes('Temp') || appDir.includes('tmp');
|
||
console.log('是否在临时目录中运行:', isTempDir);
|
||
|
||
// 5. 特殊处理便携模式下的临时目录问题
|
||
if (isTempDir) {
|
||
// 尝试通过其他方式获取实际的应用目录
|
||
// - 方法1:检查可执行文件名
|
||
const exeName = path.basename(execPath);
|
||
console.log('可执行文件名:', exeName);
|
||
|
||
// - 方法2:使用process.argv[0](启动路径)
|
||
const startupPath = process.argv[0];
|
||
console.log('启动路径:', startupPath);
|
||
|
||
// 关键点:对于便携应用,我们需要确保数据目录与实际的可执行文件(用户看到的那个)在同一目录
|
||
// 假设可执行文件名为StatExamPortable_*.exe,我们需要找到这个文件的实际位置
|
||
// 由于在临时目录中,我们需要向上查找或使用相对路径策略
|
||
|
||
// 这里使用一种通用策略:在Windows系统上,便携应用的临时目录通常在用户临时文件夹下
|
||
// 我们将数据目录放在与临时目录不同的位置,确保数据持久性
|
||
|
||
// 尝试获取用户主目录
|
||
const userHome = app.getPath('home');
|
||
console.log('用户主目录:', userHome);
|
||
|
||
// 便携式应用的数据目录应该放在一个稳定的位置,而不是临时目录
|
||
// 这里我们选择放在用户主目录下的特定文件夹
|
||
const portableDataDir = path.join(userHome, 'StatExamData');
|
||
dataDir = portableDataDir;
|
||
|
||
// 创建便携模式标记文件
|
||
const stablePortableFlagPath = path.join(portableDataDir, '..', 'portable.txt');
|
||
if (!fs.existsSync(stablePortableFlagPath)) {
|
||
try {
|
||
fs.writeFileSync(stablePortableFlagPath, '');
|
||
console.log('创建便携模式标记文件:', stablePortableFlagPath);
|
||
} catch (error) {
|
||
console.error('创建便携模式标记文件失败:', error);
|
||
}
|
||
}
|
||
|
||
} else {
|
||
// 正常情况下,数据目录与可执行文件同级
|
||
dataDir = path.join(appDir, 'data');
|
||
}
|
||
|
||
} catch (error) {
|
||
console.error('确定数据目录时出错:', error);
|
||
// 发生错误时使用默认策略
|
||
const fallbackDir = path.join(app.getPath('userData'), 'data');
|
||
dataDir = fallbackDir;
|
||
console.log('使用备用数据目录:', fallbackDir);
|
||
}
|
||
}
|
||
|
||
// 确保数据目录存在
|
||
if (!fs.existsSync(dataDir)) {
|
||
fs.mkdirSync(dataDir, { recursive: true });
|
||
console.log(`创建数据目录: ${dataDir}`);
|
||
}
|
||
|
||
// 系统数据库路径
|
||
function getSystemDbPath() {
|
||
return path.join(dataDir, 'system.db');
|
||
}
|
||
|
||
// 用户数据库路径
|
||
function getUserDbPath() {
|
||
return path.join(dataDir, 'user.db');
|
||
}
|
||
|
||
// 导出函数供其他模块使用
|
||
module.exports = {
|
||
getSystemDbPath,
|
||
getUserDbPath
|
||
}; |