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' export interface AppFileConfig { designAppPath: string } const defaults: AppFileConfig = { designAppPath: '' } let cached: AppFileConfig | null = null 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 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) log.info(`Loaded ${APP_CONFIG_FILENAME} from ${filePath}`) return cached } catch (e) { log.warn(`Skip invalid ${APP_CONFIG_FILENAME}: ${filePath}`, e) } } cached = { ...defaults } log.warn( `${APP_CONFIG_FILENAME} not found (checked: ${configSearchPaths().join(', ')}), using defaults` ) return cached } export function getDesignAppPath(): string { return loadAppFileConfig().designAppPath }