From 03ea72b5057af38f2139eeaed8d29485e3bdb9a5 Mon Sep 17 00:00:00 2001 From: Yanis Benson Date: Thu, 22 Aug 2019 02:09:37 +0300 Subject: [PATCH] Avoid triggering stdio streams creation (#101) --- index.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 67ee1eb..ad4ca00 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,6 @@ 'use strict'; const os = require('os'); +const tty = require('tty'); const hasFlag = require('has-flag'); const {env} = process; @@ -40,7 +41,7 @@ function translateLevel(level) { }; } -function supportsColor(stream) { +function supportsColor(haveStream, streamIsTTY) { if (forceColor === 0) { return 0; } @@ -55,7 +56,7 @@ function supportsColor(stream) { return 2; } - if (stream && !stream.isTTY && forceColor === undefined) { + if (haveStream && !streamIsTTY && forceColor === undefined) { return 0; } @@ -123,12 +124,12 @@ function supportsColor(stream) { } function getSupportLevel(stream) { - const level = supportsColor(stream); + const level = supportsColor(stream, stream && stream.isTTY); return translateLevel(level); } module.exports = { supportsColor: getSupportLevel, - stdout: getSupportLevel(process.stdout), - stderr: getSupportLevel(process.stderr) + stdout: translateLevel(supportsColor(true, tty.isatty(1))), + stderr: translateLevel(supportsColor(true, tty.isatty(2))) };