From 08bd5fa7134bbd098eace407088d0a9bffbc2a59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Perkki=C3=B6?= Date: Tue, 17 Dec 2024 10:48:44 +0200 Subject: [PATCH 1/5] feat: support `process.env.FORCE_TTY` --- src/node.ts | 2 +- tests/fixtures/child-process.mjs | 5 ++++ tests/main.spec.ts | 47 +++++++++++++++++++++++++++++++- 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 tests/fixtures/child-process.mjs diff --git a/src/node.ts b/src/node.ts index 0c3b41d..9573a46 100644 --- a/src/node.ts +++ b/src/node.ts @@ -3,4 +3,4 @@ import { createColors } from './index' export * from './index' -export default createColors(isatty(1)) +export default createColors(process.env.FORCE_TTY !== undefined || isatty(1)) diff --git a/tests/fixtures/child-process.mjs b/tests/fixtures/child-process.mjs new file mode 100644 index 0000000..165cd55 --- /dev/null +++ b/tests/fixtures/child-process.mjs @@ -0,0 +1,5 @@ +import c from '../../dist/node.js' + +console.log(c.green('Green')) +console.log(c.red('Red')) +console.log({ FORCE_TTY: process.env.FORCE_TTY }) diff --git a/tests/main.spec.ts b/tests/main.spec.ts index cc5a359..c9223e3 100644 --- a/tests/main.spec.ts +++ b/tests/main.spec.ts @@ -1,5 +1,8 @@ +import { fork } from 'node:child_process' import { createColors } from '../src/node' -import { assert, test } from 'vitest' +import { assert, expect, test } from 'vitest' +import { fileURLToPath } from 'node:url' +import { resolve } from 'node:path' const FMT = { reset: ['\x1b[0m', '\x1b[0m'], @@ -155,4 +158,46 @@ test('no maximum call stack error', () => { delete process.env.GITHUB_ACTIONS assert.isTrue(pc.isColorSupported) assert.exists(pc.blue(pc.blue('x').repeat(10000))) + delete process.env.FORCE_COLOR +}) + +test('non-TTY does not enable colors', async () => { + const proc = fork(resolve(__dirname, 'fixtures/child-process.mjs'), { + stdio: 'pipe', + }) + + let data = '' + proc.stdout!.on('data', (msg) => { + data += msg + }) + + await new Promise((r) => proc.on('exit', r)) + + expect(data).toMatchInlineSnapshot(` + "Green + Red + { FORCE_TTY: undefined } + " + `) +}) + +test('FORCE_TTY enables colors', async () => { + const proc = fork(resolve(__dirname, 'fixtures/child-process.mjs'), { + stdio: 'pipe', + env: { FORCE_TTY: 'true' }, + }) + + let data = '' + proc.stdout!.on('data', (msg) => { + data += msg + }) + + await new Promise((r) => proc.on('exit', r)) + + expect(data).toMatchInlineSnapshot(` + "Green + Red + { FORCE_TTY: 'true' } + " + `) }) From aae3a14fd9c26a6083aaa82d6add055c690fe0a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Perkki=C3=B6?= Date: Tue, 17 Dec 2024 10:59:32 +0200 Subject: [PATCH 2/5] ci: build before test --- .github/workflows/test.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 48a1c6a..aeeb24d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -19,6 +19,8 @@ jobs: node-version: ${{ matrix.node-version }} - name: Install dependencies run: yarn install --frozen-lockfile --ignore-engines --ignore-scripts + - name: build + run: yarn build - name: Run unit tests run: yarn test env: From 42017d50f1bc53dfd89d8e8339f9e60289bf0cb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Perkki=C3=B6?= Date: Thu, 19 Dec 2024 11:54:52 +0200 Subject: [PATCH 3/5] feat!: node endpoint to check `isatty` and `FORCE_TTY` automatically --- src/node.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/node.ts b/src/node.ts index 9573a46..dfe2c9f 100644 --- a/src/node.ts +++ b/src/node.ts @@ -1,6 +1,19 @@ import { isatty } from 'node:tty' -import { createColors } from './index' +import { + createColors as originalCreateColors, + isSupported as originalIsSupported, +} from './index' -export * from './index' +export { Colors, Formatter, getDefaultColors } from './index' -export default createColors(process.env.FORCE_TTY !== undefined || isatty(1)) +const isTTY = process.env.FORCE_TTY !== undefined || isatty(1) + +export function isSupportted() { + return originalIsSupported(isTTY) +} + +export function createColors() { + return originalCreateColors(isTTY) +} + +export default originalCreateColors(isTTY) From a63a6495d9ba7200e3c30562fd049e721fdfccf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Perkki=C3=B6?= Date: Mon, 13 Jan 2025 18:56:27 +0200 Subject: [PATCH 4/5] fix: update browser entrypoint --- src/browser.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/browser.ts b/src/browser.ts index 0db8e10..f13c7c7 100644 --- a/src/browser.ts +++ b/src/browser.ts @@ -1,5 +1,16 @@ -import { createColors } from './index' +import { + createColors as originalCreateColors, + isSupported as originalIsSupported, +} from './index' -export * from './index' +export { Colors, Formatter, getDefaultColors } from './index' -export default createColors(false) +export function isSupportted() { + return originalIsSupported() +} + +export function createColors() { + return originalCreateColors() +} + +export default originalCreateColors() From 9b27556b755ccf34edc930c268447b05e3525148 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Perkki=C3=B6?= Date: Mon, 13 Jan 2025 18:57:29 +0200 Subject: [PATCH 5/5] fix: typo --- src/browser.ts | 2 +- src/node.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/browser.ts b/src/browser.ts index f13c7c7..770a00a 100644 --- a/src/browser.ts +++ b/src/browser.ts @@ -5,7 +5,7 @@ import { export { Colors, Formatter, getDefaultColors } from './index' -export function isSupportted() { +export function isSupported() { return originalIsSupported() } diff --git a/src/node.ts b/src/node.ts index dfe2c9f..c9c62a4 100644 --- a/src/node.ts +++ b/src/node.ts @@ -8,7 +8,7 @@ export { Colors, Formatter, getDefaultColors } from './index' const isTTY = process.env.FORCE_TTY !== undefined || isatty(1) -export function isSupportted() { +export function isSupported() { return originalIsSupported(isTTY) }