feat: #2135
This commit is contained in:
1 parent
162c6db1ac
commit
093b3bf4aa
5 files changed
+41
-20
No files matched your search
@@ -3,6 +3,11 @@ import { fileURLToPath } from 'url';
|
|||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import os from 'os';
|
import os from 'os';
|
||||||
|
|
||||||
|
export function getQQDataDirOverride (): string | undefined {
|
||||||
|
const dataDir = process.env['NAPCAT_QQ_DATA_DIR'];
|
||||||
|
return dataDir ? path.resolve(dataDir) : undefined;
|
||||||
|
}
|
||||||
|
|
||||||
export class NapCatPathWrapper {
|
export class NapCatPathWrapper {
|
||||||
binaryPath: string;
|
binaryPath: string;
|
||||||
logsPath: string;
|
logsPath: string;
|
||||||
@@ -14,9 +19,12 @@ export class NapCatPathWrapper {
|
|||||||
constructor (mainPath: string = dirname(fileURLToPath(import.meta.url))) {
|
constructor (mainPath: string = dirname(fileURLToPath(import.meta.url))) {
|
||||||
this.binaryPath = mainPath;
|
this.binaryPath = mainPath;
|
||||||
let writePath: string;
|
let writePath: string;
|
||||||
|
const qqDataDir = getQQDataDirOverride();
|
||||||
|
|
||||||
if (process.env['NAPCAT_WORKDIR']) {
|
if (process.env['NAPCAT_WORKDIR']) {
|
||||||
writePath = process.env['NAPCAT_WORKDIR'];
|
writePath = process.env['NAPCAT_WORKDIR'];
|
||||||
|
} else if (qqDataDir) {
|
||||||
|
writePath = path.join(qqDataDir, 'NapCat');
|
||||||
} else if (os.platform() === 'darwin') {
|
} else if (os.platform() === 'darwin') {
|
||||||
writePath = path.join(os.homedir(), 'Library', 'Application Support', 'QQ', 'NapCat');
|
writePath = path.join(os.homedir(), 'Library', 'Application Support', 'QQ', 'NapCat');
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ import {
|
|||||||
} from '@/napcat-core/wrapper';
|
} from '@/napcat-core/wrapper';
|
||||||
import { LogLevel, LogWrapper } from '@/napcat-core/helper/log';
|
import { LogLevel, LogWrapper } from '@/napcat-core/helper/log';
|
||||||
import { QQBasicInfoWrapper } from '@/napcat-core/helper/qq-basic-info';
|
import { QQBasicInfoWrapper } from '@/napcat-core/helper/qq-basic-info';
|
||||||
import { NapCatPathWrapper } from 'napcat-common/src/path';
|
import { getQQDataDirOverride, NapCatPathWrapper } from 'napcat-common/src/path';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
import fs from 'node:fs';
|
import fs from 'node:fs';
|
||||||
import { hostname, systemName, systemVersion } from 'napcat-common/src/system';
|
import { hostname, systemName, systemVersion } from 'napcat-common/src/system';
|
||||||
@@ -240,6 +240,8 @@ export class NapCatCore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get dataPath (): string {
|
get dataPath (): string {
|
||||||
|
const override = getQQDataDirOverride();
|
||||||
|
if (override) return override;
|
||||||
let result = this.context.wrapper.NodeQQNTWrapperUtil.getNTUserDataInfoConfig();
|
let result = this.context.wrapper.NodeQQNTWrapperUtil.getNTUserDataInfoConfig();
|
||||||
if (!result) {
|
if (!result) {
|
||||||
result = path.resolve(os.homedir(), './.config/QQ');
|
result = path.resolve(os.homedir(), './.config/QQ');
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ import { QQBasicInfoWrapper } from '@/napcat-core/helper/qq-basic-info';
|
|||||||
import { statusHelperSubscription } from '@/napcat-core/helper/status';
|
import { statusHelperSubscription } from '@/napcat-core/helper/status';
|
||||||
import { applyPendingUpdates } from '@/napcat-webui-backend/src/api/UpdateNapCat';
|
import { applyPendingUpdates } from '@/napcat-webui-backend/src/api/UpdateNapCat';
|
||||||
import { migrateLegacyGlobalPath } from './global-migrate';
|
import { migrateLegacyGlobalPath } from './global-migrate';
|
||||||
|
import { getDataPaths } from './data-path';
|
||||||
import { connectToNamedPipe } from './pipe';
|
import { connectToNamedPipe } from './pipe';
|
||||||
|
|
||||||
// NapCat Shell App ES 入口文件
|
// NapCat Shell App ES 入口文件
|
||||||
@@ -54,24 +55,6 @@ async function handleUncaughtExceptions (logger: LogWrapper) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function getDataPaths (wrapper: WrapperNodeApi): [string, string] {
|
|
||||||
if (os.platform() === 'darwin') {
|
|
||||||
const userPath = os.homedir();
|
|
||||||
const appDataPath = path.resolve(userPath, './Library/Application Support/QQ');
|
|
||||||
return [appDataPath, path.join(appDataPath, 'global')];
|
|
||||||
}
|
|
||||||
let dataPath = wrapper.NodeQQNTWrapperUtil.getNTUserDataInfoConfig();
|
|
||||||
if (!dataPath) {
|
|
||||||
dataPath = path.resolve(os.homedir(), './.config/QQ');
|
|
||||||
fs.mkdirSync(dataPath, { recursive: true });
|
|
||||||
}
|
|
||||||
// Linux: global 直接位于数据目录下; Windows: global 位于 nt_qq 子目录下
|
|
||||||
const dataPathGlobal = os.platform() === 'linux'
|
|
||||||
? path.resolve(dataPath, './global')
|
|
||||||
: path.resolve(dataPath, './nt_qq/global');
|
|
||||||
return [dataPath, dataPathGlobal];
|
|
||||||
}
|
|
||||||
|
|
||||||
function getPlatformType (): PlatformType {
|
function getPlatformType (): PlatformType {
|
||||||
const platformMapping: Partial<Record<NodeJS.Platform, PlatformType>> = {
|
const platformMapping: Partial<Record<NodeJS.Platform, PlatformType>> = {
|
||||||
win32: PlatformType.KWINDOWS,
|
win32: PlatformType.KWINDOWS,
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
import fs from 'node:fs';
|
||||||
|
import os from 'node:os';
|
||||||
|
import path from 'node:path';
|
||||||
|
import { getQQDataDirOverride } from 'napcat-common/src/path';
|
||||||
|
import type { WrapperNodeApi } from 'napcat-core';
|
||||||
|
|
||||||
|
export function getDataPaths (wrapper: WrapperNodeApi): [string, string] {
|
||||||
|
const platform = os.platform();
|
||||||
|
let dataPath = getQQDataDirOverride();
|
||||||
|
if (dataPath) {
|
||||||
|
// 自定义目录必须在初始化内核前存在;创建失败时不能回退到桌面 QQ。
|
||||||
|
fs.mkdirSync(dataPath, { recursive: true });
|
||||||
|
} else if (platform === 'darwin') {
|
||||||
|
dataPath = path.resolve(os.homedir(), './Library/Application Support/QQ');
|
||||||
|
} else {
|
||||||
|
dataPath = wrapper.NodeQQNTWrapperUtil.getNTUserDataInfoConfig();
|
||||||
|
if (!dataPath) {
|
||||||
|
dataPath = path.resolve(os.homedir(), './.config/QQ');
|
||||||
|
fs.mkdirSync(dataPath, { recursive: true });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Linux/macOS: global 直接位于数据目录下;Windows: 位于 nt_qq 子目录下。
|
||||||
|
// 此处不创建 global,以免阻止后续 Linux 旧目录迁移。
|
||||||
|
const dataPathGlobal = platform === 'linux' || platform === 'darwin'
|
||||||
|
? path.resolve(dataPath, 'global')
|
||||||
|
: path.resolve(dataPath, 'nt_qq', 'global');
|
||||||
|
return [dataPath, dataPathGlobal];
|
||||||
|
}
|
||||||
@@ -12,7 +12,6 @@ import { fileURLToPath } from 'url';
|
|||||||
|
|
||||||
const __filename = fileURLToPath(import.meta.url);
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
const __dirname = path.dirname(__filename);
|
const __dirname = path.dirname(__filename);
|
||||||
const pathWrapper = new NapCatPathWrapper();
|
|
||||||
const envPath = path.join(__dirname, 'config', '.env');
|
const envPath = path.join(__dirname, 'config', '.env');
|
||||||
if (fs.existsSync(envPath)) {
|
if (fs.existsSync(envPath)) {
|
||||||
try {
|
try {
|
||||||
@@ -32,6 +31,7 @@ if (fs.existsSync(envPath)) {
|
|||||||
console.log('[NapCat] Failed to load .env file:', e);
|
console.log('[NapCat] Failed to load .env file:', e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
const pathWrapper = new NapCatPathWrapper();
|
||||||
|
|
||||||
// 环境变量配置
|
// 环境变量配置
|
||||||
const ENV = {
|
const ENV = {
|
||||||
|
|||||||
Reference in new issue
Block a user