@preventive/deptree 1.0.0-alpha.5 → 1.0.0-alpha.7

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 (79) 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 +41 -12
  5. package/pnpm.d.ts +158 -25
  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 +60 -13
  13. package/src/manifest.js +52 -0
  14. package/src/matcher.js +60 -10
  15. package/src/mount.js +104 -44
  16. package/src/npm/bins.js +54 -0
  17. package/src/npm/compat.js +97 -0
  18. package/src/npm/dev-engines.js +52 -0
  19. package/src/npm/graph.js +68 -0
  20. package/src/npm/inputs.js +75 -0
  21. package/src/npm/manifests.js +130 -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/npmrc.js +24 -0
  28. package/src/patch.js +22 -41
  29. package/src/pnpm/bins.js +152 -131
  30. package/src/pnpm/checks.js +8 -35
  31. package/src/pnpm/commands.js +53 -96
  32. package/src/pnpm/filename.js +10 -21
  33. package/src/pnpm/frozen.js +29 -75
  34. package/src/pnpm/gitignore.js +139 -0
  35. package/src/pnpm/glob.js +65 -0
  36. package/src/pnpm/graph.js +15 -43
  37. package/src/pnpm/hoist.js +68 -61
  38. package/src/pnpm/hoisted.js +234 -0
  39. package/src/pnpm/hook.js +26 -31
  40. package/src/pnpm/inputs.js +73 -56
  41. package/src/pnpm/install.js +85 -131
  42. package/src/pnpm/local.js +21 -40
  43. package/src/pnpm/minimatch.js +319 -0
  44. package/src/pnpm/nm-hoist-graph.js +299 -0
  45. package/src/pnpm/nm-hoist.js +149 -0
  46. package/src/pnpm/npm-packlist.js +217 -0
  47. package/src/pnpm/overrides.js +11 -51
  48. package/src/pnpm/package.js +121 -0
  49. package/src/pnpm/packlist.js +169 -109
  50. package/src/pnpm/patches.js +18 -25
  51. package/src/pnpm/projects.js +63 -107
  52. package/src/pnpm/readers.js +63 -83
  53. package/src/pnpm/settings.js +64 -100
  54. package/src/pnpm/tree.js +197 -169
  55. package/src/pnpm/uptodate.js +16 -28
  56. package/src/pnpm/workspace.js +124 -133
  57. package/src/project.js +57 -0
  58. package/src/soldeer/config.js +8 -24
  59. package/src/soldeer/inputs.js +5 -15
  60. package/src/soldeer/tree.js +22 -60
  61. package/src/soldeer/zip.js +17 -36
  62. package/src/tarball.js +55 -140
  63. package/src/yarn1/bins.js +9 -19
  64. package/src/yarn1/compat.js +9 -28
  65. package/src/yarn1/hoist.js +53 -68
  66. package/src/yarn1/inputs.js +19 -61
  67. package/src/yarn1/manifest.js +8 -23
  68. package/src/yarn1/package.js +24 -63
  69. package/src/yarn1/peers.js +8 -15
  70. package/src/yarn1/requests.js +19 -37
  71. package/src/yarn1/resolve.js +36 -113
  72. package/src/yarn1/settings.js +15 -43
  73. package/src/yarn1/tree.js +67 -91
  74. package/yarn1.d.ts +34 -5
  75. package/yarn1.js +1 -8
  76. package/src/pnpm/npmrc.js +0 -31
  77. package/src/pnpm/project.js +0 -97
  78. package/src/yarn1/glob.js +0 -52
  79. /package/{LICENSE → LICENSE-MIT} +0 -0
package/src/yarn1/tree.js CHANGED
@@ -1,25 +1,15 @@
1
- // A node_modules tree as yarn 1 installs it from yarn.lock, frozen and with
2
- // scripts ignored: `yarn install --frozen-lockfile --ignore-scripts`, into a
3
- // directory with no node_modules yet. The lockfile is resolved as yarn
4
- // resolves it (resolve.js), each package's peers looked for as yarn looks
5
- // (peers.js), each package left out that the host cannot run and that is
6
- // optional (compat.js), and the tree laid out as yarn hoists it (hoist.js):
7
- // each package's files copied where it lands, as yarn's fetcher leaves
8
- // them, each bin's target executable, and each workspace linked where it
9
- // lands.
10
- //
11
- // Not written: bins themselves (node_modules/.bin), yarn's own
12
- // .yarn-integrity, and anything a script would build.
1
+ // A node_modules tree as `yarn install --frozen-lockfile --ignore-scripts`
2
+ // makes it in a directory with no node_modules yet. Not written:
3
+ // node_modules/.bin, yarn's .yarn-integrity, and anything a script builds.
13
4
 
