|
1 | | -import { readFileSync, writeFileSync, existsSync } from 'fs'; |
2 | | -import { parse as parseYaml, stringify as yamlStringify } from 'yaml'; |
| 1 | +import { readFileSync, writeFileSync, existsSync, renameSync } from 'fs'; |
3 | 2 | import { parseConfigFile, REGIONS, type Config, type ConfigFile, type Region } from './schema'; |
4 | | -import { ensureConfigDir, getConfigPath } from './paths'; |
| 3 | +import { ensureConfigDir, getConfigPath, getLegacyConfigPath } from './paths'; |
5 | 4 | import { detectOutputFormat, type OutputFormat } from '../output/formatter'; |
6 | 5 | import type { GlobalFlags } from '../types/flags'; |
7 | 6 |
|
| 7 | +function migrateLegacyConfig(): void { |
| 8 | + const legacy = getLegacyConfigPath(); |
| 9 | + const current = getConfigPath(); |
| 10 | + if (existsSync(legacy) && !existsSync(current)) { |
| 11 | + try { |
| 12 | + // Parse YAML by hand — only need simple key: value pairs |
| 13 | + const raw = readFileSync(legacy, 'utf-8'); |
| 14 | + const obj: Record<string, string> = {}; |
| 15 | + for (const line of raw.split('\n')) { |
| 16 | + const m = line.match(/^(\w+):\s*(.+)$/); |
| 17 | + if (m) obj[m[1]!] = m[2]!.trim(); |
| 18 | + } |
| 19 | + writeFileSync(current, JSON.stringify(obj, null, 2) + '\n', { mode: 0o600 }); |
| 20 | + renameSync(legacy, legacy + '.bak'); |
| 21 | + } catch { /* silent */ } |
| 22 | + } |
| 23 | +} |
| 24 | + |
8 | 25 | export function readConfigFile(): ConfigFile { |
| 26 | + migrateLegacyConfig(); |
9 | 27 | const path = getConfigPath(); |
10 | 28 | if (!existsSync(path)) return {}; |
11 | 29 | try { |
12 | | - return parseConfigFile(parseYaml(readFileSync(path, 'utf-8'))); |
| 30 | + return parseConfigFile(JSON.parse(readFileSync(path, 'utf-8'))); |
13 | 31 | } catch { |
14 | 32 | return {}; |
15 | 33 | } |
16 | 34 | } |
17 | 35 |
|
18 | 36 | export async function writeConfigFile(data: Record<string, unknown>): Promise<void> { |
19 | 37 | await ensureConfigDir(); |
20 | | - writeFileSync(getConfigPath(), yamlStringify(data), { mode: 0o600 }); |
| 38 | + writeFileSync(getConfigPath(), JSON.stringify(data, null, 2) + '\n', { mode: 0o600 }); |
21 | 39 | } |
22 | 40 |
|
23 | 41 | export function loadConfig(flags: GlobalFlags): Config { |
|
0 commit comments