Skip to content

Commit 0d17a12

Browse files
authored
[CLI] Support TERM=dumb (#2929)
## Motivation for the change, related issues Detects a "dumb terminal" and CI environments in the CLI worker to avoid using rich output features such as line rewriting. Why? Because CI logs for the playground-cli tests are polluted with progress updates: ``` Downloading WordPress 8%... Downloading WordPress 9%... Downloading WordPress 10%... Downloading WordPress 11%... Downloading WordPress 12%... Downloading WordPress 13%... Downloading WordPress 14%... Downloading WordPress 15%... Resolving data reference #5: https://downloads.w.org/release/wordpress-6.3.7.zip – 23.76% Resolving data reference #5: https://downloads.w.org/release/wordpress-6.3.7.zip – 23.76% Resolving data reference #5: https://downloads.w.org/release/wordpress-6.3.7.zip – 23.76% Resolving data reference #5: https://downloads.w.org/release/wordpress-6.3.7.zip – 23.77% Resolving data reference #5: https://downloads.w.org/release/wordpress-6.3.7.zip – 23.77% Resolving data reference #5: https://downloads.w.org/release/wordpress-6.3.7.zip – 23.77% ``` ## Testing Instructions (or ideally a Blueprint) Inspect the CLI CI tests output and confirm it's clean.
1 parent 8775ba7 commit 0d17a12

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

packages/playground/cli/src/blueprints-v1/blueprints-v1-handler.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
type SpawnedWorker,
2626
type WorkerType,
2727
} from '../run-cli';
28+
import { shouldRenderProgress } from '../utils/progress';
2829

2930
/**
3031
* Boots Playground CLI workers using Blueprint version 1.
@@ -102,7 +103,7 @@ export class BlueprintsV1Handler {
102103
wpDetails.releaseUrl,
103104
`${wpDetails.version}.zip`,
104105
monitor
105-
);
106+
);
106107
logger.log(
107108
`Resolved WordPress release URL: ${wpDetails?.releaseUrl}`
108109
);
@@ -268,7 +269,7 @@ export class BlueprintsV1Handler {
268269
'latest',
269270
...(resolvedBlueprint?.preferredVersions || {}),
270271
},
271-
};
272+
};
272273
}
273274

274275
writeProgressUpdate(
@@ -279,6 +280,9 @@ export class BlueprintsV1Handler {
279280
if (this.args.verbosity === LogVerbosity.Quiet.name) {
280281
return;
281282
}
283+
if (!shouldRenderProgress(writeStream)) {
284+
return;
285+
}
282286
if (message === this.lastProgressMessage) {
283287
// Avoid repeating the same message
284288
return;

packages/playground/cli/src/blueprints-v2/blueprints-v2-handler.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
} from './worker-thread-v2';
77
import type { MessagePort as NodeMessagePort } from 'worker_threads';
88
import type { RunCLIArgs, SpawnedWorker, WorkerType } from '../run-cli';
9+
import { shouldRenderProgress } from '../utils/progress';
910

1011
/**
1112
* Boots Playground CLI workers using Blueprint version 2.
@@ -108,6 +109,9 @@ export class BlueprintsV2Handler {
108109
message: string,
109110
finalUpdate: boolean
110111
) {
112+
if (!shouldRenderProgress(writeStream)) {
113+
return;
114+
}
111115
if (message === this.lastProgressMessage) {
112116
// Avoid repeating the same message
113117
return;

packages/playground/cli/src/blueprints-v2/worker-thread-v2.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import type {
3939
PhpIniOptions,
4040
PHPInstanceCreatedHook,
4141
} from '@wp-playground/wordpress';
42+
import { shouldRenderProgress } from '../utils/progress';
4243

4344
async function mountResources(php: PHP, mounts: Mount[]) {
4445
for (const mount of mounts) {
@@ -74,7 +75,8 @@ function tracePhpWasm(processId: number, format: string, ...args: any[]) {
7475
}
7576

7677
/**
77-
* Force TTY status to preserve ANSI control codes in the output.
78+
* Force TTY status to preserve ANSI control codes in the output
79+
* when the environment is interactive.
7880
*
7981
* This script is spawned as `new Worker()` and process.stdout and process.stderr are
8082
* WritableWorkerStdio objects. By default, they strip ANSI control codes from the output
@@ -90,6 +92,9 @@ Object.defineProperty(process.stderr, 'isTTY', { value: true });
9092
const output = {
9193
lastWriteWasProgress: false,
9294
progress(data: string) {
95+
if (!shouldRenderProgress(process.stdout)) {
96+
return;
97+
}
9398
if (!process.stdout.isTTY) {
9499
// eslint-disable-next-line no-console
95100
console.log(data);
@@ -382,9 +387,8 @@ export class PlaygroundCliBlueprintV2Worker extends PHPWorker {
382387
if ((await streamedResponse!.exitCode) !== 0) {
383388
// exitCode != 1 means the blueprint execution failed. Let's throw an error.
384389
// and clean up.
385-
const syncResponse = await PHPResponse.fromStreamedResponse(
386-
streamedResponse
387-
);
390+
const syncResponse =
391+
await PHPResponse.fromStreamedResponse(streamedResponse);
388392
throw new PHPExecutionFailureError(
389393
`PHP.run() failed with exit code ${syncResponse.exitCode}.`,
390394
syncResponse,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export function shouldRenderProgress(
2+
writeStream?: { isTTY?: boolean } | null
3+
): boolean {
4+
const termIsDumb = (process.env['TERM'] || '').toLowerCase() === 'dumb';
5+
const ciFlag = (process.env['CI'] || '').toLowerCase();
6+
const runningInCI = ciFlag === '1' || ciFlag === 'true';
7+
8+
if (termIsDumb || runningInCI) {
9+
return false;
10+
}
11+
12+
if (writeStream) {
13+
return Boolean(writeStream.isTTY);
14+
}
15+
16+
return true;
17+
}

0 commit comments

Comments
 (0)