fix(config): use canonical path after legacy migration and make writes atomic

This commit is contained in:
YeonGyu-Kim
2026-04-04 14:33:52 +09:00
parent 2d72f51a92
commit 0c6907adc3
6 changed files with 80 additions and 39 deletions
+2 -13
View File
@@ -1,8 +1,9 @@
import { closeSync, existsSync, fsyncSync, openSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs"
import { existsSync, readFileSync, renameSync, rmSync } from "node:fs"
import { join, dirname, basename } from "node:path"
import { log } from "./logger"
import { CONFIG_BASENAME, LEGACY_CONFIG_BASENAME } from "./plugin-identity"
import { writeFileAtomically } from "./write-file-atomically"
function buildCanonicalPath(legacyPath: string): string {
const dir = dirname(legacyPath)
@@ -10,18 +11,6 @@ function buildCanonicalPath(legacyPath: string): string {
return join(dir, `${CONFIG_BASENAME}${ext}`)
}
function writeFileAtomically(filePath: string, content: string): void {
const tempPath = `${filePath}.tmp`
writeFileSync(tempPath, content, "utf-8")
const tempFileDescriptor = openSync(tempPath, "r")
try {
fsyncSync(tempFileDescriptor)
} finally {
closeSync(tempFileDescriptor)
}
renameSync(tempPath, filePath)
}
function archiveLegacyConfigFile(legacyPath: string): boolean {
const backupPath = `${legacyPath}.bak`