Skip to content

Commit 91051a5

Browse files
RyanLee-Devclaude
andcommitted
refactor: store config as JSON instead of YAML
- paths.ts: getConfigPath() returns config.json; add getLegacyConfigPath() - loader.ts: read/write with JSON.parse/JSON.stringify; silent one-time migration from config.yaml → config.json (old file kept as .bak) - resolver.ts, auth/login.ts, auth/logout.ts: update config.yaml strings yaml package stays (still used for --output yaml formatting). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 87fbbda commit 91051a5

5 files changed

Lines changed: 31 additions & 9 deletions

File tree

src/auth/resolver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export async function resolveCredential(config: Config): Promise<ResolvedCredent
2020

2121
// 3. API key from config file
2222
if (config.fileApiKey) {
23-
return { token: config.fileApiKey, method: 'api-key', source: 'config.yaml' };
23+
return { token: config.fileApiKey, method: 'api-key', source: 'config.json' };
2424
}
2525

2626
// 4. Environment variable

src/commands/auth/login.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export default defineCommand({
7777
);
7878
}
7979

80-
// Store key in config.yaml
80+
// Store key in config.json
8181
const existing = readConfigFile() as Record<string, unknown>;
8282
existing.api_key = key;
8383
await writeConfigFile(existing);

src/commands/auth/logout.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default defineCommand({
2323

2424
if (config.dryRun) {
2525
if (creds) console.log('Would remove ~/.minimax/credentials.json');
26-
if (hasConfigKey) console.log('Would clear api_key from ~/.minimax/config.yaml');
26+
if (hasConfigKey) console.log('Would clear api_key from ~/.minimax/config.json');
2727
if (!creds && !hasConfigKey) console.log('No credentials to clear.');
2828
console.log('No changes made.');
2929
return;
@@ -39,7 +39,7 @@ export default defineCommand({
3939
const updated = fileConfig as Record<string, unknown>;
4040
delete updated.api_key;
4141
await writeConfigFile(updated);
42-
process.stderr.write('Cleared api_key from ~/.minimax/config.yaml\n');
42+
process.stderr.write('Cleared api_key from ~/.minimax/config.json\n');
4343
} catch { /* ignore */ }
4444
}
4545

src/config/loader.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,41 @@
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';
32
import { parseConfigFile, REGIONS, type Config, type ConfigFile, type Region } from './schema';
4-
import { ensureConfigDir, getConfigPath } from './paths';
3+
import { ensureConfigDir, getConfigPath, getLegacyConfigPath } from './paths';
54
import { detectOutputFormat, type OutputFormat } from '../output/formatter';
65
import type { GlobalFlags } from '../types/flags';
76

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+
825
export function readConfigFile(): ConfigFile {
26+
migrateLegacyConfig();
927
const path = getConfigPath();
1028
if (!existsSync(path)) return {};
1129
try {
12-
return parseConfigFile(parseYaml(readFileSync(path, 'utf-8')));
30+
return parseConfigFile(JSON.parse(readFileSync(path, 'utf-8')));
1331
} catch {
1432
return {};
1533
}
1634
}
1735

1836
export async function writeConfigFile(data: Record<string, unknown>): Promise<void> {
1937
await ensureConfigDir();
20-
writeFileSync(getConfigPath(), yamlStringify(data), { mode: 0o600 });
38+
writeFileSync(getConfigPath(), JSON.stringify(data, null, 2) + '\n', { mode: 0o600 });
2139
}
2240

2341
export function loadConfig(flags: GlobalFlags): Config {

src/config/paths.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ export function getConfigDir(): string {
88
}
99

1010
export function getConfigPath(): string {
11+
return join(getConfigDir(), 'config.json');
12+
}
13+
14+
export function getLegacyConfigPath(): string {
1115
return join(getConfigDir(), 'config.yaml');
1216
}
1317

0 commit comments

Comments
 (0)