From 33958618b0c5ad2ce25e4831654c93fcae12f09d Mon Sep 17 00:00:00 2001 From: Samuel Elliott Date: Sat, 13 Aug 2022 00:49:22 +0100 Subject: [PATCH] Prevent detecting a script when redirecting output --- src/cli.ts | 5 ++++- src/common/auth/util.ts | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/cli.ts b/src/cli.ts index c8443cc..59d1011 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -49,12 +49,15 @@ export function createYargs(argv: string[]) { export type Arguments = YargsArguments>; +// Node.js docs recommend using process.stdout.isTTY (see https://github.com/samuelthomas2774/nxapi/issues/15) +const is_terminal = process.stdin.isTTY && process.stderr.isTTY; + export async function main(argv = process.argv.slice(2)) { addUserAgent('nxapi-cli'); if (process.env.NXAPI_USER_AGENT) { addUserAgent(process.env.NXAPI_USER_AGENT); - } else if (!process.stdout.isTTY) { + } else if (!is_terminal) { console.warn('[warn] The nxapi command is not running in a terminal. If using the nxapi command in a script or other program, the NXAPI_USER_AGENT environment variable should be set. See ' + USER_AGENT_INFO_URL + '.'); addUserAgent('unidentified-script'); } diff --git a/src/common/auth/util.ts b/src/common/auth/util.ts index 4461997..464c05b 100644 --- a/src/common/auth/util.ts +++ b/src/common/auth/util.ts @@ -8,7 +8,8 @@ const debug = createDebug('nxapi:auth:util'); // a script/some other program, which should be limited in case it continues to run the command // if it fails. The Electron app overrides this as the parent process (probably) won't be a // terminal, but most attempts to call getToken won't be automated. -export const SHOULD_LIMIT_USE = !process.stdout.isTTY; +// Node.js docs recommend using process.stdout.isTTY (see https://github.com/samuelthomas2774/nxapi/issues/15). +export const SHOULD_LIMIT_USE = !process.stdin.isTTY || !process.stderr.isTTY; export const LIMIT_REQUESTS = 4; export const LIMIT_PERIOD = 60 * 60 * 1000; // 60 minutes