(filePath: string, rootDir: string)
| 64 | * without loading arbitrarily large files into memory. |
| 65 | */ |
| 66 | export async function readFile(filePath: string, rootDir: string): Promise<string> { |
| 67 | const resolved = await resolveAndValidate(filePath, rootDir); |
| 68 | const stat = await fs.stat(resolved); |
| 69 | |
| 70 | if (!stat.isFile()) { |
| 71 | throw new Error(`Not a file: ${filePath}`); |
| 72 | } |
| 73 | |
| 74 | if (stat.size <= MAX_FILE_SIZE) { |
| 75 | return fs.readFile(resolved, 'utf-8'); |
| 76 | } |
| 77 | |
| 78 | if (stat.size <= MAX_FILE_SCAN_BYTES) { |
| 79 | const content = await fs.readFile(resolved, 'utf-8'); |
| 80 | if (content.length <= MAX_FILE_SIZE) { |
| 81 | return content; |
| 82 | } |
| 83 | return content.slice(0, MAX_FILE_SIZE) + `\n\n[truncated — file is ${stat.size} bytes]`; |
| 84 | } |
| 85 | |
| 86 | const content = await readFilePrefix(resolved, MAX_FILE_SCAN_BYTES); |
| 87 | return content.slice(0, MAX_FILE_SIZE) + `\n\n[truncated — file is ${stat.size} bytes]`; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * List directory entries relative to rootDir. |
no test coverage detected
searching dependent graphs…