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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. package/LICENSE-APACHE +202 -0
  2. package/npm.d.ts +288 -0
  3. package/npm.js +7 -0
  4. package/package.json +36 -10
  5. package/pnpm.d.ts +116 -22
  6. package/pnpm.js +2 -10
  7. package/soldeer.d.ts +13 -0
  8. package/soldeer.js +1 -7
  9. package/src/concurrent.js +16 -17
  10. package/src/error.js +5 -9
  11. package/src/glob.js +55 -0
  12. package/src/hash.js +64 -13
  13. package/src/manifest.js +52 -0
  14. package/src/matcher.js +59 -10
  15. package/src/mount.js +95 -44
  16. package/src/npm/bins.js +71 -0
  17. package/src/npm/compat.js +92 -0
  18. package/src/npm/dev-engines.js +52 -0
  19. package/src/npm/graph.js +68 -0
  20. package/src/npm/inputs.js +80 -0
  21. package/src/npm/manifests.js +138 -0
  22. package/src/npm/package.js +57 -0
  23. package/src/npm/ratio.js +34 -0
  24. package/src/npm/settings.js +67 -0
  25. package/src/npm/tree.js +136 -0
  26. package/src/npm/versions.js +20 -0
  27. package/src/{pnpm/npmrc.js → npmrc.js} +5 -11
  28. package/src/patch.js +21 -31
  29. package/src/pnpm/bins.js +32 -103
  30. package/src/pnpm/checks.js +8 -35
  31. package/src/pnpm/commands.js +49 -86
  32. package/src/pnpm/filename.js +10 -21
  33. package/src/pnpm/frozen.js +27 -70
  34. package/src/pnpm/gitignore.js +137 -0
  35. package/src/pnpm/glob.js +65 -0
  36. package/src/pnpm/graph.js +15 -43
  37. package/src/pnpm/hoist.js +65 -55
  38. package/src/pnpm/hook.js +26 -31
  39. package/src/pnpm/inputs.js +67 -40
  40. package/src/pnpm/install.js +72 -115
  41. package/src/pnpm/local.js +21 -40
  42. package/src/pnpm/minimatch.js +328 -0
  43. package/src/pnpm/npm-packlist.js +221 -0
  44. package/src/pnpm/overrides.js +10 -49
  45. package/src/pnpm/package.js +120 -0
  46. package/src/pnpm/packlist.js +168 -109
  47. package/src/pnpm/patches.js +18 -25
  48. package/src/pnpm/projects.js +63 -107
  49. package/src/pnpm/readers.js +46 -55
  50. package/src/pnpm/settings.js +60 -97
  51. package/src/pnpm/tree.js +134 -149
  52. package/src/pnpm/uptodate.js +16 -28
  53. package/src/pnpm/workspace.js +122 -131
  54. package/src/project.js +41 -0
  55. package/src/soldeer/config.js +8 -24
  56. package/src/soldeer/inputs.js +2 -5
  57. package/src/soldeer/tree.js +20 -56
  58. package/src/soldeer/zip.js +17 -36
  59. package/src/tarball.js +38 -142
  60. package/src/yarn1/bins.js +9 -19
  61. package/src/yarn1/compat.js +9 -28
  62. package/src/yarn1/hoist.js +53 -68
  63. package/src/yarn1/inputs.js +16 -51
  64. package/src/yarn1/manifest.js +8 -23
  65. package/src/yarn1/package.js +24 -63
  66. package/src/yarn1/peers.js +8 -15
  67. package/src/yarn1/requests.js +19 -37
  68. package/src/yarn1/resolve.js +35 -111
  69. package/src/yarn1/settings.js +14 -41
  70. package/src/yarn1/tree.js +67 -91
  71. package/yarn1.d.ts +30 -2
  72. package/yarn1.js +1 -8
  73. package/src/pnpm/project.js +0 -97
  74. package/src/yarn1/glob.js +0 -52
  75. /package/{LICENSE → LICENSE-MIT} +0 -0
@@ -1,17 +1,13 @@
1
- // What buildYarn1Tree takes, checked: the host yarn would install on, and
2
- // the files an install reads — given as text, or read from the project
3
- // (../pnpm/project.js) as yarn reads them from disk.
1
+ // What buildYarn1Tree takes, as text or read from the project as yarn does.
4
2
 