14
5
  import { parseYarn1Lockfile } from '@preventive/lockfile/yarn1.js'
15
- import { Vfs, VfsError } from '@preventive/vfs'
16
- import { dirname, relative } from '@preventive/vfs/path.js'
6
+ import { Vfs } from '@preventive/vfs'
7
+ import { compareNames, dirname, relative } from '@preventive/vfs/path.js'
17
8
  import { clean, satisfies, valid, validRange } from '@preventive/upstream/semver.js'
18
9
  import { eachConcurrently } from '../concurrent.js'
19
10
  import { DeptreeError, quote } from '../error.js'
20
- import { checkNoModules, mount } from '../mount.js'
21
- import { checkCollisions } from '../pnpm/checks.js'
22
- import { typeOf } from '../pnpm/project.js'
11
+ import { checkNoModules, isInside, makeDirs, mount, writeFiles, writeLink } from '../mount.js'
12
+ import { typeOf } from '../project.js'
23
13
  import { checkBinLinks } from './bins.js'
24
14
  import { incompatibility } from './compat.js'
25
15
  import { Hoister } from './hoist.js'
@@ -30,49 +20,32 @@ import { resolvePeers } from './peers.js'
30
20
  import { AGGREGATOR, aggregatorOf, rulesOf, topRequests, workspacesOf } from './requests.js'
31
21
  import { resolve, splitPattern } from './resolve.js'
32
22
 
33
- // What a refusal of a reference is about: the first pattern of it.
34
23
  const whereOf = (ref) => quote(ref.patterns[0])
35
24
 
36
25
  // The basenames yarn's copy passes over, wherever they are in a package.
37
26
  const IGNORED = new Set(['.bin', '.yarn-metadata.json', '.yarn-tarball.tgz'])
38
27
  const skipped = (path) => path.split('/').some((segment) => IGNORED.has(segment))
39
28
 
40
- // The name a registry reference is fetched by: what its first pattern's
41
- // `npm:` alias asks for, or its own.
42
29
  function fetchedName(ref) {
43
30
  const { range } = splitPattern(ref.patterns[0])
44
- if (!range.startsWith('npm:')) return ref.name
45
- return splitPattern(range.slice(4)).name
31
+ return range.startsWith('npm:') ? splitPattern(range.slice(4)).name : ref.name
46
32
  }
47
33
 
48
- // Each registry reference's files and package.json, a few at a time,
49
- // every tarball's URL checked before any is fetched.
50
34
  async function fetchAll(refs) {
51
35
  const fetched = new Map()
52
- const tarballs = refs.map((ref) => {
53
- const where = whereOf(ref)
54
- return { ref, where, tarball: registryTarball(ref.entry, fetchedName(ref), where) }
55
- })
56
- await eachConcurrently(tarballs, async ({ ref, where, tarball }) => {
57
- try {
58
- fetched.set(ref, await fetchYarnPackage(tarball, where))
59
- } catch (error) {
60
- throw error instanceof DeptreeError ? error : new DeptreeError(error.message, where, { cause: error })
61
- }
62
- })
36
+ const tarballs = refs.map((ref) => ({ ref, tarball: registryTarball(ref.entry, fetchedName(ref), whereOf(ref)) }))
37
+ await eachConcurrently(tarballs, async ({ ref, tarball }) => {
38
+ fetched.set(ref, { ...await fetchYarnPackage(tarball, whereOf(ref)), integrity: tarball.integrity })
39
+ }, ({ ref }) => whereOf(ref))
63
40
  return fetched
64
41
  }
65
42
 
66
- // Where the hoister puts a package, from the lockfile's directory, as
67
- // yarn's flat tree has it: under the aggregator, in the workspace's own
68
- // node_modules.
43
+ // Under the aggregator, a package goes in its workspace's node_modules.
69
44
  function locationOf(names, workspaces) {
70
45
  if (names[0] === AGGREGATOR) return `${workspaces.get(names[1]).dir}/node_modules/${names.slice(2).join('/node_modules/')}`
71
46
  return `node_modules/${names.join('/node_modules/')}`
72
47
  }
