( ctx: CommandContext, files: string[], options: ReadFilesOptions, )
| 56 | * } |
| 57 | */ |
| 58 | export async function readFiles( |
| 59 | ctx: CommandContext, |
| 60 | files: string[], |
| 61 | options: ReadFilesOptions, |
| 62 | ): Promise<ReadFilesResult> { |
| 63 | const { |
| 64 | cmdName, |
| 65 | allowStdinMarker = true, |
| 66 | stopOnError = false, |
| 67 | batchSize = DEFAULT_BATCH_SIZE, |
| 68 | } = options; |
| 69 | |
| 70 | // No files - read from stdin |
| 71 | if (files.length === 0) { |
| 72 | return { |
| 73 | files: [{ filename: "", content: ctx.stdin }], |
| 74 | stderr: "", |
| 75 | exitCode: 0, |
| 76 | }; |
| 77 | } |
| 78 | |
| 79 | const result: FileContent[] = []; |
| 80 | let stderr = ""; |
| 81 | let exitCode = 0; |
| 82 | |
| 83 | // Process files in parallel batches for better performance |
| 84 | for (let i = 0; i < files.length; i += batchSize) { |
| 85 | const batch = files.slice(i, i + batchSize); |
| 86 | const batchResults = await Promise.all( |
| 87 | batch.map(async (file) => { |
| 88 | if (allowStdinMarker && file === "-") { |
| 89 | return { |
| 90 | filename: "-", |
| 91 | content: ctx.stdin, |
| 92 | error: null as string | null, |
| 93 | }; |
| 94 | } |
| 95 | try { |
| 96 | const filePath = ctx.fs.resolvePath(ctx.cwd, file); |
| 97 | // Use binary encoding to preserve all bytes (including non-UTF-8). |
| 98 | // This is important for piping binary data through commands like cat. |
| 99 | // Text-processing commands must explicitly call `decodeBytesToUtf8` |
| 100 | // on the content before regex / parsing. |
| 101 | const content = await readBytesFrom(ctx.fs, filePath); |
| 102 | return { filename: file, content, error: null as string | null }; |
| 103 | } catch { |
| 104 | return { |
| 105 | filename: file, |
| 106 | content: EMPTY_BYTES, |
| 107 | error: `${cmdName}: ${file}: No such file or directory\n`, |
| 108 | }; |
| 109 | } |
| 110 | }), |
| 111 | ); |
| 112 | |
| 113 | // Process results in order |
| 114 | for (const r of batchResults) { |
| 115 | if (r.error) { |
no test coverage detected
searching dependent graphs…