|
| 1 | +/** |
| 2 | + * Loop command for StackMemory CLI |
| 3 | + * Repeatedly runs a shell command until a condition is met. |
| 4 | + * Useful for monitoring GitHub Actions, deploy logs, inboxes, etc. |
| 5 | + */ |
| 6 | + |
| 7 | +import { Command } from 'commander'; |
| 8 | +import { execSync } from 'child_process'; |
| 9 | +import chalk from 'chalk'; |
| 10 | + |
| 11 | +export interface LoopOptions { |
| 12 | + until?: string; |
| 13 | + untilNot?: string; |
| 14 | + untilEmpty: boolean; |
| 15 | + untilNonEmpty: boolean; |
| 16 | + untilExit: boolean; |
| 17 | + interval: string; |
| 18 | + timeout: string; |
| 19 | + quiet: boolean; |
| 20 | + json: boolean; |
| 21 | + label?: string; |
| 22 | + shell: string; |
| 23 | +} |
| 24 | + |
| 25 | +export interface LoopResult { |
| 26 | + ok: boolean; |
| 27 | + reason: 'matched' | 'timeout' | 'error'; |
| 28 | + iterations: number; |
| 29 | + elapsed: number; |
| 30 | + lastOutput: string; |
| 31 | +} |
| 32 | + |
| 33 | +function parseSeconds(value: string): number { |
| 34 | + const match = value.match(/^(\d+)(s|m|h)?$/); |
| 35 | + if (!match) return parseInt(value, 10) || 10; |
| 36 | + const num = parseInt(match[1], 10); |
| 37 | + switch (match[2]) { |
| 38 | + case 'h': |
| 39 | + return num * 3600; |
| 40 | + case 'm': |
| 41 | + return num * 60; |
| 42 | + default: |
| 43 | + return num; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +function sleep(ms: number): Promise<void> { |
| 48 | + return new Promise((resolve) => setTimeout(resolve, ms)); |
| 49 | +} |
| 50 | + |
| 51 | +function checkCondition( |
| 52 | + output: string, |
| 53 | + exitCode: number, |
| 54 | + opts: LoopOptions |
| 55 | +): boolean { |
| 56 | + if (opts.untilExit) return exitCode === 0; |
| 57 | + if (opts.untilEmpty) return output.trim().length === 0; |
| 58 | + if (opts.untilNonEmpty) return output.trim().length > 0; |
| 59 | + if (opts.until) return output.includes(opts.until); |
| 60 | + if (opts.untilNot) return !output.includes(opts.untilNot); |
| 61 | + // Default: succeed when command exits 0 |
| 62 | + return exitCode === 0; |
| 63 | +} |
| 64 | + |
| 65 | +export function createLoopCommand(): Command { |
| 66 | + return new Command('loop') |
| 67 | + .alias('watch') |
| 68 | + .description( |
| 69 | + 'Run a command repeatedly until a condition is met (monitor CI, deploys, logs)' |
| 70 | + ) |
| 71 | + .argument('<command>', 'Shell command to run on each iteration') |
| 72 | + .option('--until <pattern>', 'Stop when output contains this string') |
| 73 | + .option( |
| 74 | + '--until-not <pattern>', |
| 75 | + 'Stop when output no longer contains this string' |
| 76 | + ) |
| 77 | + .option('--until-empty', 'Stop when output is empty', false) |
| 78 | + .option('--until-non-empty', 'Stop when output is non-empty', false) |
| 79 | + .option( |
| 80 | + '--until-exit', |
| 81 | + 'Stop when command exits with code 0 (ignore output)', |
| 82 | + false |
| 83 | + ) |
| 84 | + .option('-i, --interval <duration>', 'Check interval (e.g. 10s, 1m)', '10s') |
| 85 | + .option('-t, --timeout <duration>', 'Max wait time (e.g. 30m, 1h)', '30m') |
| 86 | + .option('-q, --quiet', 'Only show final result', false) |
| 87 | + .option('--json', 'Output result as JSON', false) |
| 88 | + .option('-l, --label <name>', 'Label for this loop (shown in output)') |
| 89 | + .option('--shell <path>', 'Shell to use', '/bin/sh') |
| 90 | + .action(async (command: string, opts: LoopOptions) => { |
| 91 | + const intervalSec = parseSeconds(opts.interval); |
| 92 | + const timeoutSec = parseSeconds(opts.timeout); |
| 93 | + const label = opts.label || command.slice(0, 40); |
| 94 | + const startTime = Date.now(); |
| 95 | + const deadline = startTime + timeoutSec * 1000; |
| 96 | + let iterations = 0; |
| 97 | + let lastOutput = ''; |
| 98 | + |
| 99 | + if (!opts.quiet && !opts.json) { |
| 100 | + console.log( |
| 101 | + chalk.cyan(`[loop] ${label}`) + |
| 102 | + chalk.gray(` (every ${intervalSec}s, timeout ${timeoutSec}s)`) |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + while (Date.now() < deadline) { |
| 107 | + iterations++; |
| 108 | + let output = ''; |
| 109 | + let exitCode = 0; |
| 110 | + |
| 111 | + try { |
| 112 | + output = execSync(command, { |
| 113 | + encoding: 'utf8', |
| 114 | + timeout: Math.min(intervalSec * 1000, 60_000), |
| 115 | + shell: opts.shell, |
| 116 | + stdio: ['pipe', 'pipe', 'pipe'], |
| 117 | + }); |
| 118 | + } catch (error: unknown) { |
| 119 | + const e = error as { |
| 120 | + status?: number; |
| 121 | + stdout?: string; |
| 122 | + stderr?: string; |
| 123 | + }; |
| 124 | + exitCode = e.status ?? 1; |
| 125 | + output = (e.stdout || '') + (e.stderr || ''); |
| 126 | + } |
| 127 | + |
| 128 | + lastOutput = output; |
| 129 | + |
| 130 | + if (!opts.quiet && !opts.json) { |
| 131 | + const elapsed = Math.round((Date.now() - startTime) / 1000); |
| 132 | + const preview = output.trim().split('\n').slice(-3).join('\n'); |
| 133 | + console.log( |
| 134 | + chalk.gray(`[${elapsed}s #${iterations}]`) + |
| 135 | + (preview ? `\n${preview}` : chalk.gray(' (no output)')) |
| 136 | + ); |
| 137 | + } |
| 138 | + |
| 139 | + if (checkCondition(output, exitCode, opts)) { |
| 140 | + const result: LoopResult = { |
| 141 | + ok: true, |
| 142 | + reason: 'matched', |
| 143 | + iterations, |
| 144 | + elapsed: Date.now() - startTime, |
| 145 | + lastOutput: lastOutput.trim(), |
| 146 | + }; |
| 147 | + |
| 148 | + if (opts.json) { |
| 149 | + console.log(JSON.stringify(result)); |
| 150 | + } else if (!opts.quiet) { |
| 151 | + console.log( |
| 152 | + chalk.green(`[loop] Condition met after ${iterations} iterations`) |
| 153 | + ); |
| 154 | + } |
| 155 | + return; |
| 156 | + } |
| 157 | + |
| 158 | + // Sleep unless we'd exceed the deadline |
| 159 | + const remaining = deadline - Date.now(); |
| 160 | + if (remaining > 0) { |
| 161 | + await sleep(Math.min(intervalSec * 1000, remaining)); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + // Timeout |
| 166 | + const result: LoopResult = { |
| 167 | + ok: false, |
| 168 | + reason: 'timeout', |
| 169 | + iterations, |
| 170 | + elapsed: Date.now() - startTime, |
| 171 | + lastOutput: lastOutput.trim(), |
| 172 | + }; |
| 173 | + |
| 174 | + if (opts.json) { |
| 175 | + console.log(JSON.stringify(result)); |
| 176 | + } else { |
| 177 | + console.log( |
| 178 | + chalk.yellow( |
| 179 | + `[loop] Timed out after ${timeoutSec}s (${iterations} iterations)` |
| 180 | + ) |
| 181 | + ); |
| 182 | + } |
| 183 | + process.exit(1); |
| 184 | + }); |
| 185 | +} |
0 commit comments