Files
next-ai-draw-io/electron/main/config-manager.ts
T
dayuan.jiang 610ba6872d refactor(electron): remove the configuration-preset system and read .env with dotenv
- delete electron/settings, the settings preload and window, the
  config-presets IPC channels, the Configuration menu and the preset
  watcher in scripts/electron-dev.mjs; config-manager.ts keeps only the
  user locale (still read from config-presets.json so the saved
  language survives)
- electron/main/env-loader.ts parses .env with dotenv instead of a
  hand-written parser; electron:compile bundles dependencies
- drop the unreachable development branches in port-manager.ts and
  next-server.ts, the duplicated locale normalisation in menu-i18n.ts,
  the unused electronAPI members, resources/entitlements.mac.plist
- scripts/copy-dereferenced.cjs shared by afterPack and
  prepare-electron-build
- CI: delete ci.yml, move type check and concurrency into test.yml,
  Node 24 everywhere, docker-build only on main
- remove unused dependencies (@xmldom/xmldom, remark-gfm,
  @anthropic-ai/tokenizer, @testing-library/user-event, concurrently,
  wait-on, eslint, eslint-config-next, @vitest/coverage-v8, shx),
  .eslintrc.json and the vitest coverage block
2026-10-12 08:11:10 +09:00

58 lines
1.8 KiB
TypeScript

import { app } from "electron"
import * as fs from "fs"
import * as path from "path"
export const USER_LOCALES = ["en", "zh", "ja", "zh-Hant"] as const
export type UserLocale = (typeof USER_LOCALES)[number]
// Earlier versions kept their configuration presets in this file too; the
// name stays so the saved language survives the upgrade
const CONFIG_FILE = "config-presets.json"
function getConfigPath(): string {
return path.join(app.getPath("userData"), CONFIG_FILE)
}
/**
* Load the config file as an object ({} if it is missing or invalid)
*/
function loadConfig(): Record<string, unknown> {
try {
const configPath = getConfigPath()
if (fs.existsSync(configPath)) {
const data = JSON.parse(fs.readFileSync(configPath, "utf-8"))
if (data && typeof data === "object" && !Array.isArray(data)) {
return data
}
console.error("Ignoring invalid config file:", data)
}
} catch (error) {
console.error("Failed to load config file:", error)
}
return {}
}
/**
* Get the menu locale the user chose, if any
*/
export function getUserLocale(): UserLocale | undefined {
const { userLocale } = loadConfig()
return (USER_LOCALES as readonly unknown[]).includes(userLocale)
? (userLocale as UserLocale)
: undefined
}
/**
* Save the menu locale the user chose
*/
export function setUserLocale(locale: UserLocale): void {
const config = { ...loadConfig(), userLocale: locale }
const configPath = getConfigPath()
fs.mkdirSync(path.dirname(configPath), { recursive: true })
// Write a temp file and rename it, so a crash mid-write can't leave
// a truncated file
const tempPath = `${configPath}.tmp`
fs.writeFileSync(tempPath, JSON.stringify(config, null, 2), "utf-8")
fs.renameSync(tempPath, configPath)
}