Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop assuming CLI stdout is a TTY write stream #2154

Merged
merged 3 commits into from
Feb 22, 2025

Conversation

brandonpayton
Copy link
Member

@brandonpayton brandonpayton commented Feb 22, 2025

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.

Here are some lines that fail without a TTY context:

process.stdout.clearLine(0);
process.stdout.cursorTo(0);

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, 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.
  • 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.

@brandonpayton brandonpayton added [Type] Bug An existing feature does not function as intended [Package][@wp-playground] CLI labels Feb 22, 2025
@brandonpayton brandonpayton requested a review from a team February 22, 2025 18:59
@brandonpayton brandonpayton self-assigned this Feb 22, 2025
@brandonpayton
Copy link
Member Author

This update is testing well.

Output with TTY
% rm -r ~/.wordpress-playground; npx bun --watch ./packages/playground/cli/src/cli.ts server --blueprint=test-blueprint.json                
Starting a PHP server...
Setting up WordPress latest
Resolved WordPress release URL: https://downloads.w.org/release/wordpress-6.7.2.zip
Downloading WordPress 100%...
Booting WordPress...
PHP.request() is deprecated. Please use new PHPRequestHandler() instead.
Booted!
Caching preinstalled WordPress for the next boot...
Cached!
Running the Blueprint...
Logging in – 100%
Finished running the blueprint
WordPress is running on http://127.0.0.1:9400
Output without TTY
% rm -r ~/.wordpress-playground; npx bun --watch ./packages/playground/cli/src/cli.ts server --blueprint=test-blueprint.json                
Starting a PHP server...
Setting up WordPress latest
Resolved WordPress release URL: https://downloads.w.org/release/wordpress-6.7.2.zip
Downloading WordPress 0%...
Downloading WordPress 1%...
Downloading WordPress 2%...
Downloading WordPress 3%...
Downloading WordPress 4%...
Downloading WordPress 5%...
Downloading WordPress 6%...
Downloading WordPress 7%...
Downloading WordPress 8%...
Downloading WordPress 9%...
Downloading WordPress 10%...
Downloading WordPress 11%...
Downloading WordPress 12%...
Downloading WordPress 13%...
Downloading WordPress 14%...
Downloading WordPress 15%...
Downloading WordPress 16%...
Downloading WordPress 17%...
Downloading WordPress 18%...
Downloading WordPress 19%...
Downloading WordPress 20%...
Downloading WordPress 21%...
Downloading WordPress 22%...
Downloading WordPress 23%...
Downloading WordPress 25%...
Downloading WordPress 26%...
Downloading WordPress 27%...
Downloading WordPress 29%...
Downloading WordPress 30%...
Downloading WordPress 31%...
Downloading WordPress 33%...
Downloading WordPress 35%...
Downloading WordPress 36%...
Downloading WordPress 38%...
Downloading WordPress 39%...
Downloading WordPress 41%...
Downloading WordPress 42%...
Downloading WordPress 43%...
Downloading WordPress 44%...
Downloading WordPress 45%...
Downloading WordPress 46%...
Downloading WordPress 47%...
Downloading WordPress 48%...
Downloading WordPress 49%...
Downloading WordPress 50%...
Downloading WordPress 51%...
Downloading WordPress 52%...
Downloading WordPress 53%...
Downloading WordPress 54%...
Downloading WordPress 55%...
Downloading WordPress 56%...
Downloading WordPress 57%...
Downloading WordPress 58%...
Downloading WordPress 59%...
Downloading WordPress 60%...
Downloading WordPress 62%...
Downloading WordPress 63%...
Downloading WordPress 64%...
Downloading WordPress 65%...
Downloading WordPress 66%...
Downloading WordPress 67%...
Downloading WordPress 68%...
Downloading WordPress 69%...
Downloading WordPress 70%...
Downloading WordPress 71%...
Downloading WordPress 72%...
Downloading WordPress 73%...
Downloading WordPress 74%...
Downloading WordPress 75%...
Downloading WordPress 76%...
Downloading WordPress 77%...
Downloading WordPress 78%...
Downloading WordPress 79%...
Downloading WordPress 80%...
Downloading WordPress 81%...
Downloading WordPress 82%...
Downloading WordPress 83%...
Downloading WordPress 84%...
Downloading WordPress 85%...
Downloading WordPress 86%...
Downloading WordPress 87%...
Downloading WordPress 88%...
Downloading WordPress 89%...
Downloading WordPress 90%...
Downloading WordPress 91%...
Downloading WordPress 92%...
Downloading WordPress 93%...
Downloading WordPress 94%...
Downloading WordPress 95%...
Downloading WordPress 96%...
Downloading WordPress 97%...
Downloading WordPress 98%...
Downloading WordPress 99%...
Downloading WordPress 100%...
Booting WordPress...
Booted!
Caching preinstalled WordPress for the next boot...
Cached!
Running the Blueprint...
Downloading Kadence – 0%
Downloading Kadence – 1%
Downloading Kadence – 2%
Downloading Kadence – 3%
Downloading Kadence – 4%
Downloading Kadence – 5%
Downloading Kadence – 6%
Downloading Kadence – 7%
Downloading Kadence – 8%
Downloading Kadence – 9%
Downloading Kadence – 10%
Downloading Kadence – 11%
Downloading Kadence – 12%
Downloading Kadence – 13%
Downloading Kadence – 14%
Downloading Kadence – 15%
Downloading Kadence – 16%
Downloading Kadence – 17%
Downloading Kadence – 18%
Downloading Kadence – 19%
Downloading Kadence – 20%
Downloading Kadence – 21%
Downloading Kadence – 22%
Downloading Kadence – 23%
Downloading Kadence – 24%
Downloading Kadence – 25%
Downloading Kadence – 26%
Downloading Kadence – 27%
Downloading Kadence – 28%
Downloading Kadence – 29%
Downloading Kadence – 30%
Downloading Kadence – 31%
Downloading Kadence – 32%
Downloading Kadence – 33%
Downloading Kadence – 34%
Downloading Kadence – 35%
Downloading Kadence – 36%
Downloading Kadence – 39%
Downloading Kadence – 44%
Downloading Kadence – 45%
Downloading Kadence – 47%
Downloading Kadence – 48%
Downloading Kadence – 49%
Installing the Kadence theme – 49%
Activating kadence – 49%
Importing theme starter content – 49%
Importing theme starter content – 50%
Logging in – 50%
Logging in – 100%
Finished running the blueprint
WordPress is running on http://127.0.0.1:9400

@brandonpayton brandonpayton merged commit 18bd27a into trunk Feb 22, 2025
10 checks passed
@brandonpayton brandonpayton deleted the stop-assuming-tty-in-cli branch February 22, 2025 21:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package][@wp-playground] CLI [Type] Bug An existing feature does not function as intended
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

1 participant