bun.shell: Add .quiet(boolean)

This commit is contained in:
Michael H
2025-10-15 11:43:38 +11:00
committed by GitHub
parent b17133a9e9
commit 37ad295114
5 changed files with 37 additions and 13 deletions

View File

@@ -112,8 +112,9 @@ declare module "bun" {
* By default, the shell will write to the current process's stdout and stderr, as well as buffering that output.
*
* This configures the shell to only buffer the output.
* @param isQuiet - Whether to suppress output. Defaults to true.
*/
quiet(): this;
quiet(isQuiet?: boolean): this;
/**
* Read from stdout as a string, line by line

View File

@@ -21,7 +21,7 @@ export default [
},
setQuiet: {
fn: "setQuiet",
length: 0,
length: 1,
},
},
}),

View File

@@ -175,14 +175,14 @@ export function createBunShellTemplateFunction(createShellInterpreter_, createPa
}
}
#quiet(): this {
#quiet(isQuiet: boolean = true): this {
this.#throwIfRunning();
this.#args!.setQuiet();
this.#args!.setQuiet(isQuiet);
return this;
}
quiet(): this {
return this.#quiet();
quiet(isQuiet: boolean | undefined): this {
return this.#quiet(isQuiet ?? true);
}
nothrow(): this {
@@ -196,17 +196,17 @@ export function createBunShellTemplateFunction(createShellInterpreter_, createPa
}
async text(encoding) {
const { stdout } = (await this.#quiet()) as ShellOutput;
const { stdout } = (await this.#quiet(true)) as ShellOutput;
return stdout.toString(encoding);
}
async json() {
const { stdout } = (await this.#quiet()) as ShellOutput;
const { stdout } = (await this.#quiet(true)) as ShellOutput;
return JSON.parse(stdout.toString());
}
async *lines() {
const { stdout } = (await this.#quiet()) as ShellOutput;
const { stdout } = (await this.#quiet(true)) as ShellOutput;
if (process.platform === "win32") {
yield* stdout.toString().split(/\r?\n/);
@@ -216,7 +216,7 @@ export function createBunShellTemplateFunction(createShellInterpreter_, createPa
}
async arrayBuffer() {
const { stdout } = (await this.#quiet()) as ShellOutput;
const { stdout } = (await this.#quiet(true)) as ShellOutput;
return stdout.buffer;
}
@@ -225,7 +225,7 @@ export function createBunShellTemplateFunction(createShellInterpreter_, createPa
}
async blob() {
const { stdout } = (await this.#quiet()) as ShellOutput;
const { stdout } = (await this.#quiet(true)) as ShellOutput;
return new Blob([stdout]);
}

View File

@@ -56,8 +56,9 @@ pub fn setCwd(this: *ParsedShellScript, globalThis: *JSGlobalObject, callframe:
return .js_undefined;
}
pub fn setQuiet(this: *ParsedShellScript, _: *JSGlobalObject, _: *jsc.CallFrame) bun.JSError!jsc.JSValue {
this.quiet = true;
pub fn setQuiet(this: *ParsedShellScript, _: *JSGlobalObject, callframe: *jsc.CallFrame) bun.JSError!jsc.JSValue {
const arg = callframe.argument(0);
this.quiet = arg.toBoolean();
return .js_undefined;
}

View File

@@ -195,6 +195,28 @@ describe("bunshell", () => {
test("cmd subst", async () => {
await TestBuilder.command`echo $(echo hi)`.quiet().stdout("hi\n").run();
});
test.each([
{ value: undefined, expectedQuiet: true, description: "quiet()" },
{ value: true, expectedQuiet: true, description: "quiet(true)" },
{ value: false, expectedQuiet: false, description: "quiet(false)" },
])("$description suppresses output: $expectedQuiet", async ({ value, expectedQuiet }) => {
// Test with spawned process to check actual stdout
const quietArg = value === undefined ? "" : value.toString();
const { stdout, stderr } = Bun.spawnSync(
[BUN, "-e", `await Bun.$\`echo "test output"\`.quiet(${quietArg === undefined ? "" : quietArg})`],
{
env: { BUN_DEBUG_QUIET_LOGS: "1" },
},
);
if (expectedQuiet) {
expect(stdout.toString()).toBe("");
} else {
expect(stdout.toString()).toBe("test output\n");
}
expect(stderr.toString()).toBe("");
});
});
test("failing stmt edgecase", async () => {