exam11/background/db/path.js
2025-09-10 12:07:43 +08:00

68 lines
1.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 添加缺失的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
};