更新
This commit is contained in:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user