@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
package/src/pnpm/bins.js CHANGED
@@ -1,64 +1,19 @@
1
- // What pnpm does to the files its bins run (@pnpm/link-bins, and
2
- // bin-links' fixBin): no .bin directory is written here, but each file a
3
- // bin pnpm links runs is left as linking leaves it, executable and, where
4
- // it starts with a `#!` line that ends in CRLF, with that line ending in
5
- // LF instead. pnpm 10 gives it mode 0o755, and pnpm 11 adds 0o111 to its
6
- // mode where that lacks any of it, which for the 0o644 and 0o755 a
7
- // package's files have here is the same.
8
- //
9
- // pnpm links a package's commands (commands.js) into many .bin
10
- // directories, and runs fixBin on a file only where its command is the
11
- // one linked by that name there and the file is there:
12
- //
13
- // - each package's own node_modules/.bin, for its children's bins (or,
14
- // where a child is a `link:`, for whatever is beside it), and for those
15
- // of the packages it bundles;
16
- // - node_modules/.pnpm/node_modules/.bin, for what is privately hoisted;
17
- // - each project's node_modules/.bin, for its direct dependencies' bins,
18
- // and at the root, where anything is publicly hoisted, for all it holds,
19
- // a direct dependency's command over a hoisted one's; with pnpm 11 and
20
- // autoInstallPeers, then for the bins of each peer a direct dependency
21
- // requires, by names not linked there yet;
22
- // - where any patch is configured, each package that is patched or has
23
- // an install script is built, scripts or not, which links its own bins
24
- // beside its children's in its own .bin before its patch is applied.
25
- //
26
- // Where the command linked of two of one name is the first pnpm came to
27
- // by the order a directory is read in, which varies, and a file's mode or
28
- // text would turn on it, the tree is refused; so it is where that would
29
- // turn on the commands of a directory outside the tree that are not known
30
- // here, which may take any name: those of one a `link:` leads to whose
31
- // package.json is not given (local.js), or of a project or such a
32
- // directory by the files of its directories.bin. So is a patch that
33
- // would be applied between two links of a file whose fix it would
34
- // change: one that makes or removes a file a bin names, or one under a
35
- // directories.bin, changes a bin file with a CRLF `#!` line, or changes
36
- // the bins a package.json names, its own or a bundled package's, or the
37
- // name or version they are ranked by.
38
- //
39
- // pnpm 12 (its cmd-shim crate) only adds the 0o111 a file lacks, and
40
- // rewrites no `#!` line. It links each package's own bins into its own
41
- // .bin, beside its children's that the lockfile says have some and those
42
- // of the packages it bundles, in one go and with no build pass; a `link:`
43
- // child's take no part. A bin it links whose target is a directory fails
44
- // the install.
45
- //
46
- // pnpm 11 and 12 link a command named `node` without making its file
47
- // executable; but pnpm 12 links a project's bins again after it has
48
- // patched any package, publicly hoisted any with bins, or where its
49
- // dependencies' peers it installs have some, and there makes the file of
50
- // a direct dependency's `node` executable, where its link leads to it
51
- // already.
1
+ // The files pnpm's bin linking fixes (@pnpm/link-bins, bin-links' fixBin):
2
+ // no .bin is written, but the file of each command it links by a name is
3
+ // made executable and a CRLF ending its `#!` line made LF. pnpm 10 sets
4
+ // 0o755 and pnpm 11 adds 0o111, the same for the 0o644 and 0o755 files have
5
+ // here; pnpm 12 (its cmd-shim crate) adds 0o111 and rewrites no `#!` line.
6
+ // Where any patch is configured, pnpm 10 and 11 build each package patched
7
+ // or with an install script, which links its own bins beside its children's
8
+ // before the patch; pnpm 12 links them together, with no build pass. pnpm 9
9
+ // links as pnpm 10 does, but fails on a bin that is a directory.
52
10
 
53
11
  import { DeptreeError, quote } from '../error.js'
54
12
  import { UNKNOWN, binsOf, bundledCommands, commandsOf, compare, normalized, parseManifest } from './commands.js'
55
13
 
56
14
  const decoder = new TextDecoder('utf-8', { fatal: true })
57
15
 
