@preventive/deptree 1.0.0-alpha.4 → 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,45 +1,24 @@
1
- // Which directories pnpm 10 takes for a workspace's projects, all of them
2
- // under one lockfile: the root, always, and each directory whose
3
- // package.json a glob of pnpm-workspace.yaml's `packages` takes, as
4
- // @pnpm/fs.find-packages globs for them with tinyglobby — `<glob>/
5
- // package.{json,yaml,json5}`, less anything under node_modules or
6
- // bower_components. Without `packages`, or without pnpm-workspace.yaml,
7
- // the root is the only project. pnpm finds them on disk: findProjects
8
- // finds them so in the directories it is given, and checkWorkspace holds
9
- // the lockfile's importers to them.
10
- //
11
- // A glob is read as tinyglobby reads it, normalized as a path first, as
12
- // far as `*` and `**` go: `*` any run of characters in one directory's
13
- // name, `**` a whole name for any number of directories, neither taking a
14
- // name that starts with a dot, which only a glob that spells it takes; `!`
15
- // in front leaves out what it takes, wherever it is in the list. Anything
16
- // else a glob can say — `?`, a class, a brace, an extglob, an escape — is
17
- // refused, and so is a glob reaching outside the workspace's directory.
18
- // pnpm 11 leaves out what a `!` glob takes with micromatch too, a name
19
- // starting with a dot and all.
20
- //
21
- // pnpm 12 finds them its own way (its projects walk, with wax), and the
22
- // same but in four places: a `**` may take no name before one a glob
23
- // spells with a leading dot; nothing under a node_modules or a
24
- // bower_components is walked into or taken, wherever it is; where a glob
25
- // with a `*` takes a directory, the first of its manifests that is there
26
- // is read, which fails where it is not a file; and the projects come in
27
- // the order of their paths by name.
1
+ // The directories pnpm takes for a workspace's projects: the root, and each
2
+ // whose package.json a glob of pnpm-workspace.yaml's `packages` takes, as
3
+ // @pnpm/fs.find-packages globs for them with tinyglobby (pnpm 9: fast-glob), or
4
+ // as pnpm 12 walks (wax). pnpm 11 matches `!` globs with micromatch, dotted
5
+ // names and all.
28
6
 
29
7
  import { compareNames, normalize } from '@preventive/vfs/path.js'
30
8
  import { DeptreeError, quote } from '../error.js'
31
- import { escape } from '../matcher.js'
32
- import { typeOf } from './project.js'
9
+ import { reach, wildcard } from '../matcher.js'
10
+ import { typeOf } from '../project.js'
33
11
 
34
12
  const UNSUPPORTED = /[?[\]{}()\\]/u
35
13
 
36
- // A name's pattern: `*` any run of characters, line terminators among
37
- // them, but not a leading dot unless `dot`.
38
- const NAME = (name, dot) => new RegExp(`^${name.startsWith('*') && !dot ? '(?!\\.)' : ''}${name.split('*').map(escape).join('[^/]*')}$`, 'u')
14
+ // `*` takes line terminators too, and a leading dot only with `dot`.
15
+ function compileName(name, dot) {
16
+ const matches = wildcard(name)
17
+ const undotted = name.startsWith('*') && !dot
18
+ return { test: (entry) => !(undotted && entry.startsWith('.')) && matches(entry) }
19
+ }
39
20
 
