* Waits for a file to be created and populated (removed = false) or removed (removed = true).
(filePath: string, removed = false)
| 27 | * Waits for a file to be created and populated (removed = false) or removed (removed = true). |
| 28 | */ |
| 29 | function waitForFile(filePath: string, removed = false) { |
| 30 | return new Promise<void>((resolve, reject) => { |
| 31 | const check = () => { |
| 32 | const exists = fs.existsSync(filePath); |
| 33 | if (removed) { |
| 34 | return !exists; |
| 35 | } |
| 36 | if (!exists) { |
| 37 | return false; |
| 38 | } |
| 39 | try { |
| 40 | return fs.statSync(filePath).size > 0; |
| 41 | } catch { |
| 42 | return false; |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | if (check()) { |
| 47 | resolve(); |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | const timer = setTimeout(() => { |
| 52 | fs.unwatchFile(filePath); |
| 53 | reject( |
| 54 | new Error( |
| 55 | `Timeout: file ${filePath} ${removed ? 'not removed' : 'not found'} within ${FILE_TIMEOUT}ms`, |
| 56 | ), |
| 57 | ); |
| 58 | }, FILE_TIMEOUT); |
| 59 | |
| 60 | fs.watchFile(filePath, {interval: 500}, () => { |
| 61 | if (check()) { |
| 62 | clearTimeout(timer); |
| 63 | fs.unwatchFile(filePath); |
| 64 | resolve(); |
| 65 | } |
| 66 | }); |
| 67 | }); |
| 68 | } |
| 69 | |
| 70 | export async function startDaemon(mcpArgs: string[] = [], sessionId: string) { |
| 71 | if (isDaemonRunning(sessionId)) { |
no test coverage detected