feat: support portable config dir (#5039)

* feat: support portable config dir

* fix: remove redundant mkdir
This commit is contained in:
Song 2025-04-24 17:22:35 +08:00 committed by kangfenmao
parent 17eeab2897
commit 7d69c1274b
3 changed files with 14 additions and 0 deletions

View File

@ -2,3 +2,4 @@ export const isMac = process.platform === 'darwin'
export const isWin = process.platform === 'win32'
export const isLinux = process.platform === 'linux'
export const isDev = process.env.NODE_ENV === 'development'
export const isPortable = isWin && 'PORTABLE_EXECUTABLE_DIR' in process.env

View File

@ -12,6 +12,7 @@ import { CHERRY_STUDIO_PROTOCOL, handleProtocolUrl, registerProtocolClient } fro
import { registerShortcuts } from './services/ShortcutService'
import { TrayService } from './services/TrayService'
import { windowService } from './services/WindowService'
import { setAppDataDir } from './utils/file'
// Check for single instance lock
if (!app.requestSingleInstanceLock()) {
@ -50,6 +51,8 @@ if (!app.requestSingleInstanceLock()) {
replaceDevtoolsFont(mainWindow)
setAppDataDir()
if (process.env.NODE_ENV === 'development') {
installExtension([REDUX_DEVTOOLS, REACT_DEVELOPER_TOOLS])
.then((name) => console.log(`Added Extension: ${name}`))

View File

@ -2,6 +2,7 @@ import * as fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import { isPortable } from '@main/constant'
import { audioExts, documentExts, imageExts, textExts, videoExts } from '@shared/config/constant'
import { FileType, FileTypes } from '@types'
import { app } from 'electron'
@ -83,3 +84,12 @@ export function getConfigDir() {
export function getAppConfigDir(name: string) {
return path.join(getConfigDir(), name)
}
export function setAppDataDir() {
if (isPortable) {
const dir = path.join(path.dirname(app.getPath('exe')), 'data')
if (fs.existsSync(dir)) {
app.setPath('appData', dir)
}
}
}