All files / cli/src/utils interactive.js

50% Statements 6/12
62.5% Branches 5/8
50% Functions 1/2
37.5% Lines 3/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43                                      4x 3x     1x                                      
/**
 * Interactive terminal helpers
 *
 * `prompts` never resolves when stdin is not a TTY, so a missing argument in a
 * CI job or a piped shell used to hang until Node bailed out with "Detected
 * unsettled top-level await" (exit 13) instead of printing a usable error.
 * These helpers detect that up front and fail fast with actionable output.
 */
 
import picocolors from 'picocolors';
import { env, stdin } from 'node:process';
 
/**
 * Whether the process can show interactive prompts.
 *
 * @returns {boolean} True when stdin is an interactive terminal
 */
export function isInteractive() {
  // Explicit override wins, so callers and tests can force either mode.
  if (env.COHERENT_NON_INTERACTIVE === '1') return false;
  if (env.COHERENT_INTERACTIVE === '1') return true;
 
  // Common CI systems attach a pipe rather than a TTY.
  Eif (env.CI) return false;
 
  return Boolean(stdin.isTTY);
}
 
/**
 * Abort with guidance when a value can only come from a prompt.
 *
 * @param {string} what - The value that would have been prompted for
 * @param {string} usage - How to supply it non-interactively
 * @returns {void}
 */
export function requireInteractive(what, usage) {
  if (isInteractive()) return;
 
  console.error(picocolors.red(`❌ Cannot prompt for ${what}: no interactive terminal.`));
  console.error(picocolors.gray(`   Provide it directly: ${usage}`));
  process.exit(1);
}