@preventive/deptree 1.0.0-alpha.6 → 1.0.0-alpha.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/npm.d.ts +9 -8
- package/package.json +11 -7
- package/pnpm.d.ts +66 -18
- package/soldeer.d.ts +74 -21
- package/src/hash.js +2 -6
- package/src/matcher.js +3 -2
- package/src/mount.js +22 -2
- package/src/npm/bins.js +2 -19
- package/src/npm/compat.js +11 -6
- package/src/npm/inputs.js +4 -9
- package/src/npm/manifests.js +4 -12
- package/src/npmrc.js +1 -2
- package/src/patch.js +4 -13
- package/src/pnpm/bins.js +129 -37
- package/src/pnpm/commands.js +4 -10
- package/src/pnpm/frozen.js +3 -6
- package/src/pnpm/gitignore.js +3 -1
- package/src/pnpm/glob.js +3 -3
- package/src/pnpm/hoist.js +3 -6
- package/src/pnpm/hoisted.js +234 -0
- package/src/pnpm/inputs.js +24 -34
- package/src/pnpm/install.js +17 -20
- package/src/pnpm/local.js +15 -3
- package/src/pnpm/minimatch.js +6 -15
- package/src/pnpm/nm-hoist-graph.js +299 -0
- package/src/pnpm/nm-hoist.js +149 -0
- package/src/pnpm/npm-packlist.js +7 -11
- package/src/pnpm/overrides.js +1 -2
- package/src/pnpm/package.js +6 -3
- package/src/pnpm/packlist.js +5 -4
- package/src/pnpm/readers.js +26 -29
- package/src/pnpm/settings.js +9 -8
- package/src/pnpm/tree.js +73 -30
- package/src/pnpm/workspace.js +2 -2
- package/src/project.js +22 -6
- package/src/soldeer/git.js +52 -0
- package/src/soldeer/inputs.js +4 -11
- package/src/soldeer/tree.js +36 -22
- package/src/soldeer/zip.js +4 -5
- package/src/tarball.js +68 -11
- package/src/yarn1/bins.js +1 -1
- package/src/yarn1/inputs.js +4 -11
- package/src/yarn1/package.js +61 -9
- package/src/yarn1/requests.js +7 -5
- package/src/yarn1/resolve.js +62 -13
- package/src/yarn1/settings.js +1 -2
- package/src/yarn1/tree.js +29 -9
- package/yarn1.d.ts +62 -29
package/src/pnpm/inputs.js
CHANGED
|
@@ -7,7 +7,7 @@ import { compareVersions, valid } from '@preventive/upstream/semver.js'
|
|
|
7
7
|
import { normalize } from '@preventive/vfs/path.js'
|
|
8
8
|
import { DeptreeError, quote } from '../error.js'
|
|
9
9
|
import { readManifest } from '../manifest.js'
|
|
10
|
-
import { checkProject, readText, typeOf } from '../project.js'
|
|
10
|
+
import { checkHostKeys, checkLeftOut, checkProject, checkTexts, readText, typeOf } from '../project.js'
|
|
11
11
|
import { pinnedPnpm, readManifests } from './projects.js'
|
|
12
12
|
import { readers } from './readers.js'
|
|
13
13
|
import { checkWorkspace, findProjects, linkedManifest } from './workspace.js'
|
|
@@ -23,20 +23,7 @@ function readWorkspaceText(project, major) {
|
|
|
23
23
|
return text
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
function readRootFiles(project) {
|
|
28
|
-
const lockfile = readText(project, '/pnpm-lock.yaml')
|
|
29
|
-
if (lockfile === undefined) throw new DeptreeError('the project has no pnpm-lock.yaml, which a frozen install cannot do without')
|
|
30
|
-
return { lockfile, npmrc: readText(project, '/.npmrc') }
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function readManifestTexts(project, ids) {
|
|
34
|
-
const texts = new Map()
|
|
35
|
-
for (const id of ids) {
|
|
36
|
-
texts.set(id, readText(project, id === '.' ? '/package.json' : `/${id}/package.json`, `manifests[${quote(id)}]`))
|
|
37
|
-
}
|
|
38
|
-
return texts
|
|
39
|
-
}
|
|
26
|
+
const readManifestTexts = (project, ids) => new Map(ids.map((id) => [id, readText(project, id === '.' ? '/package.json' : `/${id}/package.json`, `manifests[${quote(id)}]`)]))
|
|
40
27
|
|
|
41
28
|
// By path from the lockfile's directory; one that is not there is left out.
|
|
42
29
|
function readPatches(project, configured) {
|
|
@@ -70,10 +57,7 @@ function pnpmOf(pnpm, root) {
|
|
|
70
57
|
|
|
71
58
|
// All of host but its pnpm, which pnpmOf reads.
|
|
72
59
|
export function checkHost(host) {
|
|
73
|
-
|
|
74
|
-
for (const key of ['node', 'os', 'cpu', 'libc']) {
|
|
75
|
-
if (typeof host[key] !== 'string' || host[key] === '') throw new TypeError(`host.${key} must be a non-empty string`)
|
|
76
|
-
}
|
|
60
|
+
checkHostKeys(host, ['node', 'os', 'cpu', 'libc'], 'node, os, cpu and libc, and pnpm where it is not pinned')
|
|
77
61
|
const { node, os, libc } = host
|
|
78
62
|
if (valid(node) === null) throw new DeptreeError(`${quote(node)} is not an exact version`, 'host.node')
|
|
79
63
|
if (os === 'win32') throw new DeptreeError('Windows is not supported: pnpm links there with junctions to absolute paths', 'host.os')
|
|
@@ -130,14 +114,24 @@ function packagesOf(workspace) {
|
|
|
130
114
|
return workspace?.packages === undefined ? undefined : readers.globs(workspace.packages, 'pnpm-workspace.yaml: packages')
|
|
131
115
|
}
|
|
132
116
|
|
|
117
|
+
// The pnpm that installs and the workspace it reads, from disk, with the root
|
|
118
|
+
// package.json read at most once.
|
|
119
|
+
function readInstalls(project, pnpm) {
|
|
120
|
+
let read
|
|
121
|
+
const root = () => (read ??= readRoot(project))
|
|
122
|
+
const installs = pnpmOf(pnpm, root)
|
|
123
|
+
const text = readWorkspaceText(project, installs.major)
|
|
124
|
+
const workspace = workspaceOf(readWorkspace(text), text, installs.major, root)
|
|
125
|
+
return { ...installs, workspace, packages: packagesOf(workspace) }
|
|
126
|
+
}
|
|
127
|
+
|
|
133
128
|
// The directories of the projects whose package.json buildPnpmTree takes.
|
|
134
129
|
export function findPnpmProjects(options) {
|
|
135
130
|
const { project, host } = options ?? {}
|
|
136
131
|
checkProject(project)
|
|
137
132
|
if (host !== undefined && (host === null || typeof host !== 'object')) throw new TypeError('host must be an object, or left out')
|
|
138
|
-
const { major } =
|
|
139
|
-
|
|
140
|
-
return findProjects(project, packagesOf(workspaceOf(readWorkspace(text), text, major, () => readRoot(project))), major)
|
|
133
|
+
const { major, packages } = readInstalls(project, host?.pnpm)
|
|
134
|
+
return findProjects(project, packages, major)
|
|
141
135
|
}
|
|
142
136
|
|
|
143
137
|
const LOCKFILE = 'lockfile must be the text of pnpm-lock.yaml, or left out with a project given to read it from'
|
|
@@ -146,15 +140,14 @@ export function inputsOf(options) {
|
|
|
146
140
|
const { lockfile, manifests, workspace, npmrc, patches, project } = options
|
|
147
141
|
if (lockfile === undefined) {
|
|
148
142
|
if (project === undefined) throw new TypeError(LOCKFILE)
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
143
|
+
checkLeftOut({ manifests, workspace, npmrc, patches })
|
|
144
|
+
const text = readText(project, '/pnpm-lock.yaml')
|
|
145
|
+
if (text === undefined) throw new DeptreeError('the project has no pnpm-lock.yaml, which a frozen install cannot do without')
|
|
146
|
+
// pnpm-workspace.yaml is read once the pnpm that installs is known.
|
|
147
|
+
return { reading: true, project, lockfile: text, npmrc: readText(project, '/.npmrc') }
|
|
153
148
|
}
|
|
154
149
|
if (typeof lockfile !== 'string') throw new TypeError(LOCKFILE)
|
|
155
|
-
|
|
156
|
-
if (value !== undefined && typeof value !== 'string') throw new TypeError(`${name} must be a string, or left out`)
|
|
157
|
-
}
|
|
150
|
+
checkTexts({ workspace, npmrc })
|
|
158
151
|
return { reading: false, lockfile, manifests, workspace, npmrc, patches }
|
|
159
152
|
}
|
|
160
153
|
|
|
@@ -168,13 +161,10 @@ export function manifestsOf(inputs, lockfile, pnpm) {
|
|
|
168
161
|
return { manifests, ...installs, workspace: workspaceOf(workspace, inputs.workspace, installs.major, () => manifests.get('.')) }
|
|
169
162
|
}
|
|
170
163
|
const { project } = inputs
|
|
171
|
-
const installs =
|
|
172
|
-
const text = readWorkspaceText(project, installs.major)
|
|
173
|
-
const effective = workspaceOf(readWorkspace(text), text, installs.major, () => readRoot(project))
|
|
174
|
-
const packages = packagesOf(effective)
|
|
164
|
+
const { packages, ...installs } = readInstalls(project, pnpm)
|
|
175
165
|
checkWorkspace(Object.keys(lockfile.importers), packages, installs.major)
|
|
176
166
|
const ids = findProjects(project, packages, installs.major)
|
|
177
|
-
return { manifests: readManifests(readManifestTexts(project, ids), lockfile), ...installs
|
|
167
|
+
return { manifests: readManifests(readManifestTexts(project, ids), lockfile), ...installs }
|
|
178
168
|
}
|
|
179
169
|
|
|
180
170
|
export function patchesOf(inputs, configured) {
|
package/src/pnpm/install.js
CHANGED
|
@@ -52,9 +52,8 @@ function effectivePlatform(pkg) {
|
|
|
52
52
|
|
|
53
53
|
const current = (value, supported) => (supported ?? ['current']).map((item) => (item === 'current' ? value : item))
|
|
54
54
|
|
|
55
|
-
const takesPlatform = (pkg, host, supported) =>
|
|
56
|
-
|
|
57
|
-
&& (host.libc === 'unknown' || checkList(current(host.libc, supported?.libc), pkg.libc ?? ['any'], host.major))
|
|
55
|
+
const takesPlatform = (pkg, host, supported) => ['os', 'cpu', 'libc'].every((key) => (key === 'libc' && host.libc === 'unknown')
|
|
56
|
+
|| checkList(current(host[key], supported?.[key]), pkg[key] ?? ['any'], host.major))
|
|
58
57
|
|
|
59
58
|
const takesEngine = (engines, node) => !engines.node || satisfies(node, engines.node, { includePrerelease: true })
|
|
60
59
|
|
|
@@ -167,6 +166,17 @@ function projectKeys(lockfile, ids, walked) {
|
|
|
167
166
|
return all
|
|
168
167
|
}
|
|
169
168
|
|
|
169
|
+
// Each key by `check`: false adds it to `skipped`, null to `incompatible`.
|
|
170
|
+
function checkEach(keys, check, skipped = new Set()) {
|
|
171
|
+
const incompatible = new Set()
|
|
172
|
+
for (const key of keys) {
|
|
173
|
+
const ok = check(key)
|
|
174
|
+
if (ok === false) skipped.add(key)
|
|
175
|
+
if (ok === null) incompatible.add(key)
|
|
176
|
+
}
|
|
177
|
+
return { skipped, incompatible }
|
|
178
|
+
}
|
|
179
|
+
|
|
170
180
|
// pnpm 11's filterLockfileByImportersAndEngine, breadth first by edge kind:
|
|
171
181
|
// an optional edge to a snapshot the host cannot run is not taken, a
|
|
172
182
|
// required one is, with a warning; each reachable one not taken is left out.
|
|
@@ -187,20 +197,14 @@ function skippedSnapshots11(lockfile, check) {
|
|
|
187
197
|
installed.add(key)
|
|
188
198
|
queue.push(...edgesOf(pkg.dependencies, false), ...edgesOf(pkg.optionalDependencies, true))
|
|
189
199
|
}
|
|
190
|
-
const
|
|
191
|
-
const incompatible = new Set()
|
|
192
|
-
for (const key of reached.keys()) {
|
|
193
|
-
const ok = check(key, lockfile.packages[key], !installed.has(key) || !required.has(key))
|
|
194
|
-
if (ok === false) skipped.add(key)
|
|
195
|
-
if (ok === null) incompatible.add(key)
|
|
196
|
-
}
|
|
200
|
+
const checked = checkEach(reached.keys(), (key) => check(key, lockfile.packages[key], !installed.has(key) || !required.has(key)))
|
|
197
201
|
const seen = new Set(starts)
|
|
198
202
|
for (const key of seen) {
|
|
199
203
|
const pkg = lockfile.packages[key]
|
|
200
|
-
if (!installed.has(key) && pkg.optional) skipped.add(key)
|
|
204
|
+
if (!installed.has(key) && pkg.optional) checked.skipped.add(key)
|
|
201
205
|
for (const edge of [...edgesOf(pkg.dependencies), ...edgesOf(pkg.optionalDependencies)]) seen.add(edge.key)
|
|
202
206
|
}
|
|
203
|
-
return
|
|
207
|
+
return checked
|
|
204
208
|
}
|
|
205
209
|
|
|
206
210
|
// The snapshots left out, and those installed that the host cannot run.
|
|
@@ -231,12 +235,5 @@ export function skippedSnapshots(lockfile, context) {
|
|
|
231
235
|
stack.push({ keys: [...next, ...projectKeys(lockfile, projects, walked)].values(), installable })
|
|
232
236
|
}
|
|
233
237
|
// @pnpm/deps.graph-builder checks the rest again, each on its own.
|
|
234
|
-
|
|
235
|
-
for (const key of picked) {
|
|
236
|
-
if (skipped.has(key)) continue
|
|
237
|
-
const ok = check(key, lockfile.packages[key])
|
|
238
|
-
if (ok === false) skipped.add(key)
|
|
239
|
-
if (ok === null) incompatible.add(key)
|
|
240
|
-
}
|
|
241
|
-
return { skipped, incompatible }
|
|
238
|
+
return checkEach([...picked].filter((key) => !skipped.has(key)), (key) => check(key, lockfile.packages[key]), skipped)
|
|
242
239
|
}
|
package/src/pnpm/local.js
CHANGED
|
@@ -13,7 +13,7 @@ import { packDirectory } from './packlist.js'
|
|
|
13
13
|
|
|
14
14
|
function manifestAt(project, dir, where) {
|
|
15
15
|
const text = readText(project, `/${dir}/package.json`, where)
|
|
16
|
-
if (text === undefined) throw new DeptreeError(`${quote(dir)} holds no package.json in the project given`, where)
|
|
16
|
+
if (text === undefined) throw new DeptreeError(`${quote(dir)} ${typeOf(project, `/${dir}`) === undefined ? 'is not in' : 'holds no package.json in'} the project given`, where)
|
|
17
17
|
return readManifest(text, where)
|
|
18
18
|
}
|
|
19
19
|
|
|
@@ -41,12 +41,24 @@ export function readDirectoryPackage(project, pkg, where, host) {
|
|
|
41
41
|
return { files, manifest, local: true }
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
+
// pnpm takes `npm:@scope/name@workspace:<range>` for a path, for its `/`,
|
|
45
|
+
// which no other resolver takes, and links to a directory of that name.
|
|
46
|
+
const ALIASED = /(?:^|\/)(npm:(@[^/]+\/[^/]+)@(workspace:[^/]*))$/u
|
|
47
|
+
|
|
48
|
+
// pnpm links to a directory that is not there all the same: pnpm 10 and 11
|
|
49
|
+
// warn as they resolve, and none does as a frozen install. Refused, with the
|
|
50
|
+
// project given or, for the spec it takes for a path, without, but where the
|
|
51
|
+
// project holds a directory of that name.
|
|
44
52
|
export function readLinked(links, nodes, projects, project) {
|
|
45
53
|
const linked = new Map()
|
|
46
|
-
if (project === undefined) return linked
|
|
47
54
|
for (const [path, target] of links) {
|
|
48
55
|
if (nodes.has(target) || projects.has(target) || linked.has(target) || target === '..' || target.startsWith('../')) continue
|
|
49
|
-
|
|
56
|
+
const aliased = ALIASED.exec(target)
|
|
57
|
+
if (aliased !== null && (project === undefined || typeOf(project, `/${target}`) === undefined)) {
|
|
58
|
+
const [, spec, name, range] = aliased
|
|
59
|
+
throw new DeptreeError(`pnpm takes ${quote(spec)} for a path, for its "/", and links to ${quote(target)}${project === undefined ? '' : ', which is not there'}; ${quote(range.replace(':', `:${name}@`))} is the spec that links the workspace package`, quote(path))
|
|
60
|
+
}
|
|
61
|
+
if (project !== undefined) linked.set(target, manifestAt(project, target, quote(path)))
|
|
50
62
|
}
|
|
51
63
|
return linked
|
|
52
64
|
}
|
package/src/pnpm/minimatch.js
CHANGED
|
@@ -43,7 +43,7 @@ export function checkParts(pattern, parts, where) {
|
|
|
43
43
|
// oxlint-disable-next-line require-unicode-regexp
|
|
44
44
|
export const caseless = (source) => new RegExp(source, 'i')
|
|
45
45
|
|
|
46
|
-
const NO_TRAVERSAL_5 = String.raw`(?!(?:^|\/)\.{1,2}(?:$|\/))`
|
|
46
|
+
export const NO_TRAVERSAL_5 = String.raw`(?!(?:^|\/)\.{1,2}(?:$|\/))`
|
|
47
47
|
const NO_TRAVERSAL_10 = String.raw`(?!(?:^|/)\.\.?(?:$|/))`
|
|
48
48
|
|
|
49
49
|
// What a regexp with `i` and no `u` takes a code unit for: its upper case
|
|
@@ -111,12 +111,6 @@ function partOf(text, { lookahead = false, traversal = false, empty = true } = {
|
|
|
111
111
|
|
|
112
112
|
const foldless = (text) => text.toUpperCase() === text.toLowerCase()
|
|
113
113
|
|
|
114
|
-
export function part5(text) {
|
|
115
|
-
if (text === '**') return GLOBSTAR
|
|
116
|
-
if (!/[*?]/u.test(text)) return text === '' || foldless(text) ? text : partOf(text)
|
|
117
|
-
return partOf(text, { lookahead: true, traversal: /^[*?]/u.test(text) })
|
|
118
|
-
}
|
|
119
|
-
|
|
120
114
|
// minimatch 10's own tests of a part, which it runs in place of its regexp.
|
|
121
115
|
const FAST = [
|
|
122
116
|
[/^\*+$/u, () => (f) => f.length !== 0 && f !== '.' && f !== '..'],
|
|
@@ -126,9 +120,11 @@ const FAST = [
|
|
|
126
120
|
[/^\.\*+$/u, () => (f) => f !== '.' && f !== '..' && f.startsWith('.')],
|
|
127
121
|
]
|
|
128
122
|
|
|
129
|
-
|
|
123
|
+
// A part as minimatch 5 reads it, or with `ten` minimatch 10.
|
|
124
|
+
export function readPart(text, ten = false) {
|
|
130
125
|
if (text === '**') return GLOBSTAR
|
|
131
126
|
if (!/[*?]/u.test(text)) return text === '' || foldless(text) ? text : partOf(text)
|
|
127
|
+
if (!ten) return partOf(text, { lookahead: true, traversal: /^[*?]/u.test(text) })
|
|
132
128
|
const fast = FAST.find(([re]) => re.test(text))
|
|
133
129
|
if (fast !== undefined) return { test: fast[1](text.match(fast[0])) }
|
|
134
130
|
return partOf(text, { traversal: /^\.{0,2}[*?]/u.test(text), empty: !/^\*+$/u.test(text) })
|
|
@@ -137,11 +133,6 @@ function part10(text) {
|
|
|
137
133
|
const hit = (part, name) => (typeof part === 'string' ? name === part : part.test(name))
|
|
138
134
|
const traversal = (name) => name === '.' || name === '..'
|
|
139
135
|
|
|
140
|
-
function traversalFrom(file, start) {
|
|
141
|
-
for (let i = start; i < file.length; i++) if (traversal(file[i])) return true
|
|
142
|
-
return false
|
|
143
|
-
}
|
|
144
|
-
|
|
145
136
|
// How a match ends where file, as long as `end`, or pattern runs out at
|
|
146
137
|
// `fi` and `pi`.
|
|
147
138
|
function ended(file, pattern, fi, pi, partial, end = file.length) {
|
|
@@ -176,7 +167,7 @@ function matchOld(file, pattern, partial) {
|
|
|
176
167
|
if (!hit(pattern[pi], file[fi])) return false
|
|
177
168
|
continue
|
|
178
169
|
}
|
|
179
|
-
if (pi + 1 === pattern.length) return !
|
|
170
|
+
if (pi + 1 === pattern.length) return !file.slice(fi).some(traversal)
|
|
180
171
|
let fr = fi
|
|
181
172
|
for (; fr < file.length; fr++) {
|
|
182
173
|
if (from(fr, pi + 1)) return true
|
|
@@ -317,7 +308,7 @@ function splitOf(path) {
|
|
|
317
308
|
// ignore-walk 8 reads them. `test(path, partial)` is whether one matches,
|
|
318
309
|
// with matchBase.
|
|
319
310
|
export function fromParts(negate, globParts, minimatch) {
|
|
320
|
-
const set = globParts.map((parts) => parts.map((part) => (typeof part === 'string' ? (minimatch === '10.2'
|
|
311
|
+
const set = globParts.map((parts) => parts.map((part) => (typeof part === 'string' ? readPart(part, minimatch === '10.2') : part)))
|
|
321
312
|
const matchOne = minimatch === '5.1.6' ? matchOld : matchNew
|
|
322
313
|
const test = (path, partial = false) => {
|
|
323
314
|
if (path === '/' && partial) return true
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
// The hoisting of @yarnpkg/nm 4.0.5 to one root (nm-hoist.js): which of the
|
|
2
|
+
// packages below it move up to it, one pass at a time.
|
|
3
|
+
|
|
4
|
+
export const REGULAR = 0
|
|
5
|
+
export const WORKSPACE = 1
|
|
6
|
+
|
|
7
|
+
const YES = 0
|
|
8
|
+
const NO = 1
|
|
9
|
+
const DEPENDS = 2
|
|
10
|
+
|
|
11
|
+
const getAliasedLocator = (node) => `${node.name}@${node.locator}`
|
|
12
|
+
|
|
13
|
+
// Depth first, as pnpm recurses, on a stack: each item of a frame's `items`
|
|
14
|
+
// goes to `visit(item, frame)`, which gives the frame to go into, if any;
|
|
15
|
+
// each frame done goes to `leave`.
|
|
16
|
+
export function depthFirst(frame, visit, leave) {
|
|
17
|
+
const stack = [frame]
|
|
18
|
+
while (stack.length > 0) {
|
|
19
|
+
const top = stack.at(-1)
|
|
20
|
+
const { value, done } = top.items.next()
|
|
21
|
+
if (done) {
|
|
22
|
+
stack.pop()
|
|
23
|
+
leave?.(top)
|
|
24
|
+
} else {
|
|
25
|
+
const next = visit(value, top)
|
|
26
|
+
if (next !== undefined) stack.push(next)
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Depth first from `start`, each node once: `visit(node, extra)` runs as a
|
|
32
|
+
// node is reached, and yields the children to go on to and what each is
|
|
33
|
+
// reached with.
|
|
34
|
+
export function walk(start, extra, visit, step) {
|
|
35
|
+
const seen = new Set()
|
|
36
|
+
const enter = (node, given) => {
|
|
37
|
+
step()
|
|
38
|
+
if (seen.has(node)) return undefined
|
|
39
|
+
seen.add(node)
|
|
40
|
+
return { items: visit(node, given) }
|
|
41
|
+
}
|
|
42
|
+
depthFirst(enter(start, extra), (value) => enter(...value))
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// What `starts` reach through `next`, but where `skip` says.
|
|
46
|
+
export function reachable(starts, next, skip = () => false) {
|
|
47
|
+
const reached = new Set()
|
|
48
|
+
const pending = [...starts]
|
|
49
|
+
while (pending.length > 0) {
|
|
50
|
+
const item = pending.pop()
|
|
51
|
+
if (reached.has(item) || skip(item)) continue
|
|
52
|
+
reached.add(item)
|
|
53
|
+
pending.push(...next(item))
|
|
54
|
+
}
|
|
55
|
+
return reached
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// A node's dependencies but its peers.
|
|
59
|
+
export function* regularDependencies(node) {
|
|
60
|
+
for (const dep of node.dependencies.values()) if (!node.peerNames.has(dep.name)) yield dep
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// The names used below the root: zero-round, each hoisted dependency's own;
|
|
64
|
+
// later, what each name a node had hoisted from it resolves to on the path.
|
|
65
|
+
export function getZeroRoundUsedDependencies(rootNodePath, step) {
|
|
66
|
+
const used = new Map()
|
|
67
|
+
walk(rootNodePath.at(-1), undefined, function* (node) {
|
|
68
|
+
for (const dep of node.hoistedDependencies.values()) used.set(dep.name, dep)
|
|
69
|
+
for (const dep of regularDependencies(node)) yield [dep]
|
|
70
|
+
}, step)
|
|
71
|
+
return used
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function getUsedDependencies(rootNodePath, step) {
|
|
75
|
+
const used = new Map()
|
|
76
|
+
walk(rootNodePath.at(-1), new Set(), function* (node, hidden) {
|
|
77
|
+
for (const dep of node.hoistedDependencies.values()) {
|
|
78
|
+
if (hidden.has(dep.name)) continue
|
|
79
|
+
step(rootNodePath.length)
|
|
80
|
+
for (const onPath of rootNodePath) {
|
|
81
|
+
const resolved = onPath.dependencies.get(dep.name)
|
|
82
|
+
if (resolved) used.set(resolved.name, resolved)
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const childrenHidden = new Set(node.dependencies.keys())
|
|
86
|
+
for (const dep of regularDependencies(node)) yield [dep, childrenHidden]
|
|
87
|
+
}, step)
|
|
88
|
+
return used
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function decoupleGraphNode(parent, node, step) {
|
|
92
|
+
if (node.decoupled) return node
|
|
93
|
+
step(node.dependencies.size + node.originalDependencies.size + node.hoistedDependencies.size + 1)
|
|
94
|
+
const clone = {
|
|
95
|
+
...node,
|
|
96
|
+
references: new Set(node.references),
|
|
97
|
+
dependencies: new Map(node.dependencies),
|
|
98
|
+
originalDependencies: new Map(node.originalDependencies),
|
|
99
|
+
hoistedDependencies: new Map(node.hoistedDependencies),
|
|
100
|
+
peerNames: new Set(node.peerNames),
|
|
101
|
+
decoupled: true,
|
|
102
|
+
}
|
|
103
|
+
const selfDep = clone.dependencies.get(node.name)
|
|
104
|
+
if (selfDep && selfDep.ident === clone.ident) clone.dependencies.set(node.name, clone)
|
|
105
|
+
parent.dependencies.set(clone.name, clone)
|
|
106
|
+
return clone
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Each package below `rootNode` by name and ident, with who depends on it,
|
|
110
|
+
// and as a peer, but the root's peers. pnpm sets no hoist priority.
|
|
111
|
+
export function buildPreferenceMap(rootNode, step) {
|
|
112
|
+
const preferenceMap = new Map()
|
|
113
|
+
const entryOf = (node) => {
|
|
114
|
+
const key = `${node.name}@${node.ident}`
|
|
115
|
+
if (!preferenceMap.has(key)) preferenceMap.set(key, { dependents: new Set(), peerDependents: new Set() })
|
|
116
|
+
return preferenceMap.get(key)
|
|
117
|
+
}
|
|
118
|
+
walk(rootNode, undefined, function* (node) {
|
|
119
|
+
for (const dep of node.dependencies.values()) {
|
|
120
|
+
if (!node.peerNames.has(dep.name)) {
|
|
121
|
+
entryOf(dep).dependents.add(node.ident)
|
|
122
|
+
yield [dep]
|
|
123
|
+
} else if (node !== rootNode) {
|
|
124
|
+
entryOf(dep).peerDependents.add(node.ident)
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}, step)
|
|
128
|
+
return preferenceMap
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// By name, the idents to try at the root, the most wanted first.
|
|
132
|
+
export function getHoistIdentMap(rootNode, preferenceMap) {
|
|
133
|
+
const identMap = new Map([[rootNode.name, [rootNode.ident]]])
|
|
134
|
+
for (const dep of regularDependencies(rootNode)) identMap.set(dep.name, [dep.ident])
|
|
135
|
+
const wanted = (key) => preferenceMap.get(key).dependents.size + preferenceMap.get(key).peerDependents.size
|
|
136
|
+
const keyList = [...preferenceMap.keys()].sort((key1, key2) => wanted(key2) - wanted(key1))
|
|
137
|
+
for (const key of keyList) {
|
|
138
|
+
const name = key.slice(0, key.indexOf('@', 1))
|
|
139
|
+
const ident = key.slice(name.length + 1)
|
|
140
|
+
if (rootNode.peerNames.has(name)) continue
|
|
141
|
+
let idents = identMap.get(name)
|
|
142
|
+
if (!idents) {
|
|
143
|
+
idents = []
|
|
144
|
+
identMap.set(name, idents)
|
|
145
|
+
}
|
|
146
|
+
if (!idents.includes(ident)) idents.push(ident)
|
|
147
|
+
}
|
|
148
|
+
return identMap
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// A node's dependencies but its peers, each after the peers it has among
|
|
152
|
+
// them.
|
|
153
|
+
function getSortedRegularDependencies(node, step) {
|
|
154
|
+
step(node.dependencies.size + 1)
|
|
155
|
+
const dependencies = new Set()
|
|
156
|
+
const frameOf = (dep) => ({ dep, items: dep.peerNames.values() })
|
|
157
|
+
for (const top of regularDependencies(node)) {
|
|
158
|
+
const seenDeps = new Set([top])
|
|
159
|
+
depthFirst(frameOf(top), (peerName) => {
|
|
160
|
+
const peerDep = node.peerNames.has(peerName) ? undefined : node.dependencies.get(peerName)
|
|
161
|
+
if (!peerDep || dependencies.has(peerDep) || seenDeps.has(peerDep)) return undefined
|
|
162
|
+
seenDeps.add(peerDep)
|
|
163
|
+
step()
|
|
164
|
+
return frameOf(peerDep)
|
|
165
|
+
}, ({ dep }) => dependencies.add(dep))
|
|
166
|
+
}
|
|
167
|
+
return dependencies
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Whether a node of `node`'s name on the path, other than it, shadows it,
|
|
171
|
+
// which the parent then records.
|
|
172
|
+
function isShadowed(nodePath, node, shadowedNodes) {
|
|
173
|
+
for (let idx = nodePath.length - 1; idx >= 1; idx--) {
|
|
174
|
+
const parentDep = nodePath[idx].dependencies.get(node.name)
|
|
175
|
+
if (!parentDep || parentDep.ident === node.ident) continue
|
|
176
|
+
const parentNode = nodePath.at(-1)
|
|
177
|
+
if (!shadowedNodes.has(parentNode)) shadowedNodes.set(parentNode, new Set())
|
|
178
|
+
shadowedNodes.get(parentNode).add(node.name)
|
|
179
|
+
return true
|
|
180
|
+
}
|
|
181
|
+
return false
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// The parent's packages `node`'s peers are, which have to go to the root
|
|
185
|
+
// before it, or null where a peer up the path is not the root's.
|
|
186
|
+
function peersDependOn(rootNode, nodePath, node) {
|
|
187
|
+
const dependsOn = new Set()
|
|
188
|
+
const checkList = new Set(node.peerNames)
|
|
189
|
+
for (let idx = nodePath.length - 1; idx >= 1; idx--) {
|
|
190
|
+
const parent = nodePath[idx]
|
|
191
|
+
for (const name of checkList) {
|
|
192
|
+
if (parent.peerNames.has(name) && parent.originalDependencies.has(name)) continue
|
|
193
|
+
const parentDepNode = parent.dependencies.get(name)
|
|
194
|
+
if (parentDepNode && rootNode.dependencies.get(name) !== parentDepNode) {
|
|
195
|
+
if (idx !== nodePath.length - 1) return null
|
|
196
|
+
dependsOn.add(parentDepNode)
|
|
197
|
+
}
|
|
198
|
+
checkList.delete(name)
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return dependsOn
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Whether `node` goes to the root, in the order the checks are made, as
|
|
205
|
+
// one shadowing it is recorded as it is found. One that waits for its peers
|
|
206
|
+
// to go first does so whatever its hoisted dependencies say.
|
|
207
|
+
function getNodeHoistInfo(rootNode, nodePath, node, usedDependencies, hoistIdents, shadowedNodes, fastLookupPossible) {
|
|
208
|
+
const no = { isHoistable: NO }
|
|
209
|
+
if (node.ident === nodePath.at(-1).ident || node.dependencyKind === WORKSPACE || rootNode.peerNames.has(node.name)) return no
|
|
210
|
+
const usedDep = usedDependencies.get(node.name)
|
|
211
|
+
if ((usedDep && usedDep.ident !== node.ident) || isShadowed(nodePath, node, shadowedNodes) || hoistIdents.get(node.name) !== node.ident) return no
|
|
212
|
+
const dependsOn = peersDependOn(rootNode, nodePath, node)
|
|
213
|
+
if (dependsOn === null) return no
|
|
214
|
+
if (dependsOn.size > 0) return { isHoistable: DEPENDS, dependsOn }
|
|
215
|
+
const used = (dep) => usedDependencies.get(dep.name) || rootNode.dependencies.get(dep.name)
|
|
216
|
+
if (!fastLookupPossible && node.hoistedDependencies.values().some((dep) => used(dep)?.ident !== dep.ident)) return no
|
|
217
|
+
return { isHoistable: YES }
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// hoistGraph's hoistNodeDependencies, of one parent: what of its own it
|
|
221
|
+
// hoists to the root, and the children it could not hoist, to go on into.
|
|
222
|
+
function hoistNodeDependencies(state, nodePath, parentNode, newNodes) {
|
|
223
|
+
const { rootNode, usedDependencies, hoistIdents, parentShadowedNodes, shadowedNodes, options, step } = state
|
|
224
|
+
const fullPath = [rootNode, ...nodePath, parentNode]
|
|
225
|
+
const dependantTree = new Map()
|
|
226
|
+
const hoistInfos = new Map()
|
|
227
|
+
for (const subDependency of getSortedRegularDependencies(parentNode, step)) {
|
|
228
|
+
step(fullPath.length + subDependency.peerNames.size + subDependency.hoistedDependencies.size)
|
|
229
|
+
const hoistInfo = getNodeHoistInfo(rootNode, fullPath, subDependency, usedDependencies, hoistIdents, shadowedNodes, options.fastLookupPossible)
|
|
230
|
+
hoistInfos.set(subDependency, hoistInfo)
|
|
231
|
+
if (hoistInfo.isHoistable === DEPENDS) {
|
|
232
|
+
for (const node of hoistInfo.dependsOn) {
|
|
233
|
+
const dependants = dependantTree.get(node.name) || new Set()
|
|
234
|
+
dependants.add(subDependency.name)
|
|
235
|
+
dependantTree.set(node.name, dependants)
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
// What cannot go, and what waits for it to.
|
|
240
|
+
const unhoistable = [...hoistInfos].filter(([, hoistInfo]) => hoistInfo.isHoistable === NO).map(([node]) => node)
|
|
241
|
+
const unhoistableNodes = reachable(unhoistable, (node) => [...dependantTree.get(node.name) ?? []].map((name) => parentNode.dependencies.get(name)))
|
|
242
|
+
for (const node of hoistInfos.keys()) {
|
|
243
|
+
if (unhoistableNodes.has(node)) continue
|
|
244
|
+
state.isGraphChanged = true
|
|
245
|
+
if (parentShadowedNodes.get(parentNode)?.has(node.name)) state.anotherRoundNeeded = true
|
|
246
|
+
parentNode.dependencies.delete(node.name)
|
|
247
|
+
parentNode.hoistedDependencies.set(node.name, node)
|
|
248
|
+
const hoistedNode = rootNode.dependencies.get(node.name)
|
|
249
|
+
if (hoistedNode) {
|
|
250
|
+
for (const reference of node.references) hoistedNode.references.add(reference)
|
|
251
|
+
} else if (rootNode.ident !== node.ident) {
|
|
252
|
+
rootNode.dependencies.set(node.name, node)
|
|
253
|
+
newNodes.add(node)
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return { unhoistableNodes, children: getSortedRegularDependencies(parentNode, step) }
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// hoistNodeDependencies from `start` down, through what it cannot hoist,
|
|
260
|
+
// each parent seen while what is below it is.
|
|
261
|
+
function hoistFrom(state, start, aliasedRootNodePathLocators, newNodes) {
|
|
262
|
+
const { seenNodes, step } = state
|
|
263
|
+
const call = (nodePath, aliasedLocatorPath, parentNode) => {
|
|
264
|
+
if (seenNodes.has(parentNode)) return undefined
|
|
265
|
+
step(aliasedLocatorPath.length + 1)
|
|
266
|
+
const nextAliasedLocatorPath = [...aliasedLocatorPath, getAliasedLocator(parentNode)]
|
|
267
|
+
const { unhoistableNodes, children } = hoistNodeDependencies(state, nodePath, parentNode, newNodes)
|
|
268
|
+
return { nodePath, nextAliasedLocatorPath, parentNode, unhoistableNodes, items: children.values() }
|
|
269
|
+
}
|
|
270
|
+
const into = (node, frame) => {
|
|
271
|
+
step(frame.nextAliasedLocatorPath.length)
|
|
272
|
+
if (!frame.unhoistableNodes.has(node) || frame.nextAliasedLocatorPath.includes(getAliasedLocator(node))) return undefined
|
|
273
|
+
seenNodes.add(frame.parentNode)
|
|
274
|
+
const next = call([...frame.nodePath, frame.parentNode], frame.nextAliasedLocatorPath, decoupleGraphNode(frame.parentNode, node, step))
|
|
275
|
+
if (next === undefined) seenNodes.delete(frame.parentNode)
|
|
276
|
+
return next
|
|
277
|
+
}
|
|
278
|
+
const first = call([], aliasedRootNodePathLocators, start)
|
|
279
|
+
if (first === undefined) return
|
|
280
|
+
depthFirst(first, into, ({ nodePath }) => {
|
|
281
|
+
if (nodePath.length > 0) seenNodes.delete(nodePath.at(-1))
|
|
282
|
+
})
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// One pass of hoisting to the root of `rootNodePath`, from each package that
|
|
286
|
+
// newly reached it, down through what stays where it is.
|
|
287
|
+
export function hoistGraph(state) {
|
|
288
|
+
const { rootNode, rootNodePath, step } = state
|
|
289
|
+
let nextNewNodes = new Set(getSortedRegularDependencies(rootNode, step))
|
|
290
|
+
const aliasedRootNodePathLocators = rootNodePath.map(getAliasedLocator)
|
|
291
|
+
do {
|
|
292
|
+
const newNodes = nextNewNodes
|
|
293
|
+
nextNewNodes = new Set()
|
|
294
|
+
for (const dep of newNodes) {
|
|
295
|
+
if (dep.locator === rootNode.locator) continue
|
|
296
|
+
hoistFrom(state, decoupleGraphNode(rootNode, dep, step), aliasedRootNodePathLocators, nextNewNodes)
|
|
297
|
+
}
|
|
298
|
+
} while (nextNewNodes.size > 0)
|
|
299
|
+
}
|