(
filePath: string,
content: string,
options: { encoding: BufferEncoding; mode?: number } = { encoding: 'utf-8' },
)
| 360 | * Sync file writes block the event loop and cause performance issues. |
| 361 | */ |
| 362 | export function writeFileSyncAndFlush_DEPRECATED( |
| 363 | filePath: string, |
| 364 | content: string, |
| 365 | options: { encoding: BufferEncoding; mode?: number } = { encoding: 'utf-8' }, |
| 366 | ): void { |
| 367 | const fs = getFsImplementation() |
| 368 | |
| 369 | // Check if the target file is a symlink to preserve it for all users |
| 370 | // Note: We don't use safeResolvePath here because we need to manually handle |
| 371 | // symlinks to ensure we write to the target while preserving the symlink itself |
| 372 | let targetPath = filePath |
| 373 | try { |
| 374 | // Try to read the symlink - if successful, it's a symlink |
| 375 | const linkTarget = fs.readlinkSync(filePath) |
| 376 | // Resolve to absolute path |
| 377 | targetPath = isAbsolute(linkTarget) |
| 378 | ? linkTarget |
| 379 | : resolve(dirname(filePath), linkTarget) |
| 380 | logForDebugging(`Writing through symlink: ${filePath} -> ${targetPath}`) |
| 381 | } catch { |
| 382 | // ENOENT (doesn't exist) or EINVAL (not a symlink) — keep targetPath = filePath |
| 383 | } |
| 384 | |
| 385 | // Try atomic write first |
| 386 | const tempPath = `${targetPath}.tmp.${process.pid}.${Date.now()}` |
| 387 | |
| 388 | // Check if target file exists and get its permissions (single stat, reused in both atomic and fallback paths) |
| 389 | let targetMode: number | undefined |
| 390 | let targetExists = false |
| 391 | try { |
| 392 | targetMode = fs.statSync(targetPath).mode |
| 393 | targetExists = true |
| 394 | logForDebugging(`Preserving file permissions: ${targetMode.toString(8)}`) |
| 395 | } catch (e) { |
| 396 | if (!isENOENT(e)) throw e |
| 397 | if (options.mode !== undefined) { |
| 398 | // Use provided mode for new files |
| 399 | targetMode = options.mode |
| 400 | logForDebugging( |
| 401 | `Setting permissions for new file: ${targetMode.toString(8)}`, |
| 402 | ) |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | try { |
| 407 | logForDebugging(`Writing to temp file: ${tempPath}`) |
| 408 | |
| 409 | // Write to temp file with flush and mode (if specified for new file) |
| 410 | const writeOptions: { |
| 411 | encoding: BufferEncoding |
| 412 | flush: boolean |
| 413 | mode?: number |
| 414 | } = { |
| 415 | encoding: options.encoding, |
| 416 | flush: true, |
| 417 | } |
| 418 | // Only set mode in writeFileSync for new files to ensure atomic permission setting |
| 419 | if (!targetExists && options.mode !== undefined) { |
no test coverage detected