/
foult080
/
terminal-db
Обзор
Документация
Войти
/
foult080
/
terminal-db
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/config/ConfigLoader.ts
44 строки
1 KB
foult080
production hardening: fix P0 bugs, remove dead code, add workspace tests
16 июл 2026, 07:34
16 июл 2026, 07:34
39c68ad
Код
Авторство
О чём код?
import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs" import { homedir } from "node:os" import { dirname, join } from "node:path" import { type AppConfig, AppConfigSchema, DEFAULT_CONFIG } from "./AppConfig.js" const DEFAULT_CONFIG_DIR = join(homedir(), ".terminal-db") const DEFAULT_CONFIG_FILE = join(DEFAULT_CONFIG_DIR, "config.json") export class ConfigLoader { private configFile: string private configDir: string constructor(configFile?: string) { this.configFile = configFile ?? DEFAULT_CONFIG_FILE this.configDir = dirname(this.configFile) } load(): AppConfig { if (!existsSync(this.configFile)) { return DEFAULT_CONFIG } try { const raw = readFileSync(this.configFile, "utf-8") const parsed = JSON.parse(raw) return AppConfigSchema.parse(parsed) } catch { return DEFAULT_CONFIG } } save(config: AppConfig): void { if (!existsSync(this.configDir)) { mkdirSync(this.configDir, { recursive: true, mode: 0o700 }) } writeFileSync(this.configFile, JSON.stringify(config, null, 2), { encoding: "utf-8", mode: 0o600 }) try { chmodSync(this.configDir, 0o700) chmodSync(this.configFile, 0o600) } catch { // permissions may not be settable on all platforms } } }