Skip to content

Commit

Permalink
fix: improve renderer logic for --silent and FORCE_COLOR settings
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerAberbach authored Mar 17, 2022
1 parent 34fe319 commit d327873
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 12 deletions.
23 changes: 22 additions & 1 deletion lib/getRenderer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
export const getRenderer = ({ debug, quiet }, env = process.env) => {
const getMainRendererOptions = ({ debug, quiet }, env) => {
if (quiet) return { renderer: 'silent' }
// Better support for dumb terminals: https://en.wikipedia.org/wiki/Computer_terminal#Dumb_terminals
const isDumbTerminal = env.TERM === 'dumb'
if (debug || isDumbTerminal || env.NODE_ENV === 'test') return { renderer: 'verbose' }
return { renderer: 'update', rendererOptions: { dateFormat: false } }
}

const getFallbackRenderer = ({ renderer }, { FORCE_COLOR }) => {
if (renderer === 'silent') {
return 'silent'
}

// If colors are being forced, then also force non-fallback rendering
if (Number(FORCE_COLOR) > 0) {
return renderer
}

return 'verbose'
}

export const getRenderer = (options, env = process.env) => {
const mainRendererOptions = getMainRendererOptions(options, env)
return {
...mainRendererOptions,
nonTTYRenderer: getFallbackRenderer(mainRendererOptions, env),
}
}
1 change: 0 additions & 1 deletion lib/runAll.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ export const runAll = async (
const listrOptions = {
ctx,
exitOnError: false,
nonTTYRenderer: 'verbose',
registerSignalListeners: false,
...getRenderer({ debug, quiet }),
}
Expand Down
51 changes: 42 additions & 9 deletions test/getRenderer.spec.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,61 @@
import { getRenderer } from '../lib/getRenderer'

describe('getRenderer', () => {
it('should return silent renderer when quiet', () => {
expect(getRenderer({ quiet: true }, {})).toEqual({ renderer: 'silent' })
it('should return silent renderers when quiet', () => {
expect(getRenderer({ quiet: true }, {})).toEqual({
renderer: 'silent',
nonTTYRenderer: 'silent',
})
})

it('should return verbose renderer when NODE_ENV=test', () => {
expect(getRenderer({}, { NODE_ENV: 'test' })).toEqual({ renderer: 'verbose' })
it('should return verbose renderers when NODE_ENV=test', () => {
expect(getRenderer({}, { NODE_ENV: 'test' })).toEqual({
renderer: 'verbose',
nonTTYRenderer: 'verbose',
})
})

it('should return test renderer when TERM=dumb', () => {
expect(getRenderer({}, { TERM: 'dumb' })).toEqual({ renderer: 'verbose' })
it('should return test renderers when TERM=dumb', () => {
expect(getRenderer({}, { TERM: 'dumb' })).toEqual({
renderer: 'verbose',
nonTTYRenderer: 'verbose',
})
})

it('should return verbose renderer when debug', () => {
expect(getRenderer({ debug: true }, {})).toEqual({ renderer: 'verbose' })
it('should return verbose renderers when debug', () => {
expect(getRenderer({ debug: true }, {})).toEqual({
renderer: 'verbose',
nonTTYRenderer: 'verbose',
})
})

it('should return update renderer by default', () => {
it('should return update main renderer and verbose fallback renderer by default', () => {
expect(getRenderer({}, {})).toEqual({
renderer: 'update',
rendererOptions: {
dateFormat: false,
},
nonTTYRenderer: 'verbose',
})
})

it('should return update main renderer and verbose fallback renderer when colors are not forced', () => {
expect(getRenderer({}, { FORCE_COLOR: '0' })).toEqual({
renderer: 'update',
rendererOptions: {
dateFormat: false,
},
nonTTYRenderer: 'verbose',
})
})

it('should return update renderers when colors are forced', () => {
expect(getRenderer({}, { FORCE_COLOR: '1' })).toEqual({
renderer: 'update',
rendererOptions: {
dateFormat: false,
},
nonTTYRenderer: 'update',
})
})
})
2 changes: 1 addition & 1 deletion test/index2.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('lintStaged', () => {
"shouldBackup": true,
},
"exitOnError": false,
"nonTTYRenderer": "verbose",
"nonTTYRenderer": "silent",
"registerSignalListeners": false,
"renderer": "silent",
}
Expand Down

0 comments on commit d327873

Please sign in to comment.