-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
why.ts
211 lines (159 loc) Β· 7.21 KB
/
why.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import {BaseCommand, WorkspaceRequiredError} from '@yarnpkg/cli';
import {Configuration, LocatorHash, Package, formatUtils, Descriptor} from '@yarnpkg/core';
import {IdentHash, Project} from '@yarnpkg/core';
import {miscUtils, structUtils, treeUtils} from '@yarnpkg/core';
import {Command, Option, Usage} from 'clipanion';
// eslint-disable-next-line arca/no-default-export
export default class WhyCommand extends BaseCommand {
static paths = [
[`why`],
];
static usage: Usage = Command.Usage({
description: `display the reason why a package is needed`,
details: `
This command prints the exact reasons why a package appears in the dependency tree.
If \`-R,--recursive\` is set, the listing will go in depth and will list, for each workspaces, what are all the paths that lead to the dependency. Note that the display is somewhat optimized in that it will not print the package listing twice for a single package, so if you see a leaf named "Foo" when looking for "Bar", it means that "Foo" already got printed higher in the tree.
`,
examples: [[
`Explain why lodash is used in your project`,
`$0 why lodash`,
]],
});
recursive = Option.Boolean(`-R,--recursive`, false, {
description: `List, for each workspace, what are all the paths that lead to the dependency`,
});
json = Option.Boolean(`--json`, false, {
description: `Format the output as an NDJSON stream`,
});
peers = Option.Boolean(`--peers`, false, {
description: `Also print the peer dependencies that match the specified name`,
});
package = Option.String();
async execute() {
const configuration = await Configuration.find(this.context.cwd, this.context.plugins);
const {project, workspace} = await Project.find(configuration, this.context.cwd);
if (!workspace)
throw new WorkspaceRequiredError(project.cwd, this.context.cwd);
await project.restoreInstallState();
const identHash = structUtils.parseIdent(this.package).identHash;
const whyTree = this.recursive
? whyRecursive(project, identHash, {configuration, peers: this.peers})
: whySimple(project, identHash, {configuration, peers: this.peers});
treeUtils.emitTree(whyTree, {
configuration,
stdout: this.context.stdout,
json: this.json,
separators: 1,
});
}
}
function whySimple(project: Project, identHash: IdentHash, {configuration, peers}: {configuration: Configuration, peers: boolean}) {
const sortedPackages = miscUtils.sortMap(project.storedPackages.values(), pkg => {
return structUtils.stringifyLocator(pkg);
});
const rootChildren: treeUtils.TreeMap = {};
const root: treeUtils.TreeNode = {children: rootChildren};
for (const pkg of sortedPackages) {
const nodeChildren: treeUtils.TreeMap = {};
const node: treeUtils.TreeNode | null = null;
for (const dependency of pkg.dependencies.values()) {
if (!peers && pkg.peerDependencies.has(dependency.identHash))
continue;
const resolution = project.storedResolutions.get(dependency.descriptorHash);
if (!resolution)
throw new Error(`Assertion failed: The resolution should have been registered`);
const nextPkg = project.storedPackages.get(resolution);
if (!nextPkg)
throw new Error(`Assertion failed: The package should have been registered`);
if (nextPkg.identHash !== identHash)
continue;
if (node === null) {
const key = structUtils.stringifyLocator(pkg);
rootChildren[key] = {value: [pkg, formatUtils.Type.LOCATOR], children: nodeChildren};
}
const key = structUtils.stringifyLocator(nextPkg);
nodeChildren[key] = {value: [{
descriptor: dependency,
locator: nextPkg,
}, formatUtils.Type.DEPENDENT]};
}
}
return root;
}
function whyRecursive(project: Project, identHash: IdentHash, {configuration, peers}: {configuration: Configuration, peers: boolean}) {
const sortedWorkspaces = miscUtils.sortMap(project.workspaces, workspace => {
return structUtils.stringifyLocator(workspace.anchoredLocator);
});
const seen: Set<LocatorHash> = new Set();
const dependents: Set<LocatorHash> = new Set();
const markAllDependents = (pkg: Package) => {
if (seen.has(pkg.locatorHash))
return dependents.has(pkg.locatorHash);
seen.add(pkg.locatorHash);
if (pkg.identHash === identHash) {
dependents.add(pkg.locatorHash);
return true;
}
let depends = false;
if (pkg.identHash === identHash)
depends = true;
for (const dependency of pkg.dependencies.values()) {
if (!peers && pkg.peerDependencies.has(dependency.identHash))
continue;
const resolution = project.storedResolutions.get(dependency.descriptorHash);
if (!resolution)
throw new Error(`Assertion failed: The resolution should have been registered`);
const nextPkg = project.storedPackages.get(resolution);
if (!nextPkg)
throw new Error(`Assertion failed: The package should have been registered`);
if (markAllDependents(nextPkg)) {
depends = true;
}
}
if (depends)
dependents.add(pkg.locatorHash);
return depends;
};
for (const workspace of sortedWorkspaces)
markAllDependents(workspace.anchoredPackage);
const printed: Set<LocatorHash> = new Set();
const rootChildren: treeUtils.TreeMap = {};
const root: treeUtils.TreeNode = {children: rootChildren};
const printAllDependents = (pkg: Package, parentChildren: treeUtils.TreeMap, dependency: Descriptor | null) => {
if (!dependents.has(pkg.locatorHash))
return;
const nodeValue = dependency !== null
? formatUtils.tuple(formatUtils.Type.DEPENDENT, {locator: pkg, descriptor: dependency})
: formatUtils.tuple(formatUtils.Type.LOCATOR, pkg);
const nodeChildren: treeUtils.TreeMap = {};
const node: treeUtils.TreeNode = {
value: nodeValue,
children: nodeChildren,
};
const key = structUtils.stringifyLocator(pkg);
parentChildren[key] = node;
// We don't want to print the children of our transitive workspace
// dependencies, as they will be printed in their own top-level branch
if (dependency !== null && project.tryWorkspaceByLocator(pkg))
return;
// We don't want to reprint the children for a package that already got
// printed as part of another branch
if (printed.has(pkg.locatorHash))
return;
printed.add(pkg.locatorHash);
for (const dependency of pkg.dependencies.values()) {
if (!peers && pkg.peerDependencies.has(dependency.identHash))
continue;
const resolution = project.storedResolutions.get(dependency.descriptorHash);
if (!resolution)
throw new Error(`Assertion failed: The resolution should have been registered`);
const nextPkg = project.storedPackages.get(resolution);
if (!nextPkg)
throw new Error(`Assertion failed: The package should have been registered`);
printAllDependents(nextPkg, nodeChildren, dependency);
}
};
for (const workspace of sortedWorkspaces)
printAllDependents(workspace.anchoredPackage, rootChildren, null);
return root;
}