@preventive/deptree 1.0.0-alpha.0 → 1.0.0-alpha.10

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.
Files changed (89) hide show
  1. package/LICENSE-APACHE +202 -0
  2. package/bin/compare.js +132 -0
  3. package/bin/deptree.js +211 -0
  4. package/bin/npmrc.js +21 -0
  5. package/cargo.d.ts +223 -0
  6. package/cargo.js +6 -0
  7. package/npm.d.ts +321 -0
  8. package/npm.js +7 -0
  9. package/package.json +93 -11
  10. package/pnpm.d.ts +422 -60
  11. package/pnpm.js +3 -8
  12. package/soldeer.d.ts +235 -0
  13. package/soldeer.js +6 -0
  14. package/src/cargo/crate.js +183 -0
  15. package/src/cargo/inputs.js +81 -0
  16. package/src/cargo/members.js +90 -0
  17. package/src/cargo/registry.js +65 -0
  18. package/src/cargo/tree.js +125 -0
  19. package/src/cargo/workspace.js +177 -0
  20. package/src/concurrent.js +32 -0
  21. package/src/error.js +8 -10
  22. package/src/glob.js +55 -0
  23. package/src/hash.js +51 -16
  24. package/src/manifest.js +52 -0
  25. package/src/matcher.js +60 -10
  26. package/src/mount.js +128 -39
  27. package/src/npm/bins.js +54 -0
  28. package/src/npm/compat.js +97 -0
  29. package/src/npm/dev-engines.js +52 -0
  30. package/src/npm/graph.js +68 -0
  31. package/src/npm/inputs.js +75 -0
  32. package/src/npm/manifests.js +130 -0
  33. package/src/npm/package.js +57 -0
  34. package/src/npm/ratio.js +34 -0
  35. package/src/npm/settings.js +67 -0
  36. package/src/npm/tree.js +139 -0
  37. package/src/npm/versions.js +20 -0
  38. package/src/npmrc.js +24 -0
  39. package/src/patch.js +26 -45
  40. package/src/pnpm/bins.js +207 -327
  41. package/src/pnpm/checks.js +8 -35
  42. package/src/pnpm/commands.js +230 -0
  43. package/src/pnpm/filename.js +19 -16
  44. package/src/pnpm/frozen.js +47 -81
  45. package/src/pnpm/gitignore.js +138 -0
  46. package/src/pnpm/glob.js +65 -0
  47. package/src/pnpm/graph.js +26 -34
  48. package/src/pnpm/hoist.js +97 -59
  49. package/src/pnpm/hoisted.js +234 -0
  50. package/src/pnpm/hook.js +37 -51
  51. package/src/pnpm/inputs.js +177 -0
  52. package/src/pnpm/install.js +140 -130
  53. package/src/pnpm/local.js +105 -34
  54. package/src/pnpm/minimatch.js +319 -0
  55. package/src/pnpm/nm-hoist-graph.js +299 -0
  56. package/src/pnpm/nm-hoist.js +149 -0
  57. package/src/pnpm/npm-packlist.js +217 -0
  58. package/src/pnpm/overrides.js +20 -53
  59. package/src/pnpm/package.js +123 -0
  60. package/src/pnpm/packlist.js +194 -0
  61. package/src/pnpm/patches.js +21 -28
  62. package/src/pnpm/projects.js +150 -101
  63. package/src/pnpm/readers.js +122 -77
  64. package/src/pnpm/settings.js +85 -93
  65. package/src/pnpm/tree.js +234 -188
  66. package/src/pnpm/uptodate.js +17 -29
  67. package/src/pnpm/workspace.js +210 -53
  68. package/src/project.js +56 -0
  69. package/src/soldeer/config.js +63 -0
  70. package/src/soldeer/git.js +52 -0
  71. package/src/soldeer/inputs.js +30 -0
  72. package/src/soldeer/tree.js +83 -0
  73. package/src/soldeer/zip.js +121 -0
  74. package/src/tarball.js +123 -125
  75. package/src/yarn1/bins.js +28 -0
  76. package/src/yarn1/compat.js +67 -0
  77. package/src/yarn1/hoist.js +249 -0
  78. package/src/yarn1/inputs.js +83 -0
  79. package/src/yarn1/manifest.js +64 -0
  80. package/src/yarn1/package.js +206 -0
  81. package/src/yarn1/peers.js +51 -0
  82. package/src/yarn1/requests.js +74 -0
  83. package/src/yarn1/resolve.js +289 -0
  84. package/src/yarn1/settings.js +95 -0
  85. package/src/yarn1/tree.js +219 -0
  86. package/yarn1.d.ts +368 -0
  87. package/yarn1.js +7 -0
  88. package/src/pnpm/npmrc.js +0 -31
  89. /package/{LICENSE → LICENSE-MIT} +0 -0
