fix(config): use canonical path after legacy migration and make writes atomic
This commit is contained in:
@@ -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`
|
||||
|
||||
|
||||
Reference in New Issue
Block a user