@preventive/deptree 1.0.0-alpha.1 → 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.1",
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,19 +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",
31
38
  "src/pnpm/packlist.js",
32
39
  "src/pnpm/patches.js",
40
+ "src/pnpm/project.js",
33
41
  "src/pnpm/projects.js",
34
42
  "src/pnpm/readers.js",
35
43
  "src/pnpm/settings.js",
36
44
  "src/pnpm/tree.js",
37
45
  "src/pnpm/uptodate.js",
38
46
  "src/pnpm/workspace.js",
39
- "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"
40
60
  ],
41
61
  "engines": {
42
62
  "node": ">=24.14.0"
@@ -65,8 +85,8 @@
65
85
  "dependencies": {
66
86
  "@preventive/archive": "1.0.0-beta.3",
67
87
  "@preventive/diff": "^1.0.0",
68
- "@preventive/lockfile": "1.0.0-alpha.1",
69
- "@preventive/upstream": "1.0.0-alpha.1",
70
- "@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"
71
91
  }
72
92
  }
package/pnpm.d.ts CHANGED
@@ -1,50 +1,87 @@
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 buildPnpmTree cannot check, as it is not given the
47
- // directories: findPnpmProjects finds them in those. A project the
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
48
85
  // lockfile has no importer for is held to an empty one, as pnpm holds it:
49
86
  // with no dependencies it is installed, and hoisted by its name, and with
50
87
  // any it is refused as not up to date. Its directory has to be given as
