p
This commit is contained in:
parent
05edaccebb
commit
7e8c417c48
@ -12,11 +12,12 @@ export async function migrateDatabases() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 确保数据目录存在
|
// 获取数据库路径
|
||||||
const systemDbPath = getSystemDbPath();
|
const systemDbPath = getSystemDbPath();
|
||||||
const userDbPath = getUserDbPath();
|
const userDbPath = getUserDbPath();
|
||||||
const dataDir = path.dirname(systemDbPath);
|
const dataDir = path.dirname(systemDbPath);
|
||||||
|
|
||||||
|
// 确保数据目录存在
|
||||||
if (!fs.existsSync(dataDir)) {
|
if (!fs.existsSync(dataDir)) {
|
||||||
fs.mkdirSync(dataDir, { recursive: true });
|
fs.mkdirSync(dataDir, { recursive: true });
|
||||||
console.log(`创建数据目录: ${dataDir}`);
|
console.log(`创建数据目录: ${dataDir}`);
|
||||||
@ -27,7 +28,7 @@ export async function migrateDatabases() {
|
|||||||
const installedSystemDbPath = path.join(appPath, 'data', 'system.db');
|
const installedSystemDbPath = path.join(appPath, 'data', 'system.db');
|
||||||
const installedUserDbPath = path.join(appPath, 'data', 'user.db');
|
const installedUserDbPath = path.join(appPath, 'data', 'user.db');
|
||||||
|
|
||||||
// 检查并复制系统数据库文件
|
// 检查并复制系统数据库文件(仅当目标文件不存在时)
|
||||||
if (fs.existsSync(installedSystemDbPath) && !fs.existsSync(systemDbPath)) {
|
if (fs.existsSync(installedSystemDbPath) && !fs.existsSync(systemDbPath)) {
|
||||||
fs.copyFileSync(installedSystemDbPath, systemDbPath);
|
fs.copyFileSync(installedSystemDbPath, systemDbPath);
|
||||||
console.log(`复制系统数据库: ${installedSystemDbPath} -> ${systemDbPath}`);
|
console.log(`复制系统数据库: ${installedSystemDbPath} -> ${systemDbPath}`);
|
||||||
@ -37,7 +38,7 @@ export async function migrateDatabases() {
|
|||||||
console.log(`系统数据库不存在: ${systemDbPath}`);
|
console.log(`系统数据库不存在: ${systemDbPath}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查并复制用户数据库文件
|
// 检查并复制用户数据库文件(仅当目标文件不存在时)
|
||||||
if (fs.existsSync(installedUserDbPath) && !fs.existsSync(userDbPath)) {
|
if (fs.existsSync(installedUserDbPath) && !fs.existsSync(userDbPath)) {
|
||||||
fs.copyFileSync(installedUserDbPath, userDbPath);
|
fs.copyFileSync(installedUserDbPath, userDbPath);
|
||||||
console.log(`复制用户数据库: ${installedUserDbPath} -> ${userDbPath}`);
|
console.log(`复制用户数据库: ${installedUserDbPath} -> ${userDbPath}`);
|
||||||
|
@ -1,9 +1,35 @@
|
|||||||
import { app } from 'electron';
|
import { app } from 'electron';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
import fs from 'fs';
|
||||||
|
|
||||||
// 获取用户数据目录
|
// 检测是否为便携模式
|
||||||
const userDataPath = app.getPath('userData');
|
let isPortable = false;
|
||||||
const dataDir = path.join(userDataPath, 'data');
|
const exePath = app.getPath('exe');
|
||||||
|
const appDir = path.dirname(exePath);
|
||||||
|
const portableFlagPath = path.join(appDir, 'portable.txt');
|
||||||
|
|
||||||
|
// 如果应用目录中存在portable.txt文件,则启用便携模式
|
||||||
|
if (fs.existsSync(portableFlagPath)) {
|
||||||
|
isPortable = true;
|
||||||
|
console.log('启用便携模式');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 根据模式选择数据目录
|
||||||
|
let dataDir;
|
||||||
|
if (isPortable) {
|
||||||
|
// 便携模式:数据存储在应用目录下的data文件夹
|
||||||
|
dataDir = path.join(appDir, 'data');
|
||||||
|
} else {
|
||||||
|
// 非便携模式:数据存储在用户数据目录
|
||||||
|
const userDataPath = app.getPath('userData');
|
||||||
|
dataDir = path.join(userDataPath, 'data');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 确保数据目录存在
|
||||||
|
if (!fs.existsSync(dataDir)) {
|
||||||
|
fs.mkdirSync(dataDir, { recursive: true });
|
||||||
|
console.log(`创建数据目录: ${dataDir}`);
|
||||||
|
}
|
||||||
|
|
||||||
// 系统数据库路径
|
// 系统数据库路径
|
||||||
export function getSystemDbPath() {
|
export function getSystemDbPath() {
|
||||||
|
@ -360,14 +360,38 @@ function getAppSaveDir() {
|
|||||||
// 开发环境:使用项目根目录
|
// 开发环境:使用项目根目录
|
||||||
return path.join(process.cwd(), 'output');
|
return path.join(process.cwd(), 'output');
|
||||||
} else {
|
} else {
|
||||||
// 生产环境:根据操作系统选择不同路径
|
// 检测是否为便携模式
|
||||||
if (process.platform === 'darwin') { // macOS
|
const exePath = app.getPath('exe');
|
||||||
// 使用用户文档目录
|
const appDir = path.dirname(exePath);
|
||||||
return path.join(app.getPath('documents'), 'ExamApp');
|
const portableFlagPath = path.join(appDir, 'portable.txt');
|
||||||
} else if (process.platform === 'win32') { // Windows
|
const isPortable = fs.existsSync(portableFlagPath);
|
||||||
return path.dirname(app.getPath('exe'));
|
|
||||||
} else { // Linux 等其他系统
|
if (isPortable) {
|
||||||
return app.getPath('userData');
|
// 便携模式:使用应用目录下的output文件夹
|
||||||
|
const outputDir = path.join(appDir, 'output');
|
||||||
|
if (!fs.existsSync(outputDir)) {
|
||||||
|
fs.mkdirSync(outputDir, { recursive: true });
|
||||||
|
}
|
||||||
|
return outputDir;
|
||||||
|
} else {
|
||||||
|
// 非便携模式:根据操作系统选择不同路径
|
||||||
|
if (process.platform === 'darwin') { // macOS
|
||||||
|
// 使用用户文档目录
|
||||||
|
const docDir = path.join(app.getPath('documents'), 'ExamApp');
|
||||||
|
if (!fs.existsSync(docDir)) {
|
||||||
|
fs.mkdirSync(docDir, { recursive: true });
|
||||||
|
}
|
||||||
|
return docDir;
|
||||||
|
} else if (process.platform === 'win32') { // Windows
|
||||||
|
// 使用用户文档目录
|
||||||
|
const docDir = path.join(app.getPath('documents'), 'ExamApp');
|
||||||
|
if (!fs.existsSync(docDir)) {
|
||||||
|
fs.mkdirSync(docDir, { recursive: true });
|
||||||
|
}
|
||||||
|
return docDir;
|
||||||
|
} else { // Linux 等其他系统
|
||||||
|
return app.getPath('userData');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user