@preventive/deptree 1.0.0-alpha.5 → 1.0.0-alpha.7

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 (79) hide show
  1. package/LICENSE-APACHE +202 -0
  2. package/npm.d.ts +288 -0
  3. package/npm.js +7 -0
  4. package/package.json +41 -12
  5. package/pnpm.d.ts +158 -25
  6. package/pnpm.js +2 -10
  7. package/soldeer.d.ts +13 -0
  8. package/soldeer.js +1 -7
  9. package/src/concurrent.js +16 -17
  10. package/src/error.js +5 -9
  11. package/src/glob.js +55 -0
  12. package/src/hash.js +60 -13
  13. package/src/manifest.js +52 -0
  14. package/src/matcher.js +60 -10
  15. package/src/mount.js +104 -44
  16. package/src/npm/bins.js +54 -0
  17. package/src/npm/compat.js +97 -0
  18. package/src/npm/dev-engines.js +52 -0
  19. package/src/npm/graph.js +68 -0
  20. package/src/npm/inputs.js +75 -0
  21. package/src/npm/manifests.js +130 -0
  22. package/src/npm/package.js +57 -0
  23. package/src/npm/ratio.js +34 -0
  24. package/src/npm/settings.js +67 -0
  25. package/src/npm/tree.js +136 -0
  26. package/src/npm/versions.js +20 -0
  27. package/src/npmrc.js +24 -0
  28. package/src/patch.js +22 -41
  29. package/src/pnpm/bins.js +152 -131
  30. package/src/pnpm/checks.js +8 -35
  31. package/src/pnpm/commands.js +53 -96
  32. package/src/pnpm/filename.js +10 -21
  33. package/src/pnpm/frozen.js +29 -75
  34. package/src/pnpm/gitignore.js +139 -0
  35. package/src/pnpm/glob.js +65 -0
  36. package/src/pnpm/graph.js +15 -43
  37. package/src/pnpm/hoist.js +68 -61
  38. package/src/pnpm/hoisted.js +234 -0
  39. package/src/pnpm/hook.js +26 -31
  40. package/src/pnpm/inputs.js +73 -56
  41. package/src/pnpm/install.js +85 -131
  42. package/src/pnpm/local.js +21 -40
  43. package/src/pnpm/minimatch.js +319 -0
  44. package/src/pnpm/nm-hoist-graph.js +299 -0
  45. package/src/pnpm/nm-hoist.js +149 -0
  46. package/src/pnpm/npm-packlist.js +217 -0
  47. package/src/pnpm/overrides.js +11 -51
  48. package/src/pnpm/package.js +121 -0
  49. package/src/pnpm/packlist.js +169 -109
  50. package/src/pnpm/patches.js +18 -25
  51. package/src/pnpm/projects.js +63 -107
  52. package/src/pnpm/readers.js +63 -83
  53. package/src/pnpm/settings.js +64 -100
  54. package/src/pnpm/tree.js +197 -169
  55. package/src/pnpm/uptodate.js +16 -28
  56. package/src/pnpm/workspace.js +124 -133
  57. package/src/project.js +57 -0
  58. package/src/soldeer/config.js +8 -24
  59. package/src/soldeer/inputs.js +5 -15
  60. package/src/soldeer/tree.js +22 -60
  61. package/src/soldeer/zip.js +17 -36
  62. package/src/tarball.js +55 -140
  63. package/src/yarn1/bins.js +9 -19
  64. package/src/yarn1/compat.js +9 -28
  65. package/src/yarn1/hoist.js +53 -68
  66. package/src/yarn1/inputs.js +19 -61
  67. package/src/yarn1/manifest.js +8 -23
  68. package/src/yarn1/package.js +24 -63
  69. package/src/yarn1/peers.js +8 -15
  70. package/src/yarn1/requests.js +19 -37
  71. package/src/yarn1/resolve.js +36 -113
  72. package/src/yarn1/settings.js +15 -43
  73. package/src/yarn1/tree.js +67 -91
  74. package/yarn1.d.ts +34 -5
  75. package/yarn1.js +1 -8
  76. package/src/pnpm/npmrc.js +0 -31
  77. package/src/pnpm/project.js +0 -97
  78. package/src/yarn1/glob.js +0 -52
  79. /package/{LICENSE → LICENSE-MIT} +0 -0
