From aa21a2df38face172a5c1e6bc231724b2ba27a60 Mon Sep 17 00:00:00 2001 From: Zoltan Kochan Date: Sat, 8 Aug 2020 12:37:01 +0300 Subject: [PATCH] fix(list): print the legend only once PR #2756 --- .changeset/lemon-rivers-happen.md | 5 +++++ fixtures/workspace-with-2-pkgs/package.json | 1 + .../packages/bar/package.json | 7 ++++++ .../packages/foo/package.json | 8 +++++++ fixtures/workspace-with-2-pkgs/pnpm-lock.yaml | 21 ++++++++++++++++++ .../workspace-with-2-pkgs/pnpm-workspace.yaml | 2 ++ packages/list/src/renderTree.ts | 9 ++++++-- packages/list/test/index.ts | 22 +++++++++++++++++++ 8 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 .changeset/lemon-rivers-happen.md create mode 100644 fixtures/workspace-with-2-pkgs/package.json create mode 100644 fixtures/workspace-with-2-pkgs/packages/bar/package.json create mode 100644 fixtures/workspace-with-2-pkgs/packages/foo/package.json create mode 100644 fixtures/workspace-with-2-pkgs/pnpm-lock.yaml create mode 100644 fixtures/workspace-with-2-pkgs/pnpm-workspace.yaml diff --git a/.changeset/lemon-rivers-happen.md b/.changeset/lemon-rivers-happen.md new file mode 100644 index 0000000000..ac4f4b9f06 --- /dev/null +++ b/.changeset/lemon-rivers-happen.md @@ -0,0 +1,5 @@ +--- +"@pnpm/list": patch +--- + +Print the legend only once. diff --git a/fixtures/workspace-with-2-pkgs/package.json b/fixtures/workspace-with-2-pkgs/package.json new file mode 100644 index 0000000000..9e26dfeeb6 --- /dev/null +++ b/fixtures/workspace-with-2-pkgs/package.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/fixtures/workspace-with-2-pkgs/packages/bar/package.json b/fixtures/workspace-with-2-pkgs/packages/bar/package.json new file mode 100644 index 0000000000..dbac8fbb9c --- /dev/null +++ b/fixtures/workspace-with-2-pkgs/packages/bar/package.json @@ -0,0 +1,7 @@ +{ + "name": "bar", + "version": "0.0.0", + "dependencies": { + "is-positive": "1.0.0" + } +} diff --git a/fixtures/workspace-with-2-pkgs/packages/foo/package.json b/fixtures/workspace-with-2-pkgs/packages/foo/package.json new file mode 100644 index 0000000000..4e779c0104 --- /dev/null +++ b/fixtures/workspace-with-2-pkgs/packages/foo/package.json @@ -0,0 +1,8 @@ +{ + "name": "foo", + "version": "0.0.0", + "dependencies": { + "is-positive": "1.0.0" + } +} + diff --git a/fixtures/workspace-with-2-pkgs/pnpm-lock.yaml b/fixtures/workspace-with-2-pkgs/pnpm-lock.yaml new file mode 100644 index 0000000000..0e3ef785bb --- /dev/null +++ b/fixtures/workspace-with-2-pkgs/pnpm-lock.yaml @@ -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= diff --git a/fixtures/workspace-with-2-pkgs/pnpm-workspace.yaml b/fixtures/workspace-with-2-pkgs/pnpm-workspace.yaml new file mode 100644 index 0000000000..eccc335f93 --- /dev/null +++ b/fixtures/workspace-with-2-pkgs/pnpm-workspace.yaml @@ -0,0 +1,2 @@ +packages: + - 'packages/**' \ No newline at end of file diff --git a/packages/list/src/renderTree.ts b/packages/list/src/renderTree.ts index 9016777454..8929222f38 100644 --- a/packages/list/src/renderTree.ts +++ b/packages/list/src/renderTree.ts @@ -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) { diff --git a/packages/list/test/index.ts b/packages/list/test/index.ts index 4ab7218bb6..b2b7399d78 100644 --- a/packages/list/test/index.ts +++ b/packages/list/test/index.ts @@ -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}