(root)
| 61 | } |
| 62 | |
| 63 | function watchDirectory(root) { |
| 64 | const watchers = []; |
| 65 | const seen = new Set(); |
| 66 | |
| 67 | function addDirectory(dir) { |
| 68 | if (seen.has(dir)) return; |
| 69 | seen.add(dir); |
| 70 | |
| 71 | const watcher = watch(dir, (event, filename) => { |
| 72 | if (!filename) { |
| 73 | scheduleRestart(); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | const path = join(dir, filename.toString()); |
| 78 | if (event === "rename") maybeAddDirectory(path); |
| 79 | scheduleRestart(); |
| 80 | }); |
| 81 | watchers.push(watcher); |
| 82 | |
| 83 | for (const entry of readdirSync(dir)) { |
| 84 | maybeAddDirectory(join(dir, entry)); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | function maybeAddDirectory(path) { |
| 89 | try { |
| 90 | const stats = statSync(path); |
| 91 | if (stats.isDirectory()) addDirectory(path); |
| 92 | } catch { |
| 93 | // The file may have been deleted between the watch event and stat call. |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | addDirectory(root); |
| 98 | return watchers; |
| 99 | } |
| 100 | |
| 101 | function shutdown() { |
| 102 | shuttingDown = true; |
no test coverage detected