(filePath: string)
| 10 | |
| 11 | /** Read file contents as UTF-8 string (async). */ |
| 12 | export async function readTextFile(filePath: string): Promise<string> { |
| 13 | const fh = await open(filePath, "r"); |
| 14 | try { |
| 15 | const size = (await fh.stat()).size; |
| 16 | const buf = Buffer.alloc(size); |
| 17 | let offset = 0; |
| 18 | while (offset < size) { |
| 19 | const { bytesRead } = await fh.read(buf, offset, size - offset, offset); |
| 20 | if (bytesRead === 0) break; |
| 21 | offset += bytesRead; |
| 22 | } |
| 23 | return buf.subarray(0, offset).toString("utf-8"); |
| 24 | } finally { |
| 25 | await fh.close(); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | /** Read file contents as UTF-8 string (sync). */ |
| 30 | export function readTextFileSync(filePath: string): string { |
no test coverage detected