73
48
 
74
- // The lockfile resolved as yarn resolves it for the project: the
75
- // workspaces, the root's patterns, and resolve.js's result.
76
49
  function resolveProject(inputs, host) {
77
50
  const lockfile = parseYarn1Lockfile(inputs.lockfile, { manifests: Object.fromEntries(inputs.manifests), semver: { clean, satisfies, valid, validRange } })
78
51
  const manifests = new Map([...inputs.manifests].map(([dir, manifest]) => [dir, fixLists(manifest)]))
@@ -80,24 +53,20 @@ function resolveProject(inputs, host) {
80
53
  const workspaces = workspacesOf(manifests)
81
54
  if (workspaces.size > 0) workspaces.set(AGGREGATOR, aggregatorOf(root, workspaces))
82
55
  const rules = rulesOf(root)
83
- const { requests, patterns } = topRequests(root, workspaces, rules)
56
+ const { requests, asked } = topRequests(root, workspaces, rules)
84
57
  const reason = incompatibility(root, host, 'manifests["."]', inputs.settings)
85
58
  if (reason !== undefined) throw new DeptreeError(reason, 'manifests["."]')
86
59
  const { project } = inputs
87
60
  const isDirectory = project === undefined ? () => undefined : (tag) => typeOf(project, `/${tag}/package.json`) !== undefined
88
- return { workspaces, topPatterns: patterns, resolved: resolve({ lockfile, workspaces, rules, top: requests, isDirectory }) }
61
+ return { workspaces, asked, resolved: resolve({ lockfile, workspaces, rules, top: requests, isDirectory }) }
89
62
  }
90
63
 
91
- // Every package fetched, as yarn fetches each before it checks any, and
92
- // read; then each the host cannot run left out where it is optional, and
93
- // refused where it is not, as yarn fails on it. By reference, each one's
94
- // package.json as yarn reads it, a workspace's its own.
64
+ // As in yarn, every package is fetched before any is checked, in the order
65
+ // its resolver hands them over. Of two its cache keeps in one place, only the
66
+ // first is fetched; the rest keep their lockfile entry as package.json, with
67
+ // no peers, bins, platforms or engines, and the first's files, and their
68
+ // URL's sha1 is held to the first's tarball.
95
69
  async function fetchChecked(resolved, host, settings) {
96
- // What yarn's resolver hands its fetcher, in its order: each reference
97
- // its patterns name, once. Of those its cache keeps in one place, two of
98
- // one package, the fetcher fetches the first and passes over the rest,
99
- // whose package.json stays their lockfile entry's: no peers, bins,
100
- // platforms or engines; their files are the first's.
101
70
  const order = [...new Set(resolved.patterns.values())]
102
71
  const first = new Map()
103
72
  for (const ref of order) if (ref.kind === 'registry' && !first.has(ref.loc)) first.set(ref.loc, ref)
@@ -107,7 +76,10 @@ async function fetchChecked(resolved, host, settings) {
107
76
  let manifest = ref.workspace?.manifest
108
77
  if (ref.kind === 'registry') {
109
78
  const head = first.get(ref.loc)
79
+ const { sha1 } = ref.entry.resolution
80
+ if (sha1 !== undefined && fetched.get(head).sha1 !== sha1) throw new DeptreeError(`the tarball's sha1 is not ${sha1}`, whereOf(ref))
110
81
  fetched.set(ref, fetched.get(head))
82
+ ref.hasBins = head === ref && fetched.get(ref).hasBins
111
83
  manifest = head === ref ? fixLists(fetched.get(ref).manifest) : { name: ref.name, version: ref.version }
112
84
  }
113
85
  const bundled = manifest.bundleDependencies ?? manifest.bundledDependencies
@@ -123,64 +95,70 @@ async function fetchChecked(resolved, host, settings) {
123
95
  return { packages: first.size, fetched, manifestOf }
124
96
  }
125
97
 
126
- // The tree as yarn hoists it, flat: each package by where it goes, in the
127
- // order yarn sorts them, by the absolute paths it compares, all under the
128
- // lockfile's directory.
129
- function layout({ resolved, manifestOf, topPatterns, workspaces }) {
98
+ // Sorted as yarn's linker sorts its absolute paths, all under one directory,
99
+ // by localeCompare in Node's locale; that changes nothing written, as a link
100
+ // sorts before what is beneath it in any locale.
101
+ function layout({ resolved, manifestOf, asked, workspaces }) {
130
102
  resolvePeers(resolved, manifestOf)
103
+ for (const ref of manifestOf.keys()) ref.dependencies = ref.asked.map(({ pattern }) => pattern)
131
104
  const hoister = new Hoister(resolved.patterns, (ref) => Object.keys(manifestOf.get(ref)?.peerDependencies ?? {}))
132
- hoister.seed(topPatterns)
105
+ hoister.seed(asked.map(({ pattern }) => pattern))
133
106
  const flat = hoister.flatten(workspaces.size > 0 ? AGGREGATOR : undefined)
134
107
  const placed = flat.map(({ names, info }) => ({ loc: locationOf(names, workspaces), info }))
135
- return placed.sort((a, b) => a.loc.localeCompare(b.loc))
108
+ return { placed: placed.sort((a, b) => a.loc.localeCompare(b.loc)), hoister }
136
109
  }
137
110
 
138
- // Where a path in the tree really is: through each workspace link on its
139
- // way, `links` by where each is to its target, in the order they are made.
111
+ // Where `path` really is, through each link in `links`, in the order made.
140
112
  function realOf(links, path) {
141
113
  for (const [link, target] of links) if (path === link || path.startsWith(`${link}/`)) path = target + path.slice(link.length)
142
114
  return path
143
115
  }
144
116
 
145
- // Each package copied where it goes, and each workspace linked: where a
146
- // package goes beneath a workspace's link, it is copied through the link,
147
- // into the workspace's own node_modules. `links` each link, by where it
148
- // is, to its target; `locations` each reference's copies, where they
149
- // really are.
117
+ // A package beneath a workspace's link is copied through it, so two of the
118
+ // hoister's places may be one real path, which `copies` maps to both.
150
119
  function writeTree(placed, fetched) {
151
120
  const vfs = new Vfs()
152
- vfs.mkdir('/node_modules', { recursive: true })
121
+ makeDirs(vfs, 'node_modules')
153
122
  const links = new Map()
154
123
  const locations = new Map()
155
- let files = 0
156
- let bytes = 0
124
+ const copies = new Map()
125
+ const left = new Map()
126
+ for (const { info: { ref } } of placed) {
127
+ if (ref.kind === 'registry') left.set(fetched.get(ref), (left.get(fetched.get(ref)) ?? 0) + 1)
128
+ }
129
+ const letGo = (pkg) => Object.assign(pkg, { files: undefined, dirs: undefined })
130
+ for (const pkg of new Set(fetched.values())) if (!left.has(pkg)) letGo(pkg)
131
+ const counted = { files: 0, bytes: 0 }
157
132
  for (const { loc, info } of placed) {
158
133
  const { ref } = info
159
134
  const dest = realOf(links, loc)
160
135
  if (!locations.has(ref)) locations.set(ref, [])
161
136
  if (!locations.get(ref).includes(dest)) locations.get(ref).push(dest)
162
137
  if (ref.kind === 'workspace') {
138
+ // readManifests has each workspace in the project.
139
+ if (!isInside(ref.workspace.dir)) throw new DeptreeError('links out of the project', whereOf(ref))
163
140
  links.set(dest, ref.workspace.dir)
164
- vfs.mkdir(`/${dirname(dest)}`, { recursive: true })
165
- vfs.symlink(relative(`/${dirname(dest)}`, `/${ref.workspace.dir}`) || '.', `/${dest}`)
141
+ writeLink(vfs, dest, relative(`/${dirname(dest)}`, `/${ref.workspace.dir}`) || '.')
166
142
  continue
167
143
  }
168
144
  const pkg = fetched.get(ref)
169
- vfs.mkdir(`/${dest}`, { recursive: true })
170
- for (const dir of pkg.dirs) if (!skipped(dir)) vfs.mkdir(`/${dest}/${dir}`, { recursive: true })
171
- for (const [path, file] of pkg.files) {
172
- if (skipped(path)) continue
173
- try {
174
- vfs.writeFile(`/${dest}/${path}`, file.data, { mode: file.mode })
175
- } catch (error) {
176
- if (error instanceof VfsError) throw new DeptreeError(`cannot be written: ${error.message}`, quote(`${dest}/${path}`), { cause: error })
177
- throw error
178
- }
179
- files++
180
- bytes += file.data.length
181
- }
145
+ const earlier = copies.get(dest)
146
+ if (earlier?.[0].ref === ref) earlier.push(info)
147
+ else copies.set(dest, [info])
148
+ writeFiles(vfs, dest, pkg, counted, skipped)
149
+ left.set(pkg, left.get(pkg) - 1)
150
+ if (left.get(pkg) === 0) letGo(pkg)
182
151
  }
183
- return { vfs, links, locations, files, bytes }
152
+ return { vfs, links, locations, copies, ...counted }
153
+ }
154
+
155
+ function listInstalled(copies, fetched, asked, hoister) {
156
+ const prod = hoister.reachedBut('dev', asked)
157
+ const required = hoister.reachedBut('optional', asked)
158
+ return [...copies].map(([path, places]) => {
159
+ const { manifest, integrity } = fetched.get(places[0].ref)
160
+ return { path, name: manifest.name, version: manifest.version, integrity, dev: !places.some((info) => prod.has(info)), optional: !places.some((info) => required.has(info)) }
161
+ }).sort((a, b) => compareNames(a.path, b.path))
184
162
  }
185
163
 
186
164
  export async function buildYarn1Tree(options) {
@@ -192,12 +170,11 @@ export async function buildYarn1Tree(options) {
192
170
  // Refused before anything is fetched; mount checks again.
193
171
  if (into !== undefined) checkNoModules(into, folded)
194
172
  checkRoot(inputs.manifests.get('.'))
195
- const { workspaces, topPatterns, resolved } = resolveProject(inputs, host)
173
+ const { workspaces, asked, resolved } = resolveProject(inputs, host)
196
174
  const { packages, fetched, manifestOf } = await fetchChecked(resolved, host, inputs.settings)
197
- const placed = layout({ resolved, manifestOf, topPatterns, workspaces })
198
- const { vfs, links, locations, files, bytes } = writeTree(placed, fetched)
199
- checkBinLinks({ placed, patterns: resolved.patterns, fetched, manifestOf, locations, realOf: (path) => realOf(links, path) })
200
- if (folded) checkCollisions(vfs)
175
+ const { placed, hoister } = layout({ resolved, manifestOf, asked, workspaces })
176
+ const { vfs, links, locations, copies, files, bytes } = writeTree(placed, fetched)
177
+ checkBinLinks({ placed, patterns: resolved.patterns, locations, realOf: (path) => realOf(links, path) })
201
178
  const stats = {
202
179
  packages,
203
180
  skipped: [...manifestOf.keys()].filter((ref) => ref.incompatible).length,
@@ -206,7 +183,6 @@ export async function buildYarn1Tree(options) {
206
183
  bytes,
207
184
  links: links.size,
208
185
  }
209
- if (into === undefined) return { vfs, stats }
210
- mount(vfs, into, folded)
211
- return { vfs: into, stats }
186
+ const installed = listInstalled(copies, fetched, asked, hoister)
187
+ return { vfs: mount(vfs, into, folded), stats, installed }
212
188
  }
package/yarn1.d.ts CHANGED
@@ -60,7 +60,8 @@ export interface Yarn1Project {
60
60
  //
61
61
  // With `lockfile`, they are given as text: `manifests` by the project's
62
62
  // directory relative to the lockfile's (`.` for the root, then each
63
- // workspace's, as findYarn1Workspaces lists them). Every workspace yarn
63
+ // workspace's, as findYarn1Workspaces lists them: a directory under the
64
+ // lockfile's, with no `.`, `..` or empty segment). Every workspace yarn
64
65
  // would find has to be given, which buildYarn1Tree cannot check where it
65
66
  // is given them, as it is not given the directories; and the lockfile
66
67
  // reader refuses a manifest of any directory the root's `workspaces` do
@@ -163,10 +164,36 @@ export interface Yarn1TreeStats {
163
164
  links: number
164
165
  }
165
166
 
167
+ // A copy of a registry package in the tree, as an SBOM would list it:
168
+ // `path` is where its files really are, from the lockfile's directory,
169
+ // which is `/` of the Vfs — node_modules/<alias>, or beneath a workspace,
170
+ // in that workspace's own node_modules. `name` and `version` are the
171
+ // package's own, which its package.json is held to, whatever alias it is
172
+ // installed as; `integrity` the sha512 its tarball is held to. `dev` is
173
+ // whether dev dependencies alone reach this copy, the root's and the
174
+ // workspaces' devDependencies: nothing else does, each dependency, and
175
+ // each peer yarn finds, looked for from where what asks for it is, as Node
176
+ // finds it and yarn marks what an install requires, past what the host
177
+ // cannot run. Two copies of one package may differ: one beneath a
178
+ // devDependency that only it needs is dev. `optional` is whether optional
179
+ // dependencies alone reach this copy, so.
180
+ export interface Yarn1Installed {
181
+ path: string
182
+ name: string
183
+ version: string
184
+ integrity: string
185
+ dev: boolean
186
+ optional: boolean
187
+ }
188
+
166
189
  // `vfs` is the one given, the tree mounted into it, or a new one.
190
+ // `installed` is each copy in the tree, once for each path, by path in
191
+ // the order of its code points, whatever the locale; a workspace, whose
192
+ // files the tree does not hold, is not among them.
167
193
  export interface Yarn1Tree {
168
194
  vfs: Vfs
169
195
  stats: Yarn1TreeStats
196
+ installed: Yarn1Installed[]
170
197
  }
171
198
 
172
199
  // The node_modules tree `yarn install --frozen-lockfile --ignore-scripts`
@@ -214,7 +241,8 @@ export interface Yarn1Tree {
214
241
  // file under one directory, none in the package's own node_modules, where
215
242
  // yarn installs its dependencies, no link or device in it, and a
216
243
  // package.json for exactly its name and version; each with the sha512
217
- // integrity and the sha1 the lockfile records. On macOS, two names in one
244
+ // integrity the lockfile records, and the sha1 each of its entries does,
245
+ // fetched or not. On macOS, two names in one
218
246
  // directory that differ only in case or normalization are refused, as
219
247
  // they would be one name there.
220
248
  //
@@ -231,9 +259,10 @@ export interface Yarn1Tree {
231
259
  // set order.
232
260
  //
233
261
  // Ranges and versions are read with npm's own semver, borrowed from the
234
- // npm beside node as @preventive/upstream borrows it, without which this
235
- // throws. yarn 1.22 reads them with the semver 5.5.0 it bundles, which
236
- // reads alike all but the edges of what a range may spell.
262
+ // npm beside node as @preventive/upstream borrows it, or with its semver
263
+ // peer where there is no npm, without either of which this throws. yarn
264
+ // 1.22 reads them with the semver 5.5.0 it bundles, which reads alike all
265
+ // but the edges of what a range may spell.
237
266
  //
238
267
  // Nothing is left to a guess: a lockfile the lockfile reader refuses, a
239
268
  // setting this does not know or does not build for, a package from
package/yarn1.js CHANGED
@@ -1,14 +1,7 @@
1
1
  // A node_modules tree built in memory from a yarn.lock, as yarn 1.22
2
- // installs it with its node_modules linker: yarn1.d.ts says what it takes
3
- // and what comes back. The project is read through the view given, and
4
- // the tree built in a Vfs; the one filesystem touched is that of the
5
- // tarball caches @preventive/upstream reads, and writes where it keeps its
6
- // own.
2
+ // installs it with its node_modules linker; see yarn1.d.ts.
7
3
  export { buildYarn1Tree } from './src/yarn1/tree.js'
8
4
  export { findYarn1Workspaces } from './src/yarn1/inputs.js'
9
5
  export { DeptreeError } from './src/error.js'
10
- // Where @preventive/upstream caches what it fetches, tarballs among it;
11
- // unset, it keeps no cache of its own, and writes nothing.
12
6
  export { setCacheDir } from '@preventive/upstream/npm.js'
13
- // What the lockfile, and the manifests read with it, are refused with.
14
7
  export { LockfileError } from '@preventive/lockfile/yarn1.js'
package/src/pnpm/npmrc.js DELETED
@@ -1,31 +0,0 @@
1
- // An .npmrc read as the `ini` package reads it for npm and pnpm, as far as
2
- // a key and the text after its `=` go: a line per setting, `#` or `;` in
3
- // front for a comment, `key[]` to add to a list, and a key with no `=`
4
- // set to true. What a value means is left to the setting that reads it,
5
- // which takes it only in a form with one reading (settings.js); a value
6
- // ini would unquote, unescape, cut at a `;` or `#`, or fill in from the
7
- // environment is refused there rather than read two ways. A section, which
8
- // npm reads under a prefix no setting here has, is refused outright.
9
-
10
- import { DeptreeError } from '../error.js'
11
-
12
- // Each setting in the order written: `line` counts from one, and `list`
13
- // says the key was written with `[]`, which is dropped from it.
14
- export function parseNpmrc(text) {
15
- const settings = []
16
- const lines = text.split(/\r\n|\r|\n/u)
17
- for (const [index, raw] of lines.entries()) {
18
- const line = raw.trim()
19
- const where = `.npmrc:${index + 1}`
20
- if (line === '' || line.startsWith('#') || line.startsWith(';')) continue
21
- if (line.startsWith('[')) throw new DeptreeError('a section is not supported', where)
22
- const eq = line.indexOf('=')
23
- let key = (eq === -1 ? line : line.slice(0, eq)).trim()
24
- const value = eq === -1 ? 'true' : line.slice(eq + 1).trim()
25
- const list = key.length > 2 && key.endsWith('[]')
26
- if (list) key = key.slice(0, -2)
27
- if (key === '') throw new DeptreeError('a setting has no name', where)
28
- settings.push({ key, value, list, line: index + 1 })
29
- }
30
- return settings
31
- }
@@ -1,97 +0,0 @@
1
- // The project a tree is built for: a view of the lockfile's directory, by
2
- // paths from `/` — a Vfs, or anything with its readdir, lstat, stat and
3
- // readFile, such as one of a directory on disk. It is only read, and only
4
- // where these say: the files pnpm reads at the root for an install, the
5
- // package.json of each project and the patches the settings name, where
6
- // buildPnpmTree is not handed them; the directories findProjects walks;
7
- // and those a `link:` or `file:` leads to (local.js).
8
-
9
- import { normalize } from '@preventive/vfs/path.js'
10
- import { DeptreeError, quote } from '../error.js'
11
-
12
- export function checkProject(project) {
13
- if (['readdir', 'lstat', 'stat', 'readFile'].some((name) => typeof project?.[name] !== 'function')) {
14
- throw new TypeError('project must be a Vfs, or have its readdir, lstat, stat and readFile')
15
- }
16
- }
17
-
18
- // What `path` leads to, links followed, or where `follow` is false, what
19
- // it is; nothing, where it leads nowhere: to no entry, through a file, or
20
- // round a loop of links. Any other failure is thrown.
21
- const NOWHERE = new Set(['ENOENT', 'ENOTDIR', 'ELOOP'])
22
- export function typeOf(project, path, follow = true) {
23
- try {
24
- return (follow ? project.stat(path) : project.lstat(path)).type
25
- } catch (error) {
26
- if (NOWHERE.has(error?.code)) return undefined
27
- throw error
28
- }
29
- }
30
-
31
- // As text handed in would be: a byte order mark kept, for what reads it
32
- // to drop as pnpm does.
33
- const decoder = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true })
34
-
35
- // The bytes of the file at `path`, as a Vfs gives them back.
36
- export function readBytes(project, path) {
37
- const bytes = project.readFile(path)
38
- if (!(bytes instanceof Uint8Array)) throw new TypeError('project.readFile must give back bytes')
39
- return bytes
40
- }
41
-
42
- // The text of the file at `path`, or undefined where nothing is.
43
- export function readText(project, path, where) {
44
- const type = typeOf(project, path)
45
- if (type === undefined) return undefined
46
- if (type !== 'file') throw new DeptreeError(`${quote(path.slice(1))} is a ${type}, not a file`, where)
47
- const bytes = readBytes(project, path)
48
- try {
49
- return decoder.decode(bytes)
50
- } catch {
51
- throw new DeptreeError(`${quote(path.slice(1))} is not UTF-8`, where)
52
- }
53
- }
54
-
55
- // pnpm-workspace.yaml, where it is. pnpm looks for it under other names
56
- // too, and refuses one it finds so.
57
- const MISNAMED = ['pnpm-workspaces.yaml', 'pnpm-workspaces.yml', 'pnpm-workspace.yml', '.pnpm-workspace.yaml', '.pnpm-workspace.yml', '.pnpm-workspaces.yaml', '.pnpm-workspaces.yml']
58
- export function readWorkspaceText(project) {
59
- const text = readText(project, '/pnpm-workspace.yaml')
60
- const misnamed = text === undefined ? MISNAMED.find((name) => typeOf(project, `/${name}`) === 'file') : undefined
61
- if (misnamed !== undefined) throw new DeptreeError('pnpm refuses a workspace manifest not named pnpm-workspace.yaml', quote(misnamed))
62
- return text
63
- }
64
-
65
- // What pnpm reads at the root for an install besides the package.json:
66
- // pnpm-lock.yaml, which a frozen install cannot do without, and
67
- // pnpm-workspace.yaml and the .npmrc, where they are.
68
- export function readRootFiles(project) {
69
- const lockfile = readText(project, '/pnpm-lock.yaml')
70
- if (lockfile === undefined) throw new DeptreeError('the project has no pnpm-lock.yaml, which a frozen install cannot do without')
71
- return { lockfile, workspace: readWorkspaceText(project), npmrc: readText(project, '/.npmrc') }
72
- }
73
-
74
- // The text of the package.json of each of `ids`, the projects'
75
- // directories, by directory.
76
- export function readManifestTexts(project, ids) {
77
- const texts = new Map()
78
- for (const id of ids) {
79
- texts.set(id, readText(project, id === '.' ? '/package.json' : `/${id}/package.json`, `manifests[${quote(id)}]`))
80
- }
81
- return texts
82
- }
83
-
84
- // The text of each patch `configured`, patchedDependencies, names, by its
85
- // path from the lockfile's directory; one that is not there left out.
86
- // One outside that directory, or absolute, is not in the project, and is
87
- // refused.
88
- export function readPatches(project, configured) {
89
- const texts = new Map()
90
- for (const [selector, spec] of Object.entries(configured ?? {})) {
91
- const path = normalize(spec)
92
- if (path === '..' || path.startsWith('../') || path.startsWith('/')) throw new DeptreeError(`the patch ${quote(spec)} is not in the project: it is outside the lockfile's directory`, `patchedDependencies[${quote(selector)}]`)
93
- const text = readText(project, `/${path}`, `patches[${quote(path)}]`)
94
- if (text !== undefined) texts.set(path, text)
95
- }
96
- return texts
97
- }
package/src/yarn1/glob.js DELETED
@@ -1,52 +0,0 @@
1
- // The globs yarn matches with minimatch's defaults, as far as the lockfile
2
- // reader lets them through: a segment at a time, `**` as any number of
3
- // whole segments, `*` and `?` within one, and no segment that starts with
4
- // a dot taken by a wildcard.
5
-
6
- import { escape } from '../matcher.js'
7
-
8
- const compiled = new Map()
9
-
10
- function compile(glob) {
11
- return glob.split('/').map((segment) => {
12
- if (segment === '**') return null
13
- const dot = segment.startsWith('.') || !/[*?]/u.test(segment) ? '' : '(?!\\.)'
14
- return new RegExp(`^${dot}${segment.split(/([*?])/u).map((part) => (part === '*' ? '[^/]*' : part === '?' ? '[^/]' : escape(part))).join('')}$`, 'u')
15
- })
16
- }
17
-
18
- // The places in `glob` that `path` reaches, a name at a time, a `**`
19
- // taking none or more, in time linear in both: a place is the index of the
20
- // glob's next segment, its length where all are taken.
21
- function placesOf(glob, path) {
22
- if (!compiled.has(glob)) compiled.set(glob, compile(glob))
23
- const tests = compiled.get(glob)
24
- const close = (places) => {
25
- for (let i = 0; i < tests.length; i++) if (places.has(i) && tests[i] === null) places.add(i + 1)
26
- return places
27
- }
28
- let places = close(new Set([0]))
29
- for (const segment of path === '' ? [] : path.split('/')) {
30
- const next = new Set()
31
- for (const i of places) {
32
- if (i === tests.length) continue
33
- if (tests[i] === null) {
34
- if (!segment.startsWith('.')) next.add(i)
35
- } else if (tests[i].test(segment)) next.add(i + 1)
36
- }
37
- places = close(next)
38
- }
39
- return { places, length: tests.length }
40
- }
41
-
42
- // Whether `glob` takes `path`.
43
- export function matchesGlob(glob, path) {
44
- const { places, length } = placesOf(glob, path)
45
- return places.has(length)
46
- }
47
-
48
- // Whether `glob` may take a path below `dir`: some of it left to take.
49
- export function reachesBelow(glob, dir) {
50
- const { places, length } = placesOf(glob, dir)
51
- return [...places].some((place) => place < length)
52
- }
File without changes