From 3b7d1e7e3d8822659f82a78cf4ef49110a1e86dd Mon Sep 17 00:00:00 2001 From: Oscar Bazaldua <511911+oscb@users.noreply.github.com> Date: Mon, 31 Oct 2022 17:48:23 -0600 Subject: [PATCH] fix: reset to process.stdin/stdout when /dev/tty is not available (#270) --- src/common/ttys.ts | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/common/ttys.ts b/src/common/ttys.ts index 2a9bec9d..f71b08de 100644 --- a/src/common/ttys.ts +++ b/src/common/ttys.ts @@ -1,6 +1,6 @@ -import fs from 'fs'; -import tty from 'tty'; -import assert from 'assert'; +import fs from "fs"; +import tty from "tty"; +import assert from "assert"; // Based on https://github.com/TooTallNate/ttys/blob/master/index.js export const ttys: { @@ -11,15 +11,23 @@ export const ttys: { if (tty.isatty(0)) { ttys.stdin = process.stdin; } else { - let ttyFd = fs.openSync('/dev/tty', 'r'); - assert(tty.isatty(ttyFd)); - ttys.stdin = new tty.ReadStream(ttyFd); + try { + let ttyFd = fs.openSync("/dev/tty", "r"); + assert(tty.isatty(ttyFd)); + ttys.stdin = new tty.ReadStream(ttyFd); + } catch { + ttys.stdin = process.stdin; + } } if (tty.isatty(1)) { ttys.stdout = process.stdout; } else { - let ttyFd = fs.openSync('/dev/tty', 'w'); - assert(tty.isatty(ttyFd)); - ttys.stdout = new tty.WriteStream(ttyFd); + try { + let ttyFd = fs.openSync("/dev/tty", "w"); + assert(tty.isatty(ttyFd)); + ttys.stdout = new tty.WriteStream(ttyFd); + } catch { + ttys.stdout = process.stdout; + } }