Skip to content

Commit

Permalink
fix: reset to process.stdin/stdout when /dev/tty is not available (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
oscb authored Oct 31, 2022
1 parent 7947b61 commit 3b7d1e7
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/common/ttys.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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;
}
}

0 comments on commit 3b7d1e7

Please sign in to comment.