()
| 7 | * Returns null if stdin is a TTY (interactive terminal) or if no data available |
| 8 | */ |
| 9 | export function readStdinSync(): string | null { |
| 10 | try { |
| 11 | // In test environments, don't attempt to read stdin to avoid hanging |
| 12 | // But allow CI environments to read stdin when it's clearly piped |
| 13 | if ( |
| 14 | process.env.NODE_ENV === "test" || |
| 15 | process.env.VITEST === "true" || |
| 16 | process.env.JEST_WORKER_ID !== undefined || |
| 17 | process.env.CONTINUE_CLI_TEST === "true" |
| 18 | ) { |
| 19 | return null; |
| 20 | } |
| 21 | |
| 22 | // Skip stdin reading in headless mode when a prompt is supplied |
| 23 | // This prevents blocking on TTY-less environments (like VSCode/IntelliJ terminal tools) |
| 24 | if (isHeadlessMode() && hasSuppliedPrompt()) { |
| 25 | return null; |
| 26 | } |
| 27 | |
| 28 | // Special handling for CI environments - allow reading if stdin is clearly not a TTY |
| 29 | if (process.env.CI === "true" && process.stdin.isTTY === true) { |
| 30 | return null; |
| 31 | } |
| 32 | |
| 33 | // In TTY-less environments (Docker, CI, VSCode/IntelliJ terminal tools), |
| 34 | // attempting to read stdin can hang or fail |
| 35 | // Only attempt to read if we're confident there's piped input |
| 36 | if (process.env.FORCE_NO_TTY === "true") { |
| 37 | return null; |
| 38 | } |
| 39 | |
| 40 | // Check if stdin is a TTY (interactive terminal) |
| 41 | if (process.stdin.isTTY === true) { |
| 42 | // Definitely a TTY, don't read |
| 43 | return null; |
| 44 | } |
| 45 | |
| 46 | // If isTTY is false, we likely have piped input |
| 47 | if (process.stdin.isTTY === false) { |
| 48 | // Try to read stdin with a fallback |
| 49 | try { |
| 50 | const stdinData = fs.readFileSync(0, "utf8"); |
| 51 | return stdinData.trim(); |
| 52 | } catch { |
| 53 | return null; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // If isTTY is undefined, be cautious and check if we can read non-blockingly |
| 58 | // This handles cases where TTY detection is unreliable |
| 59 | try { |
| 60 | // Use readFileSync with fd 0 but wrap in timeout logic |
| 61 | const stdinData = fs.readFileSync(0, "utf8"); |
| 62 | return stdinData.trim(); |
| 63 | } catch { |
| 64 | // If we can't read (EAGAIN, EWOULDBLOCK, etc.), assume no piped input |
| 65 | return null; |
| 66 | } |
no test coverage detected