@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.
- package/LICENSE-APACHE +202 -0
- package/npm.d.ts +288 -0
- package/npm.js +7 -0
- package/package.json +41 -12
- package/pnpm.d.ts +158 -25
- package/pnpm.js +2 -10
- package/soldeer.d.ts +13 -0
- package/soldeer.js +1 -7
- package/src/concurrent.js +16 -17
- package/src/error.js +5 -9
- package/src/glob.js +55 -0
- package/src/hash.js +60 -13
- package/src/manifest.js +52 -0
- package/src/matcher.js +60 -10
- package/src/mount.js +104 -44
- package/src/npm/bins.js +54 -0
- package/src/npm/compat.js +97 -0
- package/src/npm/dev-engines.js +52 -0
- package/src/npm/graph.js +68 -0
- package/src/npm/inputs.js +75 -0
- package/src/npm/manifests.js +130 -0
- package/src/npm/package.js +57 -0
- package/src/npm/ratio.js +34 -0
- package/src/npm/settings.js +67 -0
- package/src/npm/tree.js +136 -0
- package/src/npm/versions.js +20 -0
- package/src/npmrc.js +24 -0
- package/src/patch.js +22 -41
- package/src/pnpm/bins.js +152 -131
- package/src/pnpm/checks.js +8 -35
- package/src/pnpm/commands.js +53 -96
- package/src/pnpm/filename.js +10 -21
- package/src/pnpm/frozen.js +29 -75
- package/src/pnpm/gitignore.js +139 -0
- package/src/pnpm/glob.js +65 -0
- package/src/pnpm/graph.js +15 -43
- package/src/pnpm/hoist.js +68 -61
- package/src/pnpm/hoisted.js +234 -0
- package/src/pnpm/hook.js +26 -31
- package/src/pnpm/inputs.js +73 -56
- package/src/pnpm/install.js +85 -131
- package/src/pnpm/local.js +21 -40
- package/src/pnpm/minimatch.js +319 -0
- package/src/pnpm/nm-hoist-graph.js +299 -0
- package/src/pnpm/nm-hoist.js +149 -0
- package/src/pnpm/npm-packlist.js +217 -0
- package/src/pnpm/overrides.js +11 -51
- package/src/pnpm/package.js +121 -0
- package/src/pnpm/packlist.js +169 -109
- package/src/pnpm/patches.js +18 -25
- package/src/pnpm/projects.js +63 -107
- package/src/pnpm/readers.js +63 -83
- package/src/pnpm/settings.js +64 -100
- package/src/pnpm/tree.js +197 -169
- package/src/pnpm/uptodate.js +16 -28
- package/src/pnpm/workspace.js +124 -133
- package/src/project.js +57 -0
- package/src/soldeer/config.js +8 -24
- package/src/soldeer/inputs.js +5 -15
- package/src/soldeer/tree.js +22 -60
- package/src/soldeer/zip.js +17 -36
- package/src/tarball.js +55 -140
- package/src/yarn1/bins.js +9 -19
- package/src/yarn1/compat.js +9 -28
- package/src/yarn1/hoist.js +53 -68
- package/src/yarn1/inputs.js +19 -61
- package/src/yarn1/manifest.js +8 -23
- package/src/yarn1/package.js +24 -63
- package/src/yarn1/peers.js +8 -15
- package/src/yarn1/requests.js +19 -37
- package/src/yarn1/resolve.js +36 -113
- package/src/yarn1/settings.js +15 -43
- package/src/yarn1/tree.js +67 -91
- package/yarn1.d.ts +34 -5
- package/yarn1.js +1 -8
- package/src/pnpm/npmrc.js +0 -31
- package/src/pnpm/project.js +0 -97
- package/src/yarn1/glob.js +0 -52
- /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
|
+
}
|
package/src/pnpm/glob.js
ADDED
|
@@ -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
|
|
2
|
-
// from a frozen lockfile
|
|
3
|
-
//
|
|
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
|
-
//
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
// `
|
|
49
|
-
//
|
|
50
|
-
//
|
|
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
|
-
|
|
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
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
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
|
|
16
|
-
//
|
|
17
|
-
//
|
|
18
|
-
//
|
|
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
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
return
|
|
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
|
-
|
|
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
|
-
//
|
|
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
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
-
//
|
|
85
|
-
// the
|
|
86
|
-
//
|
|
87
|
-
//
|
|
88
|
-
//
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
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
|