@@ -65,8 +102,8 @@ export interface PnpmHost {
65
102
  // one is the root package.json's own specifier for `name`, and
66
103
  // `catalog:` what the workspace's catalog gives the package. One to a
67
104
  // directory — a path alone, such as `./vendor/foo`, `link:` or `file:` —
68
- // is read only with `vfs` given, and only where the directory is under
69
- // the lockfile's and holds a package.json there. pnpm links to it by a
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
70
107
  // path alone or `link:`. By `file:` it installs it as a package, of the
71
108
  // files npm-packlist's built-in rules keep, which are followed here alone:
72
109
  // a directory with a .npmignore or .gitignore, a package.json with `files`
@@ -96,31 +133,54 @@ export interface PnpmHost {
96
133
  // lockfile against the registry by before it installs — each package's
97
134
  // publish time, its tarball URL, its trust — are passed over.
98
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
+ //
99
147
  // `vfs` is a Vfs to mount the tree into, at its root, which is taken to
100
148
  // be the lockfile's directory, beside whatever it holds, such as the
101
- // projects themselves; without one, a new Vfs holds the tree alone. A Vfs
102
- // 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
103
152
  // there, is refused before anything is fetched: kept beside the tree, Node
104
153
  // would read it as the tree's, and removed, it would be the caller's lost.
105
154
  // Nothing there is written over: each directory the tree has is one there
106
155
  // or is made, and every file and link is written where nothing is. The
107
156
  // tree is built, and held to every check below, before any of it is
108
- // written, so a refusal leaves the Vfs as it was. With it given, a
109
- // directory the tree links to that is no project, and is under the
110
- // lockfile's, has to be there with a package.json, whose bins are read as
111
- // pnpm reads them; nothing outside node_modules is written, though pnpm
112
- // 10 makes the files a linked directory's bins run executable too.
113
- // Without it, or outside the lockfile's directory, such a directory's
114
- // bins are not known, and neither are those of a project that has them
115
- // by the files of its directories.bin: where one could win a command a
116
- // package's file is linked by, and so decide whether pnpm fixes that
117
- // file, and fixing it would change it, the tree is refused.
118
- 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 {
119
167
  lockfile: string
120
168
  manifests: Record<string, string> | Map<string, string>
121
169
  workspace?: string
122
170
  npmrc?: string
123
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
124
184
  host: PnpmHost
125
185
  vfs?: Vfs
126
186
  }
@@ -229,17 +289,19 @@ export interface PnpmTree {
229
289
  // TypeError is thrown for options of the wrong type.
230
290
  export function buildPnpmTree(options: PnpmTreeOptions): Promise<PnpmTree>
231
291
 
232
- // The directories of the projects pnpm finds for the workspace at the
233
- // root of `vfs`, by which buildPnpmTree takes their package.json: `.`, the
234
- // root, first, then in order each directory whose package.json a glob of
235
- // `workspace`'s `packages` takes, as pnpm `host.pnpm` finds them.
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.
236
299
  //
237
- // `vfs` is a Vfs, or anything with its readdir, lstat and stat, such as a
238
- // view of a directory on disk, by paths from `/`; nothing is written. Of
239
- // it, only the directories pnpm walks into are read: none under
240
- // node_modules or bower_components, and none whose name starts with a dot
241
- // unless a glob spells it there. A manifest is a file, or a link to one; a
242
- // link that leads nowhere, or to a directory, is none, as pnpm has it.
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.
243
305
  //
244
306
  // Refused: a root with no manifest, which pnpm takes for no project; a
245
307
  // project, the root among them, whose manifest is package.json5 or
@@ -249,12 +311,12 @@ export function buildPnpmTree(options: PnpmTreeOptions): Promise<PnpmTree>
249
311
  // project in a directory a lockfile could not key its importer by, with a
250
312
  // control, bidirectional or backslash character in its path, or a drive
251
313
  // letter; a node_modules pnpm walks into, which it does only under a
252
- // directory with a leading dot that a glob spells; and a glob
253
- // buildPnpmTree refuses, or a pnpm not 10.x or 11.x.
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.
254
317
  export interface PnpmProjectsOptions {
255
- workspace?: string
256
- host: Pick<PnpmHost, 'pnpm'>
257
- vfs: Pick<Vfs, 'readdir' | 'lstat' | 'stat'>
318
+ project: PnpmProject
319
+ host?: Pick<PnpmHost, 'pnpm'>
258
320
  }
259
321
 
260
322
  export function findPnpmProjects(options: PnpmProjectsOptions): string[]
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.
5
- export { buildPnpmTree, findPnpmProjects } from './src/pnpm/tree.js'
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.
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))
@@ -0,0 +1,125 @@
1
+ // What buildPnpmTree and findPnpmProjects take, checked: the host pnpm
2
+ // would install on, and the files an install reads — given as text, or
3
+ // read from the project (project.js) as pnpm reads them from disk.
4
+
5
+ import { parseYaml } from '@preventive/lockfile/yaml.js'
6
+ import { valid } from '@preventive/upstream/semver.js'
7
+ import { DeptreeError, quote } from '../error.js'
8
+ import { checkProject, readManifestTexts, readPatches, readRootFiles, readText, readWorkspaceText, typeOf } from './project.js'
9
+ import { pinnedPnpm, readManifest, readManifests } from './projects.js'
10
+ import { readers } from './readers.js'
11
+ import { checkWorkspace, findProjects, linkedManifest } from './workspace.js'
12
+
13
+ const LIBC = new Set(['glibc', 'musl', 'unknown'])
14
+
15
+ // The major version of `pnpm`, 10 or 11: what pnpm 11 does differently is
16
+ // read by it, where it is.
17
+ function majorOf(pnpm) {
18
+ const major = Number(valid(pnpm)?.split('.')[0])
19
+ if (major !== 10 && major !== 11) throw new DeptreeError(`pnpm ${quote(pnpm)} is not supported: only pnpm 10 and 11 are`, 'host.pnpm')
20
+ return major
21
+ }
22
+
23
+ // The pnpm that installs, and its major version: `pnpm`, host.pnpm, or
24
+ // where that is left out, the one the root package.json pins by its
25
+ // packageManager, which is the only one it takes; `root` gives that
26
+ // package.json as parsed, and is called only then.
27
+ function pnpmOf(pnpm, root) {
28
+ if (pnpm !== undefined && (typeof pnpm !== 'string' || pnpm === '')) throw new TypeError('host.pnpm must be a non-empty string, or left out')
29
+ const version = pnpm ?? pinnedPnpm(root()?.packageManager)
30
+ if (version === undefined) throw new TypeError('host.pnpm must be given where the root package.json\'s packageManager pins no pnpm')
31
+ return { pnpm: version, major: majorOf(version) }
32
+ }
33
+
34
+ // All of host but its pnpm, which pnpmOf reads.
35
+ export function checkHost(host) {
36
+ if (host === null || typeof host !== 'object') throw new TypeError('host must be an object with node, os, cpu and libc, and pnpm where it is not pinned')
37
+ for (const key of ['node', 'os', 'cpu', 'libc']) {
38
+ if (typeof host[key] !== 'string' || host[key] === '') throw new TypeError(`host.${key} must be a non-empty string`)
39
+ }
40
+ const { node, os, libc } = host
41
+ if (valid(node) === null) throw new DeptreeError(`${quote(node)} is not an exact version`, 'host.node')
42
+ if (os === 'win32') throw new DeptreeError('Windows is not supported: pnpm links there with junctions to absolute paths', 'host.os')
43
+ if (!LIBC.has(libc)) throw new DeptreeError(`expected "glibc", "musl" or "unknown", found ${quote(libc)}`, 'host.libc')
44
+ return { node, os, cpu: host.cpu, libc }
45
+ }
46
+
47
+ // pnpm-workspace.yaml as parsed; one of comments alone, or nothing, sets
48
+ // nothing, as pnpm reads it.
49
+ export const readWorkspace = (text) => (text === undefined || /^(?:[\t ]*(?:#.*)?(?:\r?\n|$))*$/u.test(text) ? undefined : parseYaml(text))
50
+
51
+ // The root package.json in `project`, as parsed, where there is one; one
52
+ // that is a link is refused unread, as findProjects refuses it.
53
+ const ROOT = 'manifests["."]'
54
+ function readRoot(project) {
55
+ if (typeOf(project, '/package.json', false) === 'symlink') throw linkedManifest('package.json')
56
+ const text = readText(project, '/package.json', ROOT)
57
+ return text === undefined ? undefined : readManifest(text, ROOT)
58
+ }
59
+
60
+ // The packages of `workspace`, pnpm-workspace.yaml as parsed, read here
61
+ // before the rest of it is, to find the projects by.
62
+ function packagesOf(workspace) {
63
+ if (workspace !== undefined && (workspace === null || typeof workspace !== 'object' || Array.isArray(workspace))) throw new DeptreeError('expected a mapping', 'pnpm-workspace.yaml')
64
+ return workspace?.packages === undefined ? undefined : readers.globs(workspace.packages, 'pnpm-workspace.yaml: packages')
65
+ }
66
+
67
+ // The directories of the projects pnpm finds in `project`, which
68
+ // buildPnpmTree takes the package.json of each of.
69
+ export function findPnpmProjects(options) {
70
+ const { project, host } = options ?? {}
71
+ checkProject(project)
72
+ if (host !== undefined && (host === null || typeof host !== 'object')) throw new TypeError('host must be an object, or left out')
73
+ const { major } = pnpmOf(host?.pnpm, () => readRoot(project))
74
+ return findProjects(project, packagesOf(readWorkspace(readWorkspaceText(project))), major)
75
+ }
76
+
77
+ const LOCKFILE = 'lockfile must be the text of pnpm-lock.yaml, or left out with a project given to read it from'
78
+
79
+ // The files an install reads but the package.json and the patches: given,
80
+ // with `lockfile`, or read from `project` without it.
81
+ export function inputsOf(options) {
82
+ const { lockfile, manifests, workspace, npmrc, patches, project } = options
83
+ if (lockfile === undefined) {
84
+ if (project === undefined) throw new TypeError(LOCKFILE)
85
+ for (const [name, value] of Object.entries({ manifests, workspace, npmrc, patches })) {
86
+ if (value !== undefined) throw new TypeError(`${name} must be left out where lockfile is: both are read from project`)
87
+ }
88
+ return { reading: true, project, ...readRootFiles(project) }
89
+ }
90
+ if (typeof lockfile !== 'string') throw new TypeError(LOCKFILE)
91
+ for (const [name, value] of Object.entries({ workspace, npmrc })) {
92
+ if (value !== undefined && typeof value !== 'string') throw new TypeError(`${name} must be a string, or left out`)
93
+ }
94
+ return { reading: false, lockfile, manifests, workspace, npmrc, patches }
95
+ }
96
+
97
+ // The package.json of every project, and the pnpm that installs as pnpmOf
98
+ // reads it from the root one: as given, or read from the project for
99
+ // each project pnpm finds there, and no other, as pnpm reads them: an
100
+ // importer the globs do not take is refused first, and one they take that
101
+ // pnpm does not find has none. `workspace` is pnpm-workspace.yaml as
102
+ // parsed; `pnpm` host.pnpm.
103
+ export function manifestsOf(inputs, workspace, lockfile, pnpm) {
104
+ if (!inputs.reading) {
105
+ const manifests = readManifests(inputs.manifests, lockfile)
106
+ return { manifests, ...pnpmOf(pnpm, () => manifests.get('.')) }
107
+ }
108
+ const { project } = inputs
109
+ const installs = pnpmOf(pnpm, () => readRoot(project))
110
+ const packages = packagesOf(workspace)
111
+ checkWorkspace(Object.keys(lockfile.importers), packages, installs.major)
112
+ const ids = findProjects(project, packages, installs.major)
113
+ return { manifests: readManifests(readManifestTexts(project, ids), lockfile), ...installs }
114
+ }
115
+
116
+ // The patch files, by path: as given, or read from the project, each that
117
+ // `configured`, patchedDependencies, names.
118
+ export function patchesOf(inputs, configured) {
119
+ if (inputs.reading) return readPatches(inputs.project, configured)
120
+ const entries = inputs.patches instanceof Map ? [...inputs.patches] : Object.entries(inputs.patches ?? {})
121
+ for (const [path, text] of entries) {
122
+ if (typeof text !== 'string') throw new TypeError(`patches[${quote(path)}] must be a string`)
123
+ }
124
+ return entries
125
+ }
package/src/pnpm/local.js CHANGED
@@ -1,65 +1,57 @@
1
- // Directories outside node_modules the tree links to or installs,
2
- // read from the Vfs given as `vfs` where there is one: those local
3
- // overrides name, and every other a `link:` leads to that is no project
4
- // and is under the lockfile's directory. Each has to hold a package.json
5
- // there, whose bins take their names as pnpm links them. An override to a
6
- // directory is read only from a Vfs given. One by `file:` has pnpm install
7
- // the directory as a package, of the files npm-packlist picks
8
- // (packlist.js); one to a tarball is refused.
1
+ // Directories outside node_modules the tree links to or installs, read
2
+ // from the project (project.js) where one is given: those local overrides
3
+ // name, and every other a `link:` leads to that is no project and is under
4
+ // the lockfile's directory. Each has to hold a package.json there, whose
5
+ // bins take their names as pnpm links them. An override to a directory is
6
+ // read only from a project given. One by `file:` has pnpm install the
7
+ // directory as a package, of the files npm-packlist picks (packlist.js);
8
+ // one to a tarball is refused.
9
9
 
10
10
  import { DeptreeError, quote } from '../error.js'
11
11
  import { packDirectory } from './packlist.js'
12
+ import { readText, typeOf } from './project.js'
12
13
  import { readManifest } from './projects.js'
13
14
 
14
- // readManifest drops a byte order mark, as pnpm does one.
15
- const decoder = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true })
16
-
17
- function manifestAt(vfs, dir, where) {
18
- const path = `/${dir}/package.json`
19
- if (!vfs.isFile(path)) throw new DeptreeError(`${quote(dir)} holds no package.json in the Vfs given`, where)
20
- let text
21
- try {
22
- text = decoder.decode(vfs.readFile(path))
23
- } catch {
24
- throw new DeptreeError(`${quote(`${dir}/package.json`)} is not UTF-8`, where)
25
- }
15
+ function manifestAt(project, dir, where) {
16
+ const text = readText(project, `/${dir}/package.json`, where)
17
+ if (text === undefined) throw new DeptreeError(`${quote(dir)} holds no package.json in the project given`, where)
26
18
  return readManifest(text, where)
27
19
  }
28
20
 
29
21
  // `overrides` is listOverrides's. It gives back the directories `file:`
30
22
  // overrides have pnpm install.
31
- export function checkLocalOverrides(overrides, vfs) {
23
+ export function checkLocalOverrides(overrides, project) {
32
24
  const installed = new Set()
33
25
  for (const { selector, local } of overrides) {
34
26
  if (local === undefined) continue
35
27
  const where = `overrides[${quote(selector)}]`
36
- if (vfs === undefined) throw new DeptreeError(`an override to a directory, ${quote(local.dir)}, is read only from a Vfs given as vfs`, where)
37
- if (local.protocol === 'file:' && vfs.isFile(`/${local.dir}`)) throw new DeptreeError(`${quote(local.dir)} is a file in the Vfs given, and an override to a tarball is not supported`, where)
38
- manifestAt(vfs, local.dir, where)
28
+ if (project === undefined) throw new DeptreeError(`an override to a directory, ${quote(local.dir)}, is read only from a project given`, where)
29
+ if (local.protocol === 'file:' && typeOf(project, `/${local.dir}`) === 'file') throw new DeptreeError(`${quote(local.dir)} is a file in the project given, and an override to a tarball is not supported`, where)
30
+ manifestAt(project, local.dir, where)
39
31
  if (local.protocol === 'file:') installed.add(local.dir)
40
32
  }
41
33
  return installed
42
34
  }
43
35
 
44
- // The package at the directory a `file:` dependency names, from `vfs`:
36
+ // The package at the directory a `file:` dependency names, from `project`:
45
37
  // its files as npm-packlist picks them, and its package.json, which has
46
38
  // to be for the name the lockfile has; the lockfile has no version.
47
- export function readDirectoryPackage(vfs, pkg, where, major) {
39
+ export function readDirectoryPackage(project, pkg, where, major) {
48
40
  const { directory } = pkg.resolution
49
- const manifest = manifestAt(vfs, directory, where)
41
+ const manifest = manifestAt(project, directory, where)
50
42
  if (manifest.name !== pkg.name) throw new DeptreeError(`its package.json is for ${quote(String(manifest.name))}`, where)
51
- return { files: packDirectory(vfs, directory, manifest, major, where), manifest }
43
+ return { files: packDirectory(project, directory, manifest, major, where), manifest }
52
44
  }
53
45
 
54
46
  // By directory, the package.json of each directory the tree links to that
55
47
  // is no node of `nodes`, no project of `projects`, and under the
56
- // lockfile's directory, read from `vfs`; none without one.
57
- export function readLinked(links, nodes, projects, vfs) {
48
+ // lockfile's directory, read from `project`; none without one.
49
+ export function readLinked(links, nodes, projects, project) {
58
50
  const linked = new Map()
59
- if (vfs === undefined) return linked
51
+ if (project === undefined) return linked
60
52
  for (const [path, target] of links) {
61
53
  if (nodes.has(target) || projects.has(target) || linked.has(target) || target === '..' || target.startsWith('../')) continue
62
- linked.set(target, manifestAt(vfs, target, quote(path)))
54
+ linked.set(target, manifestAt(project, target, quote(path)))
63
55
  }
64
56
  return linked
65
57
  }
@@ -26,6 +26,7 @@
26
26
 
27
27
  import { join } from '@preventive/vfs/path.js'
28
28
  import { DeptreeError, quote } from '../error.js'
29
+ import { readBytes } from './project.js'
29
30
 
30
31
  // The rules each name is held to, folded, wherever it is.
31
32
  const ANYWHERE = /^(?:\.git|\.svn|\.hg|cvs|\.npmrc|\.ds_store|npm-debug\.log|\.npmignore|\.gitignore|\._.*|\..*\.swp|.*\.orig)$/u
@@ -64,20 +65,20 @@ function namedByManifest(manifest) {
64
65
  // empty one bundles none, with either npm-packlist.
65
66
  const bundles = (list) => Boolean(list) && !(Array.isArray(list) && list.length === 0)
66
67
 
67
- // The files of the package at `dir` of `vfs` npm-packlist picks, with
68
+ // The files of the package at `dir` of `project` npm-packlist picks, with
68
69
  // their data and modes, by their paths from it; `manifest` is its
69
70
  // package.json as parsed.
70
- export function packDirectory(vfs, dir, manifest, major, where) {
71
+ export function packDirectory(project, dir, manifest, major, where) {
71
72
  if (manifest.files !== undefined) throw new DeptreeError('its package.json has `files`, which npm-packlist picks the files pnpm installs by, and which is not followed here', where)
72
73
  if (bundles(manifest.bundleDependencies) || bundles(manifest.bundledDependencies)) throw new DeptreeError('a directory with bundled dependencies is not supported', where)
73
74
  const named = namedByManifest(manifest)
74
75
  const files = new Map()
75
76
  const visit = (names) => {
76
- for (const entry of vfs.readdir(['', dir, ...names].join('/'))) {
77
+ for (const entry of project.readdir(['', dir, ...names].join('/'))) {
77
78
  const at = [...names, entry]
78
79
  const rel = at.join('/')
79
80
  const here = `${where}: ${quote(rel)}`
80
- const { type, mode } = vfs.lstat(`/${dir}/${rel}`)
81
+ const { type, mode } = project.lstat(`/${dir}/${rel}`)
81
82
  if (type === 'symlink') throw new DeptreeError('a link in a directory pnpm installs a copy of is not supported', here)
82
83
  if (entry === '.npmignore' || entry === '.gitignore') throw new DeptreeError(`${entry}'s rules, which npm-packlist picks the files pnpm installs by, are not followed here`, here)
83
84
  if (names.length === 0 && entry !== 'node_modules' && entry.toLowerCase() === 'node_modules') throw new DeptreeError('a name that is node_modules but for its case is kept by pnpm 10 and left out by pnpm 11', here)
@@ -91,7 +92,7 @@ export function packDirectory(vfs, dir, manifest, major, where) {
91
92
  }
92
93
  if (type === 'directory') visit(at)
93
94
  else if (mode !== 0o644 && mode !== 0o755) throw new DeptreeError(`its mode, ${mode.toString(8)}, is not 644 or 755, which is not supported`, here)
94
- else files.set(rel, { data: vfs.readFile(`/${dir}/${rel}`), mode })
95
+ else files.set(rel, { data: readBytes(project, `/${dir}/${rel}`), mode })
95
96
  }
96
97
  }
97
98
  visit([])