5
3
  import { valid } from '@preventive/upstream/semver.js'
6
4
  import { DeptreeError, quote } from '../error.js'
7
- import { checkProject, readText, typeOf } from '../pnpm/project.js'
8
- import { matchesGlob, reachesBelow } from './glob.js'
9
- import { globsOf, readManifest } from './manifest.js'
5
+ import { readManifest, readManifests } from '../manifest.js'
6
+ import { checkProject, readText, typeOf } from '../project.js'
7
+ import { walkWorkspaces } from '../glob.js'
8
+ import { globsOf } from './manifest.js'
10
9
  import { readSettings } from './settings.js'
11
10
 
12
- // The yarn that installs: host.yarn, or where that is left out, the one
13
- // the root package.json's packageManager pins, as corepack runs that one;
14
- // a 1.22.x, the line this follows.
15
11
  function yarnOf(yarn, root) {
16
12
  if (yarn !== undefined && (typeof yarn !== 'string' || yarn === '')) throw new TypeError('host.yarn must be a non-empty string, or left out')
17
13
  const { packageManager } = root
@@ -38,39 +34,18 @@ export function checkHost(host, root) {
38
34
  return { yarn: yarnOf(host.yarn, root), node: host.node, os: host.os, cpu: host.cpu }
39
35
  }
40
36
 
41
- // yarn reads a yarn.json beside each package.json it reads, or in its
42
- // stead, as the manifest of its own registry.
43
37
  const yarnJson = 'a yarn.json, which yarn reads as a manifest too, is not supported'
44
38
 
45
- // The directories of the workspaces in `project` as yarn's resolveWorkspaces
46
- // finds them: each with a package.json that a glob takes, outside
47
- // node_modules. Every directory a glob may reach is read; a link in one,
48
- // which node-glob follows or not by where it is, is refused, and so is a
49
- // node_modules a glob would take, whose package.json yarn would read.
39
+ // As yarn's resolveWorkspaces finds them.
50
40
  function findWorkspaces(project, globs) {
51
- const found = []
52
- if (globs.length === 0) return found
53
- const pending = ['']
54
- while (pending.length > 0) {
55
- const dir = pending.pop()
56
- for (const name of project.readdir(`/${dir}`)) {
57
- const path = dir === '' ? name : `${dir}/${name}`
58
- const taken = globs.some((glob) => matchesGlob(glob, path))
59
- if (!taken && !globs.some((glob) => reachesBelow(glob, path))) continue
60
- const { type } = project.lstat(`/${path}`)
61
- if (type === 'symlink') throw new DeptreeError('a link where yarn looks for workspaces is not supported', quote(path))
62
- if (type !== 'directory') continue
63
- const manifest = taken && typeOf(project, `/${path}/package.json`) !== undefined
64
- if (name === 'node_modules') {
65
- if (manifest) throw new DeptreeError('yarn would read this node_modules as a workspace, which is not supported', quote(path))
66
- continue
67
- }
68
- if (taken && typeOf(project, `/${path}/yarn.json`) !== undefined) throw new DeptreeError(yarnJson, quote(`${path}/yarn.json`))
69
- if (manifest) found.push(path)
70
- pending.push(path)
71
- }
72
- }
73
- return found.sort()
41
+ return walkWorkspaces(project, globs, {
42
+ manager: 'yarn',
43
+ enter: (path, name, taken, manifest) => {
44
+ if (name === 'node_modules' && manifest) throw new DeptreeError('yarn would read this node_modules as a workspace, which is not supported', quote(path))
45
+ if (name !== 'node_modules' && taken && typeOf(project, `/${path}/yarn.json`) !== undefined) throw new DeptreeError(yarnJson, quote(`${path}/yarn.json`))
46
+ return name !== 'node_modules'
47
+ },
48
+ })
74
49
  }
75
50
 
76
51
  function readRoot(project) {
@@ -80,8 +55,6 @@ function readRoot(project) {
80
55
  return readManifest(text, 'manifests["."]')
81
56
  }
82
57
 
83
- // The directories of the projects yarn installs in `project`: `.`, then
84
- // each workspace's, as buildYarn1Tree takes their package.json given.
85
58
  export function findYarn1Workspaces(options) {
86
59
  const { project } = options ?? {}
87
60
  checkProject(project)
@@ -90,9 +63,6 @@ export function findYarn1Workspaces(options) {
90
63
 
91
64
  const LOCKFILE = 'lockfile must be the text of yarn.lock, or left out with a project given to read it from'
92
65
 
93
- // The files an install reads, given or read from the project: yarn.lock,
94
- // each package.json by directory, the root's `.` among them, and what of
95
- // the .yarnrc and .npmrc the install follows.
96
66
  export function inputsOf(options) {
97
67
  const { lockfile, manifests, yarnrc, npmrc, project } = options
98
68
  if (project !== undefined) checkProject(project)
@@ -108,9 +78,7 @@ export function inputsOf(options) {
108
78
  read.set(dir, readManifest(readText(project, `/${dir}/package.json`, `manifests[${quote(dir)}]`), `manifests[${quote(dir)}]`))
109
79
  }
110
80
  if (typeOf(project, '/.yarnrc.yml') !== undefined) throw new DeptreeError('a .yarnrc.yml, whose yarnPath yarn 1.22 runs in its stead, is not supported', '.yarnrc.yml')
111
- const rc = readText(project, '/.yarnrc')
112
- const npm = readText(project, '/.npmrc')
113
- return { lockfile: text, manifests: read, settings: readSettings({ yarnrc: rc, npmrc: npm }), project }
81
+ return { lockfile: text, manifests: read, settings: readSettings({ yarnrc: readText(project, '/.yarnrc'), npmrc: readText(project, '/.npmrc') }), project }
114
82
  }
115
83
  if (typeof lockfile !== 'string') throw new TypeError(LOCKFILE)
116
84
  if (manifests === null || typeof manifests !== 'object') throw new TypeError('manifests must map each project\'s directory to its package.json')
@@ -118,8 +86,5 @@ export function inputsOf(options) {
118
86
  if (value !== undefined && typeof value !== 'string') throw new TypeError(`${name} must be a string, or left out`)
119
87
  }
120
88
  const settings = readSettings({ yarnrc, npmrc })
121
- const read = new Map()
122
- for (const [dir, text] of manifests instanceof Map ? manifests : Object.entries(manifests)) read.set(dir, readManifest(text, `manifests[${quote(dir)}]`))
123
- if (!read.has('.')) throw new DeptreeError('the root package.json is not given', 'manifests["."]')
124
- return { lockfile, manifests: read, settings, project }
89
+ return { lockfile, manifests: readManifests(manifests), settings, project }
125
90
  }
@@ -1,17 +1,9 @@
1
- // A package.json as yarn 1 reads one (util/normalize-manifest): its text
2
- // parsed, its dependency lists as yarn leaves them, its workspace globs,
3
- // and what of the root's yarn fails on, or would install otherwise than
4
- // this follows.
1
+ // A package.json as yarn 1's util/normalize-manifest reads it.
5
2
 
6
3
  import { DeptreeError, quote } from '../error.js'
7
4
 
8
- // yarn's readJson reads a package.json as pnpm does: a byte order mark
9
- // dropped.
10
- export { readManifest } from '../pnpm/projects.js'
11
-
12
- // yarn's cleanDependencies: a name in several of the dependency lists kept
13
- // in the first of optionalDependencies, dependencies and devDependencies,
14
- // at the first of its ranges that is neither '' nor '*'.
5
+ // yarn's cleanDependencies: a name in several of these lists is kept in the
6
+ // first, at its first range that is neither '' nor '*'.
15
7
  export function cleanDependencies(manifest) {
16
8
  const kinds = ['optionalDependencies', 'dependencies', 'devDependencies'].filter((kind) => manifest[kind] !== null && typeof manifest[kind] === 'object')
17
9
  const ranges = new Map()
@@ -32,10 +24,7 @@ export function cleanDependencies(manifest) {
32
24
  return manifest
33
25
  }
34
26
 
35
- // The lists of a package.json this reads, as yarn's normalize-manifest
36
- // leaves them: a `//` key, a comment, dropped, a value that is not one
37
- // given as `''`, and a name in several of the dependency lists kept in
38
- // one (cleanDependencies). A copy; the manifest is left as it is.
27
+ // The lists as normalize-manifest leaves them, a `//` key, a comment, dropped.
39
28
  const LISTS = ['resolutions', 'devDependencies', 'dependencies', 'optionalDependencies', 'peerDependencies']
40
29
  export function fixLists(manifest) {
41
30
  const fixed = { ...manifest }
@@ -50,16 +39,12 @@ export function fixLists(manifest) {
50
39
  return cleanDependencies(fixed)
51
40
  }
52
41
 
53
- // The workspace globs of the root, as yarn reads them.
54
- export function globsOf(root) {
55
- const value = root.workspaces
56
- if (value === undefined) return []
57
- return (Array.isArray(value) ? value : value?.packages ?? []).map((glob) => String(glob).replace(/^(?:\.\/)+|\/+$/gu, ''))
42
+ export function globsOf({ workspaces }) {
43
+ return (Array.isArray(workspaces) ? workspaces : workspaces?.packages ?? []).map((glob) => String(glob).replace(/^(?:\.\/)+|\/+$/gu, ''))
58
44
  }
59
45
 
60
- // What yarn's normalize-manifest fails on in the root's name and version,
61
- // which the lockfile reader does not read; and what of the root yarn would
62
- // install otherwise than this follows.
46
+ // What normalize-manifest fails on in the root's name and version, which
47
+ // the lockfile reader does not read; and what of the root is not followed.
63
48
  const NAME = /[/@\s+%:]/u
64
49
  const validName = (name) => !NAME.test(name) && encodeURIComponent(name) === name
65
50
  export function checkRoot(root) {
@@ -1,35 +1,18 @@
1
- // A registry package as yarn 1 installs it: its tarball fetched through
2
- // @preventive/upstream and held to the lockfile's sha512 integrity, and to
3
- // the sha1 after the `#` of its URL where there is one, as yarn checks
4
- // both; unpacked as yarn's tarball fetcher unpacks it, with tar-fs, the
5
- // first segment of each name dropped, each file's mode with 0o644 added
6
- // and masked by a umask of 0o022; and its package.json read as yarn's
7
- // normalize-manifest reads the fields it installs by.
8
- //
9
- // Held to more than yarn holds it to: a lockfile's URL of it that is the
10
- // registry's own, as npm spells it, or that on yarn's mirror; and to what
11
- // npm packs: a gzipped tarball, every entry under one directory, none in
12
- // the package's own node_modules, which npm packs only for bundled
13
- // dependencies, no link of either kind or device, no name twice as two
14
- // different files; and a package.json for exactly the name and version
15
- // the lockfile has.
1
+ // A registry package as yarn 1 installs it, each mode given 0o644 and masked
2
+ // by a 0o022 umask as yarn's tarball fetcher does with tar-fs. Held to more
3
+ // than yarn holds it to: what npm packs, every entry under one directory,
4
+ // none in its own node_modules (npm packs those only for bundled
5
+ // dependencies), and a package.json of the lockfile's name and version.
16
6
 
17
7
  import { normalize } from '@preventive/vfs/path.js'
18
8
  import { DeptreeError, quote } from '../error.js'
19
9
  import { sha1Hex } from '../hash.js'
20
- import { REGISTRY, fetchTarball, sameBytes, tarballUrl } from '../tarball.js'
21
- import { readManifest } from './manifest.js'
10
+ import { readManifest } from '../manifest.js'
11
+ import { fetchTarball, fromMirror, isModules, ownTarball, sameFile, withDirs } from '../tarball.js'
22
12
 
23
13
  const UMASK = 0o022
24
14
 
25
- // yarn's mirror of npm's registry, which serves the same tarballs at the
26
- // same paths.
27
- const YARNPKG = 'https://registry.yarnpkg.com/'
28
-
29
- // Each entry by its path in the package, as tar-fs writes it with
30
- // `strip: 1`: its first segment dropped, and the rest joined to the
31
- // package's directory as a path from `/`, so `.`, `..` and empty segments
32
- // folded; files with their bytes and mode, and directories.
15
+ // Paths as tar-fs with `strip: 1` writes them: `.`, `..` and `//` folded.
33
16
  function entriesOf(entries, where) {
34
17
  const files = new Map()
35
18
  const dirs = new Set()
@@ -40,7 +23,7 @@ function entriesOf(entries, where) {
40
23
  if (top !== undefined && first !== top) throw new DeptreeError('the tarball has entries under more than one directory', where)
41
24
  top = first
42
25
  const path = slash === -1 ? '' : normalize(`/${entry.storedName.slice(slash + 1)}`).slice(1).replace(/\/$/u, '')
43
- if (path.split('/')[0] === 'node_modules') throw new DeptreeError(`${quote(entry.storedName)} is in the package's own node_modules, where yarn installs its dependencies, which is not supported`, where)
26
+ if (isModules(path.split('/')[0])) throw new DeptreeError(`${quote(entry.storedName)} is in the package's own node_modules, where yarn installs its dependencies, which is not supported`, where)
44
27
  if (entry.type === 'directory') {
45
28
  if (path !== '') dirs.add(path)
46
29
  continue
@@ -49,38 +32,25 @@ function entriesOf(entries, where) {
49
32
  if (path === '') throw new DeptreeError(`${quote(entry.storedName)} is a file at the top of the tarball`, where)
50
33
  const file = { data: entry.data, mode: (entry.mode | 0o644) & ~UMASK & 0o777 }
51
34
  const earlier = files.get(path)
52
- if (earlier !== undefined && !(earlier.mode === file.mode && sameBytes(earlier.data, file.data))) throw new DeptreeError(`${quote(path)} is in the tarball twice`, where)
35
+ if (earlier !== undefined && !sameFile(earlier, file)) throw new DeptreeError(`${quote(path)} is in the tarball twice`, where)
53
36
  files.set(path, file)
54
37
  }
55
- for (const path of files.keys()) {
56
- const segments = path.split('/')
57
- for (let i = 1; i < segments.length; i++) dirs.add(segments.slice(0, i).join('/'))
58
- }
59
- for (const dir of dirs) if (files.has(dir)) throw new DeptreeError(`${quote(dir)} is both a file and a directory in the tarball`, where)
60
- return { files, dirs }
38
+ return withDirs(files, where, dirs)
61
39
  }
62
40
 
63
41
  const decoder = new TextDecoder('utf-8', { fatal: true })
64
42
 
65
- // The registry's tarball of a lockfile entry: its name, as an `npm:` alias
66
- // asks for it; held to be the registry's own URL for that name and
67
- // version, exactly as npm spells it, yarn's mirror taken for npm's.
43
+ // `name` is the one fetched, as an `npm:` alias asks for it.
68
44
  export function registryTarball(entry, name, where) {
69
45
  const { resolution } = entry
70
46
  if (resolution === undefined) throw new DeptreeError('a directory, by file: or link:, is not supported', where)
71
- const expected = tarballUrl(name, entry.version)
72
- const url = resolution.type === 'tarball' && resolution.tarball.startsWith(YARNPKG) ? `${REGISTRY}${resolution.tarball.slice(YARNPKG.length)}` : resolution.tarball
73
- if (url !== expected) throw new DeptreeError(`only the registry's own tarball of ${name}@${entry.version}, ${expected}, is supported`, where)
74
- const sha512 = resolution.integrity?.split(' ').find((part) => part.startsWith('sha512-'))
75
- if (sha512 === undefined) throw new DeptreeError('a tarball with no sha512 integrity is not supported', where)
76
- return { name, version: entry.version, integrity: sha512, sha1: resolution.sha1 }
47
+ const url = resolution.type === 'tarball' ? fromMirror(resolution.tarball) : resolution.tarball
48
+ return ownTarball(url, name, entry.version, resolution.integrity, where)
77
49
  }
78
50
 
79
- // The package's entries, as yarn's fetcher leaves them, and its
80
- // package.json as parsed.
81
- export async function fetchYarnPackage({ name, version, integrity, sha1 }, where) {
51
+ export async function fetchYarnPackage({ name, version, integrity }, where) {
82
52
  const { bytes, entries } = await fetchTarball(name, version, integrity, where)
83
- if (sha1 !== undefined && await sha1Hex(bytes) !== sha1) throw new DeptreeError(`the tarball's sha1 is not ${sha1}`, where)
53
+ const sha1 = await sha1Hex(bytes)
84
54
  const { files, dirs } = entriesOf(entries, where)
85
55
  const file = files.get('package.json')
86
56
  if (file === undefined) throw new DeptreeError('the tarball has no package.json', where)
@@ -92,32 +62,25 @@ export async function fetchYarnPackage({ name, version, integrity, sha1 }, where
92
62
  }
93
63
  const manifest = readManifest(text, `${where}: package.json`)
94
64
  if (manifest.name !== name || manifest.version !== version) throw new DeptreeError(`package.json is for ${quote(`${manifest.name}@${manifest.version}`)}`, where)
95
- // As yarn's fetcher leaves the package in its cache, from which it copies
96
- // it wherever it goes (base-fetcher.js): each bin's target made
97
- // executable, chmod 755, a trailing `/` of it dropped; and a .bin made
98
- // for links to them, which it fails to make over a file.
65
+ // As yarn's fetcher leaves the package in its cache (base-fetcher.js): each
66
+ // bin's target chmod 755, and a .bin made for the links.
99
67
  const bins = binsOf(manifest, { files, dirs })
100
68
  if (bins.size > 0 && files.has('.bin')) throw new DeptreeError('.bin is a file, where yarn fails to make a directory for the bins', where)
101
69
  for (const target of bins.values()) {
102
- if (target.split('/')[0] === 'node_modules') throw new DeptreeError(`its bin ${quote(target)} is in its own node_modules, where yarn installs its dependencies, which is not supported`, where)
70
+ if (isModules(target.split('/')[0])) throw new DeptreeError(`its bin ${quote(target)} is in its own node_modules, where yarn installs its dependencies, which is not supported`, where)
103
71
  const script = files.get(target.replace(/\/$/u, ''))
104
72
  if (script !== undefined) script.mode = 0o755
105
73
  }
106
- return { files, dirs, manifest }
74
+ return { files, dirs, manifest, sha1, hasBins: bins.size > 0 }
107
75
  }
108
76
 
109
- // A bin's name, and its target as a path in the package, as yarn's
110
- // normalize-manifest has them: a string `bin` named for the package, its
111
- // scope dropped; a name that is not valid, or a target outside the
112
- // package, dropped; with no `bin`, each name in directories.bin but for
113
- // one with a leading dot. `dirs` and `files` the package's. A target is
114
- // normalized as Node's path.normalize does it, which is as vfs's does: a
115
- // trailing `/` kept.
77
+ // Bins as yarn's normalize-manifest reads them, targets normalized as Node's
78
+ // path.normalize does it, a trailing `/` kept.
116
79
  const VALID_BIN_KEYS = /^(?!\.{0,2}$)[a-z0-9._-]+$/iu
117
80
 
118
81
  const outside = (path) => path.startsWith('/') || path === '..' || path.startsWith('../')
119
82
 
120
- export function binsOf(manifest, { files, dirs }) {
83
+ function binsOf(manifest, { files, dirs }) {
121
84
  let { bin } = manifest
122
85
  if (typeof manifest.name === 'string' && typeof bin === 'string' && bin.length > 0) bin = { [manifest.name.replace(/^@[^/]+\//u, '')]: bin }
123
86
  const bins = new Map()
@@ -136,13 +99,11 @@ export function binsOf(manifest, { files, dirs }) {
136
99
  if (outside(dir)) throw new DeptreeError(`directories.bin, ${quote(binDir)}, is outside the package, which is not supported`, quote(manifest.name))
137
100
  if (files.has(dir)) throw new DeptreeError(`directories.bin, ${quote(binDir)}, is a file, which yarn fails to read as a directory`, quote(manifest.name))
138
101
  const prefix = dir === '.' ? '' : `${dir}/`
139
- const names = new Set()
140
102
  for (const path of [...files.keys(), ...dirs]) {
141
103
  if (!path.startsWith(prefix)) continue
142
104
  const [first] = path.slice(prefix.length).split('/')
143
- if (first !== '' && !first.startsWith('.')) names.add(first)
105
+ if (first !== '' && !first.startsWith('.')) bins.set(first, `${prefix}${first}`)
144
106
  }
145
- for (const name of names) bins.set(name, `${prefix}${name}`)
146
107
  }
147
108
  return bins
148
109
  }
@@ -1,15 +1,11 @@
1
- // yarn 1's resolvePeerModules (package-linker.js), run before it hoists:
2
- // each package's peers are looked for along the shortest chain of names it
3
- // was requested by, and the nearest version found there that takes the
4
- // peer's range is added to its dependencies, by all of that version's
5
- // patterns. Where none is found, nothing is added, and yarn warns.
1
+ // yarn 1's resolvePeerModules (package-linker.js), run before it hoists: a
2
+ // peer is looked for along the shortest chain of names the package was asked
3
+ // for by, and the nearest in range added; where none is, yarn only warns.
6
4
 
7
5
  import { compareVersions, valid, validRange } from '@preventive/upstream/semver.js'
8
6
 
9
- // yarn's satisfiesWithPrereleases (util/semver.js): each comparator of a
10
- // set tested on its own, with no say of semver's over prereleases, and a
11
- // `<` with no prerelease of its own made a `<` its lowest one, so that
12
- // `<2.0.0` takes no 2.0.0-rc.1; read off the range as semver normalizes it.
7
+ // yarn's util/semver.js: semver's prerelease rule ignored, and `<x` with no
8
+ // prerelease read as `<x-0`, so `<2.0.0` takes no 2.0.0-rc.1.
13
9
  const OPERATORS = /^(<=|>=|<|>|=)?(.*)$/u
14
10
 
15
11
  export function satisfiesWithPrereleases(version, range, loose = false) {
@@ -24,12 +20,9 @@ export function satisfiesWithPrereleases(version, range, loose = false) {
24
20
  }))
25
21
  }
26
22
 
27
- // `resolved` is resolve.js's; `manifests` each reference's package.json,
28
- // by reference, as fetched or as the workspace has it.
29
23
  export function resolvePeers(resolved, manifests) {
30
24
  const { patterns, byName } = resolved
31
- for (const ref of new Set(patterns.values())) {
32
- const manifest = manifests.get(ref)
25
+ for (const [ref, manifest] of manifests) {
33
26
  const peers = manifest?.peerDependencies
34
27
  if (!peers) continue
35
28
  const chain = ref.requests.map((request) => request.parentNames ?? []).sort((a, b) => a.length - b.length)[0]
@@ -47,12 +40,12 @@ export function resolvePeers(resolved, manifests) {
47
40
  let found
48
41
  for (const candidate of new Set((byName.get(name) ?? []).map((pattern) => patterns.get(pattern)))) {
49
42
  const d = distance(candidate)
50
- if (Number.isFinite(d) && d < best && (range === '*' || satisfiesWithPrereleases(candidate.version, range, true))) {
43
+ if (d < best && (range === '*' || satisfiesWithPrereleases(candidate.version, range, true))) {
51
44
  best = d
52
45
  found = candidate
53
46
  }
54
47
  }
55
- if (found !== undefined) ref.dependencies.push(...found.patterns)
48
+ if (found !== undefined) ref.asked.push(...found.patterns.map((pattern) => ({ pattern, optional: false, dev: false })))
56
49
  }
57
50
  }
58
51
  }
@@ -1,24 +1,18 @@
1
- // What yarn 1 asks for at the top of an install (install.js's
2
- // fetchRequestFromCwd): the workspaces, in the order it finds them; the
3
- // aggregator it makes of them; the root's resolutions, as resolution-map.js
4
- // reads them; and the top-level requests, in order.
1
+ // What yarn 1 asks for at the top (install.js's fetchRequestFromCwd).
5
2
 
6
3
  import { clean, validRange } from '@preventive/upstream/semver.js'
7
4
  import { DeptreeError, quote } from '../error.js'
8
- import { matchesGlob } from './glob.js'
5
+ import { matchesGlob } from '../glob.js'
9
6
  import { cleanDependencies, globsOf } from './manifest.js'
10
7
 
11
- // The name yarn gives the workspace aggregator is random; this stands for
12
- // it, and sorts where a name starting so sorts.
8
+ // yarn names the workspace aggregator at random; this stands for it, and
9
+ // sorts where a name starting so sorts.
13
10
  export const AGGREGATOR = 'workspace-aggregator-00000000-0000-0000-0000-000000000000'
14
11
 
15
12
  const KINDS = ['dependencies', 'devDependencies', 'optionalDependencies']
16
13
 
17
- // The workspaces by name, in the order yarn's resolveWorkspaces finds
18
- // them: each glob in turn, its matches as node-glob sorts them; each with
19
- // its directory, its version as yarn cleans it, and its manifest.
20
- // `manifests` by directory, the root's `.` among them, as the lockfile
21
- // reader has held them to the globs.
14
+ // In the order yarn's resolveWorkspaces finds them, glob by glob, matches as
15
+ // node-glob sorts them; the lockfile reader has held `manifests` to the globs.
22
16
  export function workspacesOf(manifests) {
23
17
  const root = manifests.get('.')
24
18
  const workspaces = new Map()
@@ -36,9 +30,7 @@ export function workspacesOf(manifests) {
36
30
  return workspaces
37
31
  }
38
32
 
39
- // The aggregator yarn makes of the workspaces: the root's dependencies
40
- // and each workspace at its version, its devDependencies and
41
- // optionalDependencies.
33
+ // The virtual manifest yarn makes of the root and the workspaces.
42
34
  export function aggregatorOf(root, workspaces) {
43
35
  const dependencies = { ...root.dependencies }
44
36
  for (const workspace of workspaces.values()) dependencies[workspace.name] = workspace.version
@@ -46,36 +38,26 @@ export function aggregatorOf(root, workspaces) {
46
38
  return { name: AGGREGATOR, dir: '.', version: '1.0.0', manifest: cleanDependencies(manifest), aggregator: true }
47
39
  }
48
40
 
49
- // The resolutions yarn reads: a path of package names, the last the one
50
- // resolved, to a range or another source. One to another source is
51
- // refused here, as no source but the registry is supported; one to what
52
- // is neither, which yarn passes over, the lockfile reader has refused.
41
+ // The resolutions as resolution-map.js reads them. One to neither a range
42
+ // nor another source, which yarn passes over, the lockfile reader refuses.
53
43
  export function rulesOf(root) {
54
- const byName = new Map()
55
- for (const [path, range] of Object.entries(root.resolutions ?? {})) {
44
+ const rules = Object.entries(root.resolutions ?? {}).map(([path, range]) => {
56
45
  const names = path.match(/(?:@[^/]+\/)?[^/]+/gu) ?? [path]
57
46
  const name = names.at(-1)
58
47
  if (validRange(range) === null) throw new DeptreeError(`a resolution to ${quote(range)}, no semver range, is not supported`, `manifests["."].resolutions[${quote(path)}]`)
59
- const glob = names.length === 1 ? `**/${path}` : path
60
- if (!byName.has(name)) byName.set(name, [])
61
- byName.get(name).push({ path, name, range, glob, pattern: `${name}@${range}` })
62
- }
63
- return [...byName.values()].flat()
48
+ return { path, name, glob: names.length === 1 ? `**/${path}` : path, pattern: `${name}@${range}` }
49
+ })
50
+ return [...Map.groupBy(rules, ({ name }) => name).values()].flat()
64
51
  }
65
52
 
66
- // The top-level requests, as yarn makes them and in its order.
53
+ // The top-level requests in yarn's order; `asked`, all but the resolutions'.
67
54
  export function topRequests(root, workspaces, rules) {
68
- const requests = rules.map((rule) => ({ pattern: rule.pattern, optional: false }))
69
- const patterns = []
70
- const push = (deps, optional) => {
71
- for (const [name, range] of Object.entries(deps ?? {})) {
72
- const pattern = `${name}@${range}`
73
- patterns.push(pattern)
74
- requests.push({ pattern, optional })
75
- }
55
+ const asked = []
56
+ const push = (deps, optional, dev = false) => {
57
+ for (const [name, range] of Object.entries(deps ?? {})) asked.push({ pattern: `${name}@${range}`, optional, dev })
76
58
  }
77
59
  push(root.dependencies, false)
78
- push(root.devDependencies, false)
60
+ push(root.devDependencies, false, true)
79
61
  push(root.optionalDependencies, true)
80
62
  if (workspaces.size > 0) {
81
63
  push({ [AGGREGATOR]: '1.0.0' }, false)
@@ -86,5 +68,5 @@ export function topRequests(root, workspaces, rules) {
86
68
  }
87
69
  push(implicit, false)
88
70
  }
89
- return { requests, patterns }
71
+ return { requests: [...rules.map((rule) => ({ pattern: rule.pattern, optional: false })), ...asked], asked }
90
72
  }