(filePath: string)
| 75 | * readSync(4KB). |
| 76 | */ |
| 77 | export function readFileSyncWithMetadata(filePath: string): { |
| 78 | content: string |
| 79 | encoding: BufferEncoding |
| 80 | lineEndings: LineEndingType |
| 81 | } { |
| 82 | const fs = getFsImplementation() |
| 83 | const { resolvedPath, isSymlink } = safeResolvePath(fs, filePath) |
| 84 | |
| 85 | if (isSymlink) { |
| 86 | logForDebugging(`Reading through symlink: ${filePath} -> ${resolvedPath}`) |
| 87 | } |
| 88 | |
| 89 | const encoding = detectEncodingForResolvedPath(resolvedPath) |
| 90 | const raw = fs.readFileSync(resolvedPath, { encoding }) |
| 91 | // Detect line endings from the raw head before CRLF normalization erases |
| 92 | // the distinction. 4096 code units is ≥ detectLineEndings's 4096-byte |
| 93 | // readSync sample (line endings are ASCII, so the unit mismatch is moot). |
| 94 | const lineEndings = detectLineEndingsForString(raw.slice(0, 4096)) |
| 95 | return { |
| 96 | content: raw.replaceAll('\r\n', '\n'), |
| 97 | encoding, |
| 98 | lineEndings, |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | export function readFileSync(filePath: string): string { |
| 103 | return readFileSyncWithMetadata(filePath).content |
no test coverage detected