fix(cli): auto-detect non-TTY environment for non-interactive mode#788
Open
theothersideofgod wants to merge 1 commit intomainfrom
Open
fix(cli): auto-detect non-TTY environment for non-interactive mode#788theothersideofgod wants to merge 1 commit intomainfrom
theothersideofgod wants to merge 1 commit intomainfrom
Conversation
## 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Running
pgpm initin non-TTY environments (CI, scripts, pipes) causesERR_USE_AFTER_CLOSEerror and hangs waiting for input that will never come.Root Cause
Two issues:
CLI entry point (
index.ts): TheCLIclass was instantiated with defaultnoTty: false, ignoring--no-ttyflag,CI=trueenv var, and!process.stdin.isTTYconditions. The flag parsing happens inside the CLI class, but by then it's too late - the prompter is already configured for TTY mode.Workspace init (
workspace.ts): Calledprompter.close()before passing the prompter toscaffoldTemplate(), causing the template prompts to fail withERR_USE_AFTER_CLOSE.Solution
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 });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-ttyflag works correctlyCI=trueenvironment is detectedERR_USE_AFTER_CLOSEerrorAll 11 tests pass.