@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
package/src/mount.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
// A tree
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
// caller's lost; neither is safe. Everything is checked before anything
|
|
6
|
-
// is written, so a refusal leaves the Vfs as it was.
|
|
1
|
+
// A tree mounted into the caller's Vfs at the lockfile's directory, writing
|
|
2
|
+
// over and removing nothing; Vfs.mount judges it whole first, so a refusal
|
|
3
|
+
// leaves the Vfs as it was. A node_modules there already is refused: kept,
|
|
4
|
+
// Node would read it as the tree's, and removed, the caller's would be lost.
|
|
7
5
|
|
|
6
|
+
import { VfsError } from '@preventive/vfs'
|
|
8
7
|
import { basename, dirname } from '@preventive/vfs/path.js'
|
|
9
8
|
import { DeptreeError, quote } from './error.js'
|
|
10
9
|
|
|
@@ -13,8 +12,6 @@ export const fold = (name) => name.normalize('NFD').toLowerCase()
|
|
|
13
12
|
|
|
14
13
|
const where = (path) => `vfs[${quote(path)}]`
|
|
15
14
|
|
|
16
|
-
// Refuses a Vfs that holds a node_modules, or, where names are `folded`, a
|
|
17
|
-
// name that is one there.
|
|
18
15
|
export function checkNoModules(vfs, folded) {
|
|
19
16
|
for (const { path } of vfs.walk('/')) {
|
|
20
17
|
const name = basename(path)
|
|
@@ -24,40 +21,132 @@ export function checkNoModules(vfs, folded) {
|
|
|
24
21
|
}
|
|
25
22
|
}
|
|
26
23
|
|
|
27
|
-
|
|
24
|
+
// The same for a directory of `name` at the root alone, as Soldeer's
|
|
25
|
+
// dependencies and cargo's vendor: `what` it is called.
|
|
26
|
+
export const checkNoDir = (name, what) => (vfs, folded) => {
|
|
27
|
+
for (const there of vfs.readdir('/')) {
|
|
28
|
+
if (there === name || (folded && fold(there) === name)) {
|
|
29
|
+
throw new DeptreeError(`${what} is there already, which is neither kept beside the tree nor removed`, where(`/${there}`))
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function checkCollisions(vfs) {
|
|
35
|
+
const [clash] = vfs.collisions(fold)
|
|
36
|
+
if (clash !== undefined) throw new DeptreeError(`${quote(clash.names[0])} and ${quote(clash.names[1])} are one name on macOS`, quote(clash.path))
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// A path within a directory: no segment of it empty, `.` or `..`.
|
|
40
|
+
export const isInside = (path) => path !== '' && path.split('/').every((segment) => segment !== '' && segment !== '.' && segment !== '..')
|
|
41
|
+
|
|
42
|
+
// Whether `child` is `parent` or under it, both paths from one directory in
|
|
43
|
+
// normal form, `.` being that one.
|
|
44
|
+
export const within = (parent, child) => child === parent || (parent === '.' ? child !== '..' && !child.startsWith('../') : child.startsWith(`${parent}/`))
|
|
45
|
+
|
|
46
|
+
// What the lockfile reader refuses in a path: a backslash, a separator on
|
|
47
|
+
// Windows, and what reads otherwise than it is written (a control character,
|
|
48
|
+
// a line or paragraph separator, a bidirectional control).
|
|
49
|
+
export const UNSAFE = /[\p{Cc}\p{Zl}\p{Zp}\p{Bidi_Control}\\]/u
|
|
50
|
+
|
|
51
|
+
const asRefusal = (error, what, path) => {
|
|
52
|
+
if (error instanceof VfsError) return new DeptreeError(`cannot be ${what}: ${error.message}`, quote(path), { cause: error })
|
|
53
|
+
return error
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Each builder holds what a tarball names and where a package goes to that
|
|
57
|
+
// first, and makes no link where a package is written after. What is made
|
|
58
|
+
// is held to it again, so that one slip puts nothing out of the tree or the
|
|
59
|
+
// package's directory. A directory: a place within the tree, each one on
|
|
60
|
+
// the way made where its parent is, or there already and no link. `real`
|
|
61
|
+
// keeps the directories found so, as a package's files share them.
|
|
62
|
+
export function makeDirs(vfs, path, real = new Set()) {
|
|
63
|
+
if (!isInside(path)) throw new DeptreeError('is not a place within the tree', quote(path))
|
|
64
|
+
let at = ''
|
|
65
|
+
for (const segment of path.split('/')) {
|
|
66
|
+
at = `${at}/${segment}`
|
|
67
|
+
if (real.has(at)) continue
|
|
68
|
+
if (vfs.isSymlink(at)) throw new DeptreeError('would be written through a link', quote(at.slice(1)))
|
|
69
|
+
try {
|
|
70
|
+
if (!vfs.isDirectory(at)) vfs.mkdir(at)
|
|
71
|
+
} catch (error) {
|
|
72
|
+
throw asRefusal(error, 'made', at.slice(1))
|
|
73
|
+
}
|
|
74
|
+
real.add(at)
|
|
75
|
+
}
|
|
76
|
+
return `/${path}`
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// A file: a path within the package's directory, in a directory found as
|
|
80
|
+
// above, and no link at the path itself.
|
|
81
|
+
export function checkWrite(vfs, root, path, real) {
|
|
82
|
+
const at = `/${root}/${path}`
|
|
83
|
+
if (!isInside(root) || !isInside(path)) throw new DeptreeError('is not a path within the package', quote(at.slice(1)))
|
|
84
|
+
const parent = dirname(at)
|
|
85
|
+
if (!real.has(parent)) {
|
|
86
|
+
if (vfs.realpath(parent) !== parent) throw new DeptreeError('would be written through a link', quote(at.slice(1)))
|
|
87
|
+
real.add(parent)
|
|
88
|
+
}
|
|
89
|
+
if (vfs.isSymlink(at)) throw new DeptreeError('would be written through a link', quote(at.slice(1)))
|
|
90
|
+
return at
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// A link at `path`, a place within the tree under directories it spells,
|
|
94
|
+
// to `target`, as given.
|
|
95
|
+
export function writeLink(vfs, path, target) {
|
|
96
|
+
if (!isInside(path)) throw new DeptreeError('is not a place within the tree', quote(path))
|
|
97
|
+
if (path.includes('/')) makeDirs(vfs, dirname(path))
|
|
28
98
|
try {
|
|
29
|
-
|
|
99
|
+
vfs.symlink(target, `/${path}`)
|
|
30
100
|
} catch (error) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
//
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
101
|
+
throw asRefusal(error, 'made', path)
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// `root` is the package's directory in the tree, without a leading `/`;
|
|
106
|
+
// `links` are symlinks within it, by path, to their targets as given.
|
|
107
|
+
export function writeFiles(vfs, root, { dirs, files, links = new Map() }, stats, skip = () => false) {
|
|
108
|
+
const real = new Set()
|
|
109
|
+
makeDirs(vfs, root, real)
|
|
110
|
+
for (const dir of dirs) {
|
|
111
|
+
if (skip(dir)) continue
|
|
112
|
+
if (!isInside(dir)) throw new DeptreeError('is not a path within the package', quote(`${root}/${dir}`))
|
|
113
|
+
makeDirs(vfs, `${root}/${dir}`, real)
|
|
114
|
+
}
|
|
115
|
+
for (const [path, file] of files) {
|
|
116
|
+
if (skip(path)) continue
|
|
117
|
+
const at = checkWrite(vfs, root, path, real)
|
|
118
|
+
try {
|
|
119
|
+
vfs.writeFile(at, file.data, { mode: file.mode })
|
|
120
|
+
} catch (error) {
|
|
121
|
+
throw asRefusal(error, 'written', `${root}/${path}`)
|
|
47
122
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
} else {
|
|
59
|
-
target.writeFile(path, tree.readFile(path), { mode: tree.lstat(path).mode })
|
|
60
|
-
tree.unlink(path)
|
|
123
|
+
stats.files++
|
|
124
|
+
stats.bytes += file.data.length
|
|
125
|
+
}
|
|
126
|
+
for (const [path, target] of links) {
|
|
127
|
+
if (skip(path)) continue
|
|
128
|
+
const at = checkWrite(vfs, root, path, real)
|
|
129
|
+
try {
|
|
130
|
+
vfs.symlink(target, at)
|
|
131
|
+
} catch (error) {
|
|
132
|
+
throw asRefusal(error, 'made', `${root}/${path}`)
|
|
61
133
|
}
|
|
134
|
+
stats.links++
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// `target` with `tree` in it, or `tree` where there is none; on macOS, two
|
|
139
|
+
// names that are one there are refused first. A clash is refused by what is
|
|
140
|
+
// there; at the root, a path there is the tree's too. The tree's bytes are
|
|
141
|
+
// shared with `target`, not copied.
|
|
142
|
+
export function mount(tree, target, folded, check = checkNoModules) {
|
|
143
|
+
if (folded) checkCollisions(tree)
|
|
144
|
+
if (target === undefined) return tree
|
|
145
|
+
check(target, folded)
|
|
146
|
+
const clash = (path, [there]) => {
|
|
147
|
+
if (there !== path) throw new DeptreeError(`${quote(basename(there))} is there already, which is one name with ${quote(basename(path))} on macOS`, where(path))
|
|
148
|
+
throw new DeptreeError(`a ${target.lstat(path).type} is there already, where the tree has a ${tree.lstat(path).type}`, where(path))
|
|
62
149
|
}
|
|
150
|
+
target.mount(tree, '/', { clash, fold: folded ? fold : undefined })
|
|
151
|
+
return target
|
|
63
152
|
}
|
package/src/npm/bins.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// What bin-links does to the packages' files, the .bin links aside. Bins
|
|
2
|
+
// come from the lockfile; in a node_modules, the first package to claim a
|
|
3
|
+
// name links it, by Intl.Collator('en') on paths, as npm sorts its nodes,
|
|
4
|
+
// even where its target is missing. A linked target is made 0o755, its
|
|
5
|
+
// first line's CRLF made LF where that is a shebang in the first 2048 bytes.
|
|
6
|
+
|
|
7
|
+
import { basename, dirname, join } from '@preventive/vfs/path.js'
|
|
8
|
+
import { DeptreeError, quote } from '../error.js'
|
|
9
|
+
import { fixShebang, isModules } from '../tarball.js'
|
|
10
|
+
|
|
11
|
+
const collator = new Intl.Collator('en')
|
|
12
|
+
|
|
13
|
+
// npm-normalize-package-bin.
|
|
14
|
+
function binsOf(bin) {
|
|
15
|
+
const bins = new Map()
|
|
16
|
+
for (const [key, target] of Object.entries(bin)) {
|
|
17
|
+
const name = join('/', basename(key.replace(/[\\:]/gu, '/'))).slice(1)
|
|
18
|
+
const path = join('/', target.replace(/\\/gu, '/')).slice(1)
|
|
19
|
+
if (name !== '' && path !== '') bins.set(name, path.replace(/\/$/u, ''))
|
|
20
|
+
}
|
|
21
|
+
return bins
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function modulesOf(location) {
|
|
25
|
+
const parent = dirname(location)
|
|
26
|
+
return basename(parent) === 'node_modules' ? parent : dirname(parent)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// The files linking changes, by each package's location.
|
|
30
|
+
export function fixBins(nodes, fetched) {
|
|
31
|
+
const seen = new Set()
|
|
32
|
+
const changed = new Map()
|
|
33
|
+
for (const node of nodes.toSorted((a, b) => collator.compare(a.location, b.location))) {
|
|
34
|
+
const { location, pkg } = node
|
|
35
|
+
const { files, dirs } = fetched.get(node)
|
|
36
|
+
const where = `packages[${quote(location)}].bin`
|
|
37
|
+
for (const [name, path] of binsOf(pkg.bin)) {
|
|
38
|
+
const link = `${modulesOf(location)}/.bin/${name}`
|
|
39
|
+
if (seen.has(link)) continue
|
|
40
|
+
seen.add(link)
|
|
41
|
+
const segments = path.split('/')
|
|
42
|
+
if (isModules(segments[0])) throw new DeptreeError(`${quote(path)} is in the package's own node_modules, where npm installs its dependencies, which is not supported`, where)
|
|
43
|
+
if (segments.slice(1).some((_, i) => files.has(segments.slice(0, i + 1).join('/')))) throw new DeptreeError(`${quote(path)} runs through a file, which npm fails on`, where)
|
|
44
|
+
const file = files.get(path)
|
|
45
|
+
if (file === undefined) {
|
|
46
|
+
if (!dirs.has(path)) continue
|
|
47
|
+
throw new DeptreeError(`${quote(path)} is a directory, which is not supported`, where)
|
|
48
|
+
}
|
|
49
|
+
if (!changed.has(location)) changed.set(location, new Map())
|
|
50
|
+
changed.get(location).set(path, { data: fixShebang(file.data, 'a bin with a CRLF shebang that is not UTF-8, which npm rewrites with replacement characters, is not supported', where), mode: 0o755 })
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return changed
|
|
54
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// npm-install-checks' checkPlatform and checkEngine, and what npm leaves out
|
|
2
|
+
// for them: an optional package the host cannot run, with its optionalSet.
|
|
3
|
+
|
|
4
|
+
import { satisfies } from '@preventive/upstream/semver.js'
|
|
5
|
+
import { DeptreeError, quote } from '../error.js'
|
|
6
|
+
|
|
7
|
+
// npm-install-checks' checkList.
|
|
8
|
+
function takes(value, list) {
|
|
9
|
+
const values = typeof list === 'string' ? [list] : list
|
|
10
|
+
if (values.length === 1 && values[0] === 'any') return true
|
|
11
|
+
const negated = (entry) => entry.startsWith('!')
|
|
12
|
+
if (values.includes(`!${value}`)) return false
|
|
13
|
+
return values.some((entry) => !negated(entry) && entry === value) || values.every(negated)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function listOf(pkg, key, at) {
|
|
17
|
+
const list = pkg[key]
|
|
18
|
+
if (!list || typeof list === 'string' || (Array.isArray(list) && list.every((entry) => typeof entry === 'string'))) return list || undefined
|
|
19
|
+
throw new DeptreeError('expected a string or a sequence of strings, which npm fails without', `${at}.${key}`)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// A libc fails a host with none, whatever it names.
|
|
23
|
+
function checkPlatform(pkg, host, at) {
|
|
24
|
+
for (const key of ['os', 'cpu', 'libc']) {
|
|
25
|
+
const list = listOf(pkg, key, at)
|
|
26
|
+
if (list !== undefined && (host[key] === undefined || !takes(host[key], list))) return `its ${key}, ${quote(JSON.stringify(list))}, is not the host's`
|
|
27
|
+
}
|
|
28
|
+
return undefined
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function checkEngine(pkg, host) {
|
|
32
|
+
const { engines } = pkg
|
|
33
|
+
if (!engines) return undefined
|
|
34
|
+
const options = { includePrerelease: true }
|
|
35
|
+
if (engines.node && !satisfies(`v${host.node}`, engines.node, options)) return `its engines.node, ${quote(String(engines.node))}, does not take Node ${host.node}`
|
|
36
|
+
if (engines.npm && !satisfies(host.npm, engines.npm, options)) return `its engines.npm, ${quote(String(engines.npm))}, does not take npm ${host.npm}`
|
|
37
|
+
return undefined
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// What npm fails on of a package it does not leave out, `and` said of it.
|
|
41
|
+
export function checkRuns(manifest, host, where, engineStrict, and = '') {
|
|
42
|
+
const platform = checkPlatform(manifest, host, where)
|
|
43
|
+
if (platform !== undefined) throw new DeptreeError(`${platform}${and}, which npm fails on`, where)
|
|
44
|
+
const engine = engineStrict ? checkEngine(manifest, host) : undefined
|
|
45
|
+
if (engine !== undefined) throw new DeptreeError(`${engine}${and}, which npm fails on with engine-strict`, where)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Arborist's gatherDepSet.
|
|
49
|
+
function gatherDepSet(set, follow) {
|
|
50
|
+
const deps = new Set(set)
|
|
51
|
+
for (const node of deps) for (const edge of node.edgesOut) if (follow(edge)) deps.add(edge.to)
|
|
52
|
+
let changed = true
|
|
53
|
+
while (changed && deps.size > 0) {
|
|
54
|
+
changed = false
|
|
55
|
+
for (const dep of deps) {
|
|
56
|
+
if (dep.edgesIn.some((edge) => !deps.has(edge.from) && follow(edge))) {
|
|
57
|
+
changed = true
|
|
58
|
+
deps.delete(dep)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return deps
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Arborist's optionalSet: npm 10 gathers by edges that are not optional,
|
|
66
|
+
// npm 11 by those out of the first set, from 11.13 not from a node an
|
|
67
|
+
// earlier set left out.
|
|
68
|
+
function optionalSet(node, host, skipped) {
|
|
69
|
+
const set = new Set([node])
|
|
70
|
+
for (const member of set) for (const edge of member.edgesIn) if (!edge.optional) set.add(edge.from)
|
|
71
|
+
return gatherDepSet(set, host.reuse ? (edge) => !edge.optional : (edge) => !set.has(edge.to) && !(host.inert && skipped.has(edge.from)))
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const whereOf = (node) => `packages[${quote(node.location)}]`
|
|
75
|
+
|
|
76
|
+
// npm 11 checks optional packages in inventory order, passing over one an
|
|
77
|
+
// earlier set took; npm 10 checks them all as it extracts. What is in the
|
|
78
|
+
// node_modules of a node left out goes with it.
|
|
79
|
+
export function skippedOf(nodes, host, settings) {
|
|
80
|
+
const skipped = new Set()
|
|
81
|
+
for (const node of nodes.values()) {
|
|
82
|
+
if (node.kind !== 'package') continue
|
|
83
|
+
const where = whereOf(node)
|
|
84
|
+
if (!node.pkg.optional) {
|
|
85
|
+
checkRuns(node.manifest, host, where, settings.engineStrict, ', and it is not optional')
|
|
86
|
+
continue
|
|
87
|
+
}
|
|
88
|
+
if (!host.reuse && skipped.has(node)) continue
|
|
89
|
+
if (checkEngine(node.manifest, host) === undefined && checkPlatform(node.manifest, host, where) === undefined) continue
|
|
90
|
+
for (const member of optionalSet(node, host, skipped)) skipped.add(member)
|
|
91
|
+
}
|
|
92
|
+
for (const node of skipped) {
|
|
93
|
+
if (node.kind !== 'package') throw new DeptreeError('left out with an optional package the host cannot run, which is not supported', node.kind === 'link' ? whereOf(node) : `importers[${quote(node.location)}]`)
|
|
94
|
+
}
|
|
95
|
+
const under = (node) => node.parent !== undefined && (skipped.has(nodes.get(node.parent)) || under(nodes.get(node.parent)))
|
|
96
|
+
return new Set([...nodes.values()].filter((node) => skipped.has(node) || under(node)))
|
|
97
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// The root's devEngines, as npm-install-checks' checkDevEngines reads them
|
|
2
|
+
// before an install. The os version npm takes from the kernel is unknown.
|
|
3
|
+
|
|
4
|
+
import { satisfies, validRange } from '@preventive/upstream/semver.js'
|
|
5
|
+
import { DeptreeError, quote } from '../error.js'
|
|
6
|
+
|
|
7
|
+
const ENGINES = new Set(['packageManager', 'runtime', 'cpu', 'libc', 'os'])
|
|
8
|
+
const PROPERTIES = new Set(['name', 'version', 'onFail'])
|
|
9
|
+
const ON_FAIL = new Set(['ignore', 'warn', 'error', 'download'])
|
|
10
|
+
|
|
11
|
+
const isMapping = (value) => typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
12
|
+
|
|
13
|
+
function matches(wanted, current, at) {
|
|
14
|
+
const fail = (detail) => {
|
|
15
|
+
throw new DeptreeError(`${detail}, which npm fails on`, at)
|
|
16
|
+
}
|
|
17
|
+
if (!isMapping(wanted)) fail('expected a mapping')
|
|
18
|
+
const unknown = Object.keys(wanted).find((key) => !PROPERTIES.has(key))
|
|
19
|
+
if (unknown !== undefined) fail(`${quote(unknown)} is not a field npm knows`)
|
|
20
|
+
if (typeof wanted.name !== 'string') fail('expected a name')
|
|
21
|
+
if (typeof current.name !== 'string') fail('the host has none')
|
|
22
|
+
if ('onFail' in wanted && !ON_FAIL.has(wanted.onFail)) fail(`expected onFail to be one of ${[...ON_FAIL].join(', ')}`)
|
|
23
|
+
if (wanted.name !== current.name) return false
|
|
24
|
+
if (!('version' in wanted)) return true
|
|
25
|
+
if (typeof wanted.version !== 'string') fail('expected a version')
|
|
26
|
+
if (current.version === undefined) {
|
|
27
|
+
if (at.endsWith('.os')) throw new DeptreeError('a version of the os, which npm takes from the kernel, is not known here', at)
|
|
28
|
+
fail('the host has no version of it')
|
|
29
|
+
}
|
|
30
|
+
return validRange(wanted.version) ? satisfies(current.version, wanted.version) : wanted.version === current.version
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function checkDevEngines(devEngines, host, at) {
|
|
34
|
+
if (devEngines === undefined) return
|
|
35
|
+
if (!isMapping(devEngines)) throw new DeptreeError('expected a mapping, which npm fails without', at)
|
|
36
|
+
const current = {
|
|
37
|
+
cpu: { name: host.cpu },
|
|
38
|
+
libc: { name: host.libc },
|
|
39
|
+
os: { name: host.os },
|
|
40
|
+
packageManager: { name: 'npm', version: host.npm },
|
|
41
|
+
runtime: { name: 'node', version: `v${host.node}` },
|
|
42
|
+
}
|
|
43
|
+
for (const [engine, wanted] of Object.entries(devEngines)) {
|
|
44
|
+
if (!ENGINES.has(engine)) throw new DeptreeError(`${quote(engine)} is not an engine npm knows, which it fails on`, at)
|
|
45
|
+
const dependencies = [wanted].flat()
|
|
46
|
+
const where = `${at}.${engine}`
|
|
47
|
+
const matched = dependencies.map((dependency) => matches(dependency, current[engine], where)).some(Boolean)
|
|
48
|
+
if (dependencies.length === 0 || matched) continue
|
|
49
|
+
const onFail = dependencies.at(-1).onFail || 'error'
|
|
50
|
+
if (onFail === 'error' || onFail === 'download') throw new DeptreeError(`the host's ${engine} is not one it takes, which npm fails on`, where)
|
|
51
|
+
}
|
|
52
|
+
}
|
package/src/npm/graph.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// The lockfile's tree as Arborist holds it, for optionalSet to walk.
|
|
2
|
+
|
|
3
|
+
import { DeptreeError, quote } from '../error.js'
|
|
4
|
+
|
|
5
|
+
const IN_NODE_MODULES = /^(?:(.+)\/)?node_modules\/(?:@[^/]+\/)?[^/]+$/u
|
|
6
|
+
|
|
7
|
+
function parentOf(location, nodes) {
|
|
8
|
+
const found = IN_NODE_MODULES.exec(location)
|
|
9
|
+
if (found !== null) return found[1] ?? ''
|
|
10
|
+
const segments = location.split('/')
|
|
11
|
+
while (segments.pop() !== undefined) {
|
|
12
|
+
const dir = segments.join('/')
|
|
13
|
+
if (dir === '' || nodes.get(dir)?.kind === 'importer') return dir
|
|
14
|
+
}
|
|
15
|
+
return ''
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// A node's children by their names, folded, as npm's CaseInsensitiveMap
|
|
19
|
+
// holds them and the lockfile reader resolves an edge.
|
|
20
|
+
const childKey = (parent, name) => `${parent}\n${name.toLowerCase()}`
|
|
21
|
+
|
|
22
|
+
function childrenOf(nodes) {
|
|
23
|
+
const children = new Map()
|
|
24
|
+
for (const { location } of nodes.values()) {
|
|
25
|
+
const found = IN_NODE_MODULES.exec(location)
|
|
26
|
+
if (found !== null) children.set(childKey(found[1] ?? '', location.slice(location.lastIndexOf('node_modules/') + 'node_modules/'.length)), nodes.get(location))
|
|
27
|
+
}
|
|
28
|
+
return children
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// The link an edge to `target` is met by, the first of its name up the tree,
|
|
32
|
+
// held to lead where the lockfile reader found it to.
|
|
33
|
+
function linkOf(from, name, target, { nodes, children, links }) {
|
|
34
|
+
for (let location = from.location; location !== undefined; location = nodes.get(location).parent) {
|
|
35
|
+
const node = children.get(childKey(location, name))
|
|
36
|
+
if (node === undefined) continue
|
|
37
|
+
if (node.kind !== 'link' || `link:${links[node.location]}` !== target) break
|
|
38
|
+
return node
|
|
39
|
+
}
|
|
40
|
+
throw new DeptreeError(`its ${quote(name)} is not met by the link to ${quote(target.slice('link:'.length))} the lockfile reader found`, quote(from.location))
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Packages in lockfile order, which npm checks them in. npm reads the
|
|
44
|
+
// project's and workspaces' package.json in place of their entries.
|
|
45
|
+
export function graphOf(lockfile, manifests) {
|
|
46
|
+
const nodes = new Map()
|
|
47
|
+
const add = (location, kind, pkg) => nodes.set(location, { location, kind, pkg, parent: undefined, edgesOut: [], edgesIn: [] })
|
|
48
|
+
add('', 'root', lockfile.importers['.'])
|
|
49
|
+
for (const [dir, importer] of Object.entries(lockfile.importers)) if (dir !== '.') add(dir, 'importer', importer)
|
|
50
|
+
for (const [location, pkg] of Object.entries(lockfile.packages)) add(location, 'package', pkg)
|
|
51
|
+
for (const location of Object.keys(lockfile.links)) add(location, 'link', undefined)
|
|
52
|
+
for (const node of nodes.values()) {
|
|
53
|
+
if (node.kind !== 'root') node.parent = parentOf(node.location, nodes)
|
|
54
|
+
node.manifest = node.kind === 'link' ? undefined : manifests.get(node.location || '.') ?? node.pkg
|
|
55
|
+
}
|
|
56
|
+
const tree = { nodes, children: childrenOf(nodes), links: lockfile.links }
|
|
57
|
+
for (const node of nodes.values()) {
|
|
58
|
+
for (const [name, { type, target }] of Object.entries(node.pkg?.edges ?? {})) {
|
|
59
|
+
if (target === undefined) continue
|
|
60
|
+
const to = target.startsWith('link:') ? linkOf(node, name, target, tree) : nodes.get(target)
|
|
61
|
+
if (to === undefined) throw new DeptreeError(`its ${quote(name)} is met by ${quote(target)}, which the lockfile does not have`, quote(node.location))
|
|
62
|
+
const edge = { type, optional: type === 'optional' || type === 'peerOptional', from: node, to }
|
|
63
|
+
node.edgesOut.push(edge)
|
|
64
|
+
to.edgesIn.push(edge)
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return nodes
|
|
68
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// The host, and the files `npm ci` reads, given or read from the project.
|
|
2
|
+
|
|
3
|
+
import { valid } from '@preventive/upstream/semver.js'
|
|
4
|
+
import { DeptreeError, quote } from '../error.js'
|
|
5
|
+
import { walkWorkspaces } from '../glob.js'
|
|
6
|
+
import { readManifest, readManifests } from '../manifest.js'
|
|
7
|
+
import { checkHostKeys, checkLeftOut, checkProject, checkTexts, readText, typeOf } from '../project.js'
|
|
8
|
+
import { readSettings } from './settings.js'
|
|
9
|
+
import { profileOf } from './versions.js'
|
|
10
|
+
|
|
11
|
+
const LIBCS = new Set(['glibc', 'musl'])
|
|
12
|
+
|
|
13
|
+
export function checkHost(host) {
|
|
14
|
+
checkHostKeys(host, ['npm', 'node', 'os', 'cpu'], 'npm, node, os and cpu, and libc on Linux')
|
|
15
|
+
if (valid(host.node) !== host.node) throw new DeptreeError(`${quote(host.node)} is not an exact version`, 'host.node')
|
|
16
|
+
if (host.os === 'win32') throw new DeptreeError('Windows is not supported: npm links bins there with shims, and workspaces with junctions', 'host.os')
|
|
17
|
+
if (host.os === 'linux' ? !LIBCS.has(host.libc) : host.libc !== undefined) {
|
|
18
|
+
throw new TypeError(host.os === 'linux' ? 'host.libc must be glibc or musl on Linux' : 'host.libc must be left out but on Linux')
|
|
19
|
+
}
|
|
20
|
+
return { ...profileOf(host.npm), node: host.node, os: host.os, cpu: host.cpu, libc: host.libc }
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// As @npmcli/map-workspaces reads them; the lockfile reader refuses one
|
|
24
|
+
// that negates.
|
|
25
|
+
function globsOf(root) {
|
|
26
|
+
const { workspaces = [] } = root
|
|
27
|
+
const globs = Array.isArray(workspaces?.packages) ? workspaces.packages : workspaces
|
|
28
|
+
if (!Array.isArray(globs) || globs.some((glob) => typeof glob !== 'string')) throw new DeptreeError('expected a sequence of globs, which npm fails without', 'manifests["."].workspaces')
|
|
29
|
+
return globs.map((glob) => glob.replace(/^\.?\/+/u, ''))
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// As map-workspaces globs, ignoring node_modules, and on macOS case.
|
|
33
|
+
function findWorkspaces(project, globs, nocase) {
|
|
34
|
+
return walkWorkspaces(project, globs, { manager: 'npm', fold: nocase ? (path) => path.toLowerCase() : undefined, skip: (name) => name === 'node_modules' })
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const where = (dir) => `manifests[${quote(dir)}]`
|
|
38
|
+
|
|
39
|
+
function readManifestAt(project, dir) {
|
|
40
|
+
const text = readText(project, dir === '.' ? '/package.json' : `/${dir}/package.json`, where(dir))
|
|
41
|
+
if (text === undefined) throw new DeptreeError('the project has no package.json at its root', where(dir))
|
|
42
|
+
return readManifest(text, where(dir))
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function findNpmWorkspaces(options) {
|
|
46
|
+
const { project, os } = options ?? {}
|
|
47
|
+
checkProject(project)
|
|
48
|
+
return ['.', ...findWorkspaces(project, globsOf(readManifestAt(project, '.')), os === 'darwin')]
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const LOCKFILE = 'lockfile must be the text of package-lock.json, or left out with a project given to read it from'
|
|
52
|
+
|
|
53
|
+
function readProject(project, nocase) {
|
|
54
|
+
if (typeOf(project, '/npm-shrinkwrap.json', false) !== undefined) throw new DeptreeError('an npm-shrinkwrap.json, which npm reads in place of package-lock.json, is not supported', 'npm-shrinkwrap.json')
|
|
55
|
+
const lockfile = readText(project, '/package-lock.json')
|
|
56
|
+
if (lockfile === undefined) throw new DeptreeError('the project has no package-lock.json, which npm ci cannot do without')
|
|
57
|
+
const manifests = new Map([['.', readManifestAt(project, '.')]])
|
|
58
|
+
for (const dir of findWorkspaces(project, globsOf(manifests.get('.')), nocase)) manifests.set(dir, readManifestAt(project, dir))
|
|
59
|
+
return { lockfile, manifests, settings: readSettings(readText(project, '/.npmrc', '.npmrc')) }
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function inputsOf(options, nocase) {
|
|
63
|
+
const { lockfile, manifests, npmrc, project } = options
|
|
64
|
+
if (lockfile === undefined) {
|
|
65
|
+
if (project === undefined) throw new TypeError(LOCKFILE)
|
|
66
|
+
checkProject(project)
|
|
67
|
+
checkLeftOut({ manifests, npmrc })
|
|
68
|
+
return readProject(project, nocase)
|
|
69
|
+
}
|
|
70
|
+
if (project !== undefined) throw new TypeError('project must be left out where lockfile is given')
|
|
71
|
+
if (typeof lockfile !== 'string') throw new TypeError(LOCKFILE)
|
|
72
|
+
if (manifests === null || typeof manifests !== 'object') throw new TypeError('manifests must map each project\'s directory to its package.json')
|
|
73
|
+
checkTexts({ npmrc })
|
|
74
|
+
return { lockfile, manifests: readManifests(manifests), settings: readSettings(npmrc) }
|
|
75
|
+
}
|