Skip to content

Commit 194df50

Browse files
Merge pull request #36 from MiniMax-AI-Dev/fix/atomic-write
fix(config): use atomic write (tmp+rename) for credentials and config files
2 parents dc31183 + 0b0224d commit 194df50

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

src/auth/credentials.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { readFileSync, writeFileSync, unlinkSync, existsSync, statSync } from 'fs';
1+
import { readFileSync, writeFileSync, renameSync, unlinkSync, existsSync, statSync } from 'fs';
22
import { getCredentialsPath, ensureConfigDir } from '../config/paths';
33
import type { CredentialFile } from './types';
44

@@ -24,7 +24,9 @@ export async function loadCredentials(): Promise<CredentialFile | null> {
2424
export async function saveCredentials(creds: CredentialFile): Promise<void> {
2525
await ensureConfigDir();
2626
const path = getCredentialsPath();
27-
writeFileSync(path, JSON.stringify(creds, null, 2) + '\n', { mode: 0o600 });
27+
const tmp = path + '.tmp';
28+
writeFileSync(tmp, JSON.stringify(creds, null, 2) + '\n', { mode: 0o600 });
29+
renameSync(tmp, path);
2830
}
2931

3032
export async function clearCredentials(): Promise<void> {

src/config/loader.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { readFileSync, writeFileSync, existsSync } from 'fs';
1+
import { readFileSync, writeFileSync, renameSync, existsSync } from 'fs';
22
import { parseConfigFile, REGIONS, type Config, type ConfigFile, type Region } from './schema';
33
import { ensureConfigDir, getConfigPath } from './paths';
44
import { detectOutputFormat, type OutputFormat } from '../output/formatter';
@@ -20,7 +20,10 @@ export function readConfigFile(): ConfigFile {
2020

2121
export async function writeConfigFile(data: Record<string, unknown>): Promise<void> {
2222
await ensureConfigDir();
23-
writeFileSync(getConfigPath(), JSON.stringify(data, null, 2) + '\n', { mode: 0o600 });
23+
const path = getConfigPath();
24+
const tmp = path + '.tmp';
25+
writeFileSync(tmp, JSON.stringify(data, null, 2) + '\n', { mode: 0o600 });
26+
renameSync(tmp, path);
2427
}
2528

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

0 commit comments

Comments
 (0)