Skip to content

fix(cli): auto-detect non-TTY environment for non-interactive mode#788

Open
theothersideofgod wants to merge 1 commit intomainfrom
fix/non-tty-environment-detection
Open

fix(cli): auto-detect non-TTY environment for non-interactive mode#788
theothersideofgod wants to merge 1 commit intomainfrom
fix/non-tty-environment-detection

Conversation

@theothersideofgod
Copy link
Contributor

Problem

Running pgpm init in non-TTY environments (CI, scripts, pipes) causes ERR_USE_AFTER_CLOSE error and hangs waiting for input that will never come.

Root Cause

Two issues:

  1. CLI entry point (index.ts): The CLI class was instantiated with default noTty: false, ignoring --no-tty flag, CI=true env var, and !process.stdin.isTTY conditions. The flag parsing happens inside the CLI class, but by then it's too late - the prompter is already configured for TTY mode.

  2. Workspace init (workspace.ts): Called prompter.close() before passing the prompter to scaffoldTemplate(), causing the template prompts to fail with ERR_USE_AFTER_CLOSE.

Solution

  1. index.ts: Detect non-TTY environment before creating CLI instance: typescript const noTty = process.argv.includes('--no-tty') || process.env.CI === 'true' || !process.stdin.isTTY; const app = new CLI(commands, { ...options, noTty });

  2. workspace.ts: Remove premature prompter.close() call. Let the CLI framework close the prompter after the command completes.

Verification

Added comprehensive tests in __tests__/non-tty-detection.test.ts:

  • --no-tty flag works correctly
  • CI=true environment is detected
  • Non-TTY stdin is detected
  • Missing args fail gracefully (no hang)
  • Module creation in workspace works
  • No ERR_USE_AFTER_CLOSE error

All 11 tests pass.

## Problem

Running `pgpm init` in non-TTY environments (CI, scripts, pipes) causes
`ERR_USE_AFTER_CLOSE` error and hangs waiting for input that will never come.

## Root Cause

Two issues:

1. **CLI entry point (`index.ts`)**: The `CLI` class was instantiated with
   default `noTty: false`, ignoring `--no-tty` flag, `CI=true` env var,
   and `!process.stdin.isTTY` conditions. The flag parsing happens inside
   the CLI class, but by then it's too late - the prompter is already
   configured for TTY mode.

2. **Workspace init (`workspace.ts`)**: Called `prompter.close()` before
   passing the prompter to `scaffoldTemplate()`, causing the template
   prompts to fail with `ERR_USE_AFTER_CLOSE`.

## Solution

1. **index.ts**: Detect non-TTY environment before creating CLI instance:
   ```typescript
   const noTty = process.argv.includes('--no-tty') ||
                 process.env.CI === 'true' ||
                 !process.stdin.isTTY;
   const app = new CLI(commands, { ...options, noTty });
   ```

2. **workspace.ts**: Remove premature `prompter.close()` call. Let the
   CLI framework close the prompter after the command completes.

## Verification

Added comprehensive tests in `__tests__/non-tty-detection.test.ts`:
- `--no-tty` flag works correctly
- `CI=true` environment is detected
- Non-TTY stdin is detected
- Missing args fail gracefully (no hang)
- Module creation in workspace works
- No `ERR_USE_AFTER_CLOSE` error

All 11 tests pass.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant