electron-vue-exam-single/electron/db/path.js
2025-08-12 18:31:04 +08:00

47 lines
1.3 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.

import { app } from 'electron';
import path from 'path';
import fs from 'fs';
// 判断是否为开发环境
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');
// 如果应用目录中存在portable.txt文件则启用便携模式
if (!isDev && fs.existsSync(portableFlagPath)) {
isPortable = true;
console.log('启用便携模式');
}
// 根据模式选择数据目录
let dataDir;
if (isDev) {
// 开发环境数据存储在工程的data目录
dataDir = path.join(process.cwd(), 'data');
} else if (isPortable) {
// 便携模式数据存储在应用目录下的data文件夹
dataDir = path.join(appDir, 'data');
} else {
// 非便携模式数据存储在应用同级的data目录
dataDir = path.join(appDir, 'data');
}
// 确保数据目录存在
if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir, { recursive: true });
console.log(`创建数据目录: ${dataDir}`);
}
// 系统数据库路径
export function getSystemDbPath() {
return path.join(dataDir, 'system.db');
}
// 用户数据库路径
export function getUserDbPath() {
return path.join(dataDir, 'user.db');
}