|
| 1 | +#!/usr/bin/env node |
| 2 | +// Minimal test shim that writes NDJSON progress events to stdout, |
| 3 | +// emits diagnostic lines to stderr optionally, and exits with the |
| 4 | +// requested code. This shim accepts arguments similar to the real CLI: |
| 5 | +// node test-shims/ob-ndjson-shim.js add --format ndjson <url> [--stderr <line>] [--exit <code>] |
| 6 | + |
| 7 | +const { argv, exit, stderr, stdout } = process; |
| 8 | + |
| 9 | +function usage() { |
| 10 | + console.error('Usage: node ob-ndjson-shim.js add --format ndjson <url> [--stderr <line>] [--exit <code>]'); |
| 11 | + exit(2); |
| 12 | +} |
| 13 | + |
| 14 | +async function main() { |
| 15 | + const args = argv.slice(2); |
| 16 | + if (args.length < 3) return usage(); |
| 17 | + |
| 18 | + const cmd = args[0]; |
| 19 | + const fmtIdx = args.indexOf('--format'); |
| 20 | + const fmt = fmtIdx >= 0 ? args[fmtIdx + 1] : undefined; |
| 21 | + const url = args[args.length - 1]; |
| 22 | + |
| 23 | + // parse optional flags |
| 24 | + let stderrLine = null; |
| 25 | + let exitCode = 0; |
| 26 | + for (let i = 0; i < args.length; i++) { |
| 27 | + if (args[i] === '--stderr') { |
| 28 | + stderrLine = args[i + 1]; |
| 29 | + i++; |
| 30 | + } else if (args[i] === '--exit') { |
| 31 | + exitCode = parseInt(args[i + 1], 10) || 0; |
| 32 | + i++; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + if (cmd !== 'add' || fmt !== 'ndjson') return usage(); |
| 37 | + |
| 38 | + // Emit a downloading event, then a completed event for the provided URL |
| 39 | + const now = new Date().toISOString(); |
| 40 | + stdout.write(JSON.stringify({ phase: 'downloading', url, timestamp: now }) + '\n'); |
| 41 | + // small delay to simulate streaming |
| 42 | + await new Promise((r) => setTimeout(r, 10)); |
| 43 | + stdout.write(JSON.stringify({ phase: 'completed', url, title: 'Shim Title', id: 123, timestamp: new Date().toISOString() }) + '\n'); |
| 44 | + |
| 45 | + if (stderrLine) { |
| 46 | + stderr.write(stderrLine + '\n'); |
| 47 | + } |
| 48 | + |
| 49 | + exit(exitCode); |
| 50 | +} |
| 51 | + |
| 52 | +main().catch((err) => { |
| 53 | + console.error('Shim error:', err); |
| 54 | + exit(1); |
| 55 | +}); |
0 commit comments