@@ -0,0 +1,139 @@
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 { checkGlob, checkLength } from './minimatch.js'
7
+
8
+ // What Rust's trim_end takes off: White_Space.
9
+ const TRAILING = /[\t-\r \u0085\u00A0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000]+$/u
10
+
11
+ const encoder = new TextEncoder()
12
+
13
+ // A path as its UTF-8 bytes, as globset matches it.
14
+ export const bytesOf = (path) => encoder.encode(path)
15
+
16
+ const SLASH = 0x2F
17
+ const NEWLINE = 0x0A
18
+ const notSlash = (byte) => byte !== SLASH
19
+ const notNewline = (byte) => byte !== NEWLINE
20
+ const isSlash = (byte) => byte === SLASH
21
+
22
+ // globset's parse of a glob into tokens: a byte, `?` (ANY), `*` (STAR), and
23
+ // a `**` part, recursive only as a whole part: at the start (PREFIX), the
24
+ // end (SUFFIX), or between two (BETWEEN); a lone `**` is ALL.
25
+ function tokensOf(text) {
26
+ const glob = [...text]
27
+ const tokens = []
28
+ for (let i = 0; i < glob.length; i++) {
29
+ const char = glob[i]
30
+ if (char !== '*') {
31
+ tokens.push(...(char === '?' ? ['ANY'] : bytesOf(char)))
32
+ continue
33
+ }
34
+ if (glob[i + 1] !== '*') {
35
+ tokens.push('STAR')
36
+ continue
37
+ }
38
+ i++
39
+ const next = glob[i + 1]
40
+ if (tokens.length === 0) {
41
+ if (next !== undefined && next !== '/') tokens.push('STAR', 'STAR')
42
+ else {
43
+ tokens.push('PREFIX')
44
+ if (next === '/') i++
45
+ }
46
+ } else if (glob[i - 2] !== '/' || (next !== undefined && next !== '/')) tokens.push('STAR', 'STAR')
47
+ else {
48
+ if (next === '/') i++
49
+ const last = tokens.pop()
50
+ tokens.push(last === 'PREFIX' || last === 'SUFFIX' ? last : next === undefined ? 'SUFFIX' : 'BETWEEN')
51
+ }
52
+ }
53
+ return tokens.length === 1 && tokens[0] === 'PREFIX' ? ['ALL'] : tokens
54
+ }
55
+
56
+ // The regexp globset makes of the tokens, `.` taking any byte but a newline,
57
+ // as an automaton, which matches in time linear in the path and the glob:
58
+ // each state its steps, by what byte to where, and where it goes by none.
59
+ function automatonOf(tokens) {
60
+ const states = [{ steps: [], free: [] }]
61
+ const state = () => states.push({ steps: [], free: [] }) - 1
62
+ let at = 0
63
+ for (const token of tokens) {
64
+ const to = state()
65
+ const { steps, free } = states[at]
66
+ if (typeof token === 'number') steps.push([(byte) => byte === token, to])
67
+ else if (token === 'ANY') steps.push([notSlash, to])
68
+ else if (token === 'STAR' || token === 'ALL') {
69
+ steps.push([token === 'STAR' ? notSlash : notNewline, at])
70
+ free.push(to)
71
+ } else {
72
+ // `/.*` to the end, `/` or `/.*/` between, and `/?` or `.*/` first.
73
+ const loop = state()
74
+ states[loop].steps.push([notNewline, loop])
75
+ if (token === 'SUFFIX') {
76
+ steps.push([isSlash, loop])
77
+ states[loop].free.push(to)
78
+ } else {
79
+ states[loop].steps.push([isSlash, to])
80
+ steps.push([isSlash, to])
81
+ if (token === 'PREFIX') free.push(to, loop)
82
+ else steps.push([isSlash, loop])
83
+ }
84
+ }
85
+ at = to
86
+ }
87
+ const accept = at
88
+ const close = (set) => {
89
+ for (const index of set) for (const to of states[index].free) set.add(to)
90
+ return set
91
+ }
92
+ return (bytes) => {
93
+ let here = close(new Set([0]))
94
+ for (const byte of bytes) {
95
+ const next = new Set()
96
+ for (const index of here) for (const [take, to] of states[index].steps) if (take(byte)) next.add(to)
97
+ if (next.size === 0) return false
98
+ here = close(next)
99
+ }
100
+ return here.has(accept)
101
+ }
102
+ }
103
+
104
+ // The glob of one line, or undefined for a blank one or a comment.
105
+ export function gitignoreGlob(line, where) {
106
+ if (line.startsWith('#')) return undefined
107
+ let text = line.replace(TRAILING, '')
108
+ if (text === '') return undefined
109
+ checkLength(text, where)
110
+ const whitelist = text.startsWith('!')
111
+ if (whitelist) text = text.slice(1)
112
+ const absolute = text.startsWith('/')
113
+ if (absolute) text = text.slice(1)
114
+ const onlyDir = text.endsWith('/')
115
+ if (onlyDir) text = text.slice(0, -1)
116
+ checkGlob(text, where, { parent: true })
117
+ let actual = text
118
+ if (!absolute && !text.includes('/') && !actual.startsWith('**/') && actual !== '**') actual = `**/${actual}`
119
+ if (actual.endsWith('/**')) actual = `${actual}/*`
120
+ return { whitelist, onlyDir, matches: automatonOf(tokensOf(actual)) }
121
+ }
122
+
123
+ // The last glob to match `path`, an only-dir one only a directory: whether
124
+ // it ignores or whitelists, or undefined where none does.
125
+ export function matched(globs, path, isDir) {
126
+ const bytes = bytesOf(path)
127
+ const found = globs.findLast((glob) => (!glob.onlyDir || isDir) && glob.matches(bytes))
128
+ return found === undefined ? undefined : found.whitelist ? 'whitelist' : 'ignore'
129
+ }
130
+
131
+ // That of `path`, or else of the nearest directory above it to match.
132
+ export function matchedOrParents(globs, path, isDir) {
133
+ const parts = path.split('/')
134
+ for (let i = parts.length; i >= 0; i--) {
135
+ const found = matched(globs, parts.slice(0, i).join('/'), i === parts.length ? isDir : true)
136
+ if (found !== undefined) return found
137
+ }
138
+ return undefined
139
+ }
@@ -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,11 +1,6 @@
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
 
