fix(list): print the legend only once

PR #2756
This commit is contained in:
Zoltan Kochan
2020-08-08 12:37:01 +03:00
committed by GitHub
parent 8d2731181f
commit aa21a2df38
8 changed files with 73 additions and 2 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/list": patch
---
Print the legend only once.

View File

@@ -0,0 +1 @@
{}

View File

@@ -0,0 +1,7 @@
{
"name": "bar",
"version": "0.0.0",
"dependencies": {
"is-positive": "1.0.0"
}
}

View File

@@ -0,0 +1,8 @@
{
"name": "foo",
"version": "0.0.0",
"dependencies": {
"is-positive": "1.0.0"
}
}

View File

@@ -0,0 +1,21 @@
importers:
.:
specifiers: {}
packages/bar:
dependencies:
is-positive: 1.0.0
specifiers:
is-positive: 1.0.0
packages/foo:
dependencies:
is-positive: 1.0.0
specifiers:
is-positive: 1.0.0
lockfileVersion: 5.1
packages:
/is-positive/1.0.0:
dev: false
engines:
node: '>=0.10.0'
resolution:
integrity: sha1-iACYVrZKLx632LsBeUGEJK4EUss=

View File

@@ -0,0 +1,2 @@
packages:
- 'packages/**'

View File

@@ -26,7 +26,12 @@ export default async function (
search: boolean,
}
) {
return (await Promise.all(packages.map((pkg) => renderTreeForPackage(pkg, opts)))).filter(Boolean).join('\n\n')
const output = (
await Promise.all(packages.map((pkg) => renderTreeForPackage(pkg, opts)))
)
.filter(Boolean)
.join('\n\n')
return `${(opts.depth > -1 && output ? LEGEND : '')}${output}`
}
async function renderTreeForPackage (
@@ -55,7 +60,7 @@ async function renderTreeForPackage (
label += ' '
}
label += pkg.path
let output = (opts.depth > -1 ? LEGEND : '') + label + '\n'
let output = `${label}\n`
const useColumns = opts.depth === 0 && opts.long === false && !opts.search
for (let dependenciesField of [...DEPENDENCIES_FIELDS.sort(), 'unsavedDependencies']) {
if (pkg[dependenciesField]?.length) {

View File

@@ -26,6 +26,7 @@ const fixture = path.join(fixtures, 'fixture')
const fixtureWithNoPkgNameAndNoVersion = path.join(fixtures, 'fixture-with-no-pkg-name-and-no-version')
const fixtureWithNoPkgVersion = path.join(fixtures, 'fixture-with-no-pkg-version')
const fixtureWithExternalLockfile = path.join(fixtures, 'fixture-with-external-shrinkwrap', 'pkg')
const workspaceWith2Pkgs = path.join(fixtures, 'workspace-with-2-pkgs')
const emptyFixture = path.join(fixtures, 'empty')
const fixtureWithAliasedDep = path.join(fixtures, 'with-aliased-dep')
@@ -42,6 +43,27 @@ is-positive ${VERSION_CLR('1.0.0')}`)
t.end()
})
test('print legend only once', async (t) => {
t.equal(await list([
path.join(workspaceWith2Pkgs, 'packages/bar'),
path.join(workspaceWith2Pkgs, 'packages/foo'),
], {
lockfileDir: workspaceWith2Pkgs,
}), `${LEGEND}
bar@0.0.0 ${path.join(workspaceWith2Pkgs, 'packages/bar')}
${DEPENDENCIES}
is-positive ${VERSION_CLR('1.0.0')}
foo@0.0.0 ${path.join(workspaceWith2Pkgs, 'packages/foo')}
${DEPENDENCIES}
is-positive ${VERSION_CLR('1.0.0')}`)
t.end()
})
test('list with default parameters', async t => {
t.equal(await list([fixture], { lockfileDir: fixture }), `${LEGEND}