refactor(config): centralize home directory constant to shared config (#11158)

Replace hardcoded '.cherrystudio' directory references with HOME_CHERRY_DIR constant
This commit is contained in:
Phantom
2025-11-07 22:24:05 +08:00
committed by GitHub
parent 10e78ac60e
commit e268e69597
9 changed files with 34 additions and 24 deletions

View File

@@ -470,3 +470,6 @@ export const MACOS_TERMINALS_WITH_COMMANDS: TerminalConfigWithCommand[] = [
}) })
} }
] ]
// resources/scripts should be maintained manually
export const HOME_CHERRY_DIR = '.cherrystudio'

View File

@@ -10,6 +10,7 @@ import { getBinaryName } from '@main/utils/process'
import type { TerminalConfig, TerminalConfigWithCommand } from '@shared/config/constant' import type { TerminalConfig, TerminalConfigWithCommand } from '@shared/config/constant'
import { import {
codeTools, codeTools,
HOME_CHERRY_DIR,
MACOS_TERMINALS, MACOS_TERMINALS,
MACOS_TERMINALS_WITH_COMMANDS, MACOS_TERMINALS_WITH_COMMANDS,
terminalApps, terminalApps,
@@ -66,7 +67,7 @@ class CodeToolsService {
} }
public async getBunPath() { public async getBunPath() {
const dir = path.join(os.homedir(), '.cherrystudio', 'bin') const dir = path.join(os.homedir(), HOME_CHERRY_DIR, 'bin')
const bunName = await getBinaryName('bun') const bunName = await getBinaryName('bun')
const bunPath = path.join(dir, bunName) const bunPath = path.join(dir, bunName)
return bunPath return bunPath
@@ -362,7 +363,7 @@ class CodeToolsService {
private async isPackageInstalled(cliTool: string): Promise<boolean> { private async isPackageInstalled(cliTool: string): Promise<boolean> {
const executableName = await this.getCliExecutableName(cliTool) const executableName = await this.getCliExecutableName(cliTool)
const binDir = path.join(os.homedir(), '.cherrystudio', 'bin') const binDir = path.join(os.homedir(), HOME_CHERRY_DIR, 'bin')
const executablePath = path.join(binDir, executableName + (isWin ? '.exe' : '')) const executablePath = path.join(binDir, executableName + (isWin ? '.exe' : ''))
// Ensure bin directory exists // Ensure bin directory exists
@@ -389,7 +390,7 @@ class CodeToolsService {
logger.info(`${cliTool} is installed, getting current version`) logger.info(`${cliTool} is installed, getting current version`)
try { try {
const executableName = await this.getCliExecutableName(cliTool) const executableName = await this.getCliExecutableName(cliTool)
const binDir = path.join(os.homedir(), '.cherrystudio', 'bin') const binDir = path.join(os.homedir(), HOME_CHERRY_DIR, 'bin')
const executablePath = path.join(binDir, executableName + (isWin ? '.exe' : '')) const executablePath = path.join(binDir, executableName + (isWin ? '.exe' : ''))
const { stdout } = await execAsync(`"${executablePath}" --version`, { const { stdout } = await execAsync(`"${executablePath}" --version`, {
@@ -500,7 +501,7 @@ class CodeToolsService {
try { try {
const packageName = await this.getPackageName(cliTool) const packageName = await this.getPackageName(cliTool)
const bunPath = await this.getBunPath() const bunPath = await this.getBunPath()
const bunInstallPath = path.join(os.homedir(), '.cherrystudio') const bunInstallPath = path.join(os.homedir(), HOME_CHERRY_DIR)
const registryUrl = await this.getNpmRegistryUrl() const registryUrl = await this.getNpmRegistryUrl()
const installEnvPrefix = isWin const installEnvPrefix = isWin
@@ -550,7 +551,7 @@ class CodeToolsService {
const packageName = await this.getPackageName(cliTool) const packageName = await this.getPackageName(cliTool)
const bunPath = await this.getBunPath() const bunPath = await this.getBunPath()
const executableName = await this.getCliExecutableName(cliTool) const executableName = await this.getCliExecutableName(cliTool)
const binDir = path.join(os.homedir(), '.cherrystudio', 'bin') const binDir = path.join(os.homedir(), HOME_CHERRY_DIR, 'bin')
const executablePath = path.join(binDir, executableName + (isWin ? '.exe' : '')) const executablePath = path.join(binDir, executableName + (isWin ? '.exe' : ''))
logger.debug(`Package name: ${packageName}`) logger.debug(`Package name: ${packageName}`)
@@ -652,7 +653,7 @@ class CodeToolsService {
baseCommand = `${baseCommand} ${configParams}` baseCommand = `${baseCommand} ${configParams}`
} }
const bunInstallPath = path.join(os.homedir(), '.cherrystudio') const bunInstallPath = path.join(os.homedir(), HOME_CHERRY_DIR)
if (isInstalled) { if (isInstalled) {
// If already installed, run executable directly (with optional update message) // If already installed, run executable directly (with optional update message)

View File

@@ -30,6 +30,7 @@ import {
ToolListChangedNotificationSchema ToolListChangedNotificationSchema
} from '@modelcontextprotocol/sdk/types.js' } from '@modelcontextprotocol/sdk/types.js'
import { nanoid } from '@reduxjs/toolkit' import { nanoid } from '@reduxjs/toolkit'
import { HOME_CHERRY_DIR } from '@shared/config/constant'
import type { MCPProgressEvent } from '@shared/config/types' import type { MCPProgressEvent } from '@shared/config/types'
import { IpcChannel } from '@shared/IpcChannel' import { IpcChannel } from '@shared/IpcChannel'
import { defaultAppHeaders } from '@shared/utils' import { defaultAppHeaders } from '@shared/utils'
@@ -715,7 +716,7 @@ class McpService {
} }
public async getInstallInfo() { public async getInstallInfo() {
const dir = path.join(os.homedir(), '.cherrystudio', 'bin') const dir = path.join(os.homedir(), HOME_CHERRY_DIR, 'bin')
const uvName = await getBinaryName('uv') const uvName = await getBinaryName('uv')
const bunName = await getBinaryName('bun') const bunName = await getBinaryName('bun')
const uvPath = path.join(dir, uvName) const uvPath = path.join(dir, uvName)

View File

@@ -3,6 +3,7 @@ import { homedir } from 'node:os'
import { promisify } from 'node:util' import { promisify } from 'node:util'
import { loggerService } from '@logger' import { loggerService } from '@logger'
import { HOME_CHERRY_DIR } from '@shared/config/constant'
import * as fs from 'fs-extra' import * as fs from 'fs-extra'
import * as path from 'path' import * as path from 'path'
@@ -145,7 +146,7 @@ class OvmsManager {
*/ */
public async runOvms(): Promise<{ success: boolean; message?: string }> { public async runOvms(): Promise<{ success: boolean; message?: string }> {
const homeDir = homedir() const homeDir = homedir()
const ovmsDir = path.join(homeDir, '.cherrystudio', 'ovms', 'ovms') const ovmsDir = path.join(homeDir, HOME_CHERRY_DIR, 'ovms', 'ovms')
const configPath = path.join(ovmsDir, 'models', 'config.json') const configPath = path.join(ovmsDir, 'models', 'config.json')
const runBatPath = path.join(ovmsDir, 'run.bat') const runBatPath = path.join(ovmsDir, 'run.bat')
@@ -195,7 +196,7 @@ class OvmsManager {
*/ */
public async getOvmsStatus(): Promise<'not-installed' | 'not-running' | 'running'> { public async getOvmsStatus(): Promise<'not-installed' | 'not-running' | 'running'> {
const homeDir = homedir() const homeDir = homedir()
const ovmsPath = path.join(homeDir, '.cherrystudio', 'ovms', 'ovms', 'ovms.exe') const ovmsPath = path.join(homeDir, HOME_CHERRY_DIR, 'ovms', 'ovms', 'ovms.exe')
try { try {
// Check if OVMS executable exists // Check if OVMS executable exists
@@ -273,7 +274,7 @@ class OvmsManager {
} }
const homeDir = homedir() const homeDir = homedir()
const configPath = path.join(homeDir, '.cherrystudio', 'ovms', 'ovms', 'models', 'config.json') const configPath = path.join(homeDir, HOME_CHERRY_DIR, 'ovms', 'ovms', 'models', 'config.json')
try { try {
if (!(await fs.pathExists(configPath))) { if (!(await fs.pathExists(configPath))) {
logger.warn(`Config file does not exist: ${configPath}`) logger.warn(`Config file does not exist: ${configPath}`)
@@ -304,7 +305,7 @@ class OvmsManager {
private async applyModelPath(modelDirPath: string): Promise<boolean> { private async applyModelPath(modelDirPath: string): Promise<boolean> {
const homeDir = homedir() const homeDir = homedir()
const patchDir = path.join(homeDir, '.cherrystudio', 'ovms', 'patch') const patchDir = path.join(homeDir, HOME_CHERRY_DIR, 'ovms', 'patch')
if (!(await fs.pathExists(patchDir))) { if (!(await fs.pathExists(patchDir))) {
return true return true
} }
@@ -355,7 +356,7 @@ class OvmsManager {
logger.info(`Adding model: ${modelName} with ID: ${modelId}, Source: ${modelSource}, Task: ${task}`) logger.info(`Adding model: ${modelName} with ID: ${modelId}, Source: ${modelSource}, Task: ${task}`)
const homeDir = homedir() const homeDir = homedir()
const ovdndDir = path.join(homeDir, '.cherrystudio', 'ovms', 'ovms') const ovdndDir = path.join(homeDir, HOME_CHERRY_DIR, 'ovms', 'ovms')
const pathModel = path.join(ovdndDir, 'models', modelId) const pathModel = path.join(ovdndDir, 'models', modelId)
try { try {
@@ -468,7 +469,7 @@ class OvmsManager {
*/ */
public async checkModelExists(modelId: string): Promise<boolean> { public async checkModelExists(modelId: string): Promise<boolean> {
const homeDir = homedir() const homeDir = homedir()
const ovmsDir = path.join(homeDir, '.cherrystudio', 'ovms', 'ovms') const ovmsDir = path.join(homeDir, HOME_CHERRY_DIR, 'ovms', 'ovms')
const configPath = path.join(ovmsDir, 'models', 'config.json') const configPath = path.join(ovmsDir, 'models', 'config.json')
try { try {
@@ -495,7 +496,7 @@ class OvmsManager {
*/ */
public async updateModelConfig(modelName: string, modelId: string): Promise<boolean> { public async updateModelConfig(modelName: string, modelId: string): Promise<boolean> {
const homeDir = homedir() const homeDir = homedir()
const ovmsDir = path.join(homeDir, '.cherrystudio', 'ovms', 'ovms') const ovmsDir = path.join(homeDir, HOME_CHERRY_DIR, 'ovms', 'ovms')
const configPath = path.join(ovmsDir, 'models', 'config.json') const configPath = path.join(ovmsDir, 'models', 'config.json')
try { try {
@@ -548,7 +549,7 @@ class OvmsManager {
*/ */
public async getModels(): Promise<ModelConfig[]> { public async getModels(): Promise<ModelConfig[]> {
const homeDir = homedir() const homeDir = homedir()
const ovmsDir = path.join(homeDir, '.cherrystudio', 'ovms', 'ovms') const ovmsDir = path.join(homeDir, HOME_CHERRY_DIR, 'ovms', 'ovms')
const configPath = path.join(ovmsDir, 'models', 'config.json') const configPath = path.join(ovmsDir, 'models', 'config.json')
try { try {

View File

@@ -3,6 +3,7 @@ import type { Attributes, SpanEntity, TokenUsage, TraceCache } from '@mcp-trace/
import { convertSpanToSpanEntity } from '@mcp-trace/trace-core' import { convertSpanToSpanEntity } from '@mcp-trace/trace-core'
import { SpanStatusCode } from '@opentelemetry/api' import { SpanStatusCode } from '@opentelemetry/api'
import type { ReadableSpan } from '@opentelemetry/sdk-trace-base' import type { ReadableSpan } from '@opentelemetry/sdk-trace-base'
import { HOME_CHERRY_DIR } from '@shared/config/constant'
import fs from 'fs/promises' import fs from 'fs/promises'
import * as os from 'os' import * as os from 'os'
import * as path from 'path' import * as path from 'path'
@@ -18,7 +19,7 @@ class SpanCacheService implements TraceCache {
pri pri
constructor() { constructor() {
this.fileDir = path.join(os.homedir(), '.cherrystudio', 'trace') this.fileDir = path.join(os.homedir(), HOME_CHERRY_DIR, 'trace')
} }
createSpan: (span: ReadableSpan) => void = (span: ReadableSpan) => { createSpan: (span: ReadableSpan) => void = (span: ReadableSpan) => {

View File

@@ -1,5 +1,6 @@
import { loggerService } from '@logger' import { loggerService } from '@logger'
import { isWin } from '@main/constant' import { isWin } from '@main/constant'
import { HOME_CHERRY_DIR } from '@shared/config/constant'
import type { OcrOvConfig, OcrResult, SupportedOcrFile } from '@types' import type { OcrOvConfig, OcrResult, SupportedOcrFile } from '@types'
import { isImageFileMetadata } from '@types' import { isImageFileMetadata } from '@types'
import { exec } from 'child_process' import { exec } from 'child_process'
@@ -13,7 +14,7 @@ import { OcrBaseService } from './OcrBaseService'
const logger = loggerService.withContext('OvOcrService') const logger = loggerService.withContext('OvOcrService')
const execAsync = promisify(exec) const execAsync = promisify(exec)
const PATH_BAT_FILE = path.join(os.homedir(), '.cherrystudio', 'ovms', 'ovocr', 'run.npu.bat') const PATH_BAT_FILE = path.join(os.homedir(), HOME_CHERRY_DIR, 'ovms', 'ovocr', 'run.npu.bat')
export class OvOcrService extends OcrBaseService { export class OvOcrService extends OcrBaseService {
constructor() { constructor() {
@@ -30,7 +31,7 @@ export class OvOcrService extends OcrBaseService {
} }
private getOvOcrPath(): string { private getOvOcrPath(): string {
return path.join(os.homedir(), '.cherrystudio', 'ovms', 'ovocr') return path.join(os.homedir(), HOME_CHERRY_DIR, 'ovms', 'ovocr')
} }
private getImgDir(): string { private getImgDir(): string {

View File

@@ -5,7 +5,7 @@ import os from 'node:os'
import path from 'node:path' import path from 'node:path'
import { loggerService } from '@logger' import { loggerService } from '@logger'
import { audioExts, documentExts, imageExts, MB, textExts, videoExts } from '@shared/config/constant' import { audioExts, documentExts, HOME_CHERRY_DIR, imageExts, MB, textExts, videoExts } from '@shared/config/constant'
import type { FileMetadata, NotesTreeNode } from '@types' import type { FileMetadata, NotesTreeNode } from '@types'
import { FileTypes } from '@types' import { FileTypes } from '@types'
import chardet from 'chardet' import chardet from 'chardet'
@@ -160,7 +160,7 @@ export function getNotesDir() {
} }
export function getConfigDir() { export function getConfigDir() {
return path.join(os.homedir(), '.cherrystudio', 'config') return path.join(os.homedir(), HOME_CHERRY_DIR, 'config')
} }
export function getCacheDir() { export function getCacheDir() {
@@ -172,7 +172,7 @@ export function getAppConfigDir(name: string) {
} }
export function getMcpDir() { export function getMcpDir() {
return path.join(os.homedir(), '.cherrystudio', 'mcp') return path.join(os.homedir(), HOME_CHERRY_DIR, 'mcp')
} }
/** /**

View File

@@ -3,6 +3,7 @@ import os from 'node:os'
import path from 'node:path' import path from 'node:path'
import { isLinux, isPortable, isWin } from '@main/constant' import { isLinux, isPortable, isWin } from '@main/constant'
import { HOME_CHERRY_DIR } from '@shared/config/constant'
import { app } from 'electron' import { app } from 'electron'
// Please don't import any other modules which is not node/electron built-in modules // Please don't import any other modules which is not node/electron built-in modules
@@ -17,7 +18,7 @@ function hasWritePermission(path: string) {
} }
function getConfigDir() { function getConfigDir() {
return path.join(os.homedir(), '.cherrystudio', 'config') return path.join(os.homedir(), HOME_CHERRY_DIR, 'config')
} }
export function initAppDataDir() { export function initAppDataDir() {

View File

@@ -1,4 +1,5 @@
import { loggerService } from '@logger' import { loggerService } from '@logger'
import { HOME_CHERRY_DIR } from '@shared/config/constant'
import { spawn } from 'child_process' import { spawn } from 'child_process'
import fs from 'fs' import fs from 'fs'
import os from 'os' import os from 'os'
@@ -46,11 +47,11 @@ export async function getBinaryName(name: string): Promise<string> {
export async function getBinaryPath(name?: string): Promise<string> { export async function getBinaryPath(name?: string): Promise<string> {
if (!name) { if (!name) {
return path.join(os.homedir(), '.cherrystudio', 'bin') return path.join(os.homedir(), HOME_CHERRY_DIR, 'bin')
} }
const binaryName = await getBinaryName(name) const binaryName = await getBinaryName(name)
const binariesDir = path.join(os.homedir(), '.cherrystudio', 'bin') const binariesDir = path.join(os.homedir(), HOME_CHERRY_DIR, 'bin')
const binariesDirExists = fs.existsSync(binariesDir) const binariesDirExists = fs.existsSync(binariesDir)
return binariesDirExists ? path.join(binariesDir, binaryName) : binaryName return binariesDirExists ? path.join(binariesDir, binaryName) : binaryName
} }