@preventive/deptree 1.0.0-alpha.0 → 1.0.0-alpha.2

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/package.json CHANGED
@@ -1,17 +1,23 @@
1
1
  {
2
2
  "name": "@preventive/deptree",
3
- "version": "1.0.0-alpha.0",
4
- "description": "A node_modules tree in memory, as a package manager would install it: pnpm 10 or 11 from its lockfile, tarballs fetched and checked, patches applied",
3
+ "version": "1.0.0-alpha.2",
4
+ "description": "A node_modules tree in memory, as a package manager would install it: pnpm 10 or 11, or yarn 1.22, from its lockfile, tarballs fetched and checked, patches applied",
5
5
  "type": "module",
6
6
  "exports": {
7
7
  "./pnpm.js": {
8
8
  "types": "./pnpm.d.ts",
9
9
  "default": "./pnpm.js"
10
+ },
11
+ "./yarn1.js": {
12
+ "types": "./yarn1.d.ts",
13
+ "default": "./yarn1.js"
10
14
  }
11
15
  },
12
16
  "files": [
13
17
  "pnpm.d.ts",
14
18
  "pnpm.js",
19
+ "yarn1.d.ts",
20
+ "yarn1.js",
15
21
  "src/error.js",
16
22
  "src/hash.js",
17
23
  "src/matcher.js",
@@ -24,18 +30,33 @@
24
30
  "src/pnpm/graph.js",
25
31
  "src/pnpm/hook.js",
26
32
  "src/pnpm/hoist.js",
33
+ "src/pnpm/inputs.js",
27
34
  "src/pnpm/install.js",
28
35
  "src/pnpm/local.js",
29
36
  "src/pnpm/npmrc.js",
30
37
  "src/pnpm/overrides.js",
38
+ "src/pnpm/packlist.js",
31
39
  "src/pnpm/patches.js",
40
+ "src/pnpm/project.js",
32
41
  "src/pnpm/projects.js",
33
42
  "src/pnpm/readers.js",
34
43
  "src/pnpm/settings.js",
35
44
  "src/pnpm/tree.js",
36
45
  "src/pnpm/uptodate.js",
37
46
  "src/pnpm/workspace.js",
38
- "src/tarball.js"
47
+ "src/tarball.js",
48
+ "src/yarn1/bins.js",
49
+ "src/yarn1/compat.js",
50
+ "src/yarn1/glob.js",
51
+ "src/yarn1/hoist.js",
52
+ "src/yarn1/inputs.js",
53
+ "src/yarn1/manifest.js",
54
+ "src/yarn1/package.js",
55
+ "src/yarn1/peers.js",
56
+ "src/yarn1/requests.js",
57
+ "src/yarn1/resolve.js",
58
+ "src/yarn1/settings.js",
59
+ "src/yarn1/tree.js"
39
60
  ],
40
61
  "engines": {
41
62
  "node": ">=24.14.0"
@@ -64,8 +85,8 @@
64
85
  "dependencies": {
65
86
  "@preventive/archive": "1.0.0-beta.3",
66
87
  "@preventive/diff": "^1.0.0",
67
- "@preventive/lockfile": "1.0.0-alpha.1",
68
- "@preventive/upstream": "1.0.0-alpha.1",
69
- "@preventive/vfs": "1.0.0-beta.1"
88
+ "@preventive/lockfile": "1.0.0-alpha.2",
89
+ "@preventive/upstream": "1.0.0-alpha.2",
90
+ "@preventive/vfs": "1.0.0-beta.2"
70
91
  }
71
92
  }
package/pnpm.d.ts CHANGED
@@ -1,49 +1,91 @@
1
1
  // Hand-written against pnpm.js; a change to either belongs with the other.
2
2
 
3
- import type { Vfs } from '@preventive/vfs'
3
+ import type { NodeType, Vfs } from '@preventive/vfs'
4
4
 
5
5
  export { LockfileError, YamlError } from '@preventive/lockfile/pnpm.js'
6
6
 
7
7
  // Where @preventive/upstream caches what it fetches, tarballs among them.
8
- // Unset, which it is by default, nothing is cached and nothing is written:
9
- // tarballs are fetched every time, each once for however many snapshots it
10
- // has. It is the one place anything here touches a filesystem.
8
+ // Tarballs are fetched through it, and that is the one place anything here
9
+ // touches a filesystem. Before the network, it takes a tarball from npm's
10
+ // cache — under $npm_config_cache, or ~/.npm — or from ~/.audit/cache/tgz,
11
+ // where one there has the lockfile's integrity, and writes neither; then
12
+ // from its own cache, where one is set, which is where it writes each
13
+ // tarball it fetches. Unset, which it is by default, it has none, and
14
+ // writes nothing: a tarball in neither of the others is fetched every
15
+ // time, each once for however many snapshots it has. The project is read
16
+ // only through the view given as `project`, and the tree built in a Vfs.
11
17
  export { setCacheDir } from '@preventive/upstream/npm.js'
12
18
 
13
19
  // The machine pnpm would install on, which a tree depends on: `pnpm` is
14
20
  // the version that installs, and has to be a 10.x or an 11.x, the two
15
- // built for, each as it differs from the other; `node` the Node it runs
16
- // on, unless the settings name a nodeVersion or, for pnpm 11, the root
17
- // package.json's engines.runtime pins one;
18
- // `os`, `cpu` and `libc` as Node and pnpm name them — `linux`, `x64`,
19
- // `glibc` — with `unknown` for a libc outside Linux, as pnpm has it.
21
+ // built for, each as it differs from the other; left out, it is the one
22
+ // the root package.json's packageManager pins, which pnpm switches to, and
23
+ // has to be given where that pins none. `node` is the Node it runs on,
24
+ // unless the settings name a nodeVersion or, for pnpm 11, the root
25
+ // package.json's engines.runtime pins one; `os`, `cpu` and `libc` as Node
26
+ // and pnpm name them — `linux`, `x64`, `glibc` — with `unknown` for a libc
27
+ // outside Linux, as pnpm has it.
20
28
  // Windows is refused: pnpm links there with junctions to absolute paths.
21
29
  // An optional package the machine cannot run is left out, as pnpm leaves
22
30
  // it out; supportedArchitectures in the settings widens what is taken.
23
31
  export interface PnpmHost {
24
- pnpm: string
32
+ pnpm?: string
25
33
  node: string
26
34
  os: string
27
35
  cpu: string
28
36
  libc: 'glibc' | 'musl' | 'unknown'
29
37
  }
30
38
 
31
- // The files an install reads, as text: pnpm-lock.yaml; the package.json
32
- // of every project it installs, by the project's directory relative to the
33
- // lockfile's (`.` for the root, then as its importers are keyed); pnpm-
34
- // workspace.yaml and the .npmrc, where there are any; and every patch file
35
- // the settings' patchedDependencies name, by the path from the lockfile's
36
- // directory they name it by. Settings from anywhere else — another
37
- // .npmrc, the environment, the command line — are not read, and are taken
38
- // to be at their defaults.
39
+ // A view of the lockfile's directory, by paths from `/`: a Vfs, or
40
+ // anything with its readdir, lstat, stat and readFile, such as one of a
41
+ // directory on disk, of which only this is read: the names in a
42
+ // directory; what a path is, `file`, `directory` or `symlink`, and its
43
+ // mode, with lstat, and what it leads to with stat; and a file's bytes.
44
+ // Each throws an error whose `code` is ENOENT, ENOTDIR or ELOOP for a path
45
+ // that leads nowhere. Nothing is written to it.
46
+ export interface PnpmProject {
47
+ readdir(path: string): string[]
48
+ lstat(path: string): { type: NodeType, mode: number }
49
+ stat(path: string): { type: NodeType }
50
+ readFile(path: string): Uint8Array
51
+ }
52
+
53
+ // The files an install reads: pnpm-lock.yaml; the package.json of every
54
+ // project it installs; pnpm-workspace.yaml and the .npmrc, where there are
55
+ // any; and every patch file the settings' patchedDependencies name.
56
+ //
57
+ // With `lockfile`, they are given as text: `manifests` by the project's
58
+ // directory relative to the lockfile's (`.` for the root, then as its
59
+ // importers are keyed), `patches` by the path from the lockfile's
60
+ // directory the settings name each by, and only those.
61
+ //
62
+ // Without it, they are read from `project`, as pnpm reads them there, and
63
+ // none of them may be given: pnpm-lock.yaml, which has to be there;
64
+ // pnpm-workspace.yaml, which, where there is none, pnpm refuses under
65
+ // another name it looks for it by, such as pnpm-workspace.yml; the .npmrc;
66
+ // the package.json of every project pnpm finds, as findPnpmProjects finds
67
+ // them, and no other, an importer pnpm-workspace.yaml's packages do not
68
+ // take refused unread; and each patch the settings name, and no other,
69
+ // which has to be under the lockfile's directory. Each is read as
70
+ // UTF-8, and refused where it is not, a byte order mark kept as it is in
71
+ // text given; a refusal names each as it would given:
72
+ // `manifests["packages/x"]`, `patches["patches/p.patch"]`.
73
+ //
74
+ // Settings from anywhere else — another .npmrc, the environment, the
75
+ // command line — are not read, and are taken to be at their defaults.
39
76
  //
40
77
  // A workspace is one lockfile for several projects, each a package.json:
41
78
  // the root, and every directory pnpm-workspace.yaml's `packages` globs
42
79
  // take, as pnpm globs for them — `*` and `**`, a leading `!` to leave out,
43
80
  // and no other glob syntax. Without `packages` the root is the only
44
- // project. Every importer has to have its package.json given, and be a
45
- // project those globs take; and every project pnpm would find has to be
46
- // given, which cannot be checked here, as the directories are not.
81
+ // project. Every importer has to have its package.json, and be a project
82
+ // those globs take; and every project pnpm would find has to be given,
83
+ // which buildPnpmTree cannot check where it is given them, as it is not
84
+ // given the directories: findPnpmProjects finds them in those. A project the
85
+ // lockfile has no importer for is held to an empty one, as pnpm holds it:
86
+ // with no dependencies it is installed, and hoisted by its name, and with
87
+ // any it is refused as not up to date. Its directory has to be given as
88
+ // pnpm would key its importer: from the lockfile's, in normal form.
47
89
  //
48
90
  // Each project is held to its importer as --frozen-lockfile holds it: its
49
91
  // dependencies, devDependencies, optionalDependencies and, with
@@ -59,11 +101,18 @@ export interface PnpmHost {
59
101
  // those name none are pnpm-workspace.yaml's `overrides` read. `$name` in
60
102
  // one is the root package.json's own specifier for `name`, and
61
103
  // `catalog:` what the workspace's catalog gives the package. One to a
62
- // directory — a path alone, such as `./vendor/foo`, or by `link:` — is
63
- // read only with `vfs` given, and only where the directory is under the
64
- // lockfile's and holds a package.json there; pnpm links it. One to a copy
65
- // of a directory, by `file:`, is refused: pnpm picks the files it copies
66
- // with npm-packlist.
104
+ // directory — a path alone, such as `./vendor/foo`, `link:` or `file:` —
105
+ // is read only with `project` given, and only where the directory is
106
+ // under the lockfile's and holds a package.json there. pnpm links to it by a
107
+ // path alone or `link:`. By `file:` it installs it as a package, of the
108
+ // files npm-packlist's built-in rules keep, which are followed here alone:
109
+ // a directory with a .npmignore or .gitignore, a package.json with `files`
110
+ // or bundled dependencies, or a link in it, is refused. pnpm hardlinks
111
+ // those files from the directory into each snapshot of the package, so a
112
+ // file linking a bin makes executable is made so in every snapshot and in
113
+ // the directory, which is not written here; a CRLF `#!` line it rewrites
114
+ // is rewritten as a file of that snapshot's own. One by `file:` to a
115
+ // tarball is refused, and so is a `file:` dependency no override names.
67
116
  //
68
117
  // Of the .npmrc, only what pnpm reads for an install is read: its settings
69
118
  // by their kebab-case names, those that can change the tree held to what
@@ -84,31 +133,54 @@ export interface PnpmHost {
84
133
  // lockfile against the registry by before it installs — each package's
85
134
  // publish time, its tarball URL, its trust — are passed over.
86
135
  //
136
+ // `project` is read only where it is said to be here: for the files an
137
+ // install reads, without `lockfile`; for the directories a `link:` or an
138
+ // override leads to. With it given, a directory the tree links to that is
139
+ // no project, and is under the lockfile's, has to be there with a
140
+ // package.json, whose bins are read as pnpm reads them. Without it, or
141
+ // outside the lockfile's directory, such a directory's bins are not
142
+ // known, and neither are those of a project that has them by the files of
143
+ // its directories.bin: where one could win a command a package's file is
144
+ // linked by, and so decide whether pnpm fixes that file, and fixing it
145
+ // would change it, the tree is refused.
146
+ //
87
147
  // `vfs` is a Vfs to mount the tree into, at its root, which is taken to
88
148
  // be the lockfile's directory, beside whatever it holds, such as the
89
- // projects themselves; without one, a new Vfs holds the tree alone. A Vfs
90
- // that holds a node_modules anywhere, or on macOS a name that is one
149
+ // projects themselves — it may be `project` too; without one, a new Vfs
150
+ // holds the tree alone. Nothing the tree is built from is read from it. A
151
+ // Vfs that holds a node_modules anywhere, or on macOS a name that is one
91
152
  // there, is refused before anything is fetched: kept beside the tree, Node
92
153
  // would read it as the tree's, and removed, it would be the caller's lost.
93
154
  // Nothing there is written over: each directory the tree has is one there
94
155
  // or is made, and every file and link is written where nothing is. The
95
156
  // tree is built, and held to every check below, before any of it is
96
- // written, so a refusal leaves the Vfs as it was. With it given, a
97
- // directory the tree links to that is no project, and is under the
98
- // lockfile's, has to be there with a package.json, whose bins are read as
99
- // pnpm reads them; nothing outside node_modules is written, though pnpm
100
- // 10 makes the files a linked directory's bins run executable too.
101
- // Without it, or outside the lockfile's directory, such a directory's
102
- // bins are not known, and neither are those of a project that has them
103
- // by the files of its directories.bin: where one could win a command a
104
- // package's file is linked by, and so decide whether pnpm fixes that
105
- // file, and fixing it would change it, the tree is refused.
106
- export interface PnpmTreeOptions {
157
+ // written, so a refusal leaves the Vfs as it was. Nothing outside
158
+ // node_modules is written, though pnpm 10 makes the files a linked
159
+ // directory's bins run executable too.
160
+ //
161
+ // The two ways the files come, one or the other: given, with `lockfile`
162
+ // and `manifests`, `project` read only for directories; or read, with
163
+ // `project` and none of them.
164
+ export type PnpmTreeOptions = PnpmTreeGiven | PnpmTreeRead
165
+
166
+ export interface PnpmTreeGiven {
107
167
  lockfile: string
108
168
  manifests: Record<string, string> | Map<string, string>
109
169
  workspace?: string
110
170
  npmrc?: string
111
171
  patches?: Record<string, string> | Map<string, string>
172
+ project?: PnpmProject
173
+ host: PnpmHost
174
+ vfs?: Vfs
175
+ }
176
+
177
+ export interface PnpmTreeRead {
178
+ lockfile?: undefined
179
+ manifests?: undefined
180
+ workspace?: undefined
181
+ npmrc?: undefined
182
+ patches?: undefined
183
+ project: PnpmProject
112
184
  host: PnpmHost
113
185
  vfs?: Vfs
114
186
  }
@@ -217,6 +289,38 @@ export interface PnpmTree {
217
289
  // TypeError is thrown for options of the wrong type.
218
290
  export function buildPnpmTree(options: PnpmTreeOptions): Promise<PnpmTree>
219
291
 
292
+ // The directories of the projects pnpm finds in `project`, by which
293
+ // buildPnpmTree takes their package.json given: `.`, the root, first, then
294
+ // in order each directory whose package.json a glob of
295
+ // pnpm-workspace.yaml's `packages` takes, as pnpm `host.pnpm` finds them.
296
+ // pnpm-workspace.yaml is read from `project`, as buildPnpmTree reads it;
297
+ // so is the root package.json, where host.pnpm is left out, for the pnpm
298
+ // its packageManager pins.
299
+ //
300
+ // Of `project`, besides those, only the directories pnpm walks into are
301
+ // read: none under node_modules or bower_components, and none whose name
302
+ // starts with a dot unless a glob spells it there. A manifest is a file,
303
+ // or a link to one; a link that leads nowhere, or to a directory, is none,
304
+ // as pnpm has it.
305
+ //
306
+ // Refused: a root with no manifest, which pnpm takes for no project; a
307
+ // project, the root among them, whose manifest is package.json5 or
308
+ // package.yaml, which pnpm reads where there is no package.json, or is a
309
+ // link; a project pnpm finds through a link to a directory, which it
310
+ // follows, and a link to a directory in one followed, which is not; a
311
+ // project in a directory a lockfile could not key its importer by, with a
312
+ // control, bidirectional or backslash character in its path, or a drive
313
+ // letter; a node_modules pnpm walks into, which it does only under a
314
+ // directory with a leading dot that a glob spells; a glob buildPnpmTree
315
+ // refuses, or a pnpm not 10.x or 11.x; and a pnpm-workspace.yaml that is
316
+ // not a mapping, is not UTF-8, or is under another name pnpm refuses.
317
+ export interface PnpmProjectsOptions {
318
+ project: PnpmProject
319
+ host?: Pick<PnpmHost, 'pnpm'>
320
+ }
321
+
322
+ export function findPnpmProjects(options: PnpmProjectsOptions): string[]
323
+
220
324
  // `where` is what a refusal is about — `pnpm-workspace.yaml: nodeLinker`,
221
325
  // `.npmrc:3: node-linker`, a package's key — or undefined for the call as
222
326
  // a whole; the message leads with it. `cause` is what a package beneath
package/pnpm.js CHANGED
@@ -1,11 +1,13 @@
1
1
  // A node_modules tree built in memory from a pnpm lockfile, as pnpm 10 or
2
2
  // 11 installs it with the isolated linker: pnpm.d.ts says what it takes and
3
- // what comes back. Nothing here touches a filesystem; tarballs come
4
- // through @preventive/upstream, and whatever cache it keeps is its own.
3
+ // what comes back. The project is read through the view given, and the
4
+ // tree built in a Vfs; the one filesystem touched is that of the tarball
5
+ // caches @preventive/upstream reads, and writes where it keeps its own.
5
6
  export { buildPnpmTree } from './src/pnpm/tree.js'
7
+ export { findPnpmProjects } from './src/pnpm/inputs.js'
6
8
  export { DeptreeError } from './src/error.js'
7
9
  // Where @preventive/upstream caches what it fetches, tarballs among it;
8
- // unset, nothing is cached and nothing is written.
10
+ // unset, it keeps no cache of its own, and writes nothing.
9
11
  export { setCacheDir } from '@preventive/upstream/npm.js'
10
12
  // What the inputs beneath are refused with: the lockfile, and the YAML of
11
13
  // it and of pnpm-workspace.yaml.
package/src/hash.js CHANGED
@@ -21,3 +21,7 @@ export async function sha256Hex(text, where) {
21
21
  // Whether `bytes` are what a subresource integrity names: a sha512, the
22
22
  // only kind taken, in padded base64.
23
23
  export const matchesIntegrity = async (bytes, integrity) => integrity === `sha512-${base64(await digest('SHA-512', bytes))}`
24
+
25
+ // The hex sha1 of bytes, as yarn 1 records a tarball's after the `#` of
26
+ // its URL.
27
+ export const sha1Hex = async (bytes) => hex(await digest('SHA-1', bytes))
package/src/mount.js CHANGED
@@ -39,20 +39,27 @@ function typeAt(vfs, path) {
39
39
  // Each file is taken out of `tree` as it is written.
40
40
  export function mount(tree, target, folded) {
41
41
  checkNoModules(target, folded)
42
- const entries = [...tree.walk('/')].filter(({ path }) => path !== '/')
43
- for (const { path, type } of entries) {
44
- const existing = typeAt(target, path)
42
+ // The tree's entries, parents first, each with what is there already;
43
+ // under a directory that is not there, nothing is.
44
+ const absent = new Set()
45
+ const entries = []
46
+ for (const { path, type } of tree.walk('/')) {
47
+ if (path === '/') continue
48
+ const parent = dirname(path)
49
+ const existing = absent.has(parent) ? undefined : typeAt(target, path)
50
+ if (existing === undefined && type === 'directory') absent.add(path)
51
+ entries.push({ path, type, existing })
45
52
  if (existing !== undefined && (type !== 'directory' || existing !== 'directory')) {
46
53
  throw new DeptreeError(`a ${existing} is there already, where the tree has a ${type}`, where(path))
47
54
  }
48
- if (!folded || existing !== undefined || typeAt(target, dirname(path)) !== 'directory') continue
55
+ if (!folded || existing !== undefined || absent.has(parent)) continue
49
56
  const name = basename(path)
50
- const clash = target.readdir(dirname(path)).find((other) => fold(other) === fold(name))
57
+ const clash = target.readdir(parent).find((other) => fold(other) === fold(name))
51
58
  if (clash !== undefined) throw new DeptreeError(`${quote(clash)} is there already, which is one name with ${quote(name)} on macOS`, where(path))
52
59
  }
53
- for (const { path, type } of entries) {
60
+ for (const { path, type, existing } of entries) {
54
61
  if (type === 'directory') {
55
- if (typeAt(target, path) === undefined) target.mkdir(path)
62
+ if (existing === undefined) target.mkdir(path)
56
63
  } else if (type === 'symlink') {
57
64
  target.symlink(tree.readlink(path), path)
58
65
  } else {
package/src/pnpm/bins.js CHANGED
@@ -197,7 +197,7 @@ function bundledIn(files) {
197
197
 
198
198
  function parseManifest(file, where) {
199
199
  try {
200
- const manifest = JSON.parse(decoder.decode(file.data).replace(/^/u, ''))
200
+ const manifest = JSON.parse(decoder.decode(file.data))
201
201
  if (manifest === null || typeof manifest !== 'object' || Array.isArray(manifest)) throw new Error('not an object')
202
202
  return manifest
203
203
  } catch {
@@ -248,7 +248,7 @@ function peersOf(children, nodes) {
248
248
  // Whether pnpm builds a package, as its pkgRequiresBuild tells: an
249
249
  // install script, a binding.gyp or a .hooks directory.
250
250
  const hasInstallScript = (manifest, files) => Boolean((manifest.scripts != null && (manifest.scripts.preinstall || manifest.scripts.install || manifest.scripts.postinstall))
251
- || files.has('binding.gyp') || [...files.keys()].some((path) => /^\.hooks[\\/]/u.test(path)))
251
+ || files.has('binding.gyp') || files.keys().some((path) => /^\.hooks[\\/]/u.test(path)))
252
252
 
253
253
  // The files pnpm runs fixBin on, by the directory of the package they
254
254
  // are in: `nodes` is the graph by directory, each node with its package's
@@ -259,7 +259,7 @@ const hasInstallScript = (manifest, files) => Boolean((manifest.scripts != null
259
259
  // installed automatically, and `major` pnpm's major version.
260
260
  export function binTargets({ nodes, projects, direct, links, publicHoist, building, peers, major }) {
261
261
  const commandCache = new Map()
262
- const commandsOfDir = (dir, { raw = false } = {}) => {
262
+ const commandsOfDir = (dir, { normalize = false } = {}) => {
263
263
  const node = nodes.get(dir)
264
264
  if (node !== undefined) {
265
265
  if (!commandCache.has(dir)) commandCache.set(dir, commandsOf(dir, node.manifest, node.files, dir, quote(node.key), major))
@@ -271,7 +271,7 @@ export function binTargets({ nodes, projects, direct, links, publicHoist, buildi
271
271
  const manifest = projects.get(dir)
272
272
  if (manifest === undefined) return [UNKNOWN]
273
273
  const where = `manifests[${quote(dir)}]`
274
- return commandsOf(dir, raw ? manifest : normalized(manifest, where), undefined, undefined, where, major)
274
+ return commandsOf(dir, normalize ? normalized(manifest, where) : manifest, undefined, undefined, where, major)
275
275
  }
276
276
 
277
277
  // By each file's path in the tree, the package it is in; and of those
@@ -279,11 +279,10 @@ export function binTargets({ nodes, projects, direct, links, publicHoist, buildi
279
279
  const fixed = new Map()
280
280
  const contested = new Map()
281
281
  // One linking of commands into a directory: `ordered` says whether the
282
- // order they come in is one pnpm always has, and `blind` whether a
283
- // command not known here may be among them. It gives back the names
282
+ // order they come in is one pnpm always has. It gives back the names
284
283
  // linked, and whether others may be.
285
- const link = (commands, { ordered, blind = false, where }) => {
286
- const unknown = blind || commands.some((command) => command.unknown)
284
+ const link = (commands, { ordered, where }) => {
285
+ const unknown = commands.some((command) => command.unknown)
287
286
  const best = new Map()
288
287
  for (const command of commands) {
289
288
  if (command.unknown) continue
@@ -309,7 +308,7 @@ export function binTargets({ nodes, projects, direct, links, publicHoist, buildi
309
308
  const children = [...node.children.values()]
310
309
  const withBins = children.filter((dir) => nodes.get(dir)?.pkg.hasBin).flatMap((dir) => commandsOfDir(dir))
311
310
  if (children.every((dir) => nodes.has(dir))) link(withBins, { ordered: true, where })
312
- else link(children.flatMap((dir) => commandsOfDir(dir)), { ordered: false, where })
311
+ else link(children.flatMap((dir) => commandsOfDir(dir, { normalize: true })), { ordered: false, where })
313
312
  if (node.pkg.bundledDependencies !== undefined) link(bundledCommands(node, where, major), { ordered: false, where })
314
313
  if (building && (node.pkg.patchHash !== undefined || hasInstallScript(node.manifest, node.files))) {
315
314
  link([...withBins, ...commandsOfDir(node.dir)], { ordered: true, where })
@@ -318,7 +317,7 @@ export function binTargets({ nodes, projects, direct, links, publicHoist, buildi
318
317
 
319
318
  const PRIVATE = 'node_modules/.pnpm/node_modules/'
320
319
  const hoisted = [...links].filter(([path, dir]) => path.startsWith(PRIVATE) && nodes.get(dir)?.pkg.hasBin)
321
- link(hoisted.flatMap(([, dir]) => commandsOfDir(dir, { raw: true })), { ordered: true, where: quote(PRIVATE.slice(0, -1)) })
320
+ link(hoisted.flatMap(([, dir]) => commandsOfDir(dir)), { ordered: true, where: quote(PRIVATE.slice(0, -1)) })
322
321
 
323
322
  for (const [id, children] of direct) {
324
323
  const where = `importers[${quote(id)}]`
@@ -327,17 +326,18 @@ export function binTargets({ nodes, projects, direct, links, publicHoist, buildi
327
326
  const manifest = projects.get('.')
328
327
  const own = new Set(Object.keys({ ...manifest.devDependencies, ...manifest.dependencies, ...manifest.optionalDependencies }))
329
328
  const entries = [...links].filter(([path]) => /^node_modules\/(?:@[^/]+\/)?[^/@.][^/]*$/u.test(path))
330
- const commands = entries.flatMap(([path, dir]) => commandsOfDir(dir, { raw: true }).map((command) => ({ ...command, direct: own.has(path.slice('node_modules/'.length)) })))
329
+ const commands = entries.flatMap(([path, dir]) => commandsOfDir(dir).map((command) => ({ ...command, direct: own.has(path.slice('node_modules/'.length)) })))
331
330
  const names = new Set(commands.filter((command) => command.direct).map(({ name }) => name))
332
331
  linked = link([...commands.filter((command) => command.direct), ...commands.filter((command) => !command.direct && !names.has(command.name))], { ordered: false, where })
333
332
  } else {
334
333
  // pnpm 11 reads the bins only of what the lockfile says has some.
335
334
  const dirs = [...children.values()].filter((dir) => major < 11 || (nodes.get(dir)?.pkg.hasBin ?? true))
336
- linked = link(dirs.flatMap((dir) => commandsOfDir(dir, { raw: true })), { ordered: true, where })
335
+ linked = link(dirs.flatMap((dir) => commandsOfDir(dir)), { ordered: true, where })
337
336
  }
338
337
  if (major >= 11 && peers) {
339
- const commands = peersOf(children, nodes).flatMap((dir) => commandsOfDir(dir, { raw: true }))
340
- link(commands.filter(({ name }) => !linked.names.has(name)), { ordered: true, blind: linked.unknown, where })
338
+ // Where the names linked are not all known, neither are those left.
339
+ const commands = peersOf(children, nodes).flatMap((dir) => commandsOfDir(dir))
340
+ link([...commands.filter(({ name }) => !linked.names.has(name)), ...linked.unknown ? [UNKNOWN] : []], { ordered: true, where })
341
341
  }
342
342
  }
343
343
 
@@ -8,8 +8,9 @@
8
8
 
9
9
  import { packageKeyOf } from '@preventive/lockfile/pnpm.js'
10
10
  import { satisfies, valid, validRange } from '@preventive/upstream/semver.js'
11
- import { join, normalize } from '@preventive/vfs/path.js'
11
+ import { join } from '@preventive/vfs/path.js'
12
12
  import { DeptreeError, quote } from '../error.js'
13
+ import { catalogEntry, catalogOf } from './overrides.js'
13
14
 
14
15
  const GIT_HOSTS = new Set(['github.com', 'gitlab.com', 'bitbucket.org'])
15
16
  const SHORTCUTS = [['github:', 'github.com'], ['gitlab:', 'gitlab.com'], ['bitbucket:', 'bitbucket.org']]
@@ -57,20 +58,21 @@ export function sameSpecifier(a, b, major) {
57
58
  }
58
59
 
59
60
  // The version an importer's target resolved to, as the lockfile spells
60
- // it: a key of the alias's own name is its version and peers.
61
- export const refOf = (alias, target) => (target.startsWith(`${alias}@`) ? target.slice(alias.length + 1) : target)
61
+ // it, peers and all left out: a key of the alias's own name is its
62
+ // version.
63
+ export const resolvedOf = (alias, target) => packageKeyOf(target.startsWith(`${alias}@`) ? target.slice(alias.length + 1) : target)
62
64
 
63
65
  const targetOf = (importer, alias) => importer.dependencies[alias] ?? importer.devDependencies[alias] ?? importer.optionalDependencies[alias]
64
66
 
65
67
  // catalogResolutionsAreUpToDate.
66
68
  export function checkCatalogResolutions(importer, catalogs, where) {
67
69
  for (const [alias, specifier] of Object.entries(importer.specifiers)) {
68
- if (!specifier.startsWith('catalog:')) continue
69
- const name = specifier.slice('catalog:'.length).trim() || 'default'
70
- const version = Object.hasOwn(catalogs, name) && Object.hasOwn(catalogs[name], alias) ? catalogs[name][alias].version : undefined
70
+ const name = catalogOf(specifier)
71
+ if (name === undefined) continue
72
+ const version = catalogEntry(catalogs, name, alias)?.version
71
73
  const target = targetOf(importer, alias)
72
74
  if (version === undefined || target === undefined) continue
73
- const resolved = packageKeyOf(refOf(alias, target))
75
+ const resolved = resolvedOf(alias, target)
74
76
  if (valid(resolved) !== null && resolved !== version) throw new DeptreeError(`${quote(alias)} resolved to ${quote(resolved)}, and the lockfile's catalog ${quote(name)} to ${quote(version)}, which pnpm 11 refuses a frozen install for`, where)
75
77
  }
76
78
  }
@@ -81,7 +83,7 @@ export function checkCatalogResolutions(importer, catalogs, where) {
81
83
  function specPath(dir, path, where) {
82
84
  const clean = path.startsWith('./') ? path.slice(2) : path
83
85
  if (/^(?:~[/\\]|[/\\]|[A-Za-z]:)/u.test(clean) || clean.includes('\\')) throw new DeptreeError(`${quote(path)} is not a path from the project, which is not supported`, where)
84
- return normalize(join(dir, clean.replace(/\/+$/u, '')))
86
+ return join(dir, clean.replace(/\/+$/u, ''))
85
87
  }
86
88
 
87
89
  const isWorkspacePath = (spec) => /^(?:[./\\]|~[/\\]|[A-Za-z]:)/u.test(spec)
@@ -124,7 +126,7 @@ const inRange = (version, range) => range === '*' || range === '^' || range ===
124
126
  // version-selector-type's tag: a name that is no version or range.
125
127
  const isTag = (range) => valid(range, { loose: true }) === null && validRange(range, { loose: true }) === null && encodeURIComponent(range) === range
126
128
 
127
- const KINDS = ['optionalDependencies', 'dependencies', 'devDependencies']
129
+ export const KINDS = ['optionalDependencies', 'dependencies', 'devDependencies']
128
130
 
129
131
  // The directory a specifier names, where it names one: by `link:`,
130
132
  // `file:`, a `workspace:` path, or a path alone, as pnpm reads one. One
@@ -133,8 +135,8 @@ const KINDS = ['optionalDependencies', 'dependencies', 'devDependencies']
133
135
  // links it.
134
136
  function pathOf(spec) {
135
137
  if (spec.startsWith('link:') || spec.startsWith('file:')) return spec.slice(5)
136
- if (spec.startsWith('workspace:')) return isWorkspacePath(spec.slice('workspace:'.length)) ? spec.slice('workspace:'.length) : undefined
137
- return /^(?:[./\\]|~[/\\]|[a-z]:)/iu.test(spec) ? spec : undefined
138
+ const path = spec.startsWith('workspace:') ? spec.slice('workspace:'.length) : spec
139
+ return isWorkspacePath(path) ? path : undefined
138
140
  }
139
141
 
140
142
  // A dependency the lockfile links has to be linked where the project's
@@ -157,24 +159,30 @@ export function checkLinkTargets({ id, manifest, importer }, where) {
157
159
  }
158
160
  }
159
161
 
160
- // checkLinkedPackagesAreUpToDate, for the project at `id`, less the
161
- // directories a package.json names, which checkLinkTargets holds links to:
162
- // `manifest` is its package.json as read through the read-package hook,
163
- // `projects` every project's by its directory, as given, and
164
- // `linkWorkspacePackages` the setting.
165
- export function checkLinkedPackages({ manifest, importer, projects, linkWorkspacePackages }, where) {
162
+ // Every project's package.json, as given, by its directory, and by where
163
+ // pnpm 11 links it from, its publishConfig.directory among them; and the
164
+ // directory of each by name and version, of which no two may be one.
165
+ export function indexProjects(projects) {
166
166
  const byName = new Map()
167
167
  const byDir = new Map()
168
168
  for (const [dir, project] of projects) {
169
169
  byDir.set(dir, project)
170
170
  const directory = project.publishConfig?.directory
171
- if (typeof directory === 'string' && project.publishConfig.linkDirectory !== false) byDir.set(normalize(join(dir, directory)), project)
171
+ if (typeof directory === 'string' && project.publishConfig.linkDirectory !== false) byDir.set(join(dir, directory), project)
172
172
  if (!project.name) continue
173
173
  if (!byName.has(project.name)) byName.set(project.name, new Map())
174
174
  const version = project.version ?? '0.0.0'
175
175
  if (byName.get(project.name).has(version)) throw new DeptreeError(`${quote(String(project.name))} at ${quote(String(version))} is another project's name and version too, which leaves which pnpm 11 checks a dependency against to the order it finds them in`, `manifests[${quote(dir)}]`)
176
176
  byName.get(project.name).set(version, dir)
177
177
  }
178
+ return { projects, byName, byDir }
179
+ }
180
+
181
+ // checkLinkedPackagesAreUpToDate, less the directories a package.json
182
+ // names, which checkLinkTargets holds links to: `manifest` is the
183
+ // project's package.json as read through the read-package hook, `index`
184
+ // indexProjects's, and `linkWorkspacePackages` the setting.
185
+ export function checkLinkedPackages({ manifest, importer, index: { projects, byName, byDir }, linkWorkspacePackages }, where) {
178
186
  const outdated = (detail) => new DeptreeError(`the lockfile is not up to date with this package.json, which pnpm 11 refuses a frozen install for: ${detail}`, where)
179
187
  for (const kind of KINDS) {
180
188
  const wanted = manifest[kind]
@@ -185,15 +193,17 @@ export function checkLinkedPackages({ manifest, importer, projects, linkWorkspac
185
193
  const here = `${where}.${kind}.${alias}`
186
194
  const workspaceRange = spec.startsWith('workspace:') && !isWorkspacePath(spec.slice('workspace:'.length))
187
195
  const linked = target.startsWith('link:')
196
+ // A local directory or tarball is up to date where the lockfile has
197
+ // it, as pnpm 11's frozen install skips its own dependencies; which
198
+ // is installed is held to tree.js's checkSource.
188
199
  const local = importer.specifiers[alias].startsWith('file:') || packageKeyOf(target).includes('@file:')
189
- if (local && !linked) throw new DeptreeError('a dependency on a local directory or tarball is not supported', here)
190
200
  if (local) continue
191
201
  if (linked && pathOf(spec) !== undefined) continue
192
202
  const name = targetName(spec, alias)
193
203
  const range = versionRange(spec)
194
204
  if (linked && isTag(range)) continue
195
205
  const named = byName.get(name)
196
- const dir = linked ? target.slice('link:'.length) : named?.get(packageKeyOf(refOf(alias, target)))
206
+ const dir = linked ? target.slice('link:'.length) : named?.get(resolvedOf(alias, target))
197
207
  if (dir === undefined) {
198
208
  const taking = workspaceRange && named !== undefined ? [...named.keys()].find((version) => inRange(projects.get(named.get(version)).version, range)) : undefined
199
209
  if (taking !== undefined) throw outdated(`the workspace package ${quote(name)} (${taking}) is in the range ${quote(spec)} and not linked`)
package/src/pnpm/hoist.js CHANGED
@@ -38,9 +38,10 @@ function walk(nodes, starts) {
38
38
  const lexCompare = (a, b) => (a > b ? 1 : a < b ? -1 : 0)
39
39
 
40
40
  // The aliases hoisted from the graph, walked from `starts`: a Map of each
41
- // link's path to its target. `taken` holds the names, folded, that none
42
- // may be hoisted by: the root project's own aliases, and, with pnpm 10,
43
- // with `projects` named among them at the top, hoisted last.
41
+ // link's path to its target. `taken` holds the names none may be hoisted
42
+ // by, each alias looked up in it folded: the root project's own aliases,
43
+ // and those hoisted, folded. With pnpm 10, `projects` are named among the
44
+ // top's, and hoisted last.
44
45
  function hoistGraph(nodes, starts, taken, typeOf, projects = new Map()) {
45
46
  const root = new Map([...projects].map(([id, name]) => [name, { project: id }]))
46
47
  const listed = new Set()
@@ -103,7 +104,7 @@ export function hoist(nodes, direct, { hoistPattern, publicHoistPattern }, proje
103
104
  const takenByDependencies = new Set(starts.map(([alias]) => alias.toLowerCase()))
104
105
  const hoistedProjects = [...projects].filter(([, name]) => typeOf(name) !== undefined && !takenByDependencies.has(name.toLowerCase()))
105
106
  const taken = new Set([...rootAliases, ...hoistedProjects.map(([, name]) => name)].map((alias) => alias.toLowerCase()))
106
- const links = nodes.size === 0 ? new Map() : hoistGraph(nodes, starts, taken, typeOf)
107
+ const links = hoistGraph(nodes, starts, taken, typeOf)
107
108
  for (const [id, name] of hoistedProjects) links.set(`${typeOf(name)}/${name}`, id)
108
109
  return links
109
110
  }