Skip to content

Commit d654e7e

Browse files
wraithgarnlf
authored andcommitted
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 9ed00dc commit d654e7e

File tree

4 files changed

+32
-32
lines changed

4 files changed

+32
-32
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 & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const runScript = require('@npmcli/run-script')
77
const pacote = require('pacote')
88
const npa = require('npm-package-arg')
99
const npmFetch = require('npm-registry-fetch')
10-
const chalk = require('chalk')
1110
const replaceInfo = require('../utils/replace-info.js')
1211

1312
const otplease = require('../utils/otplease.js')
@@ -151,8 +150,6 @@ class Publish extends BaseCommand {
151150
const results = {}
152151
const json = this.npm.config.get('json')
153152
const { silent } = this.npm
154-
const noop = a => a
155-
const color = this.npm.color ? chalk : { green: noop, bold: noop }
156153
await this.setWorkspaces(filters)
157154

158155
for (const [name, workspace] of this.workspaces.entries()) {
@@ -164,9 +161,9 @@ class Publish extends BaseCommand {
164161
log.warn(
165162
'publish',
166163
`Skipping workspace ${
167-
color.green(name)
164+
this.npm.chalk.green(name)
168165
}, marked as ${
169-
color.bold('private')
166+
this.npm.chalk.bold('private')
170167
}`
171168
)
172169
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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ const LoadMockNpm = async (t, {
132132
})
133133

134134
const npm = init ? new Npm() : null
135-
t.teardown(() => npm && npm.unload())
135+
t.teardown(() => {
136+
npm && npm.unload()
137+
})
136138

137139
if (load) {
138140
await npm.load()

0 commit comments

Comments
 (0)