Skip to content

Commit

Permalink
feat(color-support): Making exception on isTTY for PM2 process manager.
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinTail committed Apr 9, 2024
1 parent 9f78e35 commit 4d2e013
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/color-support.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
36 changes: 36 additions & 0 deletions test/unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4d2e013

Please sign in to comment.