(searchToken: string, file: string)
| 89 | } |
| 90 | |
| 91 | export async function lineAndOffset(searchToken: string, file: string): Promise<FilePosition> { |
| 92 | const buffer = await readFile(file); |
| 93 | const position = buffer.indexOf(searchToken); |
| 94 | |
| 95 | if (position === -1) { |
| 96 | throw new Error('searchToken not found in file'); |
| 97 | } |
| 98 | |
| 99 | let line = 1; |
| 100 | let index = 0; |
| 101 | let offset = -1; |
| 102 | while (index <= position) { |
| 103 | const nextLine = buffer.indexOf('\n', index); |
| 104 | if (nextLine < position) { |
| 105 | line += 1; |
| 106 | index = nextLine + 1; |
| 107 | continue; |
| 108 | } else { |
| 109 | offset = position - index; |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | return { line, offset: Math.max(0, offset), position }; |
| 114 | } |
| 115 | |
| 116 | function withTimeout<T>(promise: Promise<T>, ms: number | false = 300000): Promise<T> { |
| 117 | if (ms === false) { |
no test coverage detected