Skip to content

Commit 5a30a07

Browse files
committed
fix: start consolidating color output
chalk already has a way to disable color output, so if we don't want color we can disable it there and always use that instance of chalk. This was only updated in the two commands that have real tests. Doing it in the other places is going to require making their tests real so that we don't ALSO have to rewrite their tests just to change their internal code.
1 parent 6253d19 commit 5a30a07

File tree

4 files changed

+34
-30
lines changed

4 files changed

+34
-30
lines changed

lib/commands/doctor.js

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const cacache = require('cacache')
2-
const chalk = require('chalk')
32
const fs = require('fs')
43
const fetch = require('make-fetch-happen')
54
const table = require('text-table')
@@ -102,28 +101,19 @@ class Doctor extends BaseCommand {
102101
messages.push(line)
103102
}
104103

105-
const outHead = ['Check', 'Value', 'Recommendation/Notes'].map(
106-
!this.npm.color ? h => h : h => chalk.underline(h)
107-
)
104+
const outHead = ['Check', 'Value', 'Recommendation/Notes'].map(h => this.npm.chalk.underline(h))
108105
let allOk = true
109-
const outBody = messages.map(
110-
!this.npm.color
111-
? item => {
112-
allOk = allOk && item[1]
113-
item[1] = item[1] ? 'ok' : 'not ok'
114-
item[2] = String(item[2])
115-
return item
116-
}
117-
: item => {
118-
allOk = allOk && item[1]
119-
if (!item[1]) {
120-
item[0] = chalk.red(item[0])
121-
item[2] = chalk.magenta(String(item[2]))
122-
}
123-
item[1] = item[1] ? chalk.green('ok') : chalk.red('not ok')
124-
return item
125-
}
126-
)
106+
const outBody = messages.map(item => {
107+
if (!item[1]) {
108+
allOk = false
109+
item[0] = this.npm.chalk.red(item[0])
110+
item[1] = this.npm.chalk.red('not ok')
111+
item[2] = this.npm.chalk.magenta(String(item[2]))
112+
} else {
113+
item[1] = this.npm.chalk.green('ok')
114+
}
115+
return item
116+
})
127117
const outTable = [outHead, ...outBody]
128118
const tableOpts = {
129119
stringLength: s => ansiTrim(s).length,

lib/commands/publish.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ class Publish extends BaseCommand {
152152
const json = this.npm.config.get('json')
153153
const { silent } = this.npm
154154
const noop = a => a
155-
const color = this.npm.color ? chalk : { green: noop, bold: noop }
156155
await this.setWorkspaces(filters)
157156

158157
for (const [name, workspace] of this.workspaces.entries()) {
@@ -164,9 +163,9 @@ class Publish extends BaseCommand {
164163
log.warn(
165164
'publish',
166165
`Skipping workspace ${
167-
color.green(name)
166+
this.npm.chalk.green(name)
168167
}, marked as ${
169-
color.bold('private')
168+
this.npm.chalk.bold('private')
170169
}`
171170
)
172171
continue

lib/npm.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
const EventEmitter = require('events')
22
const { resolve, dirname, join } = require('path')
33
const Config = require('@npmcli/config')
4+
const chalk = require('chalk')
5+
const which = require('which')
6+
const fs = require('@npmcli/fs')
47

58
// Patch the global fs module here at the app level
69
require('graceful-fs').gracefulify(require('fs'))
710

811
const { definitions, flatten, shorthands } = require('./utils/config/index.js')
912
const usage = require('./utils/npm-usage.js')
10-
11-
const which = require('which')
12-
const fs = require('@npmcli/fs')
13-
1413
const LogFile = require('./utils/log-file.js')
1514
const Timers = require('./utils/timers.js')
1615
const Display = require('./utils/display.js')
@@ -37,6 +36,7 @@ class Npm extends EventEmitter {
3736
#tmpFolder = null
3837
#title = 'npm'
3938
#argvClean = []
39+
#chalk = null
4040

4141
#logFile = new LogFile()
4242
#display = new Display()
@@ -322,6 +322,17 @@ class Npm extends EventEmitter {
322322
return this.flatOptions.color
323323
}
324324

325+
get chalk () {
326+
if (!this.#chalk) {
327+
let level = chalk.level
328+
if (!this.color) {
329+
level = 0
330+
}
331+
this.#chalk = new chalk.Instance({ level });
332+
}
333+
return this.#chalk
334+
}
335+
325336
get logColor () {
326337
return this.flatOptions.logColor
327338
}

test/fixtures/mock-npm.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const mockLogs = require('./mock-logs')
55
const mockGlobals = require('./mock-globals')
66
const log = require('../../lib/utils/log-shim')
77
const envConfigKeys = Object.keys(require('../../lib/utils/config/definitions.js'))
8+
const chalk = require('chalk')
9+
const chalkLevel = chalk.level
810

911
const RealMockNpm = (t, otherMocks = {}) => {
1012
const mock = {
@@ -132,7 +134,9 @@ const LoadMockNpm = async (t, {
132134
})
133135

134136
const npm = init ? new Npm() : null
135-
t.teardown(() => npm && npm.unload())
137+
t.teardown(() => {
138+
npm && npm.unload()
139+
})
136140

137141
if (load) {
138142
await npm.load()

0 commit comments

Comments
 (0)