10
5
  import { compareNames } from '@preventive/vfs/path.js'
11
6
  import { DeptreeError, quote } from '../error.js'
@@ -13,44 +8,24 @@ import { depPathToFilename } from './filename.js'
13
8
 
14
9
  const VIRTUAL_STORE = 'node_modules/.pnpm'
15
10
 
16
- // A target as a path: where a link leads, which may climb out of the
17
- // lockfile's directory, or the directory of its node in `nodes`; none
18
- // where it has no node, as a snapshot left out of the install has none.
19
- // The lockfile reader holds every other target to be a snapshot.
11
+ // Every target but a `link:` is a snapshot, as the lockfile reader ensures.
20
12
  const childOf = (target, nodes) => (target.startsWith('link:') ? target.slice(5) : nodes.get(target)?.dir)
21
13
 
22
- function childrenOf(targets, nodes) {
23
- const children = new Map()
24
- for (const [alias, target] of Object.entries(targets)) {
25
- const dir = childOf(target, nodes)
26
- if (dir !== undefined) children.set(alias, dir)
27
- }
28
- return children
29
- }
14
+ const childrenOf = (targets, nodes) => new Map(Object.entries(targets).map(([alias, target]) => [alias, childOf(target, nodes)]).filter(([, dir]) => dir !== undefined))
30
15
 
31
- // The graph pnpm 12 hoists from: a node for every snapshot, skipped or
32
- // not, by its directory, its children skipped or not; and each project's
33
- // direct dependencies but its `link:`s, skipped or not, the projects in
34
- // the order of their ids' bytes. `all` holds every snapshot's node.
16
+ // The graph pnpm 12 hoists from, of every snapshot, skipped or not.
35
17
  function hoistingOf(lockfile, all) {
36
- const nodes = new Map()
37
- for (const node of all.values()) {
38
- nodes.set(node.dir, { dir: node.dir, modules: node.modules, children: childrenOf({ ...node.pkg.dependencies, ...node.pkg.optionalDependencies }, all) })
39
- }
40
- const direct = new Map()
41
- for (const id of Object.keys(lockfile.importers).sort(compareNames)) {
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) => {
42
20
  const { devDependencies, dependencies, optionalDependencies } = lockfile.importers[id]
43
- direct.set(id, new Map([...childrenOf({ ...devDependencies, ...dependencies, ...optionalDependencies }, all)].filter(([, dir]) => nodes.has(dir))))
44
- }
21
+ return [id, new Map([...childrenOf({ ...devDependencies, ...dependencies, ...optionalDependencies }, all)].filter(([, dir]) => nodes.has(dir)))]
22
+ }))
45
23
  return { nodes, direct }
46
24
  }
47
25
 
48
- // `nodes` by snapshot key, and `direct`, by project, the children each
49
- // project links: devDependencies first, as pnpm spreads them, which is
50
- // the order hoisting walks them in; and, for pnpm 12, `hoisting`, the
51
- // graph it hoists from. No two snapshots pnpm places may have one
52
- // directory: those it installs, and with pnpm 12, which hoists from them
53
- // too, those it skips.
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.
54
29
  export async function buildGraph(lockfile, skipped, maxLength, major = 10) {
55
30
  const all = new Map()
56
31
  const keyByDir = new Map()
@@ -66,9 +41,6 @@ export async function buildGraph(lockfile, skipped, maxLength, major = 10) {
66
41
  for (const node of nodes.values()) {
67
42
  node.children = childrenOf({ ...node.pkg.dependencies, ...node.pkg.optionalDependencies }, nodes)
68
43
  }
69
- const direct = new Map()
70
- for (const [id, importer] of Object.entries(lockfile.importers)) {
71
- direct.set(id, childrenOf({ ...importer.devDependencies, ...importer.dependencies, ...importer.optionalDependencies }, nodes))
72
- }
73
- return { nodes, direct, hoisting: major >= 12 ? hoistingOf(lockfile, all) : undefined }
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 }
74
46
  }
package/src/pnpm/hoist.js CHANGED
@@ -1,21 +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
 
11
6
  import { compareNames } from '@preventive/vfs/path.js'
12
7
  import { DeptreeError, quote } from '../error.js'
13
8
  import { createMatcher } from '../matcher.js'
14
9
 
15
- // pnpm's graphWalker and getDependencies: one level at a time, but each
16
- // level's next is walked out to the bottom before the level's second
17
- // node's next is, so a node's depth is where that walk first reaches it,
18
- // 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.
19
14
  function walk(nodes, starts) {
20
15
  const visited = new Set()
21
16
  const step = (dirs) => {
@@ -28,32 +23,30 @@ function walk(nodes, starts) {
28
23
  }
29
24
  return found
30
25
  }
31
- const levels = (depth, found) => {
32
- const listed = found.map((node) => ({ node, depth }))
33
- const next = found.map((node) => step(node.children.values()))
34
- 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 }
35
30
  }
36
- 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
37
38
  }
38
39
 
39
40
  // UTF-16 order, as pnpm's lexCompare has it.
40
41
  const lexCompare = (a, b) => (a > b ? 1 : a < b ? -1 : 0)
41
42
 
42
- // The aliases hoisted from the graph, walked from `starts`: a Map of each
43
- // link's path to its target. `taken` holds the names none may be hoisted
44
- // by, each alias looked up in it folded: the root project's own aliases,
45
- // and those hoisted, folded. Of `nodes`, those `installed` may be hoisted.
46
- // With pnpm 10, `projects` are named among the top's, and hoisted last;
47
- // with pnpm 12, each is hoisted once the top's are, and takes its name.
43
+ // Each alias is looked up folded in `taken`, which gains each one hoisted.
48
44
  function hoistGraph(nodes, starts, taken, typeOf, { projects = new Map(), installed = nodes, major = 10 } = {}) {
49
45
  const named = new Map([...projects].map(([id, name]) => [name, { project: id }]))
50
46
  const root = new Map(major < 12 ? named : [])
51
- const listed = new Set()
52
- for (const [alias, dir] of starts) {
53
- if (listed.has(alias)) continue
54
- listed.add(alias)
55
- root.set(alias, dir)
56
- }
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))
57
50
  // pnpm 12 orders the nodes of one depth by their directories' names.
58
51
  const keyOf = major >= 12 ? (node) => node.modules.slice(0, -'/node_modules'.length) : (node) => node.dir
59
52
  const order = [
@@ -81,42 +74,56 @@ function hoistGraph(nodes, starts, taken, typeOf, { projects = new Map(), instal
81
74
  return links
82
75
  }
83
76
 
84
- // Where each hoisted alias is linked, and to which directory: a Map of
85
- // the link's path to its target, both relative to the lockfile's
86
- // directory. `nodes` is the graph by directory; `direct` its projects'
87
- // direct dependencies, the root project's under `.`; `projects` the
88
- // workspace's named projects by directory, where hoistWorkspacePackages
89
- // has them hoisted by name too.
90
- //
91
- // pnpm 10 puts those names first among the root's, a direct dependency of
92
- // one name replacing it. Hoisting a project does not take its name, so a
93
- // package of that name may be hoisted to the same place too; there the
94
- // project wins, as pnpm replaces a link into the store with it and leaves
95
- // its own in place of one. It takes the root project's aliases as they are
96
- // spelled, and folds the case of those it hoists.
97
- //
98
- // pnpm 11 folds the root project's aliases too. It hoists the graph, then
99
- // each project by its name where no project's direct dependency in the
100
- // graph has that name, and then the graph again with those names taken
101
- // from it; a project hoisted is not held back by a `link:` of its name, or
102
- // by a package hoisted by it. (It holds back two projects of which one's
103
- // name is a directory of the other's, which no two names a package can
104
- // have are.)
105
- //
106
- // pnpm 12 hoists from `hoisting`, graph.js's graph of every snapshot,
107
- // skipped or not, which it walks through those skipped, though none of
108
- // them is hoisted; its projects' direct dependencies are taken in the
109
- // order of their ids, and those of the root project but its `link:`s,
110
- // skipped or not, are taken from the start. Of two projects whose names
111
- // are one folded, both of which its patterns take, which it hoists turns
112
- // on an order not known here, which is refused.
113
- export function hoist(nodes, direct, { hoistPattern, publicHoistPattern }, projects = new Map(), major = 10, hoisting = undefined) {
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) {
114
121
  if (hoistPattern === undefined && publicHoistPattern === undefined) return new Map()
115
122
  const isPublic = createMatcher(publicHoistPattern ?? [])
116
123
  const isPrivate = createMatcher(hoistPattern ?? [])
117
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)
118
126
  if (major >= 12) {
119
- // Only a project a pattern takes is one pnpm 12 would hoist.
120
127
  const folded = new Map()
121
128
  for (const [id, name] of projects) {
122
129
  if (typeOf(name) === undefined) continue