@@ -0,0 +1,138 @@
1
+ // A gitignore line as pnpm 12 reads one, by the ignore crate (0.4.32) and
2
+ // its glob by globset (0.4.18): matched against a path's UTF-8 bytes, case
3
+ // and all, from the directory of the file it is in. Only `*`, `?` and `**`
4
+ // are followed of glob syntax, as in minimatch.js.
5
+
6
+ import { utf8fromStringLoose } from '@exodus/bytes/utf8.js'
7
+ import { checkGlob, checkLength } from './minimatch.js'
8
+
9
+ // What Rust's trim_end takes off: White_Space.
10
+ const TRAILING = /[\t-\r \u0085\u00A0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000]+$/u
11
+
12
+ // A path as its UTF-8 bytes, as globset matches it.
13
+ export const bytesOf = (path) => utf8fromStringLoose(path)
14
+
15
+ const SLASH = 0x2F
16
+ const NEWLINE = 0x0A
17
+ const notSlash = (byte) => byte !== SLASH
18
+ const notNewline = (byte) => byte !== NEWLINE
19
+ const isSlash = (byte) => byte === SLASH
20
+
21
+ // globset's parse of a glob into tokens: a byte, `?` (ANY), `*` (STAR), and
22
+ // a `**` part, recursive only as a whole part: at the start (PREFIX), the
23
+ // end (SUFFIX), or between two (BETWEEN); a lone `**` is ALL.
24
+ function tokensOf(text) {
25
+ const glob = [...text]
26
+ const tokens = []
27
+ for (let i = 0; i < glob.length; i++) {
28
+ const char = glob[i]
29
+ if (char !== '*') {
30
+ tokens.push(...(char === '?' ? ['ANY'] : bytesOf(char)))
31
+ continue
32
+ }
33
+ if (glob[i + 1] !== '*') {
34
+ tokens.push('STAR')
35
+ continue
36
+ }
37
+ i++
38
+ const next = glob[i + 1]
39
+ if (tokens.length === 0) {
40
+ if (next !== undefined && next !== '/') tokens.push('STAR', 'STAR')
41
+ else {
42
+ tokens.push('PREFIX')
43
+ if (next === '/') i++
44
+ }
45
+ } else if (glob[i - 2] !== '/' || (next !== undefined && next !== '/')) tokens.push('STAR', 'STAR')
46
+ else {
47
+ if (next === '/') i++
48
+ const last = tokens.pop()
49
+ tokens.push(last === 'PREFIX' || last === 'SUFFIX' ? last : next === undefined ? 'SUFFIX' : 'BETWEEN')
50
+ }
51
+ }
52
+ return tokens.length === 1 && tokens[0] === 'PREFIX' ? ['ALL'] : tokens
53
+ }
54
+
55
+ // The regexp globset makes of the tokens, `.` taking any byte but a newline,
56
+ // as an automaton, which matches in time linear in the path and the glob:
57
+ // each state its steps, by what byte to where, and where it goes by none.
58
+ function automatonOf(tokens) {
59
+ const states = [{ steps: [], free: [] }]
60
+ const state = () => states.push({ steps: [], free: [] }) - 1
61
+ let at = 0
62
+ for (const token of tokens) {
63
+ const to = state()
64
+ const { steps, free } = states[at]
65
+ if (typeof token === 'number') steps.push([(byte) => byte === token, to])
66
+ else if (token === 'ANY') steps.push([notSlash, to])
67
+ else if (token === 'STAR' || token === 'ALL') {
68
+ steps.push([token === 'STAR' ? notSlash : notNewline, at])
69
+ free.push(to)
70
+ } else {
71
+ // `/.*` to the end, `/` or `/.*/` between, and `/?` or `.*/` first.
72
+ const loop = state()
73
+ states[loop].steps.push([notNewline, loop])
74
+ if (token === 'SUFFIX') {
75
+ steps.push([isSlash, loop])
76
+ states[loop].free.push(to)
77
+ } else {
78
+ states[loop].steps.push([isSlash, to])
79
+ steps.push([isSlash, to])
80
+ if (token === 'PREFIX') free.push(to, loop)
81
+ else steps.push([isSlash, loop])
82
+ }
83
+ }
84
+ at = to
85
+ }
86
+ const accept = at
87
+ const close = (set) => {
88
+ for (const index of set) for (const to of states[index].free) set.add(to)
89
+ return set
90
+ }
91
+ return (bytes) => {
92
+ let here = close(new Set([0]))
93
+ for (const byte of bytes) {
94
+ const next = new Set()
95
+ for (const index of here) for (const [take, to] of states[index].steps) if (take(byte)) next.add(to)
96
+ if (next.size === 0) return false
97
+ here = close(next)
98
+ }
99
+ return here.has(accept)
100
+ }
101
+ }
102
+
103
+ // The glob of one line, or undefined for a blank one or a comment.
104
+ export function gitignoreGlob(line, where) {
105
+ if (line.startsWith('#')) return undefined
106
+ let text = line.replace(TRAILING, '')
107
+ if (text === '') return undefined
108
+ checkLength(text, where)
109
+ const whitelist = text.startsWith('!')
110
+ if (whitelist) text = text.slice(1)
111
+ const absolute = text.startsWith('/')
112
+ if (absolute) text = text.slice(1)
113
+ const onlyDir = text.endsWith('/')
114
+ if (onlyDir) text = text.slice(0, -1)
115
+ checkGlob(text, where, { parent: true })
116
+ let actual = text
117
+ if (!absolute && !text.includes('/') && !actual.startsWith('**/') && actual !== '**') actual = `**/${actual}`
118
+ if (actual.endsWith('/**')) actual = `${actual}/*`
119
+ return { whitelist, onlyDir, matches: automatonOf(tokensOf(actual)) }
120
+ }
121
+
122
+ // The last glob to match `path`, an only-dir one only a directory: whether
123
+ // it ignores or whitelists, or undefined where none does.
124
+ export function matched(globs, path, isDir) {
125
+ const bytes = bytesOf(path)
126
+ const found = globs.findLast((glob) => (!glob.onlyDir || isDir) && glob.matches(bytes))
127
+ return found === undefined ? undefined : found.whitelist ? 'whitelist' : 'ignore'
128
+ }
129
+
130
+ // That of `path`, or else of the nearest directory above it to match.
131
+ export function matchedOrParents(globs, path, isDir) {
132
+ const parts = path.split('/')
133
+ for (let i = parts.length; i >= 0; i--) {
134
+ const found = matched(globs, parts.slice(0, i).join('/'), i === parts.length ? isDir : true)
135
+ if (found !== undefined) return found
136
+ }
137
+ return undefined
138
+ }
@@ -0,0 +1,65 @@
1
+ // glob 8 as npm-packlist 5 globs a `files` pattern for pnpm 9 and 10: dot
2
+ // and nocase, a leading `!` or `#` taken as it is. Each path found is spelled
3
+ // as glob spells it, a directory's with the trailing slash a pattern gave
4
+ // it, and a file named by a pattern with no part to match against names
5
+ // found alone where it is there.
6
+
7
+ import { GLOBSTAR, OBJECT_5, checkGlob, checkLength, checkParts, readPart } from './minimatch.js'
8
+
9
+ // npm-packlist globs `[object Object]` for a `browser` or `main` that is an
10
+ // object.
11
+ function globParts(pattern, where) {
12
+ if (pattern === '[object Object]') return [OBJECT_5]
13
+ checkLength(pattern, where)
14
+ checkGlob(pattern, where)
15
+ const parts = pattern.split(/\/+/u)
16
+ checkParts(pattern, parts, where)
17
+ return parts.map((part) => readPart(part))
18
+ }
19
+
20
+ // `sets` are a pattern's sets of parts, or the pattern. glob walks one
21
+ // place of a pattern in one directory as often as `**` leads it there, and
22
+ // finds what it found before; each is walked once here, with no recursion.
23
+ export function glob(view, sets, where) {
24
+ const found = new Set()
25
+ const walked = new Set()
26
+ const pending = []
27
+ const all = typeof sets === 'string' ? [globParts(sets, where)] : sets
28
+ const add = (prefix, set, at) => {
29
+ const key = `${set}\0${at}\0${prefix === undefined ? '' : `/${prefix}`}`
30
+ if (walked.has(key)) return
31
+ walked.add(key)
32
+ pending.push([prefix, set, at])
33
+ }
34
+ for (const set of all.keys()) add(undefined, set, 0)
35
+ while (pending.length > 0) {
36
+ const [given, set, start] = pending.pop()
37
+ const parts = all[set]
38
+ let at = start
39
+ const strings = given === undefined ? [] : [given]
40
+ while (typeof parts[at] === 'string') strings.push(parts[at++])
41
+ if (at === parts.length) {
42
+ const path = strings.join('/')
43
+ const type = path === '' ? undefined : view.type(path)
44
+ if (type === 'directory' || (type !== undefined && !path.endsWith('/'))) found.add(path)
45
+ continue
46
+ }
47
+ const prefix = strings.length === 0 ? undefined : strings.join('/')
48
+ const entries = view.entries(prefix ?? '.')
49
+ if (entries === undefined) continue
50
+ const under = (entry) => (prefix === undefined ? entry : `${prefix}/${entry}`)
51
+ if (parts[at] === GLOBSTAR) {
52
+ add(prefix, set, at + 1)
53
+ for (const entry of entries) {
54
+ add(under(entry), set, at + 1)
55
+ add(under(entry), set, at)
56
+ }
57
+ continue
58
+ }
59
+ for (const entry of entries.filter((name) => parts[at].test(name))) {
60
+ if (at + 1 === parts.length) found.add(under(entry))
61
+ else add(under(entry), set, at + 1)
62
+ }
63
+ }
64
+ return found
65
+ }
package/src/pnpm/graph.js CHANGED
@@ -1,54 +1,46 @@
1
- // The graph a pnpm 10 install links, as @pnpm/deps.graph-builder builds it
2
- // from a frozen lockfile: a node for each snapshot installed, at
3
- // node_modules/.pnpm/<its directory>/node_modules/<its name>, with its
4
- // dependencies and optional dependencies as children by alias; and, for
5
- // each project, its direct dependencies by alias. A child left out of the
6
- // install is left out of its parent's children too. A `link:` child is the
7
- // directory it names, which is no node. Paths here are relative to the
8
- // lockfile's directory, the root of the tree.
1
+ // The graph a pnpm 9 or 10 install links, as @pnpm/deps.graph-builder builds
2
+ // it from a frozen lockfile. A child left out of the install is left out of its
3
+ // parent's children; a `link:` child is the directory it names.
9
4
 
