diff options
Diffstat (limited to 'js')
-rw-r--r-- | js/deno.ts | 2 | ||||
-rw-r--r-- | js/os.ts | 17 | ||||
-rw-r--r-- | js/os_test.ts | 5 |
3 files changed, 23 insertions, 1 deletions
diff --git a/js/deno.ts b/js/deno.ts index 1cc0a33d1..42bd38013 100644 --- a/js/deno.ts +++ b/js/deno.ts @@ -1,7 +1,7 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. // Public deno module. -export { pid, env, exit } from "./os"; +export { pid, env, exit, isTTY } from "./os"; export { chdir, cwd } from "./dir"; export { File, @@ -21,6 +21,23 @@ interface CodeInfo { sourceCode: string | undefined; } +/** Check if running in terminal. + * + * import { isTTY } from "deno"; + * console.log(isTTY().stdout); + */ +export function isTTY(): { stdin: boolean; stdout: boolean; stderr: boolean } { + const builder = flatbuffers.createBuilder(); + msg.IsTTY.startIsTTY(builder); + const inner = msg.IsTTY.endIsTTY(builder); + const baseRes = sendSync(builder, msg.Any.IsTTY, inner)!; + assert(msg.Any.IsTTYRes === baseRes.innerType()); + const res = new msg.IsTTYRes(); + assert(baseRes.inner(res) != null); + + return { stdin: res.stdin(), stdout: res.stdout(), stderr: res.stderr() }; +} + /** Exit the Deno process with optional exit code. */ export function exit(exitCode = 0): never { const builder = flatbuffers.createBuilder(); diff --git a/js/os_test.ts b/js/os_test.ts index 21ec5e69d..0784fd5e4 100644 --- a/js/os_test.ts +++ b/js/os_test.ts @@ -27,3 +27,8 @@ test(function osPid() { console.log("pid", deno.pid); assert(deno.pid > 0); }); + +// See complete tests in tools/is_tty_test.py +test(function osIsTTYSmoke() { + console.log(deno.isTTY()); +}); |