@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.
- package/LICENSE-APACHE +202 -0
- package/bin/compare.js +132 -0
- package/bin/deptree.js +211 -0
- package/bin/npmrc.js +21 -0
- package/cargo.d.ts +223 -0
- package/cargo.js +6 -0
- package/npm.d.ts +321 -0
- package/npm.js +7 -0
- package/package.json +93 -11
- package/pnpm.d.ts +422 -60
- package/pnpm.js +3 -8
- package/soldeer.d.ts +235 -0
- package/soldeer.js +6 -0
- package/src/cargo/crate.js +183 -0
- package/src/cargo/inputs.js +81 -0
- package/src/cargo/members.js +90 -0
- package/src/cargo/registry.js +65 -0
- package/src/cargo/tree.js +125 -0
- package/src/cargo/workspace.js +177 -0
- package/src/concurrent.js +32 -0
- package/src/error.js +8 -10
- package/src/glob.js +55 -0
- package/src/hash.js +51 -16
- package/src/manifest.js +52 -0
- package/src/matcher.js +60 -10
- package/src/mount.js +128 -39
- 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 +139 -0
- package/src/npm/versions.js +20 -0
- package/src/npmrc.js +24 -0
- package/src/patch.js +26 -45
- package/src/pnpm/bins.js +207 -327
- package/src/pnpm/checks.js +8 -35
- package/src/pnpm/commands.js +230 -0
- package/src/pnpm/filename.js +19 -16
- package/src/pnpm/frozen.js +47 -81
- package/src/pnpm/gitignore.js +138 -0
- package/src/pnpm/glob.js +65 -0
- package/src/pnpm/graph.js +26 -34
- package/src/pnpm/hoist.js +97 -59
- package/src/pnpm/hoisted.js +234 -0
- package/src/pnpm/hook.js +37 -51
- package/src/pnpm/inputs.js +177 -0
- package/src/pnpm/install.js +140 -130
- package/src/pnpm/local.js +105 -34
- 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 +20 -53
- package/src/pnpm/package.js +123 -0
- package/src/pnpm/packlist.js +194 -0
- package/src/pnpm/patches.js +21 -28
- package/src/pnpm/projects.js +150 -101
- package/src/pnpm/readers.js +122 -77
- package/src/pnpm/settings.js +85 -93
- package/src/pnpm/tree.js +234 -188
- package/src/pnpm/uptodate.js +17 -29
- package/src/pnpm/workspace.js +210 -53
- package/src/project.js +56 -0
- package/src/soldeer/config.js +63 -0
- package/src/soldeer/git.js +52 -0
- package/src/soldeer/inputs.js +30 -0
- package/src/soldeer/tree.js +83 -0
- package/src/soldeer/zip.js +121 -0
- package/src/tarball.js +123 -125
- package/src/yarn1/bins.js +28 -0
- package/src/yarn1/compat.js +67 -0
- package/src/yarn1/hoist.js +249 -0
- package/src/yarn1/inputs.js +83 -0
- package/src/yarn1/manifest.js +64 -0
- package/src/yarn1/package.js +206 -0
- package/src/yarn1/peers.js +51 -0
- package/src/yarn1/requests.js +74 -0
- package/src/yarn1/resolve.js +289 -0
- package/src/yarn1/settings.js +95 -0
- package/src/yarn1/tree.js +219 -0
- package/yarn1.d.ts +368 -0
- package/yarn1.js +7 -0
- package/src/pnpm/npmrc.js +0 -31
- /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
|
+
}
|
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,54 +1,46 @@
|
|
|
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
|
|
|
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
|
-
//
|
|
16
|
-
|
|
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
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
|
-
// `
|
|
33
|
-
//
|
|
34
|
-
//
|
|
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
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
|
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
|
|
|
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
|
|
14
|
-
//
|
|
15
|
-
//
|
|
16
|
-
//
|
|
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
|
|
30
|
-
|
|
31
|
-
const
|
|
32
|
-
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 }
|
|
33
30
|
}
|
|
34
|
-
|
|
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
|
-
//
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
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,
|
|
54
|
-
...
|
|
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.
|
|
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')
|
|
64
|
-
|
|
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
|
-
//
|
|
75
|
-
// the
|
|
76
|
-
//
|
|
77
|
-
//
|
|
78
|
-
//
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
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 =
|
|
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
|
}
|