Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stop assuming CLI stdout is a TTY write stream (#2154)
## Motivation for the change, related issues Related to #2127 In WordPress/wordpress-playground, we still assume that the Playground CLI is running in a TTY context, and the CLI breaks when not in a TTY context [because `process.stdout.clearLine()` and `process.stdout.clearLine()` are missing](#2127). Here are some lines that fail without a TTY context: https://github.com/WordPress/wordpress-playground/blob/f5b85399ff8dbabaf122bfa98347ce76d7f59910/packages/playground/cli/src/cli.ts#L232-L233 ## Implementation details This PR does a number of things: - Updates the progress messages to be written in-place over one another when in TTY contexts - Updates the progress messages to be written on separate lines when in non-TTY contexts - Makes sure that identical progress messages are not repeated across multiple lines in non-TTY mode - Ensures that messages following in-place progress updates are written on the next line (sometimes two messages were appearing on the same line like "Downloading WordPress 100%... Booted!") - Makes sure the progress percentage is an integer and doesn't report 100% until truly at 100%. - Explicitly declares that the nx `run-commands` executor for `playground-cli:dev` should run in TTY mode. - It doesn't seem to affect our current version of nx, but when testing with a WIP branch to upgrade nx and vite, `npx nx dev playground-cli` did seem to run in a TTY context. - Either way [the nx tty option](https://nx.dev/nx-api/nx/executors/run-commands#tty), doesn't seem to adversely affect anything, so I'm leaving it explicitly declared that way for later. ## Testing Instructions (or ideally a Blueprint) - `cd` into your Playground working dir - Create a blueprint named `test-blueprint.json` in that directory based on [this gist](https://gist.github.com/brandonpayton/70e7b73e6dd8b87053db691db70b42dc#file-blueprint-json). - Test in TTY mode - Pick a WP version Playground hasn't downloaded or simply run `rm -r ~/.wordpress-playground` to clear the Playground CLI cache. - Run `npx bun --watch ./packages/playground/cli/src/cli.ts server --blueprint=test-blueprint.json` - Observe that the Download and Blueprint progress messages are updated on a single line each as they progress to 100% - Observe that each message appears on its own line - Test in non-TTY mode - Pick a WP version Playground hasn't downloaded or simply run `rm -r ~/.wordpress-playground` to clear the Playground CLI cache. - Run `npx bun --watch ./packages/playground/cli/src/cli.ts server --blueprint=test-blueprint.json | more` - Piping output to `more` causes the CLI's stdout to not be a TTY - Hit the spacebar to proceed through the output (this may be obvious, but sometimes it took me a second or two to remember :) - Observe that each Download and Blueprint progress message is printed on a new line as they progress to 100%. - Observe that no progress messages are repeated.
- Loading branch information