From 69f5ff863fccf91935b1f62b514fd2621b761903 Mon Sep 17 00:00:00 2001 From: Luke Karrys Date: Sun, 1 Jan 2023 11:54:38 -0700 Subject: [PATCH] fix(view): convert command to use output instead of console --- lib/commands/view.js | 67 ++++++------- test/lib/commands/view.js | 196 ++++++++++++++++++-------------------- 2 files changed, 126 insertions(+), 137 deletions(-) diff --git a/lib/commands/view.js b/lib/commands/view.js index 1875a84ec306b..855b37b81d42f 100644 --- a/lib/commands/view.js +++ b/lib/commands/view.js @@ -1,8 +1,3 @@ -/* eslint-disable no-console */ -// XXX: remove console.log later - -// npm view [pkg [pkg ...]] - const chalk = require('chalk') const columns = require('cli-columns') const fs = require('fs') @@ -127,7 +122,7 @@ class View extends BaseCommand { const msg = await this.jsonData(reducedData, pckmnt._id) if (msg !== '') { - console.log(msg) + this.npm.output(msg) } } } @@ -166,10 +161,10 @@ class View extends BaseCommand { if (wholePackument) { data.map((v) => this.prettyView(pckmnt, v[Object.keys(v)[0]][''])) } else { - console.log(`${name}:`) + this.npm.output(`${name}:`) const msg = await this.jsonData(reducedData, pckmnt._id) if (msg !== '') { - console.log(msg) + this.npm.output(msg) } } } else { @@ -180,7 +175,7 @@ class View extends BaseCommand { } } if (Object.keys(results).length > 0) { - console.log(JSON.stringify(results, null, 2)) + this.npm.output(JSON.stringify(results, null, 2)) } } @@ -376,61 +371,61 @@ class View extends BaseCommand { info.license = chalk.green(info.license) } - console.log('') - console.log( + this.npm.output('') + this.npm.output( chalk.underline.bold(`${info.name}@${info.version}`) + ' | ' + info.license + ' | deps: ' + (info.deps.length ? chalk.cyan(info.deps.length) : chalk.green('none')) + ' | versions: ' + info.versions ) - info.description && console.log(info.description) + info.description && this.npm.output(info.description) if (info.repo || info.site) { - info.site && console.log(chalk.cyan(info.site)) + info.site && this.npm.output(chalk.cyan(info.site)) } const warningSign = unicode ? ' ⚠️ ' : '!!' - info.deprecated && console.log( + info.deprecated && this.npm.output( `\n${chalk.bold.red('DEPRECATED')}${ warningSign } - ${info.deprecated}` ) if (info.keywords.length) { - console.log('') - console.log('keywords:', chalk.yellow(info.keywords.join(', '))) + this.npm.output('') + this.npm.output('keywords:', chalk.yellow(info.keywords.join(', '))) } if (info.bins.length) { - console.log('') - console.log('bin:', chalk.yellow(info.bins.join(', '))) + this.npm.output('') + this.npm.output('bin:', chalk.yellow(info.bins.join(', '))) } - console.log('') - console.log('dist') - console.log('.tarball:', info.tarball) - console.log('.shasum:', info.shasum) - info.integrity && console.log('.integrity:', info.integrity) - info.unpackedSize && console.log('.unpackedSize:', info.unpackedSize) + this.npm.output('') + this.npm.output('dist') + this.npm.output('.tarball:', info.tarball) + this.npm.output('.shasum:', info.shasum) + info.integrity && this.npm.output('.integrity:', info.integrity) + info.unpackedSize && this.npm.output('.unpackedSize:', info.unpackedSize) const maxDeps = 24 if (info.deps.length) { - console.log('') - console.log('dependencies:') - console.log(columns(info.deps.slice(0, maxDeps), { padding: 1 })) + this.npm.output('') + this.npm.output('dependencies:') + this.npm.output(columns(info.deps.slice(0, maxDeps), { padding: 1 })) if (info.deps.length > maxDeps) { - console.log(`(...and ${info.deps.length - maxDeps} more.)`) + this.npm.output(`(...and ${info.deps.length - maxDeps} more.)`) } } if (info.maintainers && info.maintainers.length) { - console.log('') - console.log('maintainers:') - info.maintainers.forEach((u) => console.log('-', u)) + this.npm.output('') + this.npm.output('maintainers:') + info.maintainers.forEach((u) => this.npm.output('-', u)) } - console.log('') - console.log('dist-tags:') - console.log(columns(info.tags)) + this.npm.output('') + this.npm.output('dist-tags:') + this.npm.output(columns(info.tags)) if (info.publisher || info.modified) { let publishInfo = 'published' @@ -440,8 +435,8 @@ class View extends BaseCommand { if (info.publisher) { publishInfo += ` by ${info.publisher}` } - console.log('') - console.log(publishInfo) + this.npm.output('') + this.npm.output(publishInfo) } } } diff --git a/test/lib/commands/view.js b/test/lib/commands/view.js index d347bc9230ec8..c6a4bf8fb79f4 100644 --- a/test/lib/commands/view.js +++ b/test/lib/commands/view.js @@ -262,93 +262,88 @@ const packument = (nv, opts) => { } const loadMockNpm = async function (t, opts = {}) { - const consoleLogs = [] const mockNpm = await _loadMockNpm(t, { + command: 'view', mocks: { pacote: { packument, }, }, - globals: { - 'console.log': (...args) => { - consoleLogs.push(args) - }, - }, ...opts, }) - return { ...mockNpm, consoleLogs } + return mockNpm } t.test('package from git', async t => { - const { npm, consoleLogs } = await loadMockNpm(t, { config: { unicode: false } }) - await npm.exec('view', ['https://github.com/npm/green']) - t.matchSnapshot(consoleLogs.join('\n')) + const { view, outputs } = await loadMockNpm(t, { config: { unicode: false } }) + await view.exec(['https://github.com/npm/green']) + t.matchSnapshot(outputs.join('\n')) }) t.test('deprecated package with license, bugs, repository and other fields', async t => { - const { npm, consoleLogs } = await loadMockNpm(t, { config: { unicode: false } }) - await npm.exec('view', ['green@1.0.0']) - t.matchSnapshot(consoleLogs.join('\n')) + const { view, outputs } = await loadMockNpm(t, { config: { unicode: false } }) + await view.exec(['green@1.0.0']) + t.matchSnapshot(outputs.join('\n')) }) t.test('deprecated package with unicode', async t => { - const { npm, consoleLogs } = await loadMockNpm(t, { config: { unicode: true } }) - await npm.exec('view', ['green@1.0.0']) - t.matchSnapshot(consoleLogs.join('\n')) + const { view, outputs } = await loadMockNpm(t, { config: { unicode: true } }) + await view.exec(['green@1.0.0']) + t.matchSnapshot(outputs.join('\n')) }) t.test('package with more than 25 deps', async t => { - const { npm, consoleLogs } = await loadMockNpm(t, { config: { unicode: false } }) - await npm.exec('view', ['black@1.0.0']) - t.matchSnapshot(consoleLogs.join('\n')) + const { view, outputs } = await loadMockNpm(t, { config: { unicode: false } }) + await view.exec(['black@1.0.0']) + t.matchSnapshot(outputs.join('\n')) }) t.test('package with maintainers info as object', async t => { - const { npm, consoleLogs } = await loadMockNpm(t, { config: { unicode: false } }) - await npm.exec('view', ['pink@1.0.0']) - t.matchSnapshot(consoleLogs.join('\n')) + const { view, outputs } = await loadMockNpm(t, { config: { unicode: false } }) + await view.exec(['pink@1.0.0']) + t.matchSnapshot(outputs.join('\n')) }) t.test('package with homepage', async t => { - const { npm, consoleLogs } = await loadMockNpm(t, { config: { unicode: false } }) - await npm.exec('view', ['orange@1.0.0']) - t.matchSnapshot(consoleLogs.join('\n')) + const { view, outputs } = await loadMockNpm(t, { config: { unicode: false } }) + await view.exec(['orange@1.0.0']) + t.matchSnapshot(outputs.join('\n')) }) t.test('package with no versions', async t => { - const { npm, consoleLogs } = await loadMockNpm(t, { config: { unicode: false } }) - await npm.exec('view', ['brown']) - t.equal(consoleLogs.join('\n'), '', 'no info to display') + const { view, outputs } = await loadMockNpm(t, { config: { unicode: false } }) + await view.exec(['brown']) + t.equal(outputs.join('\n'), '', 'no info to display') }) t.test('package with no repo or homepage', async t => { - const { npm, consoleLogs } = await loadMockNpm(t, { config: { unicode: false } }) - await npm.exec('view', ['blue@1.0.0']) - t.matchSnapshot(consoleLogs.join('\n')) + const { view, outputs } = await loadMockNpm(t, { config: { unicode: false } }) + await view.exec(['blue@1.0.0']) + t.matchSnapshot(outputs.join('\n')) }) t.test('package with semver range', async t => { - const { npm, consoleLogs } = await loadMockNpm(t, { config: { unicode: false } }) - await npm.exec('view', ['blue@^1.0.0']) - t.matchSnapshot(consoleLogs.join('\n')) + const { view, outputs } = await loadMockNpm(t, { config: { unicode: false } }) + await view.exec(['blue@^1.0.0']) + t.matchSnapshot(outputs.join('\n')) }) t.test('package with no modified time', async t => { - const { npm, consoleLogs } = await loadMockNpm(t, { config: { unicode: false } }) - await npm.exec('view', ['cyan@1.0.0']) - t.matchSnapshot(consoleLogs.join('\n')) + const { view, outputs } = await loadMockNpm(t, { config: { unicode: false } }) + await view.exec(['cyan@1.0.0']) + t.matchSnapshot(outputs.join('\n')) }) t.test('package with --json and semver range', async t => { - const { npm, consoleLogs } = await loadMockNpm(t, { config: { json: true } }) - await npm.exec('view', ['cyan@^1.0.0']) - t.matchSnapshot(consoleLogs.join('\n')) + const { view, outputs } = await loadMockNpm(t, { config: { json: true } }) + await view.exec(['cyan@^1.0.0']) + t.matchSnapshot(outputs.join('\n')) }) t.test('package with --json and no versions', async t => { - const { npm, consoleLogs } = await loadMockNpm(t, { config: { json: true } }) - await npm.exec('view', ['brown']) - t.equal(consoleLogs.join('\n'), '', 'no info to display') + const { view, outputs } = await loadMockNpm(t, { config: { json: true } }) + await view.exec(['brown']) + t.equal(outputs.join('\n'), '', 'no info to display') }) t.test('package in cwd', async t => { @@ -360,72 +355,71 @@ t.test('package in cwd', async t => { } t.test('specific version', async t => { - const { npm, consoleLogs } = await loadMockNpm(t, { prefixDir }) - await npm.exec('view', ['.@1.0.0']) - t.matchSnapshot(consoleLogs.join('\n')) + const { view, outputs } = await loadMockNpm(t, { prefixDir }) + await view.exec(['.@1.0.0']) + t.matchSnapshot(outputs.join('\n')) }) t.test('non-specific version', async t => { - const { npm, consoleLogs } = await loadMockNpm(t, { prefixDir }) - await npm.exec('view', ['.']) - t.matchSnapshot(consoleLogs.join('\n')) + const { view, outputs } = await loadMockNpm(t, { prefixDir }) + await view.exec(['.']) + t.matchSnapshot(outputs.join('\n')) }) t.test('directory', async t => { - const { npm, consoleLogs } = await loadMockNpm(t, { prefixDir }) - await npm.exec('view', ['./blue']) - t.matchSnapshot(consoleLogs.join('\n')) + const { view, outputs } = await loadMockNpm(t, { prefixDir }) + await view.exec(['./blue']) + t.matchSnapshot(outputs.join('\n')) }) }) t.test('specific field names', async t => { - const { npm, consoleLogs } = await loadMockNpm(t) - t.afterEach(() => { - consoleLogs.length = 0 - }) + const { view, outputs } = await loadMockNpm(t) + t.afterEach(() => outputs.length = 0) + t.test('readme', async t => { - await npm.exec('view', ['yellow@1.0.0', 'readme']) - t.matchSnapshot(consoleLogs.join('\n')) + await view.exec(['yellow@1.0.0', 'readme']) + t.matchSnapshot(outputs.join('\n')) }) t.test('several fields', async t => { - await npm.exec('view', ['yellow@1.0.0', 'name', 'version', 'foo[bar]']) - t.matchSnapshot(consoleLogs.join('\n')) + await view.exec(['yellow@1.0.0', 'name', 'version', 'foo[bar]']) + t.matchSnapshot(outputs.join('\n')) }) t.test('several fields with several versions', async t => { - await npm.exec('view', ['yellow@1.x.x', 'author']) - t.matchSnapshot(consoleLogs.join('\n')) + await view.exec(['yellow@1.x.x', 'author']) + t.matchSnapshot(outputs.join('\n')) }) t.test('nested field with brackets', async t => { - await npm.exec('view', ['orange@1.0.0', 'dist[shasum]']) - t.matchSnapshot(consoleLogs.join('\n')) + await view.exec(['orange@1.0.0', 'dist[shasum]']) + t.matchSnapshot(outputs.join('\n')) }) t.test('maintainers with email', async t => { - await npm.exec('view', ['yellow@1.0.0', 'maintainers', 'name']) - t.matchSnapshot(consoleLogs.join('\n')) + await view.exec(['yellow@1.0.0', 'maintainers', 'name']) + t.matchSnapshot(outputs.join('\n')) }) t.test('maintainers with url', async t => { - await npm.exec('view', ['pink@1.0.0', 'maintainers']) - t.matchSnapshot(consoleLogs.join('\n')) + await view.exec(['pink@1.0.0', 'maintainers']) + t.matchSnapshot(outputs.join('\n')) }) t.test('unknown nested field ', async t => { - await npm.exec('view', ['yellow@1.0.0', 'dist.foobar']) - t.equal(consoleLogs.join('\n'), '', 'no info to display') + await view.exec(['yellow@1.0.0', 'dist.foobar']) + t.equal(outputs.join('\n'), '', 'no info to display') }) t.test('array field - 1 element', async t => { - await npm.exec('view', ['purple@1.0.0', 'maintainers.name']) - t.matchSnapshot(consoleLogs.join('\n')) + await view.exec(['purple@1.0.0', 'maintainers.name']) + t.matchSnapshot(outputs.join('\n')) }) t.test('array field - 2 elements', async t => { - await npm.exec('view', ['yellow@1.x.x', 'maintainers.name']) - t.matchSnapshot(consoleLogs.join('\n')) + await view.exec(['yellow@1.x.x', 'maintainers.name']) + t.matchSnapshot(outputs.join('\n')) }) }) @@ -495,84 +489,84 @@ t.test('workspaces', async t => { } t.test('all workspaces', async t => { - const { npm, consoleLogs } = await loadMockNpm(t, { + const { view, outputs } = await loadMockNpm(t, { prefixDir, config: { unicode: false, workspaces: true }, }) - await npm.exec('view', []) - t.matchSnapshot(consoleLogs.join('\n')) + await view.exec([]) + t.matchSnapshot(outputs.join('\n')) }) t.test('one specific workspace', async t => { - const { npm, consoleLogs } = await loadMockNpm(t, { + const { view, outputs } = await loadMockNpm(t, { prefixDir, config: { unicode: false, workspace: ['green'] }, }) - await npm.exec('view', []) - t.matchSnapshot(consoleLogs.join('\n')) + await view.exec([]) + t.matchSnapshot(outputs.join('\n')) }) t.test('all workspaces --json', async t => { - const { npm, consoleLogs } = await loadMockNpm(t, { + const { view, outputs } = await loadMockNpm(t, { prefixDir, config: { unicode: false, workspaces: true, json: true }, }) - await npm.exec('view', []) - t.matchSnapshot(consoleLogs.join('\n')) + await view.exec([]) + t.matchSnapshot(outputs.join('\n')) }) t.test('all workspaces single field', async t => { - const { npm, consoleLogs } = await loadMockNpm(t, { + const { view, outputs } = await loadMockNpm(t, { prefixDir, config: { unicode: false, workspaces: true }, }) - await npm.exec('view', ['.', 'name']) - t.matchSnapshot(consoleLogs.join('\n')) + await view.exec(['.', 'name']) + t.matchSnapshot(outputs.join('\n')) }) t.test('all workspaces nonexistent field', async t => { - const { npm, consoleLogs } = await loadMockNpm(t, { + const { view, outputs } = await loadMockNpm(t, { prefixDir, config: { unicode: false, workspaces: true }, }) - await npm.exec('view', ['.', 'foo']) - t.matchSnapshot(consoleLogs.join('\n')) + await view.exec(['.', 'foo']) + t.matchSnapshot(outputs.join('\n')) }) t.test('all workspaces nonexistent field --json', async t => { - const { npm, consoleLogs } = await loadMockNpm(t, { + const { view, outputs } = await loadMockNpm(t, { prefixDir, config: { unicode: false, workspaces: true, json: true }, }) - await npm.exec('view', ['.', 'foo']) - t.matchSnapshot(consoleLogs.join('\n')) + await view.exec(['.', 'foo']) + t.matchSnapshot(outputs.join('\n')) }) t.test('all workspaces single field --json', async t => { - const { npm, consoleLogs } = await loadMockNpm(t, { + const { view, outputs } = await loadMockNpm(t, { prefixDir, config: { unicode: false, workspaces: true, json: true }, }) - await npm.exec('view', ['.', 'name']) - t.matchSnapshot(consoleLogs.join('\n')) + await view.exec(['.', 'name']) + t.matchSnapshot(outputs.join('\n')) }) t.test('single workspace --json', async t => { - const { npm, consoleLogs } = await loadMockNpm(t, { + const { view, outputs } = await loadMockNpm(t, { prefixDir, config: { unicode: false, workspace: ['green'], json: true }, }) - await npm.exec('view', []) - t.matchSnapshot(consoleLogs.join('\n')) + await view.exec([]) + t.matchSnapshot(outputs.join('\n')) }) t.test('remote package name', async t => { - const { npm, logs, consoleLogs } = await loadMockNpm(t, { + const { view, logs, outputs } = await loadMockNpm(t, { prefixDir, config: { unicode: false, workspaces: true }, }) - await npm.exec('view', ['pink']) - t.matchSnapshot(consoleLogs.join('\n')) + await view.exec(['pink']) + t.matchSnapshot(outputs.join('\n')) t.matchSnapshot(logs.warn, 'should have warning of ignoring workspaces') }) })