Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: support process.env.FORCE_TTY #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
19 changes: 16 additions & 3 deletions src/node.ts
Original file line number Diff line number Diff line change
@@ -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(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)
5 changes: 5 additions & 0 deletions tests/fixtures/child-process.mjs
Original file line number Diff line number Diff line change
@@ -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 })
47 changes: 46 additions & 1 deletion tests/main.spec.ts
Original file line number Diff line number Diff line change
@@ -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'],
Expand Down Expand Up @@ -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' }
"
`)
})