Skip to content

Commit eb67054

Browse files
committed
fix(config): consolidate use of npm.color
The config value for `color` should never be directly read by npm, or its submodules. The derived value `npm.color` or `npm.flatOptions.color` is what we want to use. This PR consolidates the use of these values, makes sure there is only one place the value is actually calculated, and stops relying on duplicated code in the setup-log.js file for setting one of them. PR-URL: #3563 Credit: @wraithgar Close: #3563 Reviewed-by: @lukekarrys
1 parent 009ad1e commit eb67054

File tree

10 files changed

+38
-25
lines changed

10 files changed

+38
-25
lines changed

lib/exec.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ class Exec extends BaseCommand {
6868
async _exec (_args, { locationMsg, path, runPath }) {
6969
const args = [..._args]
7070
const call = this.npm.config.get('call')
71-
const color = this.npm.config.get('color')
7271
const {
7372
flatOptions,
7473
localBin,
@@ -87,7 +86,6 @@ class Exec extends BaseCommand {
8786
...flatOptions,
8887
args,
8988
call,
90-
color,
9189
localBin,
9290
locationMsg,
9391
log,
@@ -103,7 +101,7 @@ class Exec extends BaseCommand {
103101

104102
async _execWorkspaces (args, filters) {
105103
await this.setWorkspaces(filters)
106-
const color = this.npm.config.get('color')
104+
const color = this.npm.color
107105

108106
for (const path of this.workspacePaths) {
109107
const locationMsg = await getLocationMsg({ color, path })

lib/fund.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class Fund extends ArboristWorkspaceCmd {
109109
}
110110

111111
printHuman (fundingInfo) {
112-
const color = !!this.npm.color
112+
const color = this.npm.color
113113
const unicode = this.npm.config.get('unicode')
114114
const seenUrls = new Map()
115115

lib/ls.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class LS extends ArboristWorkspaceCmd {
6767

6868
async ls (args) {
6969
const all = this.npm.config.get('all')
70-
const color = !!this.npm.color
70+
const color = this.npm.color
7171
const depth = this.npm.config.get('depth')
7272
const dev = this.npm.config.get('dev')
7373
const development = this.npm.config.get('development')

lib/npm.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ const npm = module.exports = new class extends EventEmitter {
232232
process.emit('timeEnd', 'npm:load:setTitle')
233233

234234
process.emit('time', 'npm:load:setupLog')
235-
this.color = setupLog(this.config)
235+
setupLog(this.config)
236236
process.emit('timeEnd', 'npm:load:setupLog')
237237
process.env.COLOR = this.color ? '1' : '0'
238238

@@ -261,6 +261,12 @@ const npm = module.exports = new class extends EventEmitter {
261261
return flat
262262
}
263263

264+
get color () {
265+
// This is a special derived value that takes into consideration not only
266+
// the config, but whether or not we are operating in a tty.
267+
return this.flatOptions.color
268+
}
269+
264270
get lockfileVersion () {
265271
return 2
266272
}

lib/run-script.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class RunScript extends BaseCommand {
137137
path = path || this.npm.localPrefix
138138
const { scripts, name, _id } = await rpj(`${path}/package.json`)
139139
const pkgid = _id || name
140-
const color = !!this.npm.color
140+
const color = this.npm.color
141141

142142
if (!scripts)
143143
return []

lib/utils/config/definitions.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,9 @@ define('cidr', {
438438
flatten,
439439
})
440440

441+
// This should never be directly used, the flattened value is the derived value
442+
// and is sent to other modules, and is also exposed as `npm.color` for use
443+
// inside npm itself.
441444
define('color', {
442445
default: !process.env.NO_COLOR || process.env.NO_COLOR === '0',
443446
usage: '--color|--no-color|--color always',

lib/utils/setup-log.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module.exports = (config) => {
1818
const stderrTTY = process.stderr.isTTY
1919
const dumbTerm = process.env.TERM === 'dumb'
2020
const stderrNotDumb = stderrTTY && !dumbTerm
21+
// this logic is duplicated in the config 'color' flattener
2122
const enableColorStderr = color === 'always' ? true
2223
: color === false ? false
2324
: stderrTTY
@@ -58,6 +59,4 @@ module.exports = (config) => {
5859
log.enableProgress()
5960
else
6061
log.disableProgress()
61-
62-
return enableColorStdout
6362
}

test/lib/exec.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const LOG_WARN = []
2525
let PROGRESS_IGNORED = false
2626
const flatOptions = {
2727
npxCache: 'npx-cache-dir',
28+
color: false,
2829
cache: 'cache-dir',
2930
legacyPeerDeps: false,
3031
package: [],
@@ -109,12 +110,13 @@ t.afterEach(() => {
109110
LOG_WARN.length = 0
110111
PROGRESS_IGNORED = false
111112
flatOptions.legacyPeerDeps = false
112-
config.color = false
113+
flatOptions.color = false
113114
config['script-shell'] = 'shell-cmd'
114115
config.package = []
115116
flatOptions.package = []
116117
config.call = ''
117118
config.yes = true
119+
npm.color = false
118120
npm.localBin = 'local-bin'
119121
npm.globalBin = 'global-bin'
120122
})
@@ -268,7 +270,8 @@ t.test('npm exec <noargs>, run interactive shell', t => {
268270
t.test('print message with color when tty and not in CI', t => {
269271
CI_NAME = null
270272
process.stdin.isTTY = true
271-
config.color = true
273+
npm.color = true
274+
flatOptions.color = true
272275

273276
run(t, true, () => {
274277
t.strictSame(LOG_WARN, [])
@@ -1204,7 +1207,8 @@ t.test('workspaces', t => {
12041207
})
12051208
})
12061209

1207-
config.color = true
1210+
npm.color = true
1211+
flatOptions.color = true
12081212
npm._mockOutputs.length = 0
12091213
await new Promise((res, rej) => {
12101214
exec.execWorkspaces([], ['a'], er => {

test/lib/utils/error-message.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ const { resolve } = require('path')
1515
const npm = require('../../../lib/npm.js')
1616
const CACHE = '/some/cache/dir'
1717
npm.config = {
18+
flat: {
19+
color: false,
20+
},
1821
loaded: false,
1922
localPrefix: '/some/prefix/dir',
2023
get: key => {
@@ -467,7 +470,7 @@ t.test('explain ERESOLVE errors', t => {
467470
t.matchSnapshot(errorMessage(er, npm))
468471
t.match(EXPLAIN_CALLED, [[
469472
er,
470-
undefined,
473+
false,
471474
path.resolve(npm.cache, 'eresolve-report.txt'),
472475
]])
473476
t.end()

test/lib/utils/setup-log.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@ t.test('setup with color=always and unicode', t => {
8484
t.strictSame(WARN_CALLED, [['ERESOLVE', 'hello', { some: 'object' }]])
8585
WARN_CALLED.length = 0
8686

87-
t.equal(setupLog(config({
87+
setupLog(config({
8888
loglevel: 'warn',
8989
color: 'always',
9090
unicode: true,
9191
progress: false,
92-
})), true)
92+
}))
9393

9494
npmlog.warn('ERESOLVE', 'hello', { some: { other: 'object' } })
9595
t.strictSame(EXPLAIN_CALLED, [[{ some: { other: 'object' } }, true, 2]],
@@ -125,12 +125,12 @@ t.test('setup with color=true, no unicode, and non-TTY terminal', t => {
125125
process.stderr.isTTY = false
126126
process.stdout.isTTY = false
127127

128-
t.equal(setupLog(config({
128+
setupLog(config({
129129
loglevel: 'warn',
130130
color: false,
131131
progress: false,
132132
heading: 'asdf',
133-
})), false)
133+
}))
134134

135135
t.strictSame(settings, {
136136
level: 'warn',
@@ -156,12 +156,12 @@ t.test('setup with color=true, no unicode, and dumb TTY terminal', t => {
156156
process.stdout.isTTY = true
157157
process.env.TERM = 'dumb'
158158

159-
t.equal(setupLog(config({
159+
setupLog(config({
160160
loglevel: 'warn',
161161
color: true,
162162
progress: false,
163163
heading: 'asdf',
164-
})), true)
164+
}))
165165

166166
t.strictSame(settings, {
167167
level: 'warn',
@@ -187,12 +187,12 @@ t.test('setup with color=true, no unicode, and non-dumb TTY terminal', t => {
187187
process.stdout.isTTY = true
188188
process.env.TERM = 'totes not dum'
189189

190-
t.equal(setupLog(config({
190+
setupLog(config({
191191
loglevel: 'warn',
192192
color: true,
193193
progress: true,
194194
heading: 'asdf',
195-
})), true)
195+
}))
196196

197197
t.strictSame(settings, {
198198
level: 'warn',
@@ -218,12 +218,12 @@ t.test('setup with non-TTY stdout, TTY stderr', t => {
218218
process.stdout.isTTY = false
219219
process.env.TERM = 'definitely not a dummy'
220220

221-
t.equal(setupLog(config({
221+
setupLog(config({
222222
loglevel: 'warn',
223223
color: true,
224224
progress: true,
225225
heading: 'asdf',
226-
})), false)
226+
}))
227227

228228
t.strictSame(settings, {
229229
level: 'warn',
@@ -248,12 +248,12 @@ t.test('setup with TTY stdout, non-TTY stderr', t => {
248248
process.stderr.isTTY = false
249249
process.stdout.isTTY = true
250250

251-
t.equal(setupLog(config({
251+
setupLog(config({
252252
loglevel: 'warn',
253253
color: true,
254254
progress: true,
255255
heading: 'asdf',
256-
})), true)
256+
}))
257257

258258
t.strictSame(settings, {
259259
level: 'warn',

0 commit comments

Comments
 (0)