68 lines
1.8 KiB
JavaScript
68 lines
1.8 KiB
JavaScript
// 添加缺失的path模块导入
|
||
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 appDir;
|
||
let dataDir;
|
||
|
||
if (isDev) {
|
||
// 开发环境:数据存储在工程的data目录
|
||
dataDir = path.join(process.cwd(), 'data');
|
||
} else {
|
||
// 非开发环境:确定应用目录
|
||
const exePath = app.getPath('exe');
|
||
appDir = path.dirname(exePath);
|
||
|
||
// 检测是否存在便携模式标记文件
|
||
const portableFlagPath = path.join(appDir, 'portable.txt');
|
||
if (fs.existsSync(portableFlagPath)) {
|
||
isPortable = true;
|
||
}
|
||
|
||
// 关键修改:在便携模式下,data目录与可执行文件同级
|
||
// 如果可执行文件在portable-app目录中,则data目录与portable-app目录同级
|
||
if (path.basename(appDir) === 'portable-app') {
|
||
dataDir = path.join(path.dirname(appDir), 'data');
|
||
isPortable = true;
|
||
} else {
|
||
// 否则data目录与可执行文件同级
|
||
dataDir = path.join(appDir, 'data');
|
||
}
|
||
|
||
// 确保便携模式标记文件存在于data目录同级
|
||
if (isPortable) {
|
||
const flagDir = path.dirname(dataDir);
|
||
const flagPath = path.join(flagDir, 'portable.txt');
|
||
if (!fs.existsSync(flagPath)) {
|
||
fs.writeFileSync(flagPath, '');
|
||
}
|
||
}
|
||
}
|
||
|
||
// 确保数据目录存在
|
||
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
|
||
}; |