2023-05-23 07:39:00 +00:00
|
|
|
export function getStdin(timeout: number = 10): Promise<string> {
|
|
|
|
return new Promise<string>((resolve, reject) => {
|
|
|
|
// Store the data from stdin in a buffer
|
|
|
|
let buffer = ''
|
2023-05-25 08:16:44 +00:00
|
|
|
let t: NodeJS.Timeout
|
|
|
|
|
|
|
|
const dataHandler = (d: Buffer) => (buffer += d.toString())
|
|
|
|
const endHandler = () => {
|
|
|
|
clearTimeout(t)
|
|
|
|
resolve(buffer.trim())
|
|
|
|
}
|
2023-05-23 07:39:00 +00:00
|
|
|
|
|
|
|
// Stop listening for data after the timeout, otherwise hangs indefinitely
|
2023-05-25 08:16:44 +00:00
|
|
|
t = setTimeout(() => {
|
|
|
|
process.stdin.removeListener('data', dataHandler)
|
|
|
|
process.stdin.removeListener('end', endHandler)
|
|
|
|
process.stdin.pause()
|
2023-05-23 07:39:00 +00:00
|
|
|
resolve('')
|
|
|
|
}, timeout)
|
|
|
|
|
2023-05-25 08:16:44 +00:00
|
|
|
process.stdin.on('data', dataHandler)
|
|
|
|
process.stdin.on('end', endHandler)
|
2023-05-23 07:39:00 +00:00
|
|
|
})
|
|
|
|
}
|