Files
gemini-cli/packages/cli/src/utils/readStdin.ts
T

40 lines
872 B
TypeScript
Raw Normal View History

2025-04-21 17:41:44 -07:00
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
export async function readStdin(): Promise<string> {
return new Promise((resolve, reject) => {
let data = '';
process.stdin.setEncoding('utf8');
2025-06-05 06:40:33 -07:00
const onReadable = () => {
2025-04-21 17:41:44 -07:00
let chunk;
while ((chunk = process.stdin.read()) !== null) {
data += chunk;
}
2025-06-05 06:40:33 -07:00
};
2025-04-21 17:41:44 -07:00
2025-06-05 06:40:33 -07:00
const onEnd = () => {
cleanup();
2025-04-21 17:41:44 -07:00
resolve(data);
2025-06-05 06:40:33 -07:00
};
2025-04-21 17:41:44 -07:00
2025-06-05 06:40:33 -07:00
const onError = (err: Error) => {
cleanup();
2025-04-21 17:41:44 -07:00
reject(err);
2025-06-05 06:40:33 -07:00
};
const cleanup = () => {
process.stdin.removeListener('readable', onReadable);
process.stdin.removeListener('end', onEnd);
process.stdin.removeListener('error', onError);
};
process.stdin.on('readable', onReadable);
process.stdin.on('end', onEnd);
process.stdin.on('error', onError);
2025-04-21 17:41:44 -07:00
});
}