mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-03-13 15:40:57 -07:00
move stdio (#13528)
This commit is contained in:
@@ -1,68 +0,0 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2025 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { patchStdio, createInkStdio } from './stdio.js';
|
||||
import { coreEvents } from '@google/gemini-cli-core';
|
||||
|
||||
vi.mock('@google/gemini-cli-core', () => ({
|
||||
coreEvents: {
|
||||
emitOutput: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
describe('stdio utils', () => {
|
||||
let originalStdoutWrite: typeof process.stdout.write;
|
||||
let originalStderrWrite: typeof process.stderr.write;
|
||||
|
||||
beforeEach(() => {
|
||||
originalStdoutWrite = process.stdout.write;
|
||||
originalStderrWrite = process.stderr.write;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
process.stdout.write = originalStdoutWrite;
|
||||
process.stderr.write = originalStderrWrite;
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('patchStdio redirects stdout and stderr to coreEvents', () => {
|
||||
const cleanup = patchStdio();
|
||||
|
||||
process.stdout.write('test stdout');
|
||||
expect(coreEvents.emitOutput).toHaveBeenCalledWith(
|
||||
false,
|
||||
'test stdout',
|
||||
undefined,
|
||||
);
|
||||
|
||||
process.stderr.write('test stderr');
|
||||
expect(coreEvents.emitOutput).toHaveBeenCalledWith(
|
||||
true,
|
||||
'test stderr',
|
||||
undefined,
|
||||
);
|
||||
|
||||
cleanup();
|
||||
|
||||
// Verify cleanup
|
||||
expect(process.stdout.write).toBe(originalStdoutWrite);
|
||||
expect(process.stderr.write).toBe(originalStderrWrite);
|
||||
});
|
||||
|
||||
it('createInkStdio writes to real stdout/stderr bypassing patch', () => {
|
||||
const cleanup = patchStdio();
|
||||
const { stdout: inkStdout, stderr: inkStderr } = createInkStdio();
|
||||
|
||||
inkStdout.write('ink stdout');
|
||||
expect(coreEvents.emitOutput).not.toHaveBeenCalled();
|
||||
|
||||
inkStderr.write('ink stderr');
|
||||
expect(coreEvents.emitOutput).not.toHaveBeenCalled();
|
||||
|
||||
cleanup();
|
||||
});
|
||||
});
|
||||
@@ -1,113 +0,0 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2025 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import { coreEvents } from '@google/gemini-cli-core';
|
||||
|
||||
// Capture the original stdout and stderr write methods before any monkey patching occurs.
|
||||
const originalStdoutWrite = process.stdout.write.bind(process.stdout);
|
||||
const originalStderrWrite = process.stderr.write.bind(process.stderr);
|
||||
|
||||
/**
|
||||
* Writes to the real stdout, bypassing any monkey patching on process.stdout.write.
|
||||
*/
|
||||
export function writeToStdout(
|
||||
...args: Parameters<typeof process.stdout.write>
|
||||
): boolean {
|
||||
return originalStdoutWrite(...args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes to the real stderr, bypassing any monkey patching on process.stderr.write.
|
||||
*/
|
||||
export function writeToStderr(
|
||||
...args: Parameters<typeof process.stderr.write>
|
||||
): boolean {
|
||||
return originalStderrWrite(...args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Monkey patches process.stdout.write and process.stderr.write to redirect output to the provided logger.
|
||||
* This prevents stray output from libraries (or the app itself) from corrupting the UI.
|
||||
* Returns a cleanup function that restores the original write methods.
|
||||
*/
|
||||
export function patchStdio(): () => void {
|
||||
const previousStdoutWrite = process.stdout.write;
|
||||
const previousStderrWrite = process.stderr.write;
|
||||
|
||||
process.stdout.write = (
|
||||
chunk: Uint8Array | string,
|
||||
encodingOrCb?:
|
||||
| BufferEncoding
|
||||
| ((err?: NodeJS.ErrnoException | null) => void),
|
||||
cb?: (err?: NodeJS.ErrnoException | null) => void,
|
||||
) => {
|
||||
const encoding =
|
||||
typeof encodingOrCb === 'string' ? encodingOrCb : undefined;
|
||||
coreEvents.emitOutput(false, chunk, encoding);
|
||||
const callback = typeof encodingOrCb === 'function' ? encodingOrCb : cb;
|
||||
if (callback) {
|
||||
callback();
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
process.stderr.write = (
|
||||
chunk: Uint8Array | string,
|
||||
encodingOrCb?:
|
||||
| BufferEncoding
|
||||
| ((err?: NodeJS.ErrnoException | null) => void),
|
||||
cb?: (err?: NodeJS.ErrnoException | null) => void,
|
||||
) => {
|
||||
const encoding =
|
||||
typeof encodingOrCb === 'string' ? encodingOrCb : undefined;
|
||||
coreEvents.emitOutput(true, chunk, encoding);
|
||||
const callback = typeof encodingOrCb === 'function' ? encodingOrCb : cb;
|
||||
if (callback) {
|
||||
callback();
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
return () => {
|
||||
process.stdout.write = previousStdoutWrite;
|
||||
process.stderr.write = previousStderrWrite;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates proxies for process.stdout and process.stderr that use the real write methods
|
||||
* (writeToStdout and writeToStderr) bypassing any monkey patching.
|
||||
* This is used by Ink to render to the real output.
|
||||
*/
|
||||
export function createInkStdio() {
|
||||
const inkStdout = new Proxy(process.stdout, {
|
||||
get(target, prop, receiver) {
|
||||
if (prop === 'write') {
|
||||
return writeToStdout;
|
||||
}
|
||||
const value = Reflect.get(target, prop, receiver);
|
||||
if (typeof value === 'function') {
|
||||
return value.bind(target);
|
||||
}
|
||||
return value;
|
||||
},
|
||||
});
|
||||
|
||||
const inkStderr = new Proxy(process.stderr, {
|
||||
get(target, prop, receiver) {
|
||||
if (prop === 'write') {
|
||||
return writeToStderr;
|
||||
}
|
||||
const value = Reflect.get(target, prop, receiver);
|
||||
if (typeof value === 'function') {
|
||||
return value.bind(target);
|
||||
}
|
||||
return value;
|
||||
},
|
||||
});
|
||||
|
||||
return { stdout: inkStdout, stderr: inkStderr };
|
||||
}
|
||||
Reference in New Issue
Block a user