From 4d2e0138a67df153904b05b2e70259bcdc4432ca Mon Sep 17 00:00:00 2001 From: Robin Tail Date: Tue, 9 Apr 2024 19:34:46 +0200 Subject: [PATCH] feat(color-support): Making exception on isTTY for PM2 process manager. --- CHANGELOG.md | 4 ++++ src/color-support.js | 4 +++- test/unit.test.js | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b55953..04ca9c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Change log +## 3.1.0 (2024-04-10) + +- feat: add detection of color support when using PM2 process manager + ## 3.0.3 (2024-04-09) - chore: add rollup-plugin-cleanup to remove comments from d.ts file for dist, that save yet 3 KB diff --git a/src/color-support.js b/src/color-support.js index c9878fd..2535d3b 100644 --- a/src/color-support.js +++ b/src/color-support.js @@ -121,7 +121,9 @@ export const getColorSpace = (mockThis) => { // when Next.JS runtime is `edge`, process.stdout is undefined, but colors output is supported // runtime values supported colors: `nodejs`, `edge`, `experimental-edge` const isNextJS = (env.NEXT_RUNTIME || '').indexOf('edge') > -1; - const isTTY = isNextJS || (isDeno ? Deno.isatty(1) : stdout && 'isTTY' in stdout); + // PM2 does not set process.stdout.isTTY, but colors may be supported (depends on actual terminal) + const isPM2 = 'PM2_HOME' in env && 'pm_id' in env; + const isTTY = isNextJS || isPM2 || (isDeno ? Deno.isatty(1) : stdout && 'isTTY' in stdout); if (isForceDisabled) return SPACE_MONO; diff --git a/test/unit.test.js b/test/unit.test.js index 5131f3e..5d7b46b 100644 --- a/test/unit.test.js +++ b/test/unit.test.js @@ -646,6 +646,42 @@ describe('Node.JS different env', () => { const expected = SPACE_TRUE_COLORS; expect(received).toEqual(expected); }); + + test(`PM2: no isTTY but COLORTERM: 'truecolor'`, () => { + const received = colorSpace({ + process: { + env: { + PM2_HOME: "/var/www/", + pm_id: "1", + COLORTERM: 'truecolor' + }, + argv: [], + stdout: {}, + stderr: {}, + }, + + }); + const expected = SPACE_TRUE_COLORS; + expect(received).toEqual(expected); + }); + + test(`PM2: no isTTY and unsupported terminal`, () => { + const received = colorSpace({ + process: { + env: { + PM2_HOME: "/var/www/", + pm_id: "1", + TERM: 'dumb' + }, + argv: [], + stdout: {}, + stderr: {}, + }, + + }); + const expected = SPACE_MONO; + expect(received).toEqual(expected); + }); }); // Deno