@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 +27 -6
- package/pnpm.d.ts +143 -39
- package/pnpm.js +5 -3
- package/src/hash.js +4 -0
- package/src/mount.js +14 -7
- package/src/pnpm/bins.js +14 -14
- package/src/pnpm/frozen.js +30 -20
- package/src/pnpm/hoist.js +5 -4
- package/src/pnpm/hook.js +13 -22
- package/src/pnpm/inputs.js +125 -0
- package/src/pnpm/install.js +40 -29
- package/src/pnpm/local.js +36 -28
- package/src/pnpm/overrides.js +12 -5
- package/src/pnpm/packlist.js +100 -0
- package/src/pnpm/patches.js +3 -3
- package/src/pnpm/project.js +97 -0
- package/src/pnpm/projects.js +31 -18
- package/src/pnpm/settings.js +34 -21
- package/src/pnpm/tree.js +103 -83
- package/src/pnpm/uptodate.js +1 -1
- package/src/pnpm/workspace.js +161 -38
- package/src/tarball.js +22 -10
- package/src/yarn1/bins.js +38 -0
- package/src/yarn1/compat.js +86 -0
- package/src/yarn1/glob.js +52 -0
- package/src/yarn1/hoist.js +264 -0
- package/src/yarn1/inputs.js +125 -0
- package/src/yarn1/manifest.js +79 -0
- package/src/yarn1/package.js +148 -0
- package/src/yarn1/peers.js +58 -0
- package/src/yarn1/requests.js +90 -0
- package/src/yarn1/resolve.js +269 -0
- package/src/yarn1/settings.js +123 -0
- package/src/yarn1/tree.js +220 -0
- package/yarn1.d.ts +267 -0
- package/yarn1.js +14 -0
|
@@ -0,0 +1,220 @@
|
|
|
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.
|
|
13
|
+
|
|
14
|
+
import { parseYarn1Lockfile } from '@preventive/lockfile/yarn1.js'
|
|
15
|
+
import { Vfs, VfsError } from '@preventive/vfs'
|
|
16
|
+
import { dirname, relative } from '@preventive/vfs/path.js'
|
|
17
|
+
import { clean, satisfies, valid, validRange } from '@preventive/upstream/semver.js'
|
|
18
|
+
import { DeptreeError, quote } from '../error.js'
|
|
19
|
+
import { checkNoModules, mount } from '../mount.js'
|
|
20
|
+
import { checkCollisions } from '../pnpm/checks.js'
|
|
21
|
+
import { typeOf } from '../pnpm/project.js'
|
|
22
|
+
import { checkBinLinks } from './bins.js'
|
|
23
|
+
import { incompatibility } from './compat.js'
|
|
24
|
+
import { Hoister } from './hoist.js'
|
|
25
|
+
import { checkHost, inputsOf } from './inputs.js'
|
|
26
|
+
import { checkRoot, fixLists } from './manifest.js'
|
|
27
|
+
import { fetchYarnPackage, registryTarball } from './package.js'
|
|
28
|
+
import { resolvePeers } from './peers.js'
|
|
29
|
+
import { AGGREGATOR, aggregatorOf, rulesOf, topRequests, workspacesOf } from './requests.js'
|
|
30
|
+
import { resolve, splitPattern } from './resolve.js'
|
|
31
|
+
|
|
32
|
+
const CONCURRENCY = 8
|
|
33
|
+
|
|
34
|
+
// What a refusal of a reference is about: the first pattern of it.
|
|
35
|
+
const whereOf = (ref) => quote(ref.patterns[0])
|
|
36
|
+
|
|
37
|
+
// The basenames yarn's copy passes over, wherever they are in a package.
|
|
38
|
+
const IGNORED = new Set(['.bin', '.yarn-metadata.json', '.yarn-tarball.tgz'])
|
|
39
|
+
const skipped = (path) => path.split('/').some((segment) => IGNORED.has(segment))
|
|
40
|
+
|
|
41
|
+
// The name a registry reference is fetched by: what its first pattern's
|
|
42
|
+
// `npm:` alias asks for, or its own.
|
|
43
|
+
function fetchedName(ref) {
|
|
44
|
+
const { range } = splitPattern(ref.patterns[0])
|
|
45
|
+
if (!range.startsWith('npm:')) return ref.name
|
|
46
|
+
return splitPattern(range.slice(4)).name
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Each registry reference's files and package.json, a few at a time,
|
|
50
|
+
// every tarball's URL checked before any is fetched; the first failure
|
|
51
|
+
// stops the rest from starting.
|
|
52
|
+
async function fetchAll(refs) {
|
|
53
|
+
const fetched = new Map()
|
|
54
|
+
const queue = refs.map((ref) => {
|
|
55
|
+
const where = whereOf(ref)
|
|
56
|
+
return { ref, where, tarball: registryTarball(ref.entry, fetchedName(ref), where) }
|
|
57
|
+
})
|
|
58
|
+
let failed = false
|
|
59
|
+
const worker = async () => {
|
|
60
|
+
while (queue.length > 0 && !failed) {
|
|
61
|
+
const { ref, where, tarball } = queue.shift()
|
|
62
|
+
try {
|
|
63
|
+
fetched.set(ref, await fetchYarnPackage(tarball, where))
|
|
64
|
+
} catch (error) {
|
|
65
|
+
failed = true
|
|
66
|
+
throw error instanceof DeptreeError ? error : new DeptreeError(error.message, where, { cause: error })
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
await Promise.all(Array.from({ length: Math.min(CONCURRENCY, queue.length) }, worker))
|
|
71
|
+
return fetched
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Where the hoister puts a package, from the lockfile's directory, as
|
|
75
|
+
// yarn's flat tree has it: under the aggregator, in the workspace's own
|
|
76
|
+
// node_modules.
|
|
77
|
+
function locationOf(names, workspaces) {
|
|
78
|
+
if (names[0] === AGGREGATOR) return `${workspaces.get(names[1]).dir}/node_modules/${names.slice(2).join('/node_modules/')}`
|
|
79
|
+
return `node_modules/${names.join('/node_modules/')}`
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// The lockfile resolved as yarn resolves it for the project: the
|
|
83
|
+
// workspaces, the root's patterns, and resolve.js's result.
|
|
84
|
+
function resolveProject(inputs, host) {
|
|
85
|
+
const lockfile = parseYarn1Lockfile(inputs.lockfile, { manifests: Object.fromEntries(inputs.manifests), semver: { clean, satisfies, valid, validRange } })
|
|
86
|
+
const manifests = new Map([...inputs.manifests].map(([dir, manifest]) => [dir, fixLists(manifest)]))
|
|
87
|
+
const root = manifests.get('.')
|
|
88
|
+
const workspaces = workspacesOf(manifests)
|
|
89
|
+
if (workspaces.size > 0) workspaces.set(AGGREGATOR, aggregatorOf(root, workspaces))
|
|
90
|
+
const rules = rulesOf(root)
|
|
91
|
+
const { requests, patterns } = topRequests(root, workspaces, rules)
|
|
92
|
+
const reason = incompatibility(root, host, 'manifests["."]', inputs.settings)
|
|
93
|
+
if (reason !== undefined) throw new DeptreeError(reason, 'manifests["."]')
|
|
94
|
+
const { project } = inputs
|
|
95
|
+
const isDirectory = project === undefined ? () => undefined : (tag) => typeOf(project, `/${tag}/package.json`) !== undefined
|
|
96
|
+
return { workspaces, topPatterns: patterns, resolved: resolve({ lockfile, workspaces, rules, top: requests, isDirectory }) }
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Every package fetched, as yarn fetches each before it checks any, and
|
|
100
|
+
// read; then each the host cannot run left out where it is optional, and
|
|
101
|
+
// refused where it is not, as yarn fails on it. By reference, each one's
|
|
102
|
+
// package.json as yarn reads it, a workspace's its own.
|
|
103
|
+
async function fetchChecked(resolved, host, settings) {
|
|
104
|
+
// What yarn's resolver hands its fetcher, in its order: each reference
|
|
105
|
+
// its patterns name, once. Of those its cache keeps in one place, two of
|
|
106
|
+
// one package, the fetcher fetches the first and passes over the rest,
|
|
107
|
+
// whose package.json stays their lockfile entry's: no peers, bins,
|
|
108
|
+
// platforms or engines; their files are the first's.
|
|
109
|
+
const order = [...new Set(resolved.patterns.values())]
|
|
110
|
+
const first = new Map()
|
|
111
|
+
for (const ref of order) if (ref.kind === 'registry' && !first.has(ref.loc)) first.set(ref.loc, ref)
|
|
112
|
+
const fetched = await fetchAll([...first.values()])
|
|
113
|
+
const manifestOf = new Map()
|
|
114
|
+
for (const ref of order) {
|
|
115
|
+
let manifest = ref.workspace?.manifest
|
|
116
|
+
if (ref.kind === 'registry') {
|
|
117
|
+
const head = first.get(ref.loc)
|
|
118
|
+
fetched.set(ref, fetched.get(head))
|
|
119
|
+
manifest = head === ref ? fixLists(fetched.get(ref).manifest) : { name: ref.name, version: ref.version }
|
|
120
|
+
}
|
|
121
|
+
const bundled = manifest.bundleDependencies ?? manifest.bundledDependencies
|
|
122
|
+
if (bundled && !(Array.isArray(bundled) && bundled.length === 0)) throw new DeptreeError('a package with bundled dependencies is not supported', whereOf(ref))
|
|
123
|
+
manifestOf.set(ref, manifest)
|
|
124
|
+
}
|
|
125
|
+
for (const ref of order) {
|
|
126
|
+
const reason = incompatibility(manifestOf.get(ref), host, whereOf(ref), settings)
|
|
127
|
+
if (reason === undefined) continue
|
|
128
|
+
if (!ref.optional) throw new DeptreeError(`${reason}, and it is not optional, which yarn fails on`, whereOf(ref))
|
|
129
|
+
ref.incompatible = true
|
|
130
|
+
}
|
|
131
|
+
return { packages: first.size, fetched, manifestOf }
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// The tree as yarn hoists it, flat: each package by where it goes, in the
|
|
135
|
+
// order yarn sorts them, by the absolute paths it compares, all under the
|
|
136
|
+
// lockfile's directory.
|
|
137
|
+
function layout({ resolved, manifestOf, topPatterns, workspaces }) {
|
|
138
|
+
resolvePeers(resolved, manifestOf)
|
|
139
|
+
const hoister = new Hoister(resolved.patterns, (ref) => Object.keys(manifestOf.get(ref)?.peerDependencies ?? {}))
|
|
140
|
+
hoister.seed(topPatterns)
|
|
141
|
+
const flat = hoister.flatten(workspaces.size > 0 ? AGGREGATOR : undefined)
|
|
142
|
+
const placed = flat.map(({ names, info }) => ({ loc: locationOf(names, workspaces), info }))
|
|
143
|
+
return placed.sort((a, b) => a.loc.localeCompare(b.loc))
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Where a path in the tree really is: through each workspace link on its
|
|
147
|
+
// way, `links` by where each is to its target, in the order they are made.
|
|
148
|
+
function realOf(links, path) {
|
|
149
|
+
for (const [link, target] of links) if (path === link || path.startsWith(`${link}/`)) path = target + path.slice(link.length)
|
|
150
|
+
return path
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Each package copied where it goes, and each workspace linked: where a
|
|
154
|
+
// package goes beneath a workspace's link, it is copied through the link,
|
|
155
|
+
// into the workspace's own node_modules. `links` each link, by where it
|
|
156
|
+
// is, to its target; `locations` each reference's copies, where they
|
|
157
|
+
// really are.
|
|
158
|
+
function writeTree(placed, fetched) {
|
|
159
|
+
const vfs = new Vfs()
|
|
160
|
+
vfs.mkdir('/node_modules', { recursive: true })
|
|
161
|
+
const links = new Map()
|
|
162
|
+
const locations = new Map()
|
|
163
|
+
let files = 0
|
|
164
|
+
let bytes = 0
|
|
165
|
+
for (const { loc, info } of placed) {
|
|
166
|
+
const { ref } = info
|
|
167
|
+
const dest = realOf(links, loc)
|
|
168
|
+
if (!locations.has(ref)) locations.set(ref, [])
|
|
169
|
+
if (!locations.get(ref).includes(dest)) locations.get(ref).push(dest)
|
|
170
|
+
if (ref.kind === 'workspace') {
|
|
171
|
+
links.set(dest, ref.workspace.dir)
|
|
172
|
+
vfs.mkdir(`/${dirname(dest)}`, { recursive: true })
|
|
173
|
+
vfs.symlink(relative(`/${dirname(dest)}`, `/${ref.workspace.dir}`) || '.', `/${dest}`)
|
|
174
|
+
continue
|
|
175
|
+
}
|
|
176
|
+
const pkg = fetched.get(ref)
|
|
177
|
+
vfs.mkdir(`/${dest}`, { recursive: true })
|
|
178
|
+
for (const dir of pkg.dirs) if (!skipped(dir)) vfs.mkdir(`/${dest}/${dir}`, { recursive: true })
|
|
179
|
+
for (const [path, file] of pkg.files) {
|
|
180
|
+
if (skipped(path)) continue
|
|
181
|
+
try {
|
|
182
|
+
vfs.writeFile(`/${dest}/${path}`, file.data, { mode: file.mode })
|
|
183
|
+
} catch (error) {
|
|
184
|
+
if (error instanceof VfsError) throw new DeptreeError(`cannot be written: ${error.message}`, quote(`${dest}/${path}`), { cause: error })
|
|
185
|
+
throw error
|
|
186
|
+
}
|
|
187
|
+
files++
|
|
188
|
+
bytes += file.data.length
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return { vfs, links, locations, files, bytes }
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export async function buildYarn1Tree(options) {
|
|
195
|
+
const { host: given, vfs: into } = options ?? {}
|
|
196
|
+
if (into !== undefined && !(into instanceof Vfs)) throw new TypeError('vfs must be a Vfs, or left out')
|
|
197
|
+
const inputs = inputsOf(options ?? {})
|
|
198
|
+
const host = checkHost(given, inputs.manifests.get('.'))
|
|
199
|
+
const folded = host.os === 'darwin'
|
|
200
|
+
// Refused before anything is fetched; mount checks again.
|
|
201
|
+
if (into !== undefined) checkNoModules(into, folded)
|
|
202
|
+
checkRoot(inputs.manifests.get('.'))
|
|
203
|
+
const { workspaces, topPatterns, resolved } = resolveProject(inputs, host)
|
|
204
|
+
const { packages, fetched, manifestOf } = await fetchChecked(resolved, host, inputs.settings)
|
|
205
|
+
const placed = layout({ resolved, manifestOf, topPatterns, workspaces })
|
|
206
|
+
const { vfs, links, locations, files, bytes } = writeTree(placed, fetched)
|
|
207
|
+
checkBinLinks({ placed, patterns: resolved.patterns, fetched, manifestOf, locations, realOf: (path) => realOf(links, path) })
|
|
208
|
+
if (folded) checkCollisions(vfs)
|
|
209
|
+
const stats = {
|
|
210
|
+
packages,
|
|
211
|
+
skipped: [...manifestOf.keys()].filter((ref) => ref.incompatible).length,
|
|
212
|
+
installed: placed.filter(({ info }) => info.ref.kind === 'registry').length,
|
|
213
|
+
files,
|
|
214
|
+
bytes,
|
|
215
|
+
links: links.size,
|
|
216
|
+
}
|
|
217
|
+
if (into === undefined) return { vfs, stats }
|
|
218
|
+
mount(vfs, into, folded)
|
|
219
|
+
return { vfs: into, stats }
|
|
220
|
+
}
|
package/yarn1.d.ts
ADDED
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
// Hand-written against yarn1.js; a change to either belongs with the other.
|
|
2
|
+
|
|
3
|
+
import type { NodeType, Vfs } from '@preventive/vfs'
|
|
4
|
+
|
|
5
|
+
export { LockfileError } from '@preventive/lockfile/yarn1.js'
|
|
6
|
+
|
|
7
|
+
// Where @preventive/upstream caches what it fetches, tarballs among them.
|
|
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. yarn's own cache is never read. The project is read
|
|
15
|
+
// only through the view given as `project`, and the tree built in a Vfs.
|
|
16
|
+
export { setCacheDir } from '@preventive/upstream/npm.js'
|
|
17
|
+
|
|
18
|
+
// The machine yarn would install on, which a tree depends on: `yarn` is
|
|
19
|
+
// the version that installs, and has to be a 1.22.x; left out, it is the
|
|
20
|
+
// one the root package.json's packageManager pins, which corepack runs,
|
|
21
|
+
// and has to be given where that pins none, and to be that one where it
|
|
22
|
+
// does. `node` is the Node yarn runs on, an exact version; `os` and `cpu`
|
|
23
|
+
// as Node names them — `linux`, `x64`. Windows is refused: yarn links bins
|
|
24
|
+
// there with shims, and workspaces with junctions.
|
|
25
|
+
// A package whose os, cpu or engines the machine does not take is left
|
|
26
|
+
// out where it is optional, and refused where it is not, as yarn fails
|
|
27
|
+
// on it; so is the root project. ignore-platform and ignore-engines, where
|
|
28
|
+
// the settings set them, have neither checked. Of two references of one
|
|
29
|
+
// package, as a tag or an alias asked for twice makes, yarn fetches and
|
|
30
|
+
// reads the first alone, and the second has its lockfile entry for a
|
|
31
|
+
// package.json: no os, cpu, engines, peers or bins, as yarn has it, so it
|
|
32
|
+
// is never left out, and links no bins. Of engines, `node` (and `iojs`, which
|
|
33
|
+
// yarn takes for it) and `yarn` are checked as yarn checks them, and one
|
|
34
|
+
// yarn has no version for is passed over, as yarn passes over it; one any
|
|
35
|
+
// Node reports in process.versions, such as `v8` or `merve`, which yarn
|
|
36
|
+
// would check against the Node it runs on, is refused.
|
|
37
|
+
export interface Yarn1Host {
|
|
38
|
+
yarn?: string
|
|
39
|
+
node: string
|
|
40
|
+
os: string
|
|
41
|
+
cpu: string
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// A view of the lockfile's directory, by paths from `/`: a Vfs, or
|
|
45
|
+
// anything with its readdir, lstat, stat and readFile, such as one of a
|
|
46
|
+
// directory on disk, of which only this is read: the names in a
|
|
47
|
+
// directory; what a path is, `file`, `directory` or `symlink`, with lstat,
|
|
48
|
+
// and what it leads to with stat; and a file's bytes. Each throws an error
|
|
49
|
+
// whose `code` is ENOENT, ENOTDIR or ELOOP for a path that leads nowhere.
|
|
50
|
+
// Nothing is written to it.
|
|
51
|
+
export interface Yarn1Project {
|
|
52
|
+
readdir(path: string): string[]
|
|
53
|
+
lstat(path: string): { type: NodeType }
|
|
54
|
+
stat(path: string): { type: NodeType }
|
|
55
|
+
readFile(path: string): Uint8Array
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// The files an install reads: yarn.lock; the package.json of the root and
|
|
59
|
+
// of every workspace; and the .yarnrc and the .npmrc, where there are any.
|
|
60
|
+
//
|
|
61
|
+
// With `lockfile`, they are given as text: `manifests` by the project's
|
|
62
|
+
// directory relative to the lockfile's (`.` for the root, then each
|
|
63
|
+
// workspace's, as findYarn1Workspaces lists them). Every workspace yarn
|
|
64
|
+
// would find has to be given, which buildYarn1Tree cannot check where it
|
|
65
|
+
// is given them, as it is not given the directories; and the lockfile
|
|
66
|
+
// reader refuses a manifest of any directory the root's `workspaces` do
|
|
67
|
+
// not take.
|
|
68
|
+
//
|
|
69
|
+
// Without it, they are read from `project`, as yarn reads them there, and
|
|
70
|
+
// none of them may be given: yarn.lock, which has to be there; the root
|
|
71
|
+
// package.json, which has to be there too; the package.json of every
|
|
72
|
+
// directory the root's `workspaces` globs take, as yarn globs for them,
|
|
73
|
+
// outside node_modules; the .yarnrc and the .npmrc. Each is read as UTF-8,
|
|
74
|
+
// and refused where it is not; a byte order mark is dropped, as yarn drops
|
|
75
|
+
// it. A refusal names each as it would given: `manifests["packages/x"]`.
|
|
76
|
+
// Refused there: a link anywhere the globs reach, which node-glob follows
|
|
77
|
+
// or not by where it is; a node_modules the globs would take, whose
|
|
78
|
+
// package.json yarn would read as a workspace; a yarn.json, which yarn
|
|
79
|
+
// reads as a manifest too; and a .yarnrc.yml, whose yarnPath yarn 1.22
|
|
80
|
+
// runs in its stead.
|
|
81
|
+
//
|
|
82
|
+
// Settings from anywhere else — a .yarnrc or .npmrc above the project or
|
|
83
|
+
// in the home directory, the environment, the command line — are not read,
|
|
84
|
+
// and are taken to be at their defaults.
|
|
85
|
+
//
|
|
86
|
+
// Of the .yarnrc and the .npmrc, as yarn 1.22 reads them: yarn reads an
|
|
87
|
+
// option by its name from the .yarnrc, or else from the .npmrc, and takes
|
|
88
|
+
// a `--` line of the .yarnrc as a flag on its command line. Two settings
|
|
89
|
+
// are followed as yarn follows them: ignore-engines, by the flag
|
|
90
|
+
// `--ignore-engines true` or `--install.ignore-engines true`, or by the
|
|
91
|
+
// option where it is truthy as yarn reads it — a quoted "false" is; and
|
|
92
|
+
// ignore-platform, by its flag alone, as yarn does not act on the option.
|
|
93
|
+
// Options yarn reads that change what it installs are refused —
|
|
94
|
+
// ignore-optional, production, bin-links, workspaces-experimental and the
|
|
95
|
+
// like — and so is yarn-path, which has another yarn run, and
|
|
96
|
+
// global-folder, which, where it is the project's own directory, has yarn
|
|
97
|
+
// read its package.json files otherwise; any other option
|
|
98
|
+
// is passed over, as yarn reads it only for where it fetches from, how,
|
|
99
|
+
// and what it keeps, which the tree is held to the lockfile's integrities
|
|
100
|
+
// against whatever serves it, or for no install at all, as with most of
|
|
101
|
+
// npm's own. A flag is passed over where it does not change the tree —
|
|
102
|
+
// --registry, --frozen-lockfile, --network-timeout and the like — and
|
|
103
|
+
// refused where it may or is not known here: --production, --flat,
|
|
104
|
+
// --modules-folder, --ignore-optional, --no-bin-links, --no-lockfile,
|
|
105
|
+
// which has yarn read no yarn.lock, --global-folder, --cwd and the rest. A .yarnrc line indented under another, which yarn reads into it,
|
|
106
|
+
// is refused too, and so is a value of those two followed that is not true
|
|
107
|
+
// or false, or for the .yarnrc's option a quoted string.
|
|
108
|
+
//
|
|
109
|
+
// A workspace project is the root, marked private, and every directory its
|
|
110
|
+
// `workspaces` globs take — `*`, `?` and `**`, as the lockfile reader takes
|
|
111
|
+
// them — with a package.json that has a name and a version. `nohoist`,
|
|
112
|
+
// Plug'n'Play, and a root package.json's `flat` are refused.
|
|
113
|
+
//
|
|
114
|
+
// `vfs` is a Vfs to mount the tree into, at its root, which is taken to
|
|
115
|
+
// be the lockfile's directory, beside whatever it holds, such as the
|
|
116
|
+
// projects themselves — it may be `project` too; without one, a new Vfs
|
|
117
|
+
// holds the tree alone. Nothing the tree is built from is read from it. A
|
|
118
|
+
// Vfs that holds a node_modules anywhere, or on macOS a name that is one
|
|
119
|
+
// there, is refused before anything is fetched: kept beside the tree, Node
|
|
120
|
+
// would read it as the tree's, and removed, it would be the caller's lost.
|
|
121
|
+
// Nothing there is written over: each directory the tree has is one there
|
|
122
|
+
// or is made, and every file and link is written where nothing is. The
|
|
123
|
+
// tree is built, and held to every check below, before any of it is
|
|
124
|
+
// written, so a refusal leaves the Vfs as it was.
|
|
125
|
+
//
|
|
126
|
+
// The two ways the files come, one or the other: given, with `lockfile`
|
|
127
|
+
// and `manifests`; or read, with `project` and none of them. `project`
|
|
128
|
+
// given with `lockfile` is read only to tell whether a tag a top-level
|
|
129
|
+
// dependency asks for is a directory, which yarn would install instead.
|
|
130
|
+
export type Yarn1TreeOptions = Yarn1TreeGiven | Yarn1TreeRead
|
|
131
|
+
|
|
132
|
+
export interface Yarn1TreeGiven {
|
|
133
|
+
lockfile: string
|
|
134
|
+
manifests: Record<string, string> | Map<string, string>
|
|
135
|
+
yarnrc?: string
|
|
136
|
+
npmrc?: string
|
|
137
|
+
project?: Yarn1Project
|
|
138
|
+
host: Yarn1Host
|
|
139
|
+
vfs?: Vfs
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export interface Yarn1TreeRead {
|
|
143
|
+
lockfile?: undefined
|
|
144
|
+
manifests?: undefined
|
|
145
|
+
yarnrc?: undefined
|
|
146
|
+
npmrc?: undefined
|
|
147
|
+
project: Yarn1Project
|
|
148
|
+
host: Yarn1Host
|
|
149
|
+
vfs?: Vfs
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// What buildYarn1Tree counts, all of it plain numbers: `packages` the
|
|
153
|
+
// registry packages yarn resolves, each fetched; `skipped` the optional
|
|
154
|
+
// ones left out, as the host cannot run them; `installed` the copies of
|
|
155
|
+
// them in the tree; `files` and `bytes` what is written; `links` the
|
|
156
|
+
// workspaces linked.
|
|
157
|
+
export interface Yarn1TreeStats {
|
|
158
|
+
packages: number
|
|
159
|
+
skipped: number
|
|
160
|
+
installed: number
|
|
161
|
+
files: number
|
|
162
|
+
bytes: number
|
|
163
|
+
links: number
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// `vfs` is the one given, the tree mounted into it, or a new one.
|
|
167
|
+
export interface Yarn1Tree {
|
|
168
|
+
vfs: Vfs
|
|
169
|
+
stats: Yarn1TreeStats
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// The node_modules tree `yarn install --frozen-lockfile --ignore-scripts`
|
|
173
|
+
// makes, with host.yarn's node_modules linker, into a project with no
|
|
174
|
+
// node_modules yet: rooted at the lockfile's directory, each package's
|
|
175
|
+
// files copied where yarn hoists it, and each workspace linked where yarn
|
|
176
|
+
// hoists it, by a relative link to its directory, with what yarn hoists
|
|
177
|
+
// beneath it in that directory's own node_modules. The tree holds
|
|
178
|
+
// node_modules and nothing else of the projects.
|
|
179
|
+
//
|
|
180
|
+
// The lockfile is resolved as yarn resolves it, in yarn's order, which
|
|
181
|
+
// decides where a package goes: the resolutions' patterns, then the root's
|
|
182
|
+
// dependencies, devDependencies and optionalDependencies, then the
|
|
183
|
+
// workspaces, each request with all it leads to before the next, and
|
|
184
|
+
// beneath one every request at once, as yarn's resolver runs them; each
|
|
185
|
+
// package's peers looked for along the shortest chain of names that asked
|
|
186
|
+
// for it, as yarn looks for them; and the tree laid out as yarn's hoister
|
|
187
|
+
// lays it out, quirks and all. A resolution applies to a dependency by its
|
|
188
|
+
// path, as yarn applies it, and not to one of the root's own.
|
|
189
|
+
//
|
|
190
|
+
// Scripts are always ignored, as `--ignore-scripts` has yarn ignore them,
|
|
191
|
+
// whatever the settings say. No .bin is written, nor yarn's own
|
|
192
|
+
// .yarn-integrity; but each bin's target is made executable, chmod 755, in
|
|
193
|
+
// every copy of its package, as yarn's fetcher makes it in its cache,
|
|
194
|
+
// whether yarn links the bin or not. Linking makes no file of the tree
|
|
195
|
+
// executable that the fetcher has not: a bin in its package's own
|
|
196
|
+
// node_modules, where yarn installs its dependencies, is refused, as
|
|
197
|
+
// linking it would make the file there executable, and so is a package
|
|
198
|
+
// with bins and a .bin file, where yarn fails to make a directory for
|
|
199
|
+
// them; a workspace's bins are its own files, which the tree does not
|
|
200
|
+
// hold. Each file's mode is the tarball's, with
|
|
201
|
+
// read for all added and a umask of 0o022 taken off, as yarn unpacks it;
|
|
202
|
+
// .bin, .yarn-metadata.json and .yarn-tarball.tgz in a package are left
|
|
203
|
+
// out, as yarn's copy leaves them out.
|
|
204
|
+
//
|
|
205
|
+
// The lockfile is held to what the lockfile reader holds it to, with the
|
|
206
|
+
// manifests, and to what a frozen install holds it to: every pattern
|
|
207
|
+
// asked for has its entry, and each entry a version in the range of its
|
|
208
|
+
// pattern, as yarn would otherwise resolve the pattern anew, frozen or not.
|
|
209
|
+
// The root package.json's packageManager, where it has one, has to be
|
|
210
|
+
// `yarn@` host.yarn exactly, as corepack would run the one it names.
|
|
211
|
+
//
|
|
212
|
+
// And to more than yarn holds it to, where a lockfile yarn writes, or a
|
|
213
|
+
// package the registry serves, always holds: each tarball gzipped, every
|
|
214
|
+
// file under one directory, none in the package's own node_modules, where
|
|
215
|
+
// yarn installs its dependencies, no link or device in it, and a
|
|
216
|
+
// 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
|
|
218
|
+
// directory that differ only in case or normalization are refused, as
|
|
219
|
+
// they would be one name there.
|
|
220
|
+
//
|
|
221
|
+
// Packages come from npm's registry alone: each lockfile entry's
|
|
222
|
+
// `resolved` has to be the registry's own URL of its tarball, exactly as
|
|
223
|
+
// npm spells it, https://registry.npmjs.org/<name>/-/<base>-<version>.tgz
|
|
224
|
+
// (a `#` and the sha1 after it aside), or that same URL on yarn's mirror,
|
|
225
|
+
// https://registry.yarnpkg.com/, which is taken for npm's; it is fetched
|
|
226
|
+
// from npm's through @preventive/upstream. Each is asked for by a semver
|
|
227
|
+
// range, an `npm:` alias, or a tag — the last only where the project is
|
|
228
|
+
// given, and a directory of the project of that name is not there, which
|
|
229
|
+
// yarn would install instead, and only where yarn would not have two
|
|
230
|
+
// requests of tags wait on the filesystem at once, which it answers in no
|
|
231
|
+
// set order.
|
|
232
|
+
//
|
|
233
|
+
// 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.
|
|
237
|
+
//
|
|
238
|
+
// Nothing is left to a guess: a lockfile the lockfile reader refuses, a
|
|
239
|
+
// setting this does not know or does not build for, a package from
|
|
240
|
+
// anywhere but the registry — git, a tarball's URL, a directory by
|
|
241
|
+
// `file:` or `link:` — a range that is none of those above, a package
|
|
242
|
+
// with bundled dependencies, another package manager, a check above that
|
|
243
|
+
// fails — each is refused with a DeptreeError or a LockfileError that says
|
|
244
|
+
// where. A TypeError is thrown for options of the wrong type.
|
|
245
|
+
export function buildYarn1Tree(options: Yarn1TreeOptions): Promise<Yarn1Tree>
|
|
246
|
+
|
|
247
|
+
// The directories of the projects yarn finds in `project`, by which
|
|
248
|
+
// buildYarn1Tree takes their package.json given: `.`, the root, first,
|
|
249
|
+
// then each directory with a package.json that a glob of the root's
|
|
250
|
+
// `workspaces` takes, outside node_modules, as yarn finds them, and as
|
|
251
|
+
// buildYarn1Tree reads them from a project. Refused as buildYarn1Tree
|
|
252
|
+
// refuses it: a link where the globs reach, a node_modules they would
|
|
253
|
+
// take, a yarn.json; and a root with no package.json.
|
|
254
|
+
export interface Yarn1WorkspacesOptions {
|
|
255
|
+
project: Yarn1Project
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export function findYarn1Workspaces(options: Yarn1WorkspacesOptions): string[]
|
|
259
|
+
|
|
260
|
+
// `where` is what a refusal is about — `.yarnrc:3`, `manifests["."]`, a
|
|
261
|
+
// package's pattern — or undefined for the call as a whole; the message
|
|
262
|
+
// leads with it. `cause` is what a package beneath refused with, where one
|
|
263
|
+
// did.
|
|
264
|
+
export class DeptreeError extends Error {
|
|
265
|
+
constructor(detail: string, where?: string, options?: { cause?: unknown })
|
|
266
|
+
where: string | undefined
|
|
267
|
+
}
|
package/yarn1.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
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.
|
|
7
|
+
export { buildYarn1Tree } from './src/yarn1/tree.js'
|
|
8
|
+
export { findYarn1Workspaces } from './src/yarn1/inputs.js'
|
|
9
|
+
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
|
+
export { setCacheDir } from '@preventive/upstream/npm.js'
|
|
13
|
+
// What the lockfile, and the manifests read with it, are refused with.
|
|
14
|
+
export { LockfileError } from '@preventive/lockfile/yarn1.js'
|