UNPKG

@cloud-copilot/cli

Version:

A standardized library for CLI building TypeScript CLI applications

38 lines 1.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.readStdin = readStdin; const process_1 = require("process"); /** * Read from stdin until the stream ends, timeout, or an error occurs * * @returns the string input from stdin */ async function readStdin(readWait) { return new Promise((resolve, reject) => { // If the input is not a TTY, we are most likely receiving data from a pipe. const definitelyReceivingData = !process.stdin.isTTY; if (!readWait || readWait <= 0) { readWait = definitelyReceivingData ? 10_000 : 20; } let data = ''; const timeout = setTimeout(() => { if (data.length === 0) { process_1.stdin.pause(); process_1.stdin.removeAllListeners('data'); resolve(data); } }, readWait); process_1.stdin.on('data', (chunk) => { data += chunk; }); process_1.stdin.on('end', () => { resolve(data); clearTimeout(timeout); }); process_1.stdin.on('error', (err) => { reject(err); clearTimeout(timeout); }); }); } //# sourceMappingURL=stdin.js.map