58
- // Where the peers pnpm 11 links the bins of into a project's .bin lead:
59
- // each peer a direct dependency has that is not optional and that its
60
- // snapshot resolves, by the path of its link beside the dependency, in
61
- // the order of those paths.
16
+ // The peers whose bins pnpm 11 links into a project's .bin.
62
17
  function peersOf(children, nodes) {
63
18
  const byPath = new Map()
64
19
  for (const dir of children.values()) {
@@ -74,19 +29,14 @@ function peersOf(children, nodes) {
74
29
  return [...byPath.keys()].sort().map((path) => byPath.get(path))
75
30
  }
76
31
 
77
- // Whether pnpm builds a package, as its pkgRequiresBuild tells: an
78
- // install script, a binding.gyp, which pnpm 11 and 12 pass over where
79
- // package.json's gypfile is false, or a .hooks directory.
32
+ // pnpm's pkgRequiresBuild.
80
33
  export const requiresBuild = (manifest, files, major) => Boolean((manifest.scripts != null && (manifest.scripts.preinstall || manifest.scripts.install || manifest.scripts.postinstall))
81
34
  || (files.has('binding.gyp') && !(major >= 11 && manifest.gypfile === false)) || files.keys().some((path) => /^\.hooks[\\/]/u.test(path)))
82
35
 
83
- // What linking commands into .bin directories fixes: `fixed`, by each
84
- // file's path in the tree, the package it is in, and `contested`, of those
85
- // that may be fixed or not, the package and why. `link` links commands
86
- // into one directory, where `ordered` says whether the order they come in
87
- // is one pnpm always has, and `fixNode` whether pnpm 12 fixes the file of
88
- // a direct dependency's `node`; it gives back the names linked, and
89
- // whether others may be.
36
+ // `contested` holds the files that may be fixed or not, and why; `ordered`
37
+ // is whether pnpm always comes to the commands in this order; `fixNode`
38
+ // whether pnpm 12 links a project's bins again, which fixes a direct
39
+ // dependency's `node` already linked.
90
40
  function linker(major) {
91
41
  const fixed = new Map()
92
42
  const contested = new Map()
@@ -116,13 +66,8 @@ function linker(major) {
116
66
  return { fixed, contested, link }
117
67
  }
118
68
 
119
- // The files pnpm runs fixBin on, by the directory of the package they
120
- // are in: `nodes` is the graph by directory, each node with its package's
121
- // `files` and `manifest`; `projects` each project's package.json by its
122
- // directory, `direct` each project's direct dependencies, `links` every
123
- // link in the tree, `publicHoist` whether anything is publicly hoisted,
124
- // `building` whether any patch is configured, `peers` whether peers are
125
- // installed automatically, and `major` pnpm's major version.
69
+ // The files pnpm runs fixBin on, as sets of paths by package directory.
70
+ // `building` is whether any patch is configured, `peers` autoInstallPeers.
126
71
  export function binTargets({ nodes, projects, direct, links, publicHoist, building, peers, major }) {
127
72
  const commandCache = new Map()
128
73
  const commandsOfDir = (dir, { normalize = false } = {}) => {
@@ -131,16 +76,14 @@ export function binTargets({ nodes, projects, direct, links, publicHoist, buildi
131
76
  if (!commandCache.has(dir)) commandCache.set(dir, commandsOf(dir, node.manifest, node.files, dir, quote(node.key), major))
132
77
  return commandCache.get(dir)
133
78
  }
134
- // A project, or a directory a `link:` leads to, whose bins are outside
135
- // the tree but may take a name; one whose package.json is not given
136
- // (local.js) may take any.
79
+ // A project or `link:` target, outside the tree: its bins may take any
80
+ // name where its package.json is not given (local.js).
137
81
  const manifest = projects.get(dir)
138
82
  if (manifest === undefined) return [UNKNOWN]
139
83
  const where = `manifests[${quote(dir)}]`
140
84
  return commandsOf(dir, normalize ? normalized(manifest, where) : manifest, undefined, undefined, where, major)
141
85
  }
142
- // Whether pnpm 12 takes a node to have bins: the lockfile says it has,
143
- // or it is a directory, for which the lockfile says nothing.
86
+ // The lockfile has no hasBin for a directory, whose bins pnpm 12 reads.
144
87
  const mayHaveBin = (dir) => nodes.get(dir)?.pkg.hasBin === true || nodes.get(dir)?.pkg.resolution.type === 'directory'
145
88
 
146
89
  const { fixed, contested, link } = linker(major)
@@ -178,15 +121,15 @@ export function binTargets({ nodes, projects, direct, links, publicHoist, buildi
178
121
  if (id === '.' && publicHoist) {
179
122
  const manifest = projects.get('.')
180
123
  const own = new Set(Object.keys({ ...manifest.devDependencies, ...manifest.dependencies, ...manifest.optionalDependencies }))
124
+ const isOwn = (path) => own.has(path.slice('node_modules/'.length))
181
125
  // pnpm 12 links what it hoists of the projects apart, before.
182
126
  const entries = [...links].filter(([path, dir]) => /^node_modules\/(?:@[^/]+\/)?[^/@.][^/]*$/u.test(path)
183
- && (major < 12 || own.has(path.slice('node_modules/'.length)) || mayHaveBin(dir)))
184
- const commands = entries.flatMap(([path, dir]) => commandsOfDir(dir).map((command) => ({ ...command, direct: own.has(path.slice('node_modules/'.length)) })))
127
+ && (major < 12 || isOwn(path) || mayHaveBin(dir)))
128
+ const commands = entries.flatMap(([path, dir]) => commandsOfDir(dir).map((command) => ({ ...command, direct: isOwn(path) })))
185
129
  const names = new Set(commands.filter((command) => command.direct).map(({ name }) => name))
186
- relink ||= major >= 12 && entries.some(([path, dir]) => !own.has(path.slice('node_modules/'.length)) && mayHaveBin(dir))
187
- linked = link([...commands.filter((command) => command.direct), ...commands.filter((command) => !command.direct && !names.has(command.name))], { ordered: false, where, fixNode: relink })
130
+ relink ||= major >= 12 && entries.some(([path, dir]) => !isOwn(path) && mayHaveBin(dir))
131
+ linked = link([...commands.filter((command) => command.direct), ...commands.filter((command) => !names.has(command.name))], { ordered: false, where, fixNode: relink })
188
132
  } else {
189
- // pnpm 11 reads the bins only of what the lockfile says has some.
190
133
  const dirs = [...children.values()].filter((dir) => major < 11 || !nodes.has(dir) || (major >= 12 ? mayHaveBin(dir) : nodes.get(dir).pkg.hasBin))
191
134
  // pnpm 12 comes to them in the order of a map, which varies.
192
135
  linked = link(dirs.flatMap((dir) => commandsOfDir(dir)), { ordered: major < 12, where, fixNode: relink })
@@ -201,18 +144,13 @@ export function binTargets({ nodes, projects, direct, links, publicHoist, buildi
201
144
  return fixedFiles(nodes, fixed, contested, major)
202
145
  }
203
146
 
204
- // The files `fixed` has fixBin run on, by the directory of their package.
205
- // One `contested` may be or not, which is refused, unless it is fixed
206
- // anyway, is not there, or is one fixBin leaves as it is: executable, with
207
- // no CRLF `#!` line, and not patched.
208
147
  function fixedFiles(nodes, fixed, contested, major) {
209
- // pnpm 12 reads the `#!` line of a file it links, and fails on one
210
- // that is a directory: `path`, in the package, '' for the package itself.
148
+ // pnpm 9 and 12 read each linked file's `#!` line. '' is the package.
211
149
  const checkDirectory = (node, path) => {
212
- if (major < 12) return
150
+ if (major >= 10 && major < 12) return
213
151
  const prefix = `${path}/`
214
152
  if (path === '' || node.files.keys().some((name) => name.startsWith(prefix))) {
215
- throw new DeptreeError(`its bin ${quote(path === '' ? '.' : path)} is a directory, which pnpm 12 fails on`, quote(node.key))
153
+ throw new DeptreeError(`its bin ${quote(path === '' ? '.' : path)} is a directory, which pnpm ${major} fails on`, quote(node.key))
216
154
  }
217
155
  }
218
156
  for (const [target, { owner, why }] of contested) {
@@ -235,18 +173,14 @@ function fixedFiles(nodes, fixed, contested, major) {
235
173
  return byNode
236
174
  }
237
175
 
238
-
239
- // Whether a file starts with a `#!` line ending in CRLF, as fixBin reads
240
- // its first 2048 bytes to tell.
176
+ // As fixBin tells it, reading only the first 2048 bytes.
241
177
  function hasCrlfShebang(data) {
242
178
  if (data[0] !== 0x23 || data[1] !== 0x21) return false
243
179
  const newline = data.subarray(0, 2048).indexOf(0x0a)
244
180
  return newline >= 4 && data[newline - 1] === 0x0d
245
181
  }
246
182
 
247
- // fixBin on a file: mode 0o755, and a CRLF ending the `#!` line made LF.
248
- // pnpm reads and writes the file as UTF-8 to do that, which would change
249
- // one that is not; that is refused. pnpm 12 only adds the 0o111 it lacks.
183
+ // pnpm reads and writes the file as UTF-8, which changes one that is not.
250
184
  export function fixBin(file, where, major) {
251
185
  if (major >= 12) return { data: file.data, mode: file.mode | 0o111 }
252
186
  if (!hasCrlfShebang(file.data)) return { data: file.data, mode: 0o755 }
@@ -264,12 +198,8 @@ export function fixBin(file, where, major) {
264
198
 
265
199
  const BIN_FIELDS = (manifest) => JSON.stringify([manifest.name, manifest.version, manifest.bin, manifest.directories?.bin])
266
200
 
267
- // A patch is applied between two times its package's bins are linked,
268
- // so it may not change what either does: `patched` is the package's files
269
- // with the patch applied, `targets` those that fixBin is run on. pnpm
270
- // reads package.json again after it, for the name and version a command
271
- // is taken by as well as the bins. It gives back the package's
272
- // package.json, patched, as parsed.
201
+ // A patch is applied between two links of its package's bins, so it may not
202
+ // change what either does. It gives back the patched package.json.
273
203
  export function checkPatchOfBins(node, patched, targets, where, major) {
274
204
  const file = patched.get('package.json')
275
205
  if (file?.data === undefined) throw new DeptreeError('the patch removes package.json', where)
@@ -284,7 +214,6 @@ export function checkPatchOfBins(node, patched, targets, where, major) {
284
214
  throw new DeptreeError(`the patch makes or removes ${quote(path)}, which a bin names, and which pnpm links before and after it`, where)
285
215
  }
286
216
  }
287
- // pnpm 12 rewrites no bin.
288
217
  if (major >= 12) return manifest
289
218
  for (const path of targets) {
290
219
  const before = node.files.get(path).data
@@ -1,31 +1,20 @@
1
- // Checks of a lockfile and of the tree built from it that pnpm does not
2
- // make, each of something a lockfile pnpm writes always holds to, or a
3
- // tree it installs always is: where one fails, the lockfile was not
4
- // written by pnpm as it stands, or the tree would not be one tree
5
- // everywhere it is installed.
1
+ // Checks pnpm does not make, of what a lockfile pnpm writes always holds to or
2
+ // a tree it installs always is.
6
3
 
7
4
  import { DeptreeError, quote } from '../error.js'
8
- import { fold } from '../mount.js'
9
5
 
10
- // A snapshot is optional exactly where no importer reaches it through
11
- // dependencies alone, its devDependencies counted among them, as pnpm
12
- // marks it: one marked otherwise would be left out, or kept, where it
13
- // should not be.
6
+ // pnpm marks a snapshot optional exactly where no importer requires it; one
7
+ // marked otherwise would be left out, or kept, where it should not be.
14
8
  export function checkOptional(lockfile) {
15
9
  const required = new Set()
16
- const queue = []
17
10
  const reach = (targets) => {
18
- for (const target of Object.values(targets)) {
19
- if (target.startsWith('link:') || required.has(target)) continue
20
- required.add(target)
21
- queue.push(target)
22
- }
11
+ for (const target of Object.values(targets)) if (!target.startsWith('link:')) required.add(target)
23
12
  }
24
13
  for (const importer of Object.values(lockfile.importers)) {
25
14
  reach(importer.dependencies)
26
15
  reach(importer.devDependencies)
27
16
  }
28
- while (queue.length > 0) reach(lockfile.packages[queue.pop()].dependencies)
17
+ for (const key of required) reach(lockfile.packages[key].dependencies)
29
18
  for (const [key, pkg] of Object.entries(lockfile.packages)) {
30
19
  if (pkg.optional === required.has(key)) {
31
20
  throw new DeptreeError(`marked ${pkg.optional ? 'optional where an importer requires it' : 'required where only optional dependencies reach it'}, which pnpm never writes`, `snapshots[${quote(key)}]`)
@@ -33,24 +22,8 @@ export function checkOptional(lockfile) {
33
22
  }
34
23
  }
35
24
 
36
- // macOS's filesystems take a name whatever its case and normalization, so
37
- // two names that differ only in those are one there, and one would be
38
- // lost: such a tree is refused where the host is macOS.
39
- export function checkCollisions(vfs) {
40
- for (const { path, type } of vfs.walk('/')) {
41
- if (type !== 'directory') continue
42
- const folded = new Map()
43
- for (const name of vfs.readdir(path)) {
44
- const key = fold(name)
45
- if (folded.has(key)) throw new DeptreeError(`${quote(folded.get(key))} and ${quote(name)} are one name on macOS`, quote(path))
46
- folded.set(key, name)
47
- }
48
- }
49
- }
50
-
51
- // Every link into node_modules leads to a directory in the tree; a link
52
- // out of it leads to a project or a `link:` directory, which the tree
53
- // does not hold.
25
+ // Only links into node_modules are checked: a link out of it leads to a
26
+ // project or a `link:` directory, which the tree does not hold.
54
27
  export function checkLinks(vfs, links) {
55
28
  for (const [path, target] of links) {
56
29
  if (!target.startsWith('node_modules/') && !target.includes('/node_modules/')) continue
@@ -1,27 +1,15 @@
1
- // A package's commands, as pnpm reads them to link its bins
2
- // (@pnpm/package-bins; with pnpm 12, its bin_resolver crate), and which of
3
- // two of one name in a .bin directory pnpm links.
4
- //
5
- // A package's bins are its `bin` — a path, or paths by command — or, with
6
- // none, every file under `directories.bin` that is not a dotfile or in a
7
- // dot directory, each by its name. A command that is not a name, or a
8
- // path that leads out of the package, is passed over, as pnpm 11 passes
9
- // over one named `.` or `..` or nothing. pnpm 12 reads a package.json as it
10
- // is, and its bins otherwise: see commands12.
11
- //
12
- // Of two commands of one name, pnpm links the one the package of that
13
- // name has (with pnpm 11 and 12, or npm's `npx`, and pnpm's `pn`, `pnpx`,
14
- // `pnx` and `pnpm`), else the one of the package whose name sorts last (by
15
- // `localeCompare`, read here as English; with pnpm 12, the one whose name
16
- // sorts first by its bytes), else of the later version, else the first it
17
- // came to.
1
+ // A package's commands as @pnpm/package-bins reads them to link its bins, or
2
+ // pnpm 12's bin_resolver crate, which reads a package.json unnormalized. pnpm 9
3
+ // filters a command by its name before it drops the scope, and links a `bin`
4
+ // string or a directories.bin wherever it leads.
18
5
 
19
6
  import { compareVersions, valid } from '@preventive/upstream/semver.js'
20
7
  import { basename, compareNames, join } from '@preventive/vfs/path.js'
21
8
  import { DeptreeError, quote } from '../error.js'
9
+ import { checkNesting } from '../manifest.js'
22
10
 
23
- // A package whose directory has no package.json is still linked by these
24
- // names by pnpm 10, to the runtime's binary inside it.
11
+ // A package whose directory has no package.json is still linked by these names
12
+ // by pnpm 10, to the runtime's binary inside it; pnpm 9 links none.
25
13
  const RUNTIMES = { __proto__: null, node: 'bin/node', deno: 'deno', bun: 'bun' }
26
14
 
27
15
  // The packages pnpm 11 and 12 take to own a command by a name not their own.
@@ -30,9 +18,7 @@ const OWNERS = { __proto__: null, npx: ['npm'], pn: ['pnpm', '@pnpm/exe'], pnpm:
30
18
  const collator = new Intl.Collator('en')
31
19
  const decoder = new TextDecoder('utf-8', { fatal: true })
32
20
 
33
- // The path `rel` names in the package at `dir`, as path.join spells it, or
34
- // undefined where it is not in the package, as is-subdir tells; the
35
- // package itself is `dir`, which is `.` for the root project.
21
+ // As path.join spells it, or undefined where is-subdir finds it out of `dir`.
36
22
  function inPackage(dir, rel, where) {
37
23
  if (typeof rel !== 'string') throw new DeptreeError(`${quote(String(rel))} is not a path, which pnpm fails on`, where)
38
24
  const path = join(dir, rel)
@@ -41,13 +27,10 @@ function inPackage(dir, rel, where) {
41
27
  return inside ? path : undefined
42
28
  }
43
29
 
44
- // A command that stands for those of a directory whose bins are not known
45
- // here, which may take any name.
30
+ // Stands for the commands of a directory whose bins are not known here.
46
31
  export const UNKNOWN = { unknown: true }
47
32
 
48
- // The files of `files` under `dir`, as tinyglobby's `**` finds them: none
49
- // a dotfile or in a dot directory, unless `dots`, as pnpm 12's walk finds
50
- // them. `files` holds paths relative to `base`.
33
+ // As tinyglobby's `**` finds them, or with `dots` as pnpm 12's walk does.
51
34
  function filesUnder(files, base, dir, { dots = false } = {}) {
52
35
  const prefix = dir === base ? '' : `${dir.slice(base.length + 1)}/`
53
36
  const found = []
@@ -60,9 +43,7 @@ function filesUnder(files, base, dir, { dots = false } = {}) {
60
43
  return found
61
44
  }
62
45
 
63
- // The files found under `root`, each a command by its name. Of two of one
64
- // name, which wins turns on the order they were found in: tinyglobby lists
65
- // them in the order the directories are read in.
46
+ // Which of two of one name wins turns on the order directories are read in.
66
47
  function filesAsCommands(found, root, common) {
67
48
  const names = found.map((path) => basename(path))
68
49
  const counts = new Map()
@@ -70,9 +51,8 @@ function filesAsCommands(found, root, common) {
70
51
  return found.map((path, i) => ({ ...common, name: names[i], target: `${root}/${path}`, unordered: counts.get(names[i]) > 1 }))
71
52
  }
72
53
 
73
- // Whether pnpm takes a package to own a command, which wins it the name.
74
- // pnpm 11 looks a name up among OWNERS as a plain object's key, and fails
75
- // on one that Object.prototype has.
54
+ // pnpm 11 looks a name up among OWNERS as a plain object's key, and fails on
55
+ // one that Object.prototype has.
76
56
  function owns({ name, pkgName, own }, major, where) {
77
57
  if (own || name === pkgName) return true
78
58
  if (major < 11) return false
@@ -80,35 +60,22 @@ function owns({ name, pkgName, own }, major, where) {
80
60
  return OWNERS[name]?.includes(pkgName) === true
81
61
  }
82
62
 
83
- // pnpm's runtimeHasNodeDownloaded: whether engines.runtime asks for a Node
84
- // to download, the first runtime of a list named node deciding. pnpm fails
85
- // on a list with nothing in it where it reads one, before that.
63
+ // pnpm's runtimeHasNodeDownloaded.
86
64
  function downloadsNode(runtime, where) {
87
65
  if (!runtime) return false
88
66
  if (!Array.isArray(runtime)) return runtime.name === 'node' && runtime.onFail === 'download'
89
67
  for (const item of runtime) {
90
- if (item === null || item === undefined) throw new DeptreeError('its engines.runtime lists nothing where pnpm reads a runtime, which pnpm fails on', where)
68
+ if (item == null) throw new DeptreeError('its engines.runtime lists nothing where pnpm reads a runtime, which pnpm fails on', where)
91
69
  if (item.name === 'node') return item.onFail === 'download'
92
70
  }
93
71
  return false
94
72
  }
95
73
 
96
- // What pnpm 12 takes for a command's name: one encodeURIComponent leaves
97
- // as it is, or `$`, but neither `.` nor `..`.
74
+ // pnpm 12's command names: those encodeURIComponent leaves as they are.
98
75
  const safeName = (name) => name === '$' || (name !== '.' && name !== '..' && /^[\w\-.!~*'()]+$/u.test(name))
99
76
 
100
- // A package's commands as pnpm 12 reads them: a string `bin` names one
101
- // after the package, where it has a name; an object names each of its
102
- // string values; any other `bin` but '' names none, and only '' or none
103
- // at all leaves directories.bin, where it is a string, to name each file
104
- // under it, dotfiles and those in dot directories among them, by its
105
- // name. A scoped command drops its scope; a name that is not safe, or a
106
- // target that is absolute or out of the package, is passed over.
107
- //
108
- // pnpm 12 reads a package's own bins off what its store holds of its
109
- // package.json where it has the package already, which leaves out a null
110
- // `bin`, and off the package.json where it fetches the package, which
111
- // keeps one: so a null `bin` beside a directories.bin is refused.
77
+ // pnpm 12 reads a package's own package.json from its store where it has the
78
+ // package, which drops a null `bin`, and as fetched otherwise.
112
79
  function commands12(dir, manifest, files, base, where) {
113
80
  const name = typeof manifest.name === 'string' ? manifest.name : undefined
114
81
  const common = { pkgName: name ?? '', pkgVersion: manifest.version, owner: base }
@@ -136,32 +103,36 @@ function commands12(dir, manifest, files, base, where) {
136
103
  return filesAsCommands(filesUnder(files, base, root, { dots: true }).filter((path) => safeName(basename(path))), root, common)
137
104
  }
138
105
 
139
- // A package's commands: `dir` is where the package is, `manifest` its
140
- // package.json, `files` the files of the package holding it (itself, or
141
- // the one that bundles it), by their paths under `base`. Both are
142
- // undefined for a directory outside the tree, a project or one a `link:`
143
- // leads to, whose commands are known only where its package.json names
144
- // them, and whose files are not fixed.
106
+ const outOf = (rel, where) => new DeptreeError(`${quote(rel)} leads out of the package, which pnpm 9 links a bin to all the same, and that is not supported`, where)
107
+
108
+ const uriSafe = (name) => name === encodeURIComponent(name) || name === '$'
109
+
110
+ // pnpm 9 also takes a scope with whatever follows it.
111
+ const takes9 = (command) => uriSafe(command) || command[0] === '@'
112
+
113
+ // `files` are the holding package's (itself, or the one bundling it), by path
114
+ // under `base`; both are undefined for a project or `link:` target.
145
115
  export function commandsOf(dir, manifest, files, base, where, major) {
146
116
  if (major >= 12) return commands12(dir, manifest, files, base, where)
147
117
  const common = { pkgName: manifest.name, pkgVersion: manifest.version, owner: base }
148
118
  if (typeof manifest.bin === 'string' && !manifest.name) throw new DeptreeError('it has a bin and no name, which pnpm fails on', where)
149
- // pnpm looks for the Node it would download from where the package is,
150
- // and fails where there is none.
151
- if (downloadsNode(manifest.engines?.runtime, where)) {
119
+ // pnpm 9 knows no runtime.
120
+ if (major >= 10 && downloadsNode(manifest.engines?.runtime, where)) {
152
121
  throw new DeptreeError('its engines.runtime has pnpm look for a Node to run its bins with, which is not supported', where)
153
122
  }
154
123
  if (manifest.bin) {
155
- const entries = typeof manifest.bin === 'string' ? [[manifest.name, manifest.bin]] : Object.entries(manifest.bin)
124
+ const string = typeof manifest.bin === 'string'
125
+ const entries = string ? [[manifest.name, manifest.bin]] : Object.entries(manifest.bin)
156
126
  const commands = []
157
127
  for (const [command, rel] of entries) {
158
128
  const name = command[0] === '@' ? command.slice(command.indexOf('/') + 1) : command
159
- if (name !== encodeURIComponent(name) && name !== '$') continue
160
- if (name === '' || name === '.' || name === '..') {
129
+ if (!(major < 10 ? string || takes9(command) : uriSafe(name))) continue
130
+ if (name === '' || name === '.' || name === '..' || name.includes('/')) {
161
131
  if (major >= 11) continue
162
132
  throw new DeptreeError(`a bin named ${quote(name)} is not supported`, where)
163
133
  }
164
134
  const target = inPackage(dir, rel, where)
135
+ if (target === undefined && major < 10 && string) throw outOf(rel, where)
165
136
  if (target !== undefined) commands.push({ ...common, name, target })
166
137
  }
167
138
  return commands
@@ -169,13 +140,15 @@ export function commandsOf(dir, manifest, files, base, where, major) {
169
140
  const binDir = manifest.directories?.bin
170
141
  if (!binDir) return []
171
142
  const root = inPackage(dir, binDir, where)
143
+ if (root === undefined && major < 10) throw outOf(binDir, where)
172
144
  if (root === undefined) return []
173
145
  if (files === undefined) return [UNKNOWN]
174
146
  return filesAsCommands(filesUnder(files, base, root), root, common)
175
147
  }
176
148
 
177
- // compareCommandsInConflict, which pnpm keeps the greater of; with pnpm 12,
178
- // pick_winner, whose ties keep the first.
149
+ // compareCommandsInConflict, which pnpm keeps the greater of (pnpm 12's
150
+ // pick_winner, which keeps the first of a tie). pnpm compares names with
151
+ // localeCompare, read here as English.
179
152
  export function compare(a, b, where, major) {
180
153
  const aOwns = owns(a, major, where)
181
154
  const bOwns = owns(b, major, where)
@@ -193,11 +166,9 @@ export function compare(a, b, where, major) {
193
166
  return compareVersions(a.pkgVersion, b.pkgVersion)
194
167
  }
195
168
 
196
- // A package.json as normalize-package-data leaves one, or refuses it,
197
- // where pnpm reads one with readPackageJson: its name trimmed, and each
198
- // of its name and version one npm takes.
169
+ // As normalize-package-data leaves it for pnpm's readPackageJson.
199
170
  export function normalized(manifest, where) {
200
- const name = manifest.name ? manifest.name : ''
171
+ const name = manifest.name || ''
201
172
  if (typeof name !== 'string') throw new DeptreeError('its package.json\'s name is not a string, which pnpm fails on', where)
202
173
  const trimmed = name.trim()
203
174
  const scoped = /^@([^/]+)\/([^/]+)$/u.exec(trimmed)
@@ -210,9 +181,7 @@ export function normalized(manifest, where) {
210
181
  return { ...manifest, name: trimmed, version }
211
182
  }
212
183
 
213
- // The packages a package bundles, as pnpm's readModulesDir finds them in
214
- // its node_modules: each directory there not named with a leading dot,
215
- // and each in a scope's directory.
184
+ // As pnpm's readModulesDir finds them.
216
185
  function bundledIn(files) {
217
186
  const names = new Set()
218
187
  for (const path of files.keys()) {
@@ -228,16 +197,15 @@ export function parseManifest(file, where) {
228
197
  try {
229
198
  const manifest = JSON.parse(decoder.decode(file.data))
230
199
  if (manifest === null || typeof manifest !== 'object' || Array.isArray(manifest)) throw new Error('not an object')
231
- return manifest
232
- } catch {
200
+ return checkNesting(manifest, where)
201
+ } catch (error) {
202
+ if (error instanceof DeptreeError) throw error
233
203
  throw new DeptreeError('a package.json pnpm reads for its bins is not JSON, which pnpm fails on', where)
234
204
  }
235
205
  }
236
206
 
237
- // The commands of the packages `node` bundles. For one with no
238
- // package.json pnpm 11 reads the bins of the package.json of the nearest
239
- // directory above it whose publishConfig.directory it is, as far up as the
240
- // filesystem goes, which is refused.
207
+ // For a bundled package with no package.json, pnpm 11 reads the bins of any
208
+ // directory above it, up to /, whose publishConfig.directory it is.
241
209
  export function bundledCommands(node, where, major) {
242
210
  const commands = []
243
211
  for (const name of bundledIn(node.files)) {
@@ -247,21 +215,16 @@ export function bundledCommands(node, where, major) {
247
215
  if (file !== undefined) {
248
216
  const manifest = parseManifest(file, here)
249
217
  commands.push(...commandsOf(dir, major >= 12 ? manifest : normalized(manifest, here), node.files, node.dir, here, major))
250
- } else if (major >= 12) {
251
- continue
252
218
  } else if (major >= 11) {
253
- throw new DeptreeError('it bundles a package with no package.json, whose bins pnpm 11 looks for above it', here)
254
- } else if (name in RUNTIMES) {
219
+ if (major < 12) throw new DeptreeError('it bundles a package with no package.json, whose bins pnpm 11 looks for above it', here)
220
+ } else if (major >= 10 && name in RUNTIMES) {
255
221
  commands.push({ name, target: `${dir}/${RUNTIMES[name]}`, owner: node.dir, own: true, pkgName: '', pkgVersion: '' })
256
222
  }
257
223
  }
258
224
  return commands
259
225
  }
260
226
 
261
- // Every path the package at `node` and those it bundles name as bins,
262
- // relative to it, whether pnpm links them or not, where its package.json
263
- // is `manifest` and its files are `files`: each with the commands that
264
- // name it and the package name and version each is ranked by.
227
+ // Every path the package and those it bundles name as bins, linked or not.
265
228
  export function binsOf(node, manifest, files, major) {
266
229
  const where = quote(node.key)
267
230
  const bins = new Map()
@@ -1,34 +1,21 @@
1
- // The directory pnpm 10 gives a snapshot under node_modules/.pnpm, as its
2
- // depPathToFilename (@pnpm/dependency-path) spells it: the snapshot key
3
- // with each character a filesystem may refuse made `+`, the parentheses of
4
- // the peers and the patch made `_` and the last one dropped, and, where
5
- // that runs past `virtualStoreDirMaxLength` or has a capital in it, cut
6
- // short and ended with `_` and 32 hex characters of its SHA-256.
7
- // `a@1.0.0(b@2.0.0)` is `a@1.0.0_b@2.0.0`, `@s/a@1.0.0` `@s+a@1.0.0`.
8
- //
9
- // pnpm 11 escapes a trailing dot or space as well, each made `+`, and then
10
- // hashes the name whatever its length; a snapshot of the registry never
11
- // ends in either. pnpm 9 hashed with MD5 in base32, which is not done here.
12
- //
13
- // pnpm 12 measures and cuts the name by its UTF-8 bytes, back to where a
14
- // character starts, and takes only an ASCII capital for one; a name not
15
- // all ASCII, as a `file:` path may have, is the only one that tells.
1
+ // The directory pnpm gives a snapshot under node_modules/.pnpm, as its
2
+ // depPathToFilename (@pnpm/dependency-path) spells it: `a@1.0.0(b@2.0.0)` is
3
+ // `a@1.0.0_b@2.0.0`, `@s/a@1.0.0` `@s+a@1.0.0`, and one too long or with a
4
+ // capital is cut short and hashed.
16
5
 
17
6
  import { quote } from '../error.js'
18
- import { sha256Hex } from '../hash.js'
7
+ import { md5Base32, sha256Hex } from '../hash.js'
19
8
 
20
- const HASHED = 33 // `_` and 32 hex characters
21
9
  const encoder = new TextEncoder()
22
10
 
23
- // The name cut to `length` units: UTF-16 ones, or for pnpm 12 bytes, as
24
- // many whole characters as fit in them.
11
+ // pnpm 12 cuts by bytes, back to where a character starts.
25
12
  function cut(filename, length, major) {
26
13
  if (major < 12) return filename.slice(0, length)
27
14
  return filename.slice(0, encoder.encodeInto(filename, new Uint8Array(length)).read)
28
15
  }
29
16
 
30
17
  export async function depPathToFilename(depPath, maxLength, major = 10) {
31
- let filename = depPath.replace(/[\\/:*?"<>|#]/gu, '+')
18
+ let filename = depPath.replace(major < 10 ? /[\\/:*?"<>|]/gu : /[\\/:*?"<>|#]/gu, '+')
32
19
  if (filename.includes('(')) filename = filename.replace(/\)$/u, '').replace(/\)\(|\(|\)/gu, '_')
33
20
  const hashed = filename
34
21
  const trailing = major >= 11 ? /[. ]*$/u.exec(filename)[0].length : 0
@@ -36,5 +23,7 @@ export async function depPathToFilename(depPath, maxLength, major = 10) {
36
23
  const length = major >= 12 ? encoder.encode(filename).length : filename.length
37
24
  const lower = major >= 12 ? filename.replace(/[A-Z]+/gu, (capitals) => capitals.toLowerCase()) : filename.toLowerCase()
38
25
  if (trailing === 0 && length <= maxLength && filename === lower) return filename
39
- return `${cut(filename, Math.max(maxLength - HASHED, 0), major)}_${(await sha256Hex(hashed, quote(depPath))).slice(0, 32)}`
26
+ // By pnpm's createBase32Hash, or createShortHash.
27
+ const hash = `_${major < 10 ? md5Base32(hashed, quote(depPath)) : (await sha256Hex(hashed, quote(depPath))).slice(0, 32)}`
28
+ return `${cut(filename, Math.max(maxLength - hash.length, 0), major)}${hash}`
40
29
  }