diff options
author | Gar <gar+gh@danger.computer> | 2022-04-20 17:12:39 +0300 |
---|---|---|
committer | nlf <nlf@github.com> | 2022-05-03 21:16:03 +0300 |
commit | d654e7e9146f123a9806cfd9a17150eb1f6075a4 (patch) | |
tree | d947301d0f1fcbb4ee3e24db76b5470e966cc2f8 | |
parent | 9ed00dc9f31e5f9237c981d11784dd6cd4bd1df4 (diff) |
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.
-rw-r--r-- | lib/commands/doctor.js | 34 | ||||
-rw-r--r-- | lib/commands/publish.js | 7 | ||||
-rw-r--r-- | lib/npm.js | 19 | ||||
-rw-r--r-- | test/fixtures/mock-npm.js | 4 |
4 files changed, 32 insertions, 32 deletions
diff --git a/lib/commands/doctor.js b/lib/commands/doctor.js index ca0438f1a..f5bee1eb8 100644 --- a/lib/commands/doctor.js +++ b/lib/commands/doctor.js @@ -1,5 +1,4 @@ const cacache = require('cacache') -const chalk = require('chalk') const fs = require('fs') const fetch = require('make-fetch-happen') const table = require('text-table') @@ -102,28 +101,19 @@ class Doctor extends BaseCommand { messages.push(line) } - const outHead = ['Check', 'Value', 'Recommendation/Notes'].map( - !this.npm.color ? h => h : h => chalk.underline(h) - ) + const outHead = ['Check', 'Value', 'Recommendation/Notes'].map(h => this.npm.chalk.underline(h)) let allOk = true - const outBody = messages.map( - !this.npm.color - ? item => { - allOk = allOk && item[1] - item[1] = item[1] ? 'ok' : 'not ok' - item[2] = String(item[2]) - return item - } - : item => { - allOk = allOk && item[1] - if (!item[1]) { - item[0] = chalk.red(item[0]) - item[2] = chalk.magenta(String(item[2])) - } - item[1] = item[1] ? chalk.green('ok') : chalk.red('not ok') - return item - } - ) + const outBody = messages.map(item => { + if (!item[1]) { + allOk = false + item[0] = this.npm.chalk.red(item[0]) + item[1] = this.npm.chalk.red('not ok') + item[2] = this.npm.chalk.magenta(String(item[2])) + } else { + item[1] = this.npm.chalk.green('ok') + } + return item + }) const outTable = [outHead, ...outBody] const tableOpts = { stringLength: s => ansiTrim(s).length, diff --git a/lib/commands/publish.js b/lib/commands/publish.js index ff3036693..cbf0ccf4d 100644 --- a/lib/commands/publish.js +++ b/lib/commands/publish.js @@ -7,7 +7,6 @@ const runScript = require('@npmcli/run-script') const pacote = require('pacote') const npa = require('npm-package-arg') const npmFetch = require('npm-registry-fetch') -const chalk = require('chalk') const replaceInfo = require('../utils/replace-info.js') const otplease = require('../utils/otplease.js') @@ -151,8 +150,6 @@ class Publish extends BaseCommand { const results = {} const json = this.npm.config.get('json') const { silent } = this.npm - const noop = a => a - const color = this.npm.color ? chalk : { green: noop, bold: noop } await this.setWorkspaces(filters) for (const [name, workspace] of this.workspaces.entries()) { @@ -164,9 +161,9 @@ class Publish extends BaseCommand { log.warn( 'publish', `Skipping workspace ${ - color.green(name) + this.npm.chalk.green(name) }, marked as ${ - color.bold('private') + this.npm.chalk.bold('private') }` ) continue diff --git a/lib/npm.js b/lib/npm.js index c819a0807..732362565 100644 --- a/lib/npm.js +++ b/lib/npm.js @@ -1,16 +1,15 @@ const EventEmitter = require('events') const { resolve, dirname, join } = require('path') const Config = require('@npmcli/config') +const chalk = require('chalk') +const which = require('which') +const fs = require('@npmcli/fs') // Patch the global fs module here at the app level require('graceful-fs').gracefulify(require('fs')) const { definitions, flatten, shorthands } = require('./utils/config/index.js') const usage = require('./utils/npm-usage.js') - -const which = require('which') -const fs = require('@npmcli/fs') - const LogFile = require('./utils/log-file.js') const Timers = require('./utils/timers.js') const Display = require('./utils/display.js') @@ -37,6 +36,7 @@ class Npm extends EventEmitter { #tmpFolder = null #title = 'npm' #argvClean = [] + #chalk = null #logFile = new LogFile() #display = new Display() @@ -322,6 +322,17 @@ class Npm extends EventEmitter { return this.flatOptions.color } + get chalk () { + if (!this.#chalk) { + let level = chalk.level + if (!this.color) { + level = 0 + } + this.#chalk = new chalk.Instance({ level }) + } + return this.#chalk + } + get logColor () { return this.flatOptions.logColor } diff --git a/test/fixtures/mock-npm.js b/test/fixtures/mock-npm.js index 4263dc8fb..c9701ebc2 100644 --- a/test/fixtures/mock-npm.js +++ b/test/fixtures/mock-npm.js @@ -132,7 +132,9 @@ const LoadMockNpm = async (t, { }) const npm = init ? new Npm() : null - t.teardown(() => npm && npm.unload()) + t.teardown(() => { + npm && npm.unload() + }) if (load) { await npm.load() |