Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/how.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,14 @@ Sometimes `npm-ls` got hiccups - caused by individual broken project installatio
Then, this tool may also read `package.json` files inside the `node_module` directory as an additional information source.

Expected transformations, assumptions and results are [described here](result.md).

## Omitting dependencies

When `--omit` (`dev`, `optional`, `peer`) is configured, relying on `npm-ls --omit=...` alone is not sufficient:
`npm-ls` only filters direct edges on root and workspace packages, and without `--package-lock-only` its JSON output omits `dev`, `optional`, and `devOptional` node flags.

To handle transitive and `devOptional` dependencies consistently, this tool:
1. Indexes all deduplicated packages from the `npm-ls` tree while classifying dependency edges using each package's manifest metadata (`devDependencies` on root and workspaces, `optionalDependencies`, `_dependencies`, `peerDependencies`, and `peerDependenciesMeta`).
2. Excludes edges whose dependency type is omitted, except `extraneous` entries.
3. Walks reachable packages starting from the root package, skipping nodes flagged as `dev`, `optional`, `peer`, or `devOptional` (when both `dev` and `optional` are omitted) for the configured `--omit` types.

2 changes: 2 additions & 0 deletions src/_types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export interface PackageData {
dev?: boolean
/*** is dev-dependency AND is (transitive) optional */
devOptional?: boolean
/** is peer-dependency */
peer?: boolean
/** is not required by any dependency */
extraneous?: boolean
/** is bundled with another package */
Expand Down
100 changes: 92 additions & 8 deletions src/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export class BomBuilder {
throw new Error(`unexpected path ${JSON.stringify(rootPath)}`)
}

const allPackages = this.gatherPackages(data)
const allPackages = this.gatherPackages(data, rootPath)
const allComponents = new Map(iterableMap(
allPackages,
([p, packageData]) => [p, this.makeComponentWithPackageData(packageData, p)]
Expand Down Expand Up @@ -329,8 +329,12 @@ export class BomBuilder {
}

/* eslint-disable-next-line complexity -- ack */
private gatherPackages(data: any): Map<PackagePath, PackageData> {
private gatherPackages (data: any, rootPath: PackagePath): Map<PackagePath, PackageData> {
const packages = new Map<PackagePath, PackageData>()
const topPaths = new Set<PackagePath>([rootPath])
/* eslint-disable-next-line @typescript-eslint/no-unsafe-member-access -- ack */
const hasWorkspaces = data.workspaces !== undefined && data.workspaces !== null
const omitActive = this.omitDependencyTypes.size > 0
const todo: Array<typeof data> = [data]
let w // eslint-disable-line @typescript-eslint/init-declarations -- ack
/* eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- ack */
Expand All @@ -355,18 +359,98 @@ export class BomBuilder {
d.optional ??= w.optional
d.devOptional ??= w.devOptional
d.dev ??= w.dev
d.peer ??= w.peer
const isTop = topPaths.has(wpath)
// `dependencies` might be missing to prevent circles...
/* eslint-disable-next-line @typescript-eslint/no-unsafe-argument -- ack */
const dependencies: Array<typeof data> = Object.values(w.dependencies ?? {})
for (const { path: depPath } of dependencies) {
for (const [depName, depObj] of Object.entries<any>(w.dependencies ?? {})) {
if (depObj === null || typeof depObj !== 'object') { continue }
const depPath = depObj.path
if (!isString(depPath)) { continue }
d.dependencies.add(depPath)
const depResolved = depObj.resolved
if (hasWorkspaces && wpath === rootPath && isString(depResolved) && depResolved.startsWith('file:')) {
topPaths.add(depPath)
}
if (!omitActive || depObj.extraneous === true || !this.shouldOmitDependencyEdge(w, depName, isTop)) {
d.dependencies.add(depPath)
}
todo.push(depObj)
}
/* eslint-disable-next-line @typescript-eslint/no-unsafe-argument -- fp */
todo.push(...dependencies)
/* eslint-enable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment */
}
return packages
return omitActive
? this.filterReachablePackages(packages, rootPath)
: packages
}

private filterReachablePackages (
packages: Map<PackagePath, PackageData>,
rootPath: PackagePath
): Map<PackagePath, PackageData> {
const reachable = new Map<PackagePath, PackageData>()
const queue: PackagePath[] = [rootPath]
let currentPath: PackagePath | undefined // eslint-disable-line @typescript-eslint/init-declarations -- ack
while ((currentPath = queue.shift()) !== undefined) {
if (reachable.has(currentPath)) { continue }
const pkg = packages.get(currentPath)
if (pkg === undefined) { continue }
if (currentPath !== rootPath && this.shouldOmitPackage(pkg)) { continue }
const filteredDeps = new Set<PackagePath>()
for (const depPath of pkg.dependencies) {
const depPkg = packages.get(depPath)
if (depPkg === undefined || this.shouldOmitPackage(depPkg)) { continue }
filteredDeps.add(depPath)
if (!reachable.has(depPath)) {
queue.push(depPath)
}
}
pkg.dependencies = filteredDeps
reachable.set(currentPath, pkg)
}
return reachable
}

private shouldOmitDependencyEdge (parent: any, depName: string, isTop: boolean): boolean {
/* eslint-disable @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access -- ack */
if (isTop && Object.hasOwn(parent.devDependencies ?? {}, depName)) {
return this.omitDependencyTypes.has('dev')
}
if (Object.hasOwn(parent.optionalDependencies ?? {}, depName)) {
return this.omitDependencyTypes.has('optional')
}
if (Object.hasOwn(parent._dependencies ?? {}, depName)) {
return false
}
if (Object.hasOwn(parent.peerDependencies ?? {}, depName)) {
if (this.omitDependencyTypes.has('peer')) {
return true
}
if (parent.peerDependenciesMeta?.[depName]?.optional === true) {
return this.omitDependencyTypes.has('optional')
}
}
/* eslint-enable @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access */
return false
}

private shouldOmitPackage (data: PackageData): boolean {
if (data.dev === true && this.omitDependencyTypes.has('dev')) {
return true
}
if (data.optional === true && this.omitDependencyTypes.has('optional')) {
return true
}
if (
data.devOptional === true &&
this.omitDependencyTypes.has('dev') &&
this.omitDependencyTypes.has('optional')
) {
return true
}
if (data.peer === true && this.omitDependencyTypes.has('peer')) {
return true
}
return false
}

private * fetchLicenseEvidence (dirPath: string): Generator<License> {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading