-
-
Notifications
You must be signed in to change notification settings - Fork 424
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve renderer logic for
--silent
and FORCE_COLOR
settings
- Loading branch information
1 parent
34fe319
commit d327873
Showing
4 changed files
with
65 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters