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

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 (75) 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 +36 -10
  5. package/pnpm.d.ts +116 -22
  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 +64 -13
  13. package/src/manifest.js +52 -0
  14. package/src/matcher.js +59 -10
  15. package/src/mount.js +95 -44
  16. package/src/npm/bins.js +71 -0
  17. package/src/npm/compat.js +92 -0
  18. package/src/npm/dev-engines.js +52 -0
  19. package/src/npm/graph.js +68 -0
  20. package/src/npm/inputs.js +80 -0
  21. package/src/npm/manifests.js +138 -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/{pnpm/npmrc.js → npmrc.js} +5 -11
  28. package/src/patch.js +21 -31
  29. package/src/pnpm/bins.js +32 -103
  30. package/src/pnpm/checks.js +8 -35
  31. package/src/pnpm/commands.js +49 -86
  32. package/src/pnpm/filename.js +10 -21
  33. package/src/pnpm/frozen.js +27 -70
  34. package/src/pnpm/gitignore.js +137 -0
  35. package/src/pnpm/glob.js +65 -0
  36. package/src/pnpm/graph.js +15 -43
  37. package/src/pnpm/hoist.js +65 -55
  38. package/src/pnpm/hook.js +26 -31
  39. package/src/pnpm/inputs.js +67 -40
  40. package/src/pnpm/install.js +72 -115
  41. package/src/pnpm/local.js +21 -40
  42. package/src/pnpm/minimatch.js +328 -0
  43. package/src/pnpm/npm-packlist.js +221 -0
  44. package/src/pnpm/overrides.js +10 -49
  45. package/src/pnpm/package.js +120 -0
  46. package/src/pnpm/packlist.js +168 -109
  47. package/src/pnpm/patches.js +18 -25
  48. package/src/pnpm/projects.js +63 -107
  49. package/src/pnpm/readers.js +46 -55
  50. package/src/pnpm/settings.js +60 -97
  51. package/src/pnpm/tree.js +134 -149
  52. package/src/pnpm/uptodate.js +16 -28
  53. package/src/pnpm/workspace.js +122 -131
  54. package/src/project.js +41 -0
  55. package/src/soldeer/config.js +8 -24
  56. package/src/soldeer/inputs.js +2 -5
  57. package/src/soldeer/tree.js +20 -56
  58. package/src/soldeer/zip.js +17 -36
  59. package/src/tarball.js +38 -142
  60. package/src/yarn1/bins.js +9 -19
  61. package/src/yarn1/compat.js +9 -28
  62. package/src/yarn1/hoist.js +53 -68
  63. package/src/yarn1/inputs.js +16 -51
  64. package/src/yarn1/manifest.js +8 -23
  65. package/src/yarn1/package.js +24 -63
  66. package/src/yarn1/peers.js +8 -15
  67. package/src/yarn1/requests.js +19 -37
  68. package/src/yarn1/resolve.js +35 -111
  69. package/src/yarn1/settings.js +14 -41
  70. package/src/yarn1/tree.js +67 -91
  71. package/yarn1.d.ts +30 -2
  72. package/yarn1.js +1 -8
  73. package/src/pnpm/project.js +0 -97
  74. package/src/yarn1/glob.js +0 -52
  75. /package/{LICENSE → LICENSE-MIT} +0 -0
@@ -1,15 +1,11 @@
1
- // What pnpm 11's frozen install holds each project to beyond what pnpm
2
- // 10's does (@pnpm/lockfile.verification): specifiers that name one git
3
- // repository and commit alike are the same; a catalog dependency has to
4
- // have resolved to the version the lockfile's catalog records; and a
5
- // dependency on a workspace package has to be linked exactly where the
6
- // package's version is in its range — which pnpm 10 checks only when it
7
- // resolves.
1
+ // What pnpm 11's frozen install checks of each project that pnpm 10 checks only
2
+ // when it resolves (@pnpm/lockfile.verification).
8
3
 
9
4
  import { packageKeyOf } from '@preventive/lockfile/pnpm.js'
10
5
  import { satisfies, valid, validRange } from '@preventive/upstream/semver.js'
11
6
  import { join } from '@preventive/vfs/path.js'
12
7
  import { DeptreeError, quote } from '../error.js'
8
+ import { own } from '../manifest.js'
13
9
  import { catalogEntry, catalogOf } from './overrides.js'
14
10
 
15
11
  const GIT_HOSTS = new Set(['github.com', 'gitlab.com', 'bitbucket.org'])
@@ -22,8 +18,7 @@ function gitUrl(host, path, committish) {
22
18
  return `git+https://${host}/${repository}.git${committish ? `#${committish}` : ''}`
23
19
  }
24
20
 
25
- // A git specifier as one URL, as gitSpecifiersAreEquivalent spells it, or
26
- // undefined for one that is not.
21
+ // A git specifier as one URL, as gitSpecifiersAreEquivalent spells it.
27
22
  function gitSpecifier(specifier) {
28
23
  const hash = specifier.indexOf('#')
29
24
  const committish = hash === -1 ? undefined : specifier.slice(hash + 1)
@@ -47,9 +42,7 @@ function gitSpecifier(specifier) {
47
42
  return gitUrl(host, path, committish)
48
43
  }
49
44
 
50
- // dependencySpecifiersAreEqual: pnpm 11 takes two specifiers of one git
51
- // repository and commit, however spelled, to be the same; pnpm 10 only
52
- // two spelled the same.
45
+ // dependencySpecifiersAreEqual: pnpm 11 matches git specifiers however spelled.
53
46
  export function sameSpecifier(a, b, major) {
54
47
  if (a === b) return true
55
48
  if (major < 11 || a === undefined || b === undefined) return false
@@ -57,9 +50,7 @@ export function sameSpecifier(a, b, major) {
57
50
  return git !== undefined && git === gitSpecifier(b)
58
51
  }
59
52
 
60
- // The version an importer's target resolved to, as the lockfile spells
61
- // it, peers and all left out: a key of the alias's own name is its
62
- // version.
53
+ // A target under the alias's own name is spelled as its version alone.
63
54
  export const resolvedOf = (alias, target) => packageKeyOf(target.startsWith(`${alias}@`) ? target.slice(alias.length + 1) : target)
64
55
 
65
56
  const targetOf = (importer, alias) => importer.dependencies[alias] ?? importer.devDependencies[alias] ?? importer.optionalDependencies[alias]
@@ -77,9 +68,7 @@ export function checkCatalogResolutions(importer, catalogs, where) {
77
68
  }
78
69
  }
79
70
 
80
- // A path pnpm resolves from a directory, `./` and all, relative to the
81
- // lockfile's directory as the lockfile's links are; one from the home
82
- // directory or the root cannot be told here.
71
+ // Relative to the lockfile's directory, as the lockfile's links are.
83
72
  function specPath(dir, path, where) {
84
73
  const clean = path.startsWith('./') ? path.slice(2) : path
85
74
  if (/^(?:~[/\\]|[/\\]|[A-Za-z]:)/u.test(clean) || clean.includes('\\')) throw new DeptreeError(`${quote(path)} is not a path from the project, which is not supported`, where)
@@ -87,40 +76,22 @@ function specPath(dir, path, where) {
87
76
  }
88
77
 
89
78
  const isWorkspacePath = (spec) => /^(?:[./\\]|~[/\\]|[A-Za-z]:)/u.test(spec)
90
- // Of two paths in the lockfile's normal form, whether one is the other or
91
- // in it.
92
79
  const within = (parent, child) => child === parent || (parent === '.' ? child !== '..' && !child.startsWith('../') : child.startsWith(`${parent}/`))
93
80
 
94
- // The name of the package a specifier asks for, under `alias`.
95
- export function targetName(spec, alias) {
81
+ export function parseSpec(spec, alias) {
96
82
  if (spec.startsWith('workspace:')) {
97
83
  const raw = spec.slice('workspace:'.length)
98
84
  const at = raw.lastIndexOf('@')
99
- return isWorkspacePath(raw) || at <= 0 ? alias : raw.slice(0, at)
85
+ return { name: isWorkspacePath(raw) || at <= 0 ? alias : raw.slice(0, at), range: at > 0 ? raw.slice(at + 1) || '*' : raw }
100
86
  }
101
87
  if (spec.startsWith('npm:')) {
102
88
  const raw = spec.slice('npm:'.length)
103
- if (validRange(raw) !== null) return alias
104
- const at = raw.lastIndexOf('@')
105
- return at > 0 ? raw.slice(0, at) : raw
106
- }
107
- return alias
108
- }
109
-
110
- // The range of the version a specifier asks for.
111
- export function versionRange(spec) {
112
- if (spec.startsWith('workspace:')) {
113
- const raw = spec.slice('workspace:'.length)
114
- const at = raw.lastIndexOf('@')
115
- return at > 0 ? raw.slice(at + 1) || '*' : raw
116
- }
117
- if (spec.startsWith('npm:')) {
118
- const raw = spec.slice('npm:'.length)
119
- if (validRange(raw) !== null) return raw
120
- const at = raw.indexOf('@', 1)
121
- return at === -1 ? '*' : raw.slice(at + 1) || '*'
89
+ if (validRange(raw) !== null) return { name: alias, range: raw }
90
+ const last = raw.lastIndexOf('@')
91
+ const first = raw.indexOf('@', 1)
92
+ return { name: last > 0 ? raw.slice(0, last) : raw, range: first === -1 ? '*' : raw.slice(first + 1) || '*' }
122
93
  }
123
- return spec
94
+ return { name: alias, range: spec }
124
95
  }
125
96
 
126
97
  const inRange = (version, range) => range === '*' || range === '^' || range === '~' || (typeof version === 'string' && satisfies(version, range, { loose: true }))
@@ -130,27 +101,21 @@ const isTag = (range) => valid(range, { loose: true }) === null && validRange(ra
130
101
 
131
102
  export const KINDS = ['optionalDependencies', 'dependencies', 'devDependencies']
132
103
 
133
- // The directory a specifier names, where it names one: by `link:`,
134
- // `file:`, a `workspace:` path, or a path alone, as pnpm reads one. One
135
- // led by a backslash, a path to pnpm on Windows alone, is taken for one
136
- // too, for specPath to refuse, as a lockfile pnpm writes elsewhere never
137
- // links it.
104
+ // A path led by a backslash, a path only on Windows, is taken too for specPath
105
+ // to refuse, as a lockfile pnpm writes elsewhere never links it.
138
106
  function pathOf(spec) {
139
107
  if (spec.startsWith('link:') || spec.startsWith('file:')) return spec.slice(5)
140
108
  const path = spec.startsWith('workspace:') ? spec.slice('workspace:'.length) : spec
141
109
  return isWorkspacePath(path) ? path : undefined
142
110
  }
143
111
 
144
- // A dependency the lockfile links has to be linked where the project's
145
- // package.json, read through the read-package hook, names a directory for
146
- // it, as a lockfile pnpm writes always has it: pnpm 11 holds a `link:` or
147
- // `workspace:` path to that, and pnpm 10, or pnpm 11 a path alone, would
148
- // link whatever the lockfile says. A `file:` one is pnpm's to link, where
149
- // it does.
112
+ // Stricter than pnpm, which checks only a `link:` or `workspace:` path, and
113
+ // only from pnpm 11: a lockfile pnpm writes always links where the hooked
114
+ // package.json names a directory.
150
115
  export function checkLinkTargets({ id, manifest, importer }, where) {
151
116
  for (const kind of KINDS) {
152
117
  for (const [alias, target] of Object.entries(importer[kind])) {
153
- const spec = manifest[kind]?.[alias]
118
+ const spec = own(manifest[kind], alias)
154
119
  if (!spec || !target.startsWith('link:') || importer.specifiers[alias].startsWith('file:')) continue
155
120
  const path = pathOf(spec)
156
121
  const linkedTo = target.slice('link:'.length)
@@ -161,9 +126,7 @@ export function checkLinkTargets({ id, manifest, importer }, where) {
161
126
  }
162
127
  }
163
128
 
164
- // Every project's package.json, as given, by its directory, and by where
165
- // pnpm 11 links it from, its publishConfig.directory among them; and the
166
- // directory of each by name and version, of which no two may be one.
129
+ // pnpm 11 links a project from its publishConfig.directory too.
167
130
  export function indexProjects(projects) {
168
131
  const byName = new Map()
169
132
  const byDir = new Map()
@@ -180,29 +143,23 @@ export function indexProjects(projects) {
180
143
  return { projects, byName, byDir }
181
144
  }
182
145
 
183
- // checkLinkedPackagesAreUpToDate, less the directories a package.json
184
- // names, which checkLinkTargets holds links to: `manifest` is the
185
- // project's package.json as read through the read-package hook, `index`
186
- // indexProjects's, and `linkWorkspacePackages` the setting.
146
+ // checkLinkedPackagesAreUpToDate on the hooked package.json, less the
147
+ // directories it names, which checkLinkTargets checks.
187
148
  export function checkLinkedPackages({ manifest, importer, index: { projects, byName, byDir }, linkWorkspacePackages }, where) {
188
149
  const outdated = (detail) => new DeptreeError(`the lockfile is not up to date with this package.json, which pnpm 11 refuses a frozen install for: ${detail}`, where)
189
150
  for (const kind of KINDS) {
190
151
  const wanted = manifest[kind]
191
152
  if (wanted == null) continue
192
153
  for (const [alias, target] of Object.entries(importer[kind])) {
193
- const spec = wanted[alias]
154
+ const spec = own(wanted, alias)
194
155
  if (!spec) continue
195
156
  const here = `${where}.${kind}.${alias}`
196
157
  const workspaceRange = spec.startsWith('workspace:') && !isWorkspacePath(spec.slice('workspace:'.length))
197
158
  const linked = target.startsWith('link:')
198
- // A local directory or tarball is up to date where the lockfile has
199
- // it, as pnpm 11's frozen install skips its own dependencies; which
200
- // is installed is held to tree.js's checkSource.
201
- const local = importer.specifiers[alias].startsWith('file:') || packageKeyOf(target).includes('@file:')
202
- if (local) continue
159
+ // pnpm 11's frozen install skips a local directory or tarball.
160
+ if (importer.specifiers[alias].startsWith('file:') || packageKeyOf(target).includes('@file:')) continue
203
161
  if (linked && pathOf(spec) !== undefined) continue
204
- const name = targetName(spec, alias)
205
- const range = versionRange(spec)
162
+ const { name, range } = parseSpec(spec, alias)
206
163
  if (linked && isTag(range)) continue
207
164
  const named = byName.get(name)
208
165
  const dir = linked ? target.slice('link:'.length) : named?.get(resolvedOf(alias, target))
@@ -0,0 +1,137 @@
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
+ // A path as its UTF-8 bytes, as globset matches it.
12
+ export const bytesOf = (path) => new TextEncoder().encode(path)
13
+
14
+ const SLASH = 0x2F
15
+ const NEWLINE = 0x0A
16
+ const notSlash = (byte) => byte !== SLASH
17
+ const notNewline = (byte) => byte !== NEWLINE
18
+ const isSlash = (byte) => byte === SLASH
19
+
20
+ // globset's parse of a glob into tokens: a byte, `?` (ANY), `*` (STAR), and
21
+ // a `**` part, recursive only as a whole part: at the start (PREFIX), the
22
+ // end (SUFFIX), or between two (BETWEEN); a lone `**` is ALL.
23
+ function tokensOf(text) {
24
+ const glob = [...text]
25
+ const tokens = []
26
+ for (let i = 0; i < glob.length; i++) {
27
+ const char = glob[i]
28
+ if (char !== '*') {
29
+ tokens.push(...(char === '?' ? ['ANY'] : bytesOf(char)))
30
+ continue
31
+ }
32
+ if (glob[i + 1] !== '*') {
33
+ tokens.push('STAR')
34
+ continue
35
+ }
36
+ i++
37
+ const next = glob[i + 1]
38
+ if (tokens.length === 0) {
39
+ if (next !== undefined && next !== '/') tokens.push('STAR', 'STAR')
40
+ else {
41
+ tokens.push('PREFIX')
42
+ if (next === '/') i++
43
+ }
44
+ } else if (glob[i - 2] !== '/' || (next !== undefined && next !== '/')) tokens.push('STAR', 'STAR')
45
+ else {
46
+ if (next === '/') i++
47
+ const last = tokens.pop()
48
+ tokens.push(last === 'PREFIX' || last === 'SUFFIX' ? last : next === undefined ? 'SUFFIX' : 'BETWEEN')
49
+ }
50
+ }
51
+ return tokens.length === 1 && tokens[0] === 'PREFIX' ? ['ALL'] : tokens
52
+ }
53
+
54
+ // The regexp globset makes of the tokens, `.` taking any byte but a newline,
55
+ // as an automaton, which matches in time linear in the path and the glob:
56
+ // each state its steps, by what byte to where, and where it goes by none.
57
+ function automatonOf(tokens) {
58
+ const states = [{ steps: [], free: [] }]
59
+ const state = () => states.push({ steps: [], free: [] }) - 1
60
+ let at = 0
61
+ for (const token of tokens) {
62
+ const to = state()
63
+ const { steps, free } = states[at]
64
+ if (typeof token === 'number') steps.push([(byte) => byte === token, to])
65
+ else if (token === 'ANY') steps.push([notSlash, to])
66
+ else if (token === 'STAR' || token === 'ALL') {
67
+ steps.push([token === 'STAR' ? notSlash : notNewline, at])
68
+ free.push(to)
69
+ } else {
70
+ // `/.*` to the end, `/` or `/.*/` between, and `/?` or `.*/` first.
71
+ const loop = state()
72
+ states[loop].steps.push([notNewline, loop])
73
+ if (token === 'SUFFIX') {
74
+ steps.push([isSlash, loop])
75
+ states[loop].free.push(to)
76
+ } else {
77
+ states[loop].steps.push([isSlash, to])
78
+ steps.push([isSlash, to])
79
+ if (token === 'PREFIX') free.push(to, loop)
80
+ else steps.push([isSlash, loop])
81
+ }
82
+ }
83
+ at = to
84
+ }
85
+ const accept = at
86
+ const close = (set) => {
87
+ for (const index of set) for (const to of states[index].free) set.add(to)
88
+ return set
89
+ }
90
+ return (bytes) => {
91
+ let here = close(new Set([0]))
92
+ for (const byte of bytes) {
93
+ const next = new Set()
94
+ for (const index of here) for (const [take, to] of states[index].steps) if (take(byte)) next.add(to)
95
+ if (next.size === 0) return false
96
+ here = close(next)
97
+ }
98
+ return here.has(accept)
99
+ }
100
+ }
101
+
102
+ // The glob of one line, or undefined for a blank one or a comment.
103
+ export function gitignoreGlob(line, where) {
104
+ if (line.startsWith('#')) return undefined
105
+ let text = line.replace(TRAILING, '')
106
+ if (text === '') return undefined
107
+ checkLength(text, where)
108
+ const whitelist = text.startsWith('!')
109
+ if (whitelist) text = text.slice(1)
110
+ const absolute = text.startsWith('/')
111
+ if (absolute) text = text.slice(1)
112
+ const onlyDir = text.endsWith('/')
113
+ if (onlyDir) text = text.slice(0, -1)
114
+ checkGlob(text, where, { parent: true })
115
+ let actual = text
116
+ if (!absolute && !text.includes('/') && !actual.startsWith('**/') && actual !== '**') actual = `**/${actual}`
117
+ if (actual.endsWith('/**')) actual = `${actual}/*`
118
+ return { whitelist, onlyDir, matches: automatonOf(tokensOf(actual)) }
119
+ }
120
+
121
+ // The last glob to match `path`, an only-dir one only a directory: whether
122
+ // it ignores or whitelists, or undefined where none does.
123
+ export function matched(globs, path, isDir) {
124
+ const bytes = bytesOf(path)
125
+ const found = globs.findLast((glob) => (!glob.onlyDir || isDir) && glob.matches(bytes))
126
+ return found === undefined ? undefined : found.whitelist ? 'whitelist' : 'ignore'
127
+ }
128
+
129
+ // That of `path`, or else of the nearest directory above it to match.
130
+ export function matchedOrParents(globs, path, isDir) {
131
+ const parts = path.split('/')
132
+ for (let i = parts.length; i >= 0; i--) {
133
+ const found = matched(globs, parts.slice(0, i).join('/'), i === parts.length ? isDir : true)
134
+ if (found !== undefined) return found
135
+ }
136
+ return undefined
137
+ }
@@ -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, part5 } from './minimatch.js'
8
+
9
+ // npm-packlist globs `[object Object]` for a `browser` or `main` that is an
10
+ // object.
11
+ export 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(part5)
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,23 +23,24 @@ 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 : [])
@@ -81,42 +77,56 @@ function hoistGraph(nodes, starts, taken, typeOf, { projects = new Map(), instal
81
77
  return links
82
78
  }
83
79
 
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) {
80
+ // pnpm 9's hoistGraph walks the lockfile from every project's direct
81
+ // dependencies, the first project to list an alias keeping it. A `link:` or
82
+ // skipped child takes its alias though nothing is linked by it, as does a
83
+ // project, and the root's specifiers are taken from the start. Snapshots of
84
+ // one depth are taken in the order of their keys.
85
+ function hoist9({ lockfile, nodes }, typeOf, projects) {
86
+ const root = Object.create(null)
87
+ for (const [id, name] of projects) root[name] = { project: id }
88
+ const direct = Object.create(null)
89
+ const starts = []
90
+ for (const importer of Object.values(lockfile.importers)) {
91
+ for (const [alias, target] of Object.entries({ ...importer.devDependencies, ...importer.dependencies, ...importer.optionalDependencies })) {
92
+ if (target.startsWith('link:')) continue
93
+ starts.push(target)
94
+ if (!(alias in direct)) direct[alias] = target
95
+ }
96
+ }
97
+ Object.assign(root, direct)
98
+ const graph = new Map([...nodes].map(([key, { pkg }]) => [key, { key, children: new Map(Object.entries({ ...pkg.dependencies, ...pkg.optionalDependencies })) }]))
99
+ const order = [{ children: Object.entries(root), key: '', depth: -1 }, ...walk(graph, starts).map(({ node, depth }) => ({ children: node.children, key: node.key, depth }))]
100
+ order.sort((a, b) => a.depth - b.depth || lexCompare(a.key, b.key))
101
+ const taken = new Set(Object.keys(lockfile.importers['.']?.specifiers ?? {}))
102
+ const links = new Map()
103
+ for (const { children } of order) {
104
+ for (const [alias, target] of children) {
105
+ const where = typeOf(alias)
106
+ if (where === undefined || taken.has(alias.toLowerCase())) continue
107
+ taken.add(alias.toLowerCase())
108
+ if (target?.project !== undefined) links.set(`${where}/${alias}`, target.project)
109
+ else if (nodes.has(target)) links.set(`${where}/${alias}`, nodes.get(target).dir)
110
+ }
111
+ }
112
+ return links
113
+ }
114
+
115
+ // `projects` are the named projects by directory, which hoistWorkspacePackages
116
+ // hoists by name too. pnpm 10 lists their names first among the root's aliases,
117
+ // a direct dependency of the same name replacing one; a hoisted project takes
118
+ // no name, and wins over a package hoisted to its place, as pnpm replaces a
119
+ // link into the store with it but not its own with one. pnpm 10 takes the
120
+ // root's aliases as spelled; pnpm 11 folds them too, and hoists a project
121
+ // unless a project's direct dependency in the graph has its name. pnpm 12 walks
122
+ // `hoisting`, through the skipped snapshots though it hoists none of them.
123
+ export function hoist(nodes, direct, { hoistPattern, publicHoistPattern }, projects = new Map(), major = 10, hoisting) {
114
124
  if (hoistPattern === undefined && publicHoistPattern === undefined) return new Map()
115
125
  const isPublic = createMatcher(publicHoistPattern ?? [])
116
126
  const isPrivate = createMatcher(hoistPattern ?? [])
117
127
  const typeOf = (alias) => (isPublic(alias) ? 'node_modules' : isPrivate(alias) ? 'node_modules/.pnpm/node_modules' : undefined)
128
+ if (major < 10) return hoist9(hoisting, typeOf, projects)
118
129
  if (major >= 12) {
119
- // Only a project a pattern takes is one pnpm 12 would hoist.
120
130
  const folded = new Map()
121
131
  for (const [id, name] of projects) {
122
132
  if (typeOf(name) === undefined) continue