5
+ import { compareNames } from '@preventive/vfs/path.js'
10
6
  import { DeptreeError, quote } from '../error.js'
11
7
  import { depPathToFilename } from './filename.js'
12
8
 
13
9
  const VIRTUAL_STORE = 'node_modules/.pnpm'
14
10
 
15
- // A target as a path: a node's directory, or where a link leads, which
16
- // may climb out of the lockfile's directory.
17
- function childOf(target, nodes, skipped) {
18
- if (target.startsWith('link:')) return target.slice(5)
19
- if (skipped.has(target)) return undefined
20
- return nodes.get(target).dir
21
- }
11
+ // Every target but a `link:` is a snapshot, as the lockfile reader ensures.
12
+ const childOf = (target, nodes) => (target.startsWith('link:') ? target.slice(5) : nodes.get(target)?.dir)
22
13
 
23
- function childrenOf(targets, nodes, skipped) {
24
- const children = new Map()
25
- for (const [alias, target] of Object.entries(targets)) {
26
- const dir = childOf(target, nodes, skipped)
27
- if (dir !== undefined) children.set(alias, dir)
28
- }
29
- return children
14
+ const childrenOf = (targets, nodes) => new Map(Object.entries(targets).map(([alias, target]) => [alias, childOf(target, nodes)]).filter(([, dir]) => dir !== undefined))
15
+
16
+ // The graph pnpm 12 hoists from, of every snapshot, skipped or not.
17
+ function hoistingOf(lockfile, all) {
18
+ const nodes = new Map([...all.values()].map((node) => [node.dir, { modules: node.modules, children: childrenOf({ ...node.pkg.dependencies, ...node.pkg.optionalDependencies }, all) }]))
19
+ const direct = new Map(Object.keys(lockfile.importers).sort(compareNames).map((id) => {
20
+ const { devDependencies, dependencies, optionalDependencies } = lockfile.importers[id]
21
+ return [id, new Map([...childrenOf({ ...devDependencies, ...dependencies, ...optionalDependencies }, all)].filter(([, dir]) => nodes.has(dir)))]
22
+ }))
23
+ return { nodes, direct }
30
24
  }
31
25
 
32
- // `nodes` by snapshot key, and `direct`, by project, the children each
33
- // project links: devDependencies first, as pnpm spreads them, which is
34
- // the order hoisting walks them in.
26
+ // `direct` has devDependencies first, as pnpm spreads them, for hoisting to
27
+ // walk. pnpm 9 hoists from the lockfile instead, and pnpm 12 from the
28
+ // snapshots, where skipped ones hold directories too.
35
29
  export async function buildGraph(lockfile, skipped, maxLength, major = 10) {
36
- const nodes = new Map()
30
+ const all = new Map()
37
31
  const keyByDir = new Map()
38
32
  for (const [key, pkg] of Object.entries(lockfile.packages)) {
39
- if (skipped.has(key)) continue
33
+ if (major < 12 && skipped.has(key)) continue
40
34
  const store = `${VIRTUAL_STORE}/${await depPathToFilename(key, maxLength, major)}`
41
35
  const dir = `${store}/node_modules/${pkg.name}`
42
36
  if (keyByDir.has(dir)) throw new DeptreeError(`${quote(keyByDir.get(dir))} and ${quote(key)} would be installed in one directory`, quote(dir))
43
37
  keyByDir.set(dir, key)
44
- nodes.set(key, { key, pkg, name: pkg.name, dir, modules: `${store}/node_modules` })
38
+ all.set(key, { key, pkg, name: pkg.name, dir, modules: `${store}/node_modules` })
45
39
  }
40
+ const nodes = new Map([...all].filter(([key]) => !skipped.has(key)))
46
41
  for (const node of nodes.values()) {
47
- node.children = childrenOf({ ...node.pkg.dependencies, ...node.pkg.optionalDependencies }, nodes, skipped)
48
- }
49
- const direct = new Map()
50
- for (const [id, importer] of Object.entries(lockfile.importers)) {
51
- direct.set(id, childrenOf({ ...importer.devDependencies, ...importer.dependencies, ...importer.optionalDependencies }, nodes, skipped))
42
+ node.children = childrenOf({ ...node.pkg.dependencies, ...node.pkg.optionalDependencies }, nodes)
52
43
  }
53
- return { nodes, direct }
44
+ const direct = new Map(Object.entries(lockfile.importers).map(([id, importer]) => [id, childrenOf({ ...importer.devDependencies, ...importer.dependencies, ...importer.optionalDependencies }, nodes)]))
45
+ return { nodes, direct, hoisting: major >= 12 ? hoistingOf(lockfile, all) : major < 10 ? { lockfile, nodes } : undefined }
54
46
  }
package/src/pnpm/hoist.js CHANGED
@@ -1,19 +1,16 @@
1
- // Hoisting as pnpm 10 does it (@pnpm/hoist): every alias in the graph a
2
- // pattern matches gets a link to what it names, in node_modules/.pnpm/
3
- // node_modules for the private pattern and in the root node_modules for
4
- // the public one, which wins where both match. The first to take an alias
5
- // keeps it, and the order is pnpm's: the projects' direct dependencies
6
- // first, then the graph by depth, a node's depth being where pnpm's walk
7
- // first reaches it, and nodes of one depth in the order of their
8
- // directories. An alias is taken with its case folded, and the root
9
- // project's own aliases are taken before anything, as they were spelled.
1
+ // Hoisting as @pnpm/hoist does it: an alias a pattern matches is linked in
2
+ // node_modules (public) or node_modules/.pnpm/node_modules (private), and the
3
+ // first to take it keeps it, in pnpm's order: the projects' direct
4
+ // dependencies, then the graph by depth, then by directory.
10
5
 
6
+ import { compareNames } from '@preventive/vfs/path.js'
7
+ import { DeptreeError, quote } from '../error.js'
11
8
  import { createMatcher } from '../matcher.js'
12
9
 
13
- // pnpm's graphWalker and getDependencies: one level at a time, but each
14
- // level's next is walked out to the bottom before the level's second
15
- // node's next is, so a node's depth is where that walk first reaches it,
16
- // which is not always the least.
10
+ // pnpm's graphWalker: each node's children are walked to the bottom before the
11
+ // next node's, so a node's depth is not always its least. A level's nodes are
12
+ // listed, and all their children stepped to, before the first child's are;
13
+ // on a stack of its own, where pnpm recurses, so a long chain runs out of none.
17
14
  function walk(nodes, starts) {
18
15
  const visited = new Set()
19
16
  const step = (dirs) => {
@@ -26,42 +23,48 @@ function walk(nodes, starts) {
26
23
  }
27
24
  return found
28
25
  }
29
- const levels = (depth, found) => {
30
- const listed = found.map((node) => ({ node, depth }))
31
- const next = found.map((node) => step(node.children.values()))
32
- return [...listed, ...next.flatMap((level) => levels(depth + 1, level))]
26
+ const walked = []
27
+ const level = (depth, found) => {
28
+ for (const node of found) walked.push({ node, depth })
29
+ return { depth: depth + 1, next: found.map((node) => step(node.children.values())), i: 0 }
33
30
  }
34
- return levels(0, step(starts))
31
+ const stack = [level(0, step(starts))]
32
+ while (stack.length > 0) {
33
+ const top = stack.at(-1)
34
+ if (top.i === top.next.length) stack.pop()
35
+ else stack.push(level(top.depth, top.next[top.i++]))
36
+ }
37
+ return walked
35
38
  }
36
39
 
37
40
  // UTF-16 order, as pnpm's lexCompare has it.
38
41
  const lexCompare = (a, b) => (a > b ? 1 : a < b ? -1 : 0)
39
42
 
40
- // The aliases hoisted from the graph, walked from `starts`: a Map of each
41
- // link's path to its target. `taken` holds the names, folded, that none
42
- // may be hoisted by: the root project's own aliases, and, with pnpm 10,
43
- // with `projects` named among them at the top, hoisted last.
44
- function hoistGraph(nodes, starts, taken, typeOf, projects = new Map()) {
45
- const root = new Map([...projects].map(([id, name]) => [name, { project: id }]))
46
- const listed = new Set()
47
- for (const [alias, dir] of starts) {
48
- if (listed.has(alias)) continue
49
- listed.add(alias)
50
- root.set(alias, dir)
51
- }
43
+ // Each alias is looked up folded in `taken`, which gains each one hoisted.
44
+ function hoistGraph(nodes, starts, taken, typeOf, { projects = new Map(), installed = nodes, major = 10 } = {}) {
45
+ const named = new Map([...projects].map(([id, name]) => [name, { project: id }]))
46
+ const root = new Map(major < 12 ? named : [])
47
+ // The first of each alias in `starts`.
48
+ const first = new Map(starts.toReversed())
49
+ for (const [alias] of starts) root.set(alias, first.get(alias))
50
+ // pnpm 12 orders the nodes of one depth by their directories' names.
51
+ const keyOf = major >= 12 ? (node) => node.modules.slice(0, -'/node_modules'.length) : (node) => node.dir
52
52
  const order = [
53
- { children: root, dir: '', depth: -1 },
54
- ...walk(nodes, starts.map(([, dir]) => dir)).map(({ node, depth }) => ({ children: node.children, dir: node.dir, depth })),
53
+ { children: root, key: '', depth: -1 },
54
+ ...major >= 12 ? [{ children: named, key: '', depth: -1 }] : [],
55
+ ...walk(nodes, starts.map(([, dir]) => dir)).map(({ node, depth }) => ({ children: node.children, key: keyOf(node), depth })),
55
56
  ]
56
- order.sort((a, b) => a.depth - b.depth || lexCompare(a.dir, b.dir))
57
+ order.sort((a, b) => a.depth - b.depth || (major >= 12 ? compareNames(a.key, b.key) : lexCompare(a.key, b.key)))
57
58
  const links = new Map()
58
59
  const hoistedProjects = new Map()
59
60
  for (const { children } of order) {
60
61
  for (const [alias, dir] of children) {
61
62
  const where = typeOf(alias)
62
63
  if (where === undefined || taken.has(alias.toLowerCase())) continue
63
- if (typeof dir === 'object') hoistedProjects.set(`${where}/${alias}`, dir.project)
64
- else if (nodes.has(dir)) {
64
+ if (typeof dir === 'object') {
65
+ if (major >= 12) taken.add(alias.toLowerCase())
66
+ hoistedProjects.set(`${where}/${alias}`, dir.project)
67
+ } else if (installed.has(dir)) {
65
68
  taken.add(alias.toLowerCase())
66
69
  links.set(`${where}/${alias}`, dir)
67
70
  }
@@ -71,39 +74,74 @@ function hoistGraph(nodes, starts, taken, typeOf, projects = new Map()) {
71
74
  return links
72
75
  }
73
76
 
74
- // Where each hoisted alias is linked, and to which directory: a Map of
75
- // the link's path to its target, both relative to the lockfile's
76
- // directory. `nodes` is the graph by directory; `direct` its projects'
77
- // direct dependencies, the root project's under `.`; `projects` the
78
- // workspace's named projects by directory, where hoistWorkspacePackages
79
- // has them hoisted by name too.
80
- //
81
- // pnpm 10 puts those names first among the root's, a direct dependency of
82
- // one name replacing it. Hoisting a project does not take its name, so a
83
- // package of that name may be hoisted to the same place too; there the
84
- // project wins, as pnpm replaces a link into the store with it and leaves
85
- // its own in place of one. It takes the root project's aliases as they are
86
- // spelled, and folds the case of those it hoists.
87
- //
88
- // pnpm 11 folds the root project's aliases too. It hoists the graph, then
89
- // each project by its name where no project's direct dependency in the
90
- // graph has that name, and then the graph again with those names taken
91
- // from it; a project hoisted is not held back by a `link:` of its name, or
92
- // by a package hoisted by it. (It holds back two projects of which one's
93
- // name is a directory of the other's, which no two names a package can
94
- // have are.)
95
- export function hoist(nodes, direct, { hoistPattern, publicHoistPattern }, projects = new Map(), major = 10) {
77
+ // pnpm 9's hoistGraph walks the lockfile from every project's direct
78
+ // dependencies, the first project to list an alias keeping it. A `link:` or
79
+ // skipped child takes its alias though nothing is linked by it, as does a
80
+ // project, and the root's specifiers are taken from the start. Snapshots of
81
+ // one depth are taken in the order of their keys.
82
+ function hoist9({ lockfile, nodes }, typeOf, projects) {
83
+ const root = Object.create(null)
84
+ for (const [id, name] of projects) root[name] = { project: id }
85
+ const direct = Object.create(null)
86
+ const starts = []
87
+ for (const importer of Object.values(lockfile.importers)) {
88
+ for (const [alias, target] of Object.entries({ ...importer.devDependencies, ...importer.dependencies, ...importer.optionalDependencies })) {
89
+ if (target.startsWith('link:')) continue
90
+ starts.push(target)
91
+ if (!(alias in direct)) direct[alias] = target
92
+ }
93
+ }
94
+ Object.assign(root, direct)
95
+ const graph = new Map([...nodes].map(([key, { pkg }]) => [key, { key, children: new Map(Object.entries({ ...pkg.dependencies, ...pkg.optionalDependencies })) }]))
96
+ const order = [{ children: Object.entries(root), key: '', depth: -1 }, ...walk(graph, starts).map(({ node, depth }) => ({ children: node.children, key: node.key, depth }))]
97
+ order.sort((a, b) => a.depth - b.depth || lexCompare(a.key, b.key))
98
+ const taken = new Set(Object.keys(lockfile.importers['.']?.specifiers ?? {}))
99
+ const links = new Map()
100
+ for (const { children } of order) {
101
+ for (const [alias, target] of children) {
102
+ const where = typeOf(alias)
103
+ if (where === undefined || taken.has(alias.toLowerCase())) continue
104
+ taken.add(alias.toLowerCase())
105
+ if (target?.project !== undefined) links.set(`${where}/${alias}`, target.project)
106
+ else if (nodes.has(target)) links.set(`${where}/${alias}`, nodes.get(target).dir)
107
+ }
108
+ }
109
+ return links
110
+ }
111
+
112
+ // `projects` are the named projects by directory, which hoistWorkspacePackages
113
+ // hoists by name too. pnpm 10 lists their names first among the root's aliases,
114
+ // a direct dependency of the same name replacing one; a hoisted project takes
115
+ // no name, and wins over a package hoisted to its place, as pnpm replaces a
116
+ // link into the store with it but not its own with one. pnpm 10 takes the
117
+ // root's aliases as spelled; pnpm 11 folds them too, and hoists a project
118
+ // unless a project's direct dependency in the graph has its name. pnpm 12 walks
119
+ // `hoisting`, through the skipped snapshots though it hoists none of them.
120
+ export function hoist(nodes, direct, { hoistPattern, publicHoistPattern }, projects = new Map(), major = 10, hoisting) {
96
121
  if (hoistPattern === undefined && publicHoistPattern === undefined) return new Map()
97
122
  const isPublic = createMatcher(publicHoistPattern ?? [])
98
123
  const isPrivate = createMatcher(hoistPattern ?? [])
99
124
  const typeOf = (alias) => (isPublic(alias) ? 'node_modules' : isPrivate(alias) ? 'node_modules/.pnpm/node_modules' : undefined)
125
+ if (major < 10) return hoist9(hoisting, typeOf, projects)
126
+ if (major >= 12) {
127
+ const folded = new Map()
128
+ for (const [id, name] of projects) {
129
+ if (typeOf(name) === undefined) continue
130
+ const key = name.toLowerCase()
131
+ if (folded.has(key)) throw new DeptreeError(`its name and ${quote(folded.get(key))}'s are one with their case folded, of which pnpm 12 hoists one by an order not known here`, `manifests[${quote(id)}].name`)
132
+ folded.set(key, id)
133
+ }
134
+ const starts = [...hoisting.direct.values()].flatMap((children) => [...children])
135
+ const taken = new Set([...hoisting.direct.get('.')?.keys() ?? []].map((alias) => alias.toLowerCase()))
136
+ return hoistGraph(hoisting.nodes, starts, taken, typeOf, { projects, installed: nodes, major })
137
+ }
100
138
  const starts = [...direct.values()].flatMap((children) => [...children].filter(([, dir]) => nodes.has(dir)))
101
139
  const rootAliases = [...direct.get('.')?.keys() ?? []]
102
- if (major < 11) return hoistGraph(nodes, starts, new Set(rootAliases), typeOf, projects)
140
+ if (major < 11) return hoistGraph(nodes, starts, new Set(rootAliases), typeOf, { projects })
103
141
  const takenByDependencies = new Set(starts.map(([alias]) => alias.toLowerCase()))
104
142
  const hoistedProjects = [...projects].filter(([, name]) => typeOf(name) !== undefined && !takenByDependencies.has(name.toLowerCase()))
105
143
  const taken = new Set([...rootAliases, ...hoistedProjects.map(([, name]) => name)].map((alias) => alias.toLowerCase()))
106
- const links = nodes.size === 0 ? new Map() : hoistGraph(nodes, starts, taken, typeOf)
144
+ const links = hoistGraph(nodes, starts, taken, typeOf)
107
145
  for (const [id, name] of hoistedProjects) links.set(`${typeOf(name)}/${name}`, id)
108
146
  return links
109
147
  }