(
filePath: string,
value: unknown,
options: WriteConfigFileOptions = {}
)
| 66 | } |
| 67 | |
| 68 | function writeJsonFileSync( |
| 69 | filePath: string, |
| 70 | value: unknown, |
| 71 | options: WriteConfigFileOptions = {} |
| 72 | ): void { |
| 73 | const directory = path.dirname(filePath); |
| 74 | const tempFilePath = path.join( |
| 75 | directory, |
| 76 | `.${path.basename(filePath)}.${process.pid}.${Date.now()}.tmp` |
| 77 | ); |
| 78 | const content = `${JSON.stringify(value, null, options.indent ?? 2)}\n`; |
| 79 | |
| 80 | fs.mkdirSync(directory, { recursive: true }); |
| 81 | |
| 82 | try { |
| 83 | fs.writeFileSync(tempFilePath, content, { |
| 84 | encoding: 'utf8', |
| 85 | mode: options.mode, |
| 86 | }); |
| 87 | fs.renameSync(tempFilePath, filePath); |
| 88 | } catch (error) { |
| 89 | try { |
| 90 | fs.rmSync(tempFilePath, { force: true }); |
| 91 | } catch { |
| 92 | // Best-effort cleanup for failed atomic writes. |
| 93 | } |
| 94 | |
| 95 | throw error; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | export function readConfigFile<S extends z.ZodType>( |
| 100 | configPath: string, |
no test coverage detected