fix(reviewing): filter the same print message (#7430)

close #7429

---------

Co-authored-by: Zoltan Kochan <z@kochan.io>
This commit is contained in:
btea
2023-12-26 19:54:41 +08:00
committed by GitHub
parent 97b450e1f2
commit 09f6103490
3 changed files with 25 additions and 9 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/list": patch
"pnpm": patch
---
`pnpm list --parseable` should not print the same dependency multiple times [#7429](https://github.com/pnpm/pnpm/issues/7429).

View File

@@ -14,20 +14,26 @@ export async function renderParseable (
search: boolean
}
) {
return pkgs.map((pkg) => renderParseableForPackage(pkg, opts)).filter(p => p.length !== 0).join('\n')
const depPaths = new Set<string>()
return pkgs
.map(renderParseableForPackage.bind(null, depPaths, opts))
.filter(p => p.length !== 0)
.join('\n')
}
function renderParseableForPackage (
pkg: PackageDependencyHierarchy,
depPaths: Set<string>,
opts: {
long: boolean
depth: number
alwaysPrintRootPackage: boolean
search: boolean
}
},
pkg: PackageDependencyHierarchy
) {
const pkgs = sortPackages(
flatten(
depPaths,
[
...(pkg.optionalDependencies ?? []),
...(pkg.dependencies ?? []),
@@ -66,13 +72,19 @@ interface PackageInfo {
}
function flatten (
depPaths: Set<string>,
nodes: PackageNode[]
): PackageInfo[] {
let packages: PackageInfo[] = []
for (const node of nodes) {
packages.push(node)
// The content output by renderParseable is flat,
// so we can deduplicate packages that are repeatedly dependent on multiple packages.
if (!depPaths.has(node.path)) {
depPaths.add(node.path)
packages.push(node)
}
if (node.dependencies?.length) {
packages = packages.concat(flatten(node.dependencies))
packages = packages.concat(flatten(depPaths, node.dependencies))
}
}
return packages

View File

@@ -261,8 +261,7 @@ test('parseable list in workspace with private package', async () => {
lockfileDir: workspaceWithPrivatePkgs,
})).toBe(`${path.join(workspaceWithPrivatePkgs, 'packages/private')}
${path.join(workspaceWithPrivatePkgs, 'node_modules/.pnpm/is-positive@1.0.0/node_modules/is-positive')}
${path.join(workspaceWithPrivatePkgs, 'packages/public')}
${path.join(workspaceWithPrivatePkgs, 'node_modules/.pnpm/is-positive@1.0.0/node_modules/is-positive')}`)
${path.join(workspaceWithPrivatePkgs, 'packages/public')}`)
})
test('long parseable list in workspace with private package', async () => {
@@ -275,8 +274,7 @@ test('long parseable list in workspace with private package', async () => {
lockfileDir: workspaceWithPrivatePkgs,
})).toBe(`${path.join(workspaceWithPrivatePkgs, 'packages/private')}:private@1.0.0:PRIVATE
${path.join(workspaceWithPrivatePkgs, 'node_modules/.pnpm/is-positive@1.0.0/node_modules/is-positive')}:is-positive@1.0.0
${path.join(workspaceWithPrivatePkgs, 'packages/public')}:public@1.0.0
${path.join(workspaceWithPrivatePkgs, 'node_modules/.pnpm/is-positive@1.0.0/node_modules/is-positive')}:is-positive@1.0.0`)
${path.join(workspaceWithPrivatePkgs, 'packages/public')}:public@1.0.0`)
})
test('JSON list in workspace with private package', async () => {