40
- // A glob as the names of the paths it takes, normalized as tinyglobby
41
- // does: each `**`, or the pattern of one name; `dot` whether `**` takes a
42
- // name with a leading dot.
21
+ // A glob normalized as tinyglobby does, as `**` or a pattern per name.
43
22
  function compile(glob, where, dot) {
44
23
  const path = normalize(`./${glob}/package.json`)
45
24
  if (path.startsWith('../')) throw new DeptreeError(`${quote(glob)} reaches outside the workspace's directory, which is not supported`, where)
@@ -47,40 +26,12 @@ function compile(glob, where, dot) {
47
26
  if (glob === '' || glob.startsWith('/') || UNSUPPORTED.test(glob) || glob.includes('!') || names.some((name) => name !== '**' && name.includes('**'))) {
48
27
  throw new DeptreeError(`${quote(glob)} is not supported: only \`*\`, \`**\` as a whole name and a leading \`!\` are`, where)
49
28
  }
50
- return { names: names.map((name) => (name === '**' ? name : NAME(name, dot))), dot }
51
- }
52
-
53
- // Which places in a glob the path of `names` leads to: for each index,
54
- // whether the glob's names before it take the path, a name at a time, in
55
- // time the glob's length for each and with no recursion, however many
56
- // `**` it has. A `**` may take no name, so reaching one reaches the next.
57
- function reach({ names: glob, dot }, names) {
58
- let here = Array.from({ length: glob.length + 1 }, (_, g) => g === 0)
59
- let next = Array.from({ length: glob.length + 1 }, () => false)
60
- const onward = (places) => {
61
- for (let g = 0; g < glob.length; g++) if (places[g] && glob[g] === '**') places[g + 1] = true
62
- }
63
- onward(here)
64
- for (const name of names) {
65
- next.fill(false)
66
- for (let g = 0; g < glob.length; g++) {
67
- if (!here[g]) continue
68
- if (glob[g] !== '**') next[g + 1] ||= glob[g].test(name)
69
- else if (dot || !name.startsWith('.')) next[g] = true
70
- }
71
- onward(next)
72
- ;[here, next] = [next, here]
73
- }
74
- return here
29
+ return { names: names.map((name) => (name === '**' ? name : compileName(name, dot))), dot, raw: names }
75
30
  }
76
31
 
77
- // Whether a glob takes the path of `names`.
78
- const takes = (glob, names) => reach(glob, names)[glob.names.length]
32
+ const takes = (glob, names) => reach(glob.names, names, glob.dot).at(-1)
79
33
 
80
- // Whether tinyglobby walks into the directory at `names` for a glob, as
81
- // its partial matcher has it: name by name, each has to be taken by the
82
- // glob's name in the same place, until a `**` there takes one, which
83
- // takes whatever is below it. A `**` that takes none is not tried, so a
34
+ // tinyglobby's partial matcher: a `**` that takes no name is not tried, so a
84
35
  // name with a leading dot a `**` stands before is never walked into.
85
36
  function enters({ names: glob, dot }, names) {
86
37
  for (const [i, name] of names.entries()) {
@@ -91,33 +42,100 @@ function enters({ names: glob, dot }, names) {
91
42
  return true
92
43
  }
93
44
 
94
- // `packages`, the globs or undefined, as the globs that take and those
95
- // that leave out, with how pnpm `major` walks for them: which directories
96
- // it enters for a glob, which it ignores, and which manifests it drops
97
- // whatever the globs say. pnpm 12 reads the manifest of a directory a
98
- // glob with a `*` takes, which is `wild`, whatever it is.
45
+ // A manifest in a directory tinyglobby walks into is not ignored for what
46
+ // find-packages ignores; pnpm 12 drops it whatever the globs say. `wild` is
47
+ // whether a glob with a `*` takes it (manifestOf).
99
48
  function compileAll(packages, major) {
49
+ if (major < 10) return compile9(packages)
100
50
  const twelve = major >= 12
101
- const globs = { include: [], exclude: [], enters: twelve ? enters12 : enters, ignored: twelve ? ignored12 : ignored, dropped: twelve ? ignored12 : () => false }
51
+ const [enters_, ignored_, dropped] = twelve ? [enters12, underLeftOut, underLeftOut] : [enters, ignored, () => false]
52
+ const include = []
53
+ const exclude = []
102
54
  for (const glob of packages ?? []) {
103
- const exclude = glob.startsWith('!')
104
- const compiled = compile(exclude ? glob.slice(1) : glob, 'pnpm-workspace.yaml: packages', exclude && major >= 11)
105
- globs[exclude ? 'exclude' : 'include'].push({ ...compiled, wild: twelve && glob.includes('*') })
55
+ const negative = glob.startsWith('!')
56
+ const compiled = compile(negative ? glob.slice(1) : glob, 'pnpm-workspace.yaml: packages', negative && major >= 11)
57
+ ;(negative ? exclude : include).push({ ...compiled, wild: twelve && glob.includes('*') })
106
58
  }
107
- return globs
59
+ return {
60
+ walked: (names) => include.some((glob) => enters_(glob, names)) && !ignored_(names),
61
+ taken: takenBy(include, exclude, dropped),
62
+ wild: (names) => include.some((glob) => glob.wild && takes(glob, names)),
63
+ manifests: MANIFESTS,
64
+ through: [],
65
+ }
66
+ }
67
+
68
+ const takenBy = (include, exclude, dropped) => (names) => include.some((glob) => takes(glob, names)) && !exclude.some((glob) => takes(glob, names)) && !dropped(names)
69
+
70
+ const startsWith = (names, start) => start.length <= names.length && start.every((name, i) => names[i] === name)
71
+
72
+ // fast-glob's partial matcher: past the names before its first `**`, any.
73
+ function partial({ names: glob, raw }, names) {
74
+ const star = raw.indexOf('**')
75
+ if (star === -1 && glob.length <= names.length) return false
76
+ if (star !== -1 && names.length > star) return true
77
+ return names.every((name, i) => glob[i].test(name))
108
78
  }
109
79
 
110
- // The manifest pnpm reads of the directory at `names`, given the names in
111
- // it: the first of them there that is a file, a link that leads nowhere
112
- // passed over. pnpm 12 reads the first there, where a glob with a `*`
113
- // takes the directory, and fails where that is not a file.
80
+ const isDynamic = (glob) => glob.raw.some((name) => name.includes('*'))
81
+
82
+ // pnpm 9 globs with fast-glob 3: a glob with no `*` it looks up where it
83
+ // leads, and one with a `*` it walks for from the names before that
84
+ // (`base`), from the top for all of them where one has none there. Under
85
+ // a base it walks into every directory its globs may reach (`partial`),
86
+ // those with a leading dot among them, but where the ignore globs take it
87
+ // with `**` taking no leading dot, or where a `!` glob takes it whole;
88
+ // and takes a manifest a glob takes and no `!` glob, nor an ignore glob,
89
+ // with `**` and `*` taking leading dots. It reads every manifest a
90
+ // directory has, so one but package.json is the one refused (MANIFESTS_9),
91
+ // and fails on a glob that leads through a file (`through`).
92
+ function compile9(packages) {
93
+ const where = 'pnpm-workspace.yaml: packages'
94
+ const include = []
95
+ const exclude = []
96
+ const excludeWhole = []
97
+ for (const glob of packages ?? []) {
98
+ if (glob.startsWith('!')) {
99
+ exclude.push(compile(glob.slice(1), where, true))
100
+ excludeWhole.push(compile(glob.slice(1), where, false))
101
+ } else include.push(compile(glob, where, false))
102
+ }
103
+ const dynamic = include.filter(isDynamic)
104
+ const baseOf = (glob) => glob.raw.slice(0, glob.raw.findIndex((name) => name.includes('*')))
105
+ const groups = Map.groupBy(dynamic, (glob) => baseOf(glob).join('/'))
106
+ const tasks = groups.has('') ? [{ base: [], globs: dynamic }] : [...groups.values()].map((globs) => ({ base: baseOf(globs[0]), globs }))
107
+ const skipped = (names) => ignored(names) || (MANIFESTS.includes(names.at(-1)) && excludeWhole.some((glob) => takes(glob, [...names.slice(0, -1), 'package.json'])))
108
+ // A task walks into a directory only where it walked into its parent; each
109
+ // answer is kept for the walk.
110
+ const reacher = ({ base, globs }) => {
111
+ const known = new Map()
112
+ const reaches = (names) => {
113
+ if (names.length <= base.length) return names.length === base.length && startsWith(names, base)
114
+ const key = names.join('/')
115
+ if (!known.has(key)) known.set(key, reaches(names.slice(0, -1)) && globs.some((glob) => partial(glob, names)) && !skipped(names))
116
+ return known.get(key)
117
+ }
118
+ return reaches
119
+ }
120
+ const reachers = tasks.map(reacher)
121
+ const through = [...tasks.map((task) => task.base), ...include.filter((glob) => !isDynamic(glob)).map((glob) => glob.raw.slice(0, -1))]
122
+ return {
123
+ walked: (names) => through.some((path) => startsWith(path, names)) || reachers.some((reaches) => reaches(names)),
124
+ taken: takenBy(include, exclude, underLeftOut),
125
+ wild: () => false,
126
+ manifests: MANIFESTS_9,
127
+ through,
128
+ }
129
+ }
130
+
131
+ // The first of `globs.manifests` that is a file; pnpm 12 reads the first there
132
+ // where a `wild` glob takes the directory, failing where it is not a file.
114
133
  function manifestOf(project, globs, names, entries) {
115
- const wild = () => globs.include.some((glob) => glob.wild && takes(glob, [...names, 'package.json']))
116
- for (const name of MANIFESTS) {
134
+ for (const name of globs.manifests) {
117
135
  if (!entries.includes(name)) continue
118
136
  const type = typeOf(project, `/${[...names, name].join('/')}`)
119
137
  if (type === 'file') return name
120
- if (type !== undefined && wild()) throw new DeptreeError(`pnpm 12 reads this as a manifest, and fails on it as a ${type}`, quote([...names, name].join('/')))
138
+ if (type !== undefined && globs.wild([...names, 'package.json'])) throw new DeptreeError(`pnpm 12 reads this as a manifest, and fails on it as a ${type}`, quote([...names, name].join('/')))
121
139
  }
122
140
  return undefined
123
141
  }
@@ -130,48 +148,30 @@ function byNames(a, b) {
130
148
  return x.length - y.length
131
149
  }
132
150
 
133
- // What find-packages leaves out, as tinyglobby reads it, which does not
134
- // take a name with a leading dot for `**`: what is under node_modules or
135
- // bower_components.
151
+ // find-packages leaves out what is under these, by globs whose `**`, as
152
+ // tinyglobby reads them, takes no name with a leading dot; as fast-glob
153
+ // reads them for a manifest, any (underLeftOut).
136
154
  const LEFT_OUT = ['node_modules', 'bower_components']
137
- const IGNORED = LEFT_OUT.map((name) => ({ names: ['**', NAME(name, false), '**'], dot: false }))
155
+ const IGNORED = LEFT_OUT.map((name) => ({ names: ['**', compileName(name, false), '**'], dot: false }))
138
156
  const ignored = (names) => IGNORED.some((glob) => takes(glob, names))
139
157
 
140
- // What pnpm 12 never walks into or takes: anything with one of those in
141
- // its path.
142
- const ignored12 = (names) => names.some((name) => LEFT_OUT.includes(name))
143
-
144
- // Whether pnpm 12 walks into the directory at `names` for a glob: where
145
- // the glob's names before its last may take it.
146
- const enters12 = (glob, names) => reach(glob, names).slice(0, glob.names.length).some(Boolean)
158
+ const underLeftOut = (names) => names.some((name) => LEFT_OUT.includes(name))
147
159
 
148
- // Whether the globs take the manifest at `names`. What find-packages
149
- // ignores it is not ignored for, in a directory tinyglobby walks into.
150
- const taken = ({ include, exclude, dropped }, names) => include.some((glob) => takes(glob, names)) && !exclude.some((glob) => takes(glob, names)) && !dropped(names)
160
+ // pnpm 12 walks into a directory a glob's names before its last may take.
161
+ const enters12 = (glob, names) => reach(glob.names, names, glob.dot).slice(0, -1).some(Boolean)
151
162
 
152
- // Whether tinyglobby walks into the directory at `names`, which it does
153
- // through a link to one too; or pnpm 12.
154
- const walked = (globs, names) => globs.include.some((glob) => globs.enters(glob, names)) && !globs.ignored(names)
155
-
156
- // `ids` are the projects' directories, `.` the root; `packages` the globs,
157
- // or undefined; `major` pnpm's major version. pnpm finds a project where
158
- // tinyglobby walks into each directory down to it, and the globs take its
159
- // manifest.
160
163
  export function checkWorkspace(ids, packages, major = 10) {
161
164
  const globs = compileAll(packages, major)
162
165
  for (const id of ids) {
163
166
  if (id === '.') continue
164
167
  const names = id.split('/')
165
- if (!names.every((_, i) => walked(globs, names.slice(0, i + 1))) || !taken(globs, [...names, 'package.json'])) {
168
+ if (!names.every((_, i) => globs.walked(names.slice(0, i + 1))) || !globs.taken([...names, 'package.json'])) {
166
169
  throw new DeptreeError(`pnpm-workspace.yaml's packages ${packages === undefined ? 'are not set' : 'do not take this directory'}, so pnpm would not install it as a project`, `importers[${quote(id)}]`)
167
170
  }
168
171
  }
169
172
  }
170
173
 
171
- // Of what @preventive/lockfile holds an importer's key to, what a project
172
- // under the lockfile's directory has to be: its path from there, in normal
173
- // form, with no drive letter, and no control, bidirectional or backslash
174
- // character, which the lockfile could not name it by.
174
+ // The importer keys @preventive/lockfile takes for a project.
175
175
  const UNSAFE = /[\p{Cc}\p{Zl}\p{Zp}\p{Bidi_Control}\\]/u
176
176
  export function checkProjectId(id, where) {
177
177
  if (id.split('/').some((name) => name === '' || name === '.' || name === '..' || UNSAFE.test(name)) || /^[A-Za-z]:/u.test(id)) {
@@ -179,38 +179,29 @@ export function checkProjectId(id, where) {
179
179
  }
180
180
  }
181
181
 
182
- // A manifest pnpm would read through a link, the root's among them, is
183
- // refused before it is read.
184
182
  export const linkedManifest = (file) => new DeptreeError('a link pnpm would read a project\'s manifest through is not supported', quote(file))
185
183
 
186
- // The names pnpm reads a project's manifest by, the first of them there
187
- // winning.
184
+ // pnpm reads the first of these a project has; pnpm 9 each, so one but
185
+ // package.json is named first.
188
186
  const MANIFESTS = ['package.json', 'package.json5', 'package.yaml']
187
+ const MANIFESTS_9 = ['package.json5', 'package.yaml', 'package.json']
189
188
 
190
- // The directories of the projects pnpm `major` finds by the globs of
191
- // `packages` in `project` (project.js): `.`, the root, then the rest in
192
- // order. Every directory tinyglobby walks into is read, and no other, a
193
- // link to one followed as it follows it. A manifest is a file, or a link
194
- // to one; a link that leads nowhere, or to a directory, is none.
195
- // Refused, as not read here: a root with no manifest, which pnpm takes
196
- // for no project; a project, the root among them, whose manifest is
197
- // package.json5, package.yaml or a link; a project found
198
- // through a link, and a link to a directory in one followed, which is not
199
- // followed; a project in a directory a lockfile could not key its
200
- // importer by; and a node_modules tinyglobby walks into, which it does
201
- // only under a directory with a leading dot, as what is installed there
202
- // is no project buildPnpmTree builds.
189
+ // Every directory tinyglobby walks into is read, and no other. It walks into
190
+ // a node_modules only under a directory with a leading dot, which is
191
+ // refused, as what is installed there is no project buildPnpmTree builds.
203
192
  export function findProjects(project, packages, major = 10) {
204
193
  const globs = compileAll(packages, major)
194
+ for (const path of globs.through) {
195
+ const end = path.findIndex((_, i) => typeOf(project, `/${path.slice(0, i + 1).join('/')}`) === 'file')
196
+ if (end !== -1) throw new DeptreeError('a glob leads through this file, which pnpm 9 fails on', quote(path.slice(0, end + 1).join('/')))
197
+ }
205
198
  const ids = []
206
- // The directories left to read, by their names, and the link each is
207
- // reached through, if any: a stack, not recursion, however deep the tree.
208
199
  const pending = [{ names: [] }]
209
200
  while (pending.length > 0) {
210
201
  const { names, link } = pending.pop()
211
202
  const dir = names.join('/')
212
203
  const entries = project.readdir(`/${dir}`)
213
- const found = names.length === 0 || taken(globs, [...names, 'package.json']) ? manifestOf(project, globs, names, entries) : undefined
204
+ const found = names.length === 0 || globs.taken([...names, 'package.json']) ? manifestOf(project, globs, names, entries) : undefined
214
205
  if (names.length === 0 && found === undefined) throw new DeptreeError('pnpm takes a workspace with no manifest at its root for one with no root project, which is not supported', quote('package.json'))
215
206
  if (found !== undefined) {
216
207
  const file = [...names, found].join('/')
@@ -224,7 +215,7 @@ export function findProjects(project, packages, major = 10) {
224
215
  }
225
216
  for (const entry of entries) {
226
217
  const at = [...names, entry]
227
- if (!walked(globs, at)) continue
218
+ if (!globs.walked(at)) continue
228
219
  const path = at.join('/')
229
220
  const { type } = project.lstat(`/${path}`)
230
221
  const linked = type === 'symlink' && typeOf(project, `/${path}`) === 'directory'
package/src/project.js ADDED
@@ -0,0 +1,41 @@
1
+ // The project, the lockfile's directory, as a Vfs or anything with its
2
+ // methods, by paths from `/`; it is only read, and only where a builder says.
3
+
4
+ import { DeptreeError, quote } from './error.js'
5
+
6
+ export function checkProject(project) {
7
+ if (['readdir', 'lstat', 'stat', 'readFile'].some((name) => typeof project?.[name] !== 'function')) {
8
+ throw new TypeError('project must be a Vfs, or have its readdir, lstat, stat and readFile')
9
+ }
10
+ }
11
+
12
+ const NOWHERE = new Set(['ENOENT', 'ENOTDIR', 'ELOOP'])
13
+ export function typeOf(project, path, follow = true) {
14
+ try {
15
+ return (follow ? project.stat(path) : project.lstat(path)).type
16
+ } catch (error) {
17
+ if (NOWHERE.has(error?.code)) return undefined
18
+ throw error
19
+ }
20
+ }
21
+
22
+ // A BOM is kept, as in text handed in, for the reader to drop as pnpm does.
23
+ const decoder = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true })
24
+
25
+ export function readBytes(project, path) {
26
+ const bytes = project.readFile(path)
27
+ if (!(bytes instanceof Uint8Array)) throw new TypeError('project.readFile must give back bytes')
28
+ return bytes
29
+ }
30
+
31
+ export function readText(project, path, where) {
32
+ const type = typeOf(project, path)
33
+ if (type === undefined) return undefined
34
+ if (type !== 'file') throw new DeptreeError(`${quote(path.slice(1))} is a ${type}, not a file`, where)
35
+ const bytes = readBytes(project, path)
36
+ try {
37
+ return decoder.decode(bytes)
38
+ } catch {
39
+ throw new DeptreeError(`${quote(path.slice(1))} is not UTF-8`, where)
40
+ }
41
+ }
@@ -1,18 +1,12 @@
1
- // The config `soldeer install` reads, as Soldeer 0.12 finds and reads it:
2
- // foundry.toml where it has a [dependencies] table; else soldeer.toml,
3
- // where there is no foundry.toml. Where neither holds, Soldeer asks which
4
- // one to make its config, which is refused. Of foundry.toml, what Soldeer
5
- // would fail on as it adds `dependencies` to the default profile's libs is
6
- // refused; of the config, its [soldeer] settings where Soldeer would fail
7
- // on them, and recursive_deps.
1
+ // The config Soldeer 0.12 reads: foundry.toml where it has a [dependencies]
2
+ // table, else soldeer.toml where there is no foundry.toml.
8
3
 
9
4
  import { TomlError, isInlineTable, parseToml } from '@preventive/lockfile/toml.js'
10
5
  import { DeptreeError } from '../error.js'
11
6
 
12
7
  const isTable = (value) => typeof value === 'object' && value !== null && Object.getPrototypeOf(value) === null
13
- // A table toml_edit gives as one, as a header or a dotted key makes it:
14
- // one written inline it gives as a value, which Soldeer reads no
15
- // dependencies from, and fails on for a profile.
8
+ // A table toml_edit gives as one, from a header or a dotted key, not inline:
9
+ // Soldeer reads no dependencies from that, and fails on it for a profile.
16
10
  const isTableItem = (value) => isTable(value) && !isInlineTable(value)
17
11
 
18
12
  function parse(text, file) {
@@ -37,16 +31,9 @@ function checkLibs(doc) {
37
31
  if (libs !== undefined && (!Array.isArray(libs) || isTableItem(libs[0]))) throw new DeptreeError('not an array, which Soldeer fails on', 'foundry.toml: profile.default.libs')
38
32
  }
39
33
 
40
- function checkDependencies(doc, file) {
41
- if (doc.dependencies !== undefined && !isTableItem(doc.dependencies)) {
42
- throw new DeptreeError('not a table, which Soldeer reads no dependencies from, is not supported', `${file}: dependencies`)
43
- }
44
- }
45
-
46
34
  const BOOLEANS = ['remappings_generate', 'remappings_regenerate', 'remappings_version', 'recursive_deps']
47
35
 
48
- // read_soldeer_config's SoldeerConfig, by serde: each field of its type,
49
- // any other passed over.
36
+ // read_soldeer_config's SoldeerConfig, by serde; unknown fields passed over.
50
37
  function checkSettings(settings, file) {
51
38
  if (settings === undefined) return
52
39
  const where = `${file}: soldeer`
@@ -55,25 +42,22 @@ function checkSettings(settings, file) {
55
42
  if (settings[key] !== undefined && typeof settings[key] !== 'boolean') throw new DeptreeError('not true or false, which Soldeer fails on', `${where}.${key}`)
56
43
  }
57
44
  if (settings.remappings_prefix !== undefined && typeof settings.remappings_prefix !== 'string') throw new DeptreeError('not a string, which Soldeer fails on', `${where}.remappings_prefix`)
58
- if (settings.remappings_location !== undefined && settings.remappings_location !== 'txt' && settings.remappings_location !== 'config') {
59
- throw new DeptreeError('not "txt" or "config", which Soldeer fails on', `${where}.remappings_location`)
60
- }
45
+ if (![undefined, 'txt', 'config'].includes(settings.remappings_location)) throw new DeptreeError('not "txt" or "config", which Soldeer fails on', `${where}.remappings_location`)
61
46
  if (settings.recursive_deps === true) throw new DeptreeError('has Soldeer install what each dependency depends on too, which is not supported', `${where}.recursive_deps`)
62
47
  }
63
48
 
64
49
  const ASKS = 'Soldeer asks which file to make its config, which is not supported'
65
50
 
66
- // The file Soldeer reads its config from, and that config, parsed.
67
51
  export function configOf({ foundry, soldeer }) {
68
52
  if (foundry === undefined && soldeer === undefined) throw new DeptreeError(`neither foundry.toml nor soldeer.toml is there, so ${ASKS}`)
69
53
  const file = foundry === undefined ? 'soldeer.toml' : 'foundry.toml'
70
54
  const config = parse(foundry ?? soldeer, file)
71
55
  if (foundry === undefined) {
72
- checkDependencies(config, file)
56
+ if (config.dependencies !== undefined && !isTableItem(config.dependencies)) throw new DeptreeError('not a table, which Soldeer reads no dependencies from, is not supported', 'soldeer.toml: dependencies')
73
57
  } else {
74
58
  if (!isTableItem(config.dependencies)) throw new DeptreeError(`no [dependencies] table, so ${ASKS}`, file)
75
59
  checkLibs(config)
76
60
  }
77
61
  checkSettings(config.soldeer, file)
78
- return { file, config }
62
+ return config
79
63
  }
@@ -1,9 +1,7 @@
1
- // What buildSoldeerTree takes, checked: the host Soldeer runs on, and the
2
- // files `soldeer install` reads — given as text, or read from the project
3
- // (../pnpm/project.js) as Soldeer reads them at the project's root.
1
+ // What buildSoldeerTree takes, as text or read from the project's root.
4
2
 
5
3
  import { DeptreeError, quote } from '../error.js'
6
- import { checkProject, readText } from '../pnpm/project.js'
4
+ import { checkProject, readText } from '../project.js'
7
5
 
8
6
  export function checkHost(host) {
9
7
  if (host === null || typeof host !== 'object') throw new TypeError('host must be an object with soldeer and os')
@@ -27,7 +25,6 @@ export function inputsOf(options) {
27
25
  }
28
26
  const lock = readText(project, '/soldeer.lock', 'soldeer.lock')
29
27
  if (lock === undefined) throw new DeptreeError('the project has no soldeer.lock, without which Soldeer resolves each dependency anew')
30
- // Soldeer reads soldeer.toml only where it cannot read foundry.toml.
31
28
  const foundryText = readText(project, '/foundry.toml', 'foundry.toml')
32
29
  return { lockfile: lock, foundry: foundryText, soldeer: foundryText === undefined ? readText(project, '/soldeer.toml', 'soldeer.toml') : undefined }
33
30
  }
@@ -1,26 +1,20 @@
1
- // The dependencies folder `soldeer install` makes from a soldeer.lock: each
2
- // registry dependency's zip fetched, held to the lockfile's checksum, and
3
- // extracted in a folder of its own (zip.js).
1
+ // The dependencies folder `soldeer install` makes from a soldeer.lock.
4
2
 
5
3
  import { parseSoldeerLockfile } from '@preventive/lockfile/soldeer.js'
6
4
  import { getZip } from '@preventive/upstream/soldeer.js'
7
- import { Vfs, VfsError } from '@preventive/vfs'
5
+ import { Vfs } from '@preventive/vfs'
8
6
  import { eachConcurrently } from '../concurrent.js'
9
7
  import { DeptreeError, quote } from '../error.js'
10
- import { fold, mount } from '../mount.js'
11
- import { checkCollisions } from '../pnpm/checks.js'
8
+ import { fold, makeDirs, mount, writeFiles } from '../mount.js'
12
9
  import { configOf } from './config.js'
13
10
  import { checkHost, inputsOf } from './inputs.js'
14
11
  import { extractZip } from './zip.js'
15
12
 
16
- // What the registry takes, as @preventive/upstream/soldeer.js holds a name
17
- // and a version to: of these, Soldeer's sanitize_filename leaves a folder
18
- // `<name>-<version>` as it is on Unix.
13
+ // What the registry takes, as @preventive/upstream/soldeer.js checks a name
14
+ // and version, which Soldeer's sanitize_filename leaves as they are on Unix.
19
15
  const NAME = /^(?=.{3,100}$)[@\da-z][\da-z-]*[\da-z]$/u
20
16
  const VERSION = /^(?=.{1,128}$)[\dA-Za-z][\w.+-]*$/u
21
17
 
22
- // Refuses a Vfs that holds a dependencies folder at its root, or, where
23
- // names are `folded`, a name that is one there.
24
18
  function checkNoDependencies(vfs, folded) {
25
19
  for (const name of vfs.readdir('/')) {
26
20
  if (name === 'dependencies' || (folded && fold(name) === 'dependencies')) {
@@ -29,66 +23,34 @@ function checkNoDependencies(vfs, folded) {
29
23
  }
30
24
  }
31
25
 
32
- // The registry dependencies, each with the folder Soldeer installs it in;
33
- // any other kind is refused. Where names are `folded`, a folder is one
34
- // with another that differs from it only in case or normalization.
26
+ const about = (name) => `dependencies[${quote(name)}]`
27
+
35
28
  function registryDependencies(lock, config, folded) {
36
29
  const dependencies = Object.values(lock.dependencies).map((dependency) => ({ ...dependency, folder: `${dependency.name}-${dependency.version}` }))
37
30
  const key = (folder) => (folded ? fold(folder) : folder)
38
31
  const folders = new Set(dependencies.map(({ folder }) => key(folder)))
39
32
  for (const { type, name, version } of dependencies) {
40
- const where = `dependencies[${quote(name)}]`
33
+ const where = about(name)
41
34
  if (type === 'git') throw new DeptreeError('a git dependency, which Soldeer clones with its history, is not supported', where)
42
35
  if (type === 'private') throw new DeptreeError('a private dependency, which the registry hands out to those signed in alone, is not supported', where)
43
36
  if (config.dependencies?.[name]?.url !== undefined) throw new DeptreeError('a dependency from a URL of its own, rather than the registry, is not supported', where)
44
37
  if (!NAME.test(name)) throw new DeptreeError('a name the registry does not take', where)
45
38
  if (!VERSION.test(version)) throw new DeptreeError(`${quote(version)} is a version the registry does not take`, where)
46
39
  }
47
- // Soldeer downloads each zip beside the folders, as `<folder>.zip`.
48
40
  for (const { name, folder } of dependencies) {
49
- if (folders.has(key(`${folder}.zip`))) throw new DeptreeError(`its zip is downloaded as ${quote(`${folder}.zip`)}, a folder Soldeer installs another dependency in`, `dependencies[${quote(name)}]`)
41
+ if (folders.has(key(`${folder}.zip`))) throw new DeptreeError(`its zip is downloaded as ${quote(`${folder}.zip`)}, a folder Soldeer installs another dependency in`, about(name))
50
42
  }
51
43
  return dependencies
52
44
  }
53
45
 
54
- // Each dependency's zip, fetched and extracted a few at a time, by its
55
- // folder.
56
46
  async function fetchAll(dependencies) {
57
47
  const extracted = new Map()
58
48
  await eachConcurrently(dependencies, async ({ name, version, checksum, folder }) => {
59
- const where = `dependencies[${quote(name)}]`
60
- try {
61
- extracted.set(folder, await extractZip(await getZip(name, version, checksum), where))
62
- } catch (error) {
63
- throw error instanceof DeptreeError ? error : new DeptreeError(error.message, where, { cause: error })
64
- }
65
- })
49
+ extracted.set(folder, await extractZip(await getZip(name, version, checksum), checksum, about(name)))
50
+ }, ({ name }) => about(name))
66
51
  return extracted
67
52
  }
68
53
 
69
- function writeTree(extracted) {
70
- const vfs = new Vfs()
71
- vfs.mkdir('/dependencies')
72
- let files = 0
73
- let bytes = 0
74
- for (const [folder, { dirs, files: written }] of extracted) {
75
- const root = `/dependencies/${folder}`
76
- vfs.mkdir(root)
77
- for (const dir of dirs) vfs.mkdir(`${root}/${dir}`, { recursive: true })
78
- for (const [path, file] of written) {
79
- try {
80
- vfs.writeFile(`${root}/${path}`, file.data, { mode: file.mode })
81
- } catch (error) {
82
- if (error instanceof VfsError) throw new DeptreeError(`cannot be written: ${error.message}`, quote(`dependencies/${folder}/${path}`), { cause: error })
83
- throw error
84
- }
85
- files++
86
- bytes += file.data.length
87
- }
88
- }
89
- return { vfs, files, bytes }
90
- }
91
-
92
54
  export async function buildSoldeerTree(options) {
93
55
  const { host: given, vfs: into } = options ?? {}
94
56
  if (into !== undefined && !(into instanceof Vfs)) throw new TypeError('vfs must be a Vfs, or left out')
@@ -97,15 +59,17 @@ export async function buildSoldeerTree(options) {
97
59
  const folded = host.os === 'darwin'
98
60
  // Refused before anything is fetched; mount checks again.
99
61
  if (into !== undefined) checkNoDependencies(into, folded)
100
- const { config } = configOf(inputs)
62
+ const config = configOf(inputs)
101
63
  const lock = parseSoldeerLockfile(inputs.lockfile, { config })
102
64
  const dependencies = registryDependencies(lock, config, folded)
103
65
  const extracted = await fetchAll(dependencies)
66
+ const vfs = new Vfs()
67
+ makeDirs(vfs, 'dependencies')
68
+ const stats = { dependencies: dependencies.length, files: 0, bytes: 0 }
104
69
  // In the lockfile's order, whatever order the fetches finished in.
105
- const { vfs, files, bytes } = writeTree(dependencies.map(({ folder }) => [folder, extracted.get(folder)]))
106
- if (folded) checkCollisions(vfs)
107
- const stats = { dependencies: dependencies.length, files, bytes }
108
- if (into === undefined) return { vfs, stats }
109
- mount(vfs, into, folded, checkNoDependencies)
110
- return { vfs: into, stats }
70
+ for (const { folder } of dependencies) {
71
+ writeFiles(vfs, `dependencies/${folder}`, extracted.get(folder), stats)
72
+ }
73
+ const installed = dependencies.map(({ name, version, checksum, folder }) => ({ path: `dependencies/${folder}`, name, version, checksum }))
74
+ return { vfs: mount(vfs, into, folded, checkNoDependencies), stats, installed }
111
75
  }