( options: CliOptions, signal?: AbortSignal )
| 168 | } |
| 169 | |
| 170 | export async function watchMirrow( |
| 171 | options: CliOptions, |
| 172 | signal?: AbortSignal |
| 173 | ): Promise<void> { |
| 174 | const { input, output, depth } = options; |
| 175 | const abortSignal = signal ?? new AbortController().signal; |
| 176 | |
| 177 | const inputStats = await fs.stat(input).catch(() => { |
| 178 | throw new Error(`Input not found: ${input}`); |
| 179 | }); |
| 180 | |
| 181 | const outputStats = await fs.stat(output).catch(() => undefined); |
| 182 | |
| 183 | if (inputStats.isDirectory()) { |
| 184 | if (outputStats && !outputStats.isDirectory()) { |
| 185 | throw new Error("Input is a directory, but output is not"); |
| 186 | } |
| 187 | } else if (outputStats && outputStats.isDirectory()) { |
| 188 | throw new Error("Input is a file, but output is a directory"); |
| 189 | } |
| 190 | |
| 191 | await runMirrow(options); |
| 192 | console.log( |
| 193 | `Watching ${formatPath(input)} (depth: ${depth === Infinity ? "unbound" : depth |
| 194 | })` |
| 195 | ); |
| 196 | console.log("Press Ctrl+C to stop.\n"); |
| 197 | |
| 198 | const enqueue = createTaskQueue(); |
| 199 | |
| 200 | if (inputStats.isDirectory()) { |
| 201 | await watchDirectory(input, output, depth, abortSignal, enqueue); |
| 202 | } else { |
| 203 | await watchFile(input, output, abortSignal, enqueue); |
| 204 | } |
| 205 | |
| 206 | console.log("Watcher stopped."); |
| 207 | } |
| 208 | |
| 209 | async function watchDirectory( |
| 210 | inputDir: string, |
no test coverage detected