|
| 1 | +import { existsSync, readFileSync, readSync } from 'node:fs' |
1 | 2 | import { CLI_CONTRACT } from '../contract/commands.js' |
2 | 3 | import type { FlagSpec } from '../contract/types.js' |
3 | 4 | import { V2_OPERATIONS, type V2OperationName } from '../generated/v2-api.js' |
@@ -37,6 +38,89 @@ export function takesJson(field: FieldSpec, flag: FlagSpec): boolean { |
37 | 38 | return flag.json === true || JSON_KINDS.has(field.kind) |
38 | 39 | } |
39 | 40 |
|
| 41 | +/** |
| 42 | + * Drains stdin synchronously. |
| 43 | + * |
| 44 | + * `readFileSync(0)` looks like the obvious way to do this and fails on the one |
| 45 | + * case that matters: a pipe is opened non-blocking, so a single read of an |
| 46 | + * upstream process that has not written yet returns EAGAIN rather than waiting, |
| 47 | + * and `export … | import --workflow @-` died with a raw stack trace. Reading in |
| 48 | + * a loop and treating EAGAIN as "not ready yet" is what makes a pipe work. |
| 49 | + * |
| 50 | + * `Atomics.wait` is the only synchronous sleep available; without it the retry |
| 51 | + * spins a core for as long as the writer takes. |
| 52 | + */ |
| 53 | +function readStdin(): string { |
| 54 | + const idle = new Int32Array(new SharedArrayBuffer(4)) |
| 55 | + const buffer = Buffer.alloc(64 * 1024) |
| 56 | + const chunks: Buffer[] = [] |
| 57 | + |
| 58 | + for (;;) { |
| 59 | + let read: number |
| 60 | + try { |
| 61 | + read = readSync(0, buffer, 0, buffer.length, null) |
| 62 | + } catch (error) { |
| 63 | + const code = (error as NodeJS.ErrnoException).code |
| 64 | + if (code === 'EAGAIN') { |
| 65 | + Atomics.wait(idle, 0, 0, 5) |
| 66 | + continue |
| 67 | + } |
| 68 | + // Some platforms report end-of-input on a pipe as EOF rather than 0. |
| 69 | + if (code === 'EOF') break |
| 70 | + throw error |
| 71 | + } |
| 72 | + if (read === 0) break |
| 73 | + chunks.push(Buffer.from(buffer.subarray(0, read))) |
| 74 | + } |
| 75 | + |
| 76 | + return Buffer.concat(chunks).toString('utf8') |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * Resolves a JSON flag's argument, which may name a file instead of carrying |
| 81 | + * the document inline. |
| 82 | + * |
| 83 | + * `@path` reads the file and `@-` reads stdin, the curl convention. A workflow |
| 84 | + * export is hundreds of lines, and the shell makes passing that literally |
| 85 | + * unpleasant — unquoted `$(cat f.json)` word-splits into broken JSON, and the |
| 86 | + * quoted form is easy to get wrong. `@` cannot collide with a real value |
| 87 | + * because JSON only ever starts with `{ [ " -`, a digit, or t/f/n. |
| 88 | + */ |
| 89 | +function readJsonArgument(raw: string, flagName: string): { text: string; from: string } { |
| 90 | + if (!raw.startsWith('@')) return { text: raw, from: '' } |
| 91 | + |
| 92 | + const path = raw.slice(1) |
| 93 | + if (path === '-') { |
| 94 | + if (process.stdin.isTTY) { |
| 95 | + throw new SimApiError(`--${flagName} @- reads stdin, but nothing is piped in`, 0) |
| 96 | + } |
| 97 | + try { |
| 98 | + return { text: readStdin(), from: ' (read from stdin)' } |
| 99 | + } catch (error) { |
| 100 | + throw new SimApiError(`--${flagName} cannot read stdin: ${(error as Error).message}`, 0) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + try { |
| 105 | + return { text: readFileSync(path, 'utf8'), from: ` (read from ${path})` } |
| 106 | + } catch (error) { |
| 107 | + throw new SimApiError(`--${flagName} cannot read ${path}: ${(error as Error).message}`, 0) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Points at `@` when a value that failed to parse looks like a filename. |
| 113 | + * |
| 114 | + * `--workflow export.json` is the natural first guess, and "must be valid JSON" |
| 115 | + * alone gives no clue that passing a file is even supported. |
| 116 | + */ |
| 117 | +function pathHint(raw: string): string { |
| 118 | + if (raw.startsWith('@') || /^\s*[[{"\-\d]|^\s*(true|false|null)/.test(raw)) return '' |
| 119 | + return existsSync(raw) |
| 120 | + ? `. ${raw} is a file — pass it as @${raw}` |
| 121 | + : '. To read a file, pass @path (or @- for stdin)' |
| 122 | +} |
| 123 | + |
40 | 124 | /** |
41 | 125 | * Turns the string argv provides into the value the contract expects. |
42 | 126 | * |
@@ -66,10 +150,14 @@ export function coerce(raw: unknown, field: FieldSpec, flag: FlagSpec, flagName: |
66 | 150 |
|
67 | 151 | if (takesJson(field, flag)) { |
68 | 152 | if (typeof raw !== 'string') return raw |
| 153 | + const source = readJsonArgument(raw, flagName) |
69 | 154 | try { |
70 | | - return JSON.parse(raw) |
| 155 | + return JSON.parse(source.text) |
71 | 156 | } catch (error) { |
72 | | - throw new SimApiError(`--${flagName} must be valid JSON: ${(error as Error).message}`, 0) |
| 157 | + throw new SimApiError( |
| 158 | + `--${flagName} must be valid JSON${source.from}: ${(error as Error).message}${pathHint(raw)}`, |
| 159 | + 0 |
| 160 | + ) |
73 | 161 | } |
74 | 162 | } |
75 | 163 |
|
|
0 commit comments