data path
This commit is contained in:
parent
d9e7c04453
commit
c028846dd3
@ -1,4 +1,4 @@
|
|||||||
const path = require('path'); // 添加缺失的path模块导入
|
const path = require('path');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const { app } = require('electron');
|
const { app } = require('electron');
|
||||||
|
|
||||||
@ -8,49 +8,40 @@ const isDev = process.env.NODE_ENV === 'development' || !app.isPackaged;
|
|||||||
// 检测是否为便携模式
|
// 检测是否为便携模式
|
||||||
let isPortable = false;
|
let isPortable = false;
|
||||||
let appDir;
|
let appDir;
|
||||||
|
|
||||||
if (!isDev) {
|
|
||||||
const exePath = app.getPath('exe');
|
|
||||||
appDir = path.dirname(exePath);
|
|
||||||
|
|
||||||
// 检查应用目录下是否存在portable.txt文件
|
|
||||||
const portableFlagPath = path.join(appDir, 'portable.txt');
|
|
||||||
if (fs.existsSync(portableFlagPath)) {
|
|
||||||
isPortable = true;
|
|
||||||
console.log('启用便携模式');
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果可执行文件在portable-app目录中,也认为是便携模式
|
|
||||||
if (path.basename(appDir) === 'portable-app') {
|
|
||||||
isPortable = true;
|
|
||||||
console.log('检测到可执行文件在portable-app目录中,启用便携模式');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 根据模式选择数据目录
|
|
||||||
let dataDir;
|
let dataDir;
|
||||||
|
|
||||||
if (isDev) {
|
if (isDev) {
|
||||||
// 开发环境:数据存储在工程的data目录
|
// 开发环境:数据存储在工程的data目录
|
||||||
dataDir = path.join(process.cwd(), 'data');
|
dataDir = path.join(process.cwd(), 'data');
|
||||||
} else if (isPortable) {
|
} else {
|
||||||
// 便携模式:
|
// 非开发环境:确定应用目录
|
||||||
// 如果可执行文件在portable-app目录中,则数据目录与portable-app目录同级
|
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') {
|
if (path.basename(appDir) === 'portable-app') {
|
||||||
dataDir = path.join(path.dirname(appDir), 'data');
|
dataDir = path.join(path.dirname(appDir), 'data');
|
||||||
|
isPortable = true;
|
||||||
} else {
|
} else {
|
||||||
// 否则数据存储在应用目录下的data文件夹
|
// 否则data目录与可执行文件同级
|
||||||
dataDir = path.join(appDir, 'data');
|
dataDir = path.join(appDir, 'data');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 确保便携模式标记文件存在
|
// 确保便携模式标记文件存在于data目录同级
|
||||||
const portableFlagPath = path.join(path.dirname(dataDir), 'portable.txt');
|
if (isPortable) {
|
||||||
if (!fs.existsSync(portableFlagPath)) {
|
const flagDir = path.dirname(dataDir);
|
||||||
fs.writeFileSync(portableFlagPath, '');
|
const flagPath = path.join(flagDir, 'portable.txt');
|
||||||
console.log('创建便携模式标记文件');
|
if (!fs.existsSync(flagPath)) {
|
||||||
|
fs.writeFileSync(flagPath, '');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// 非便携模式:数据存储在应用同级的data目录
|
|
||||||
dataDir = path.join(appDir, 'data');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 确保数据目录存在
|
// 确保数据目录存在
|
||||||
|
@ -84,7 +84,8 @@ function buildPortableApp() {
|
|||||||
const projectRoot = __dirname;
|
const projectRoot = __dirname;
|
||||||
const buildDir = path.join(projectRoot, 'dist_electron');
|
const buildDir = path.join(projectRoot, 'dist_electron');
|
||||||
const portableAppDir = path.join(buildDir, 'portable-app');
|
const portableAppDir = path.join(buildDir, 'portable-app');
|
||||||
const destDataDir = path.join(buildDir, 'data'); // data目录与portable-app目录同级
|
// 修改:data目录放入portable-app目录内
|
||||||
|
const destDataDir = path.join(portableAppDir, 'data');
|
||||||
|
|
||||||
// 确保目标data目录存在
|
// 确保目标data目录存在
|
||||||
if (!fs.existsSync(destDataDir)) {
|
if (!fs.existsSync(destDataDir)) {
|
||||||
|
Loading…
Reference in New Issue
Block a user