(workspaceId: string)
| 59 | } |
| 60 | |
| 61 | export function logs(workspaceId: string): void { |
| 62 | const logFile = resolveLogFile(workspaceId); |
| 63 | let position = 0; |
| 64 | |
| 65 | /** |
| 66 | * Output any new content appended since the last read. |
| 67 | * Returns true when the workflow completion marker is detected. |
| 68 | */ |
| 69 | function flush(): boolean { |
| 70 | try { |
| 71 | const { size } = fs.statSync(logFile); |
| 72 | if (size <= position) return false; |
| 73 | |
| 74 | const data = readRange(logFile, position, size); |
| 75 | process.stdout.write(data); |
| 76 | position = size; |
| 77 | |
| 78 | return COMPLETION_PATTERN.test(data); |
| 79 | } catch { |
| 80 | // File deleted or unreadable — treat as done |
| 81 | return true; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | console.log(`Tailing workflow log: ${logFile}`); |
| 86 | |
| 87 | // 1. Output existing content |
| 88 | if (flush()) { |
| 89 | process.exit(0); |
| 90 | } |
| 91 | |
| 92 | // 2. Watch for appended content via chokidar |
| 93 | const watcher = watch(logFile, { persistent: true }); |
| 94 | |
| 95 | const shutdown = (): void => { |
| 96 | watcher.close().finally(() => process.exit(0)); |
| 97 | // Safety net — force exit if watcher.close() stalls |
| 98 | setTimeout(() => process.exit(0), 1000).unref(); |
| 99 | }; |
| 100 | |
| 101 | watcher.on('change', () => { |
| 102 | if (flush()) shutdown(); |
| 103 | }); |
| 104 | |
| 105 | process.on('SIGINT', shutdown); |
| 106 | } |
no test coverage detected