This commit is contained in:
24kycj
2026-05-28 08:41:10 +08:00
parent 22b17aed35
commit 505e72ce28
19 changed files with 589 additions and 134 deletions
+72
View File
@@ -0,0 +1,72 @@
import { app } from 'electron'
import fs from 'fs'
import path from 'path'
import log from 'electron-log'
import { getProcessExecDir } from './native-path'
export const APP_CONFIG_FILENAME = 'cardsoon.config.json'
/** 与 cardsoon.config.json 键名一致,后续配置在此扩展 */
export interface AppFileConfig {
designAppPath: string
}
const defaults: AppFileConfig = {
designAppPath: ''
}
let cached: AppFileConfig | null = null
let loadedFrom = ''
function bundledConfigPath(): string {
if (app.isPackaged) {
return path.join(process.resourcesPath, APP_CONFIG_FILENAME)
}
return path.join(app.getAppPath(), 'resources', APP_CONFIG_FILENAME)
}
function configSearchPaths(): string[] {
const besideExe = path.join(getProcessExecDir(), APP_CONFIG_FILENAME)
const bundled = bundledConfigPath()
if (besideExe === bundled) return [besideExe]
return [besideExe, bundled]
}
function parseConfigFile(filePath: string): AppFileConfig {
const raw = JSON.parse(fs.readFileSync(filePath, 'utf8')) as Record<string, unknown>
return {
designAppPath: String(raw.designAppPath ?? '').trim()
}
}
export function loadAppFileConfig(): AppFileConfig {
if (cached) return cached
for (const filePath of configSearchPaths()) {
if (!fs.existsSync(filePath)) continue
try {
cached = parseConfigFile(filePath)
loadedFrom = filePath
log.info(`Loaded ${APP_CONFIG_FILENAME} from ${filePath}`)
return cached
} catch (e) {
log.warn(`Skip invalid ${APP_CONFIG_FILENAME}: ${filePath}`, e)
}
}
cached = { ...defaults }
loadedFrom = ''
log.warn(
`${APP_CONFIG_FILENAME} not found (checked: ${configSearchPaths().join(', ')}), using defaults`
)
return cached
}
export function getAppConfigLoadedPath(): string {
loadAppFileConfig()
return loadedFrom
}
export function getDesignAppPath(): string {
return loadAppFileConfig().designAppPath
}