@exodus/stasis 1.0.0-beta.9 → 1.0.0-rc.1

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 (67) hide show
  1. package/README.md +6 -2
  2. package/bin/stasis.js +222 -334
  3. package/package.json +38 -14
  4. package/src/audit-corrections.js +11 -42
  5. package/src/audit.js +256 -212
  6. package/src/babel.js +119 -82
  7. package/src/bundle-from-lockfile.js +9 -21
  8. package/src/bundle-reader.js +24 -8
  9. package/src/bundle.js +0 -3
  10. package/src/cmd/build.js +211 -117
  11. package/src/cmd/bundle-options.js +347 -0
  12. package/src/cmd/bundle.js +1664 -1001
  13. package/src/cmd/diff.js +1 -4
  14. package/src/cmd/github-bundle.js +48 -21
  15. package/src/cmd/output.js +25 -12
  16. package/src/cmd/sbom.js +6 -9
  17. package/src/cmd/tokens.js +60 -0
  18. package/src/define.js +218 -0
  19. package/src/diff.js +73 -129
  20. package/src/esbuild.js +0 -4
  21. package/src/flow.js +27 -0
  22. package/src/installed.js +265 -0
  23. package/src/json-stream.js +4 -9
  24. package/src/loader-mock.js +3 -8
  25. package/src/loader.js +1 -5
  26. package/src/loaders/bash.js +43 -120
  27. package/src/loaders/cargo.js +301 -808
  28. package/src/loaders/foundry.js +112 -290
  29. package/src/loaders/paths.js +15 -26
  30. package/src/loaders/php.js +211 -492
  31. package/src/loaders/rust.js +518 -1461
  32. package/src/loaders/solidity-ownership.js +51 -142
  33. package/src/loaders/solidity.js +42 -188
  34. package/src/loaders/toml.js +7 -17
  35. package/src/lockfile.js +0 -3
  36. package/src/metro-assets.js +41 -0
  37. package/src/metro-resolver.js +58 -133
  38. package/src/metro-transformer.js +0 -6
  39. package/src/metro.js +0 -4
  40. package/src/mock.js +32 -80
  41. package/src/parse.js +8 -7
  42. package/src/prune.js +0 -3
  43. package/src/resolve-fields.js +152 -249
  44. package/src/resolve-node.js +29 -45
  45. package/src/resolve-typescript.js +166 -298
  46. package/src/rollup.js +0 -4
  47. package/src/run-installed.js +10 -0
  48. package/src/sbom.js +70 -97
  49. package/src/scan.js +240 -282
  50. package/src/sixel.js +310 -0
  51. package/src/table.js +95 -0
  52. package/src/validate-notes.d.ts +19 -0
  53. package/src/validate-notes.js +72 -0
  54. package/src/validate.d.ts +159 -0
  55. package/src/validate.js +1315 -0
  56. package/src/vfs-bundle/entries.js +50 -46
  57. package/src/vfs-bundle/github.js +114 -94
  58. package/src/vfs-bundle/glob.js +379 -0
  59. package/src/vfs-bundle/lazy.js +99 -0
  60. package/src/vfs-bundle/prisma.js +235 -105
  61. package/src/vfs-bundle/soldeer.js +28 -49
  62. package/src/vfs-bundle/tree.js +418 -277
  63. package/src/vfs-bundle/yarnclean.js +79 -0
  64. package/src/vfs-bundle.js +24 -55
  65. package/src/webpack.js +0 -4
  66. package/src/why.js +79 -231
  67. package/src/installed-commits.js +0 -182
@@ -1,30 +1,20 @@
1
- import { join, normalize, relative } from 'node:path'
1
+ import { join, normalize, posix, relative } from 'node:path'
2
2
  import { fileURLToPath } from 'node:url'
3
3
 
4
4
  import { readJson } from '@exodus/stasis-core/bundle-util'
5
5
  import { isAutoExcludedDir, isPlainObject, posixPathEscapes, relativeEscapes } from '@exodus/stasis-core/util'
6
6
  import { Vfs } from '@preventive/vfs'
7
7
  import { checkVfsOptions, fieldResolverFor } from '../cmd/bundle.js'
8
+ import { configuredBuilds } from '../cmd/bundle-options.js'
8
9
  import { foundrySourceDir } from '../loaders/foundry.js'
9
10
  import { resolveConditions } from '../resolve-fields.js'
10
11
  import { resolveTypescriptFallback, typescriptExportsTarget } from '../resolve-typescript.js'
11
- import { checkKind, checkVersion, packageManagerOf, vfsHost } from './tree.js'
12
+ import { checkKind, checkLazy, checkVersion, packageManagerOf, vfsHost } from './tree.js'
12
13
 
13
14
  const JS = /\.(?:[cm]?[jt]s|[jt]sx)$/u
14
- // Node's own conditions for require() and for import, which the build's are added to; with
15
- // `mainFields` or `metro`, the field resolver's bundler ones instead (no `node`).
16
15
  const NODE_CONDITIONS = [['require', 'node', 'node-addons', 'module-sync'], ['import', 'node', 'node-addons', 'module-sync']]
17
16
  const FIELD_CONDITIONS = ['commonjs', 'module'].map((format) => resolveConditions(format))
18
17
 
19
- // The entry points the package.json in `dir` names, as paths from `dir`, resolved as the JS build
20
- // with the given options resolves: as Node does, `conditions` added, or with `mainFields` or
21
- // `metro` through the build's field resolver, once per platform of `platforms` under `metro`; what
22
- // that misses mapped as tsc maps it under `typescript`, as the build's fallback maps it. In
23
- // that order: its own entry, as the build resolves `./` there (`main`, or the first of the main
24
- // fields, else index); each subpath `exports` holds (but a pattern), as the package's name resolves
25
- // for require() and for import, with the conditions the build adds (the RN ones under `metro`) to
26
- // Node's, or with `mainFields` or `metro` to the field resolver's; and each `bin`. Only JS files in
27
- // `dir` that are there, named from within it; none without a package.json.
28
18
  function packageEntries(host, dir, { conditions = [], mainFields, metro = false, platforms = [], typescript = false } = {}) {
29
19
  const real = host.realpath(dir)
30
20
  const manifest = join(real, 'package.json')
@@ -38,8 +28,6 @@ function packageEntries(host, dir, { conditions = [], mainFields, metro = false,
38
28
  const passes = metro || mainFields !== undefined
39
29
  ? (metro ? platforms : [null]).map((platform) => fieldResolverFor(platform, { mainFields, metro, conditions, typescript, host }))
40
30
  : [{ extras: conditions, mainFields: ['main'] }]
41
- // Node's resolution of `specifier`, else under `typescript` the miss `mapped` as tsc maps it; a
42
- // real path either way, or undefined.
43
31
  const viaNode = (specifier, names, extras, mapped) => {
44
32
  const set = new Set([...names, ...extras])
45
33
  try {
@@ -77,35 +65,18 @@ function packageEntries(host, dir, { conditions = [], mainFields, metro = false,
77
65
  return [...found]
78
66
  }
79
67
 
80
- // Directories whose .sol files are none of a project's entry points: its tests, scripts and mocks,
81
- // what it depends on or builds, and what no walk descends into.
82
68
  const SKIPPED_DIRS = new Set(['test', 'tests', 'script', 'scripts', 'mock', 'mocks', 'lib', 'node_modules', 'dependencies', 'out', 'cache', 'artifacts', 'build'])
83
- const isSkippedDir = (name) => SKIPPED_DIRS.has(name.toLowerCase()) || isAutoExcludedDir(name)
84
- // A .sol file but a test or script by its name.
85
69
  const isEntrySol = (entry) => entry.isFile() && entry.name.endsWith('.sol') && !/\.(?:t|s|test|spec)\.sol$/u.test(entry.name)
86
70
 
87
- // The .sol files under the directory `rel` of `real` that are entry points by name, but those in a
88
- // skipped directory; sorted. Links are not followed.
89
71
  function solidityFiles(host, real, rel) {
90
- const out = []
91
- const walk = (sub) => {
92
- for (const entry of host.readdir(join(real, sub))) {
93
- const path = sub === '.' ? entry.name : `${sub}/${entry.name}`
94
- if (entry.isDirectory()) {
95
- if (!isSkippedDir(entry.name)) walk(path)
96
- } else if (isEntrySol(entry)) {
97
- out.push(path)
98
- }
99
- }
100
- }
101
- walk(rel)
102
- return out.toSorted()
72
+ const walk = (sub) => host.readdir(join(real, sub)).flatMap((entry) => {
73
+ const path = sub === '.' ? entry.name : `${sub}/${entry.name}`
74
+ if (entry.isDirectory()) return SKIPPED_DIRS.has(entry.name.toLowerCase()) || isAutoExcludedDir(entry.name) ? [] : walk(path)
75
+ return isEntrySol(entry) ? [path] : []
76
+ })
77
+ return walk(rel).toSorted()
103
78
  }
104
79
 
105
- // The entry points of the Soldeer project in `dir`, by name and layout, as paths from `dir`: its
106
- // .sol files directly in it, then those under contracts/, then those under its source directory
107
- // (its foundry.toml's default profile's `src`, else `src`); none of its tests, scripts or mocks,
108
- // nor any in what it depends on or builds. A link to a directory is none.
109
80
  function solidityEntries(host, dir) {
110
81
  const real = host.realpath(dir)
111
82
  const isDirectory = (rel) => !posixPathEscapes(rel) && host.stat(join(real, rel))?.isDirectory() && host.readlink(join(real, rel)) === null
@@ -115,30 +86,63 @@ function solidityEntries(host, dir) {
115
86
  return [...new Set([...own, ...under('contracts'), ...(src === 'contracts' ? [] : under(src))])]
116
87
  }
117
88
 
118
- // By the kind of bundle a package manager builds: an entry its options are checked over before
119
- // there are any, the entries the build takes where none are given, and what having none says.
120
- export const KINDS = {
89
+ // `prefix*suffix`, one `*` in the last segment, whose `*` takes no leading `.`, as a shell's; null for no pattern.
90
+ export function entryPattern(name, entry) {
91
+ if (typeof entry !== 'string' || !entry.includes('*')) return null
92
+ const dir = entry.slice(0, entry.lastIndexOf('/') + 1)
93
+ const [prefix, suffix, ...more] = entry.slice(dir.length).split('*')
94
+ if (dir.includes('*') || more.length > 0) throw new Error(`${name}: ${JSON.stringify(entry)}: a pattern holds one *, in its last segment (prefix*suffix)`)
95
+ const matches = (file) => file.length >= prefix.length + suffix.length && file.startsWith(prefix) && file.endsWith(suffix) && (prefix !== '' || !file.startsWith('.'))
96
+ return { dir, matches }
97
+ }
98
+
99
+ export function expandEntries(name, host, cwd, entries) {
100
+ if (!Array.isArray(entries)) return entries
101
+ return entries.flatMap((entry) => {
102
+ const pattern = entryPattern(name, entry)
103
+ if (pattern === null) return [entry]
104
+ const dir = posix.resolve(cwd, pattern.dir)
105
+ const files = host.stat(dir)?.isDirectory() ? host.readdir(dir).filter((dirent) => dirent.isFile() && pattern.matches(dirent.name)) : []
106
+ if (files.length === 0) throw new Error(`${name}: ${JSON.stringify(entry)} matches no file`)
107
+ return files.map((dirent) => `${pattern.dir}${dirent.name}`).toSorted()
108
+ })
109
+ }
110
+
111
+ const KINDS = {
121
112
  js: { standIn: 'index.js', entries: packageEntries, none: 'has no package.json naming a JS entry point there' },
122
113
  sol: { standIn: 'index.sol', entries: solidityEntries, none: 'has no .sol entry point directly in it, under contracts/, or under its source directory' },
123
114
  }
124
115
 
125
116
  const EMPTY = vfsHost(new Vfs())
126
117
 
127
- // The build's checks of `options` before anything is fetched, over an empty tree, which never
128
- // decides them: for the package manager given, else for any; over the entries given, else over one
129
- // of the kind the default ones are. -> the PACKAGE_MANAGERS entry of the one given
118
+ export function defaultEntries(name, host, dir, packageManagers, options) {
119
+ const configured = configuredBuilds(name, { cwd: dir, host, options })
120
+ if (configured !== null) return { entries: configured.entries, none: 'has a stasis.config.json whose entries name none' }
121
+ const kinds = new Set(packageManagers.map((pm) => packageManagerOf(name, pm).kind))
122
+ if (kinds.size > 1) return { entries: [], none: 'has a stasis.config.json whose lockfiles lay out JS and Solidity trees, and no entries naming which to bundle' }
123
+ const kind = KINDS[[...kinds][0]]
124
+ checkVfsOptions(name, { ...options, entries: [kind.standIn], cwd: '/', host: EMPTY, fetched: false })
125
+ return { entries: kind.entries(host, dir, options), none: kind.none }
126
+ }
127
+
128
+ // Over an empty tree, before anything is fetched, but for what a configured entry's options may set (`partial`).
130
129
  export function checkAhead(name, packageManager, options) {
131
130
  checkVersion(name, { packageManager, ...options })
131
+ if (options.lazy !== undefined && typeof options.lazy !== 'boolean') throw new TypeError(`${name}: lazy must be a boolean`)
132
132
  const pm = packageManager === undefined ? undefined : packageManagerOf(name, packageManager)
133
+ if (options.lazy && pm !== undefined) checkLazy(name, packageManager)
133
134
  const refusal = (entries) => {
134
135
  try {
135
- checkKind(name, checkVfsOptions(name, { ...options, entries, cwd: '/', host: EMPTY, fetched: false }), packageManager === undefined ? undefined : [packageManager])
136
+ checkKind(name, checkVfsOptions(name, { ...options, entries, cwd: '/', host: EMPTY, fetched: false, partial: true }), packageManager === undefined ? undefined : [packageManager])
136
137
  return null
137
138
  } catch (error) {
138
139
  return error
139
140
  }
140
141
  }
141
- const refusals = options.entries === undefined ? (pm === undefined ? Object.values(KINDS) : [KINDS[pm.kind]]).map(({ standIn }) => refusal([standIn])) : [refusal(options.entries)]
142
+ // A pattern is checked as an entry of the kind its extension names, if any (not `src/*`).
143
+ const told = Array.isArray(options.entries) ? options.entries.filter((entry) => !entryPattern(name, entry) || /^\.[^*]+$/u.test(posix.extname(entry))) : options.entries
144
+ const entries = told?.length === 0 && options.entries.length > 0 ? undefined : told
145
+ const refusals = entries === undefined ? (pm === undefined ? Object.values(KINDS) : [KINDS[pm.kind]]).map(({ standIn }) => refusal([standIn])) : [refusal(entries)]
142
146
  if (!refusals.includes(null)) throw refusals[0]
143
147
  return pm
144
148
  }
@@ -1,34 +1,29 @@
1
1
  import { posix } from 'node:path'
2
2
 
3
3
  import { isValidRepoField } from '@exodus/stasis-core/bundle'
4
- import { posixPathEscapes } from '@exodus/stasis-core/util'
4
+ import { CONFIG_FILE, memoized, posixPathEscapes } from '@exodus/stasis-core/util'
5
5
  import { decompress } from '@preventive/archive/compression.js'
6
6
  import { ArchiveError, unpack } from '@preventive/archive/tar.js'
7
7
  import { createClient } from '@preventive/upstream/github.js'
8
8
  import { vfsFromEntries } from '@preventive/vfs'
9
- import { buildVfsBundle } from '../cmd/bundle.js'
10
- import { KINDS, checkAhead } from './entries.js'
11
- import { checkCache, checkTarget, detectPackageManager, installedAlone, lockfileOf, lockfilesListed, noLockfile, packageManagerOf, vfsHost } from './tree.js'
9
+ import { buildVfsBundle, configuredLockfiles, debugTimed, prismaClientsOf } from '../cmd/bundle.js'
10
+ import { checkAhead, defaultEntries, entryPattern, expandEntries } from './entries.js'
11
+ import { checkCache, checkTarget, detectPackageManager, installedAlone, listsFile, lockfileOf, lockfilesListed, lockfilesManagers, noLockfile, packageManagerOf, vfsHost } from './tree.js'
12
+ import { YARNCLEAN } from './yarnclean.js'
12
13
 
13
14
  // As upstream's tree verification bounds a tarball's unpacked size.
14
15
  const MAX_TAR_BYTES = 2 ** 30
15
- // What a git tree holds (upstream's verification refuses anything else from GitHub).
16
+ // Upstream's verification refuses anything else from GitHub.
16
17
  const TREE_TYPES = new Set(['file', 'directory', 'symlink'])
17
- // Lockfile references to a path above the lockfile's directory (conservatively: any importer's):
18
- // pnpm's and yarn's specs, and package-lock.json's package keys and `resolved` paths, `..` itself
19
- // (`link:..`, npm's `".."`) or under it.
18
+ // A lockfile's reference to `..` or under it (conservatively: any importer's).
20
19
  const LOCKFILE_ESCAPE = /(?:link:|file:|directory: |["'])\.\.(?![^/"'\s])/u
21
20
  const TSCONFIG = /(?:^|\/)[jt]sconfig[^/]*\.json$/u
22
21
 
23
- // Whether `target`, a path relative to the file `from`, resolves outside the tree.
24
22
  const resolvesOutside = (from, target) => target.startsWith('/') || posixPathEscapes(posix.join(posix.dirname(from), target))
25
23
 
26
- // The mode a checkout gives an entry under the usual umask 022. Git keeps only a file's executable
27
- // bit (upstream verifies that much), and GitHub's tarballs are written with git archive's tar.umask
28
- // 0002, which makes them 664 and 775 besides.
24
+ // As a checkout under umask 022 makes them: git keeps only the executable bit, and GitHub's tar.umask is 0002.
29
25
  const checkoutMode = ({ type, mode }) => (type === 'symlink' ? mode : type === 'directory' || mode & 0o100 ? 0o755 : 0o644)
30
26
 
31
- // A GitHub tarball's entries, its one top directory dropped, with the modes a checkout has.
32
27
  async function treeEntries(tarball, where) {
33
28
  const entries = []
34
29
  for (const entry of unpack(await decompress(tarball, 'gzip', { limit: MAX_TAR_BYTES }))) {
@@ -39,10 +34,8 @@ async function treeEntries(tarball, where) {
39
34
  return entries
40
35
  }
41
36
 
42
- // The first symlink pointing outside the tree, which would otherwise resolve within it.
43
37
  const escapingLink = (entries) => entries.find((entry) => entry.type === 'symlink' && resolvesOutside(entry.name, entry.linkname))
44
38
 
45
- // Whether a subtree's files refer to a path above it: a lockfile link or a [jt]sconfig path.
46
39
  function refersAbove(entries, lockfile) {
47
40
  const text = (entry) => new TextDecoder().decode(entry.data)
48
41
  return entries.some((entry) => entry.type === 'file' && (entry.name === lockfile
@@ -50,26 +43,24 @@ function refersAbove(entries, lockfile) {
50
43
  : TSCONFIG.test(entry.name) && [...text(entry).matchAll(/"(\.\.?\/[^"]*)"/gu)].some(([, path]) => resolvesOutside(entry.name, path))))
51
44
  }
52
45
 
53
- // The directories above `directory`, the repo's root (undefined) last.
46
+ // The repo's root (undefined) last.
54
47
  function ancestorsOf(directory) {
55
48
  const out = []
56
49
  for (let dir = posix.dirname(directory); dir !== '.'; dir = posix.dirname(dir)) out.push(dir)
57
50
  return [...out, undefined]
58
51
  }
59
52
 
60
- // The files of the subtree `tree`, or null where one is a symlink out of it.
61
- async function treeFiles(client, { github, tree, where }) {
62
- let files
53
+ // Upstream refuses symlinks above the tarball's root (its top directory) unless told how far they may climb: to the repo's.
54
+ async function treeFiles(client, { github, tree, directory, where }) {
63
55
  try {
64
- files = await treeEntries(await client.getRepoTreeTarball({ repo: github, tree }), where)
56
+ const files = await treeEntries(await client.getRepoTreeTarball({ repo: github, tree, symlinksAbove: directory.split('/').length - 1 }), where)
57
+ return escapingLink(files) ? null : files
65
58
  } catch (error) {
66
59
  if (error instanceof ArchiveError) return null // e.g. a symlink out of the subtree
67
60
  throw error
68
61
  }
69
- return escapingLink(files) ? null : files
70
62
  }
71
63
 
72
- // The whole repo's files at `sha`.
73
64
  async function repoFiles(client, { github, sha, where }) {
74
65
  const files = await treeEntries(await client.getRepoTarball({ repo: github, sha }), where)
75
66
  const link = escapingLink(files)
@@ -77,44 +68,54 @@ async function repoFiles(client, { github, sha, where }) {
77
68
  return files
78
69
  }
79
70
 
80
- // Checked as the Bundle checks them, before anything is fetched; a tag's name is upstream's to check.
81
- function checkRepo(name, { github, sha, tag, directory }) {
71
+ // Checked as the Bundle checks them, before anything is fetched; a `path` as a `directory`, but for a pattern's `*`.
72
+ function checkRepo(name, { github, sha, tag, directory, path }) {
82
73
  if (github === undefined) throw new Error(`${name}: github is required`)
83
74
  if (tag !== undefined && (typeof tag !== 'string' || tag === '')) throw new TypeError(`${name}: tag must be a non-empty string`)
84
75
  if (sha !== undefined && tag !== undefined) throw new Error(`${name}: sha and tag both name the commit: give one`)
76
+ if (directory !== undefined && path !== undefined) throw new Error(`${name}: directory and path both name the directory: give one`)
85
77
  for (const [key, value] of Object.entries({ github, commit: sha, directory: directory || undefined })) {
86
78
  if (value !== undefined && !isValidRepoField(key, value)) throw new Error(`${name}: invalid ${key}: ${JSON.stringify(value)}`)
87
79
  }
80
+ if (path && !isValidRepoField('directory', entryPattern(name, path) ? path.replace('*', '_') : path)) throw new Error(`${name}: invalid path: ${JSON.stringify(path)}`)
88
81
  }
89
82
 
90
- // listRepoDir's listing of a directory of the repo at `sha`, each listed once.
91
- function lister(client, { github, sha }) {
92
- const listings = new Map()
93
- return (directory) => {
94
- if (!listings.has(directory)) listings.set(directory, client.listRepoDir({ repo: github, sha, directory }))
95
- return listings.get(directory)
96
- }
97
- }
83
+ const SYMLINK = '120000'
98
84
 
99
- // Of no package manager given, the one whose lockfile alone `directory` (or the repo's root) and
100
- // the directories above it list, which is then the only one that may install it; refused where
101
- // none does; undefined where more than one does, or the listings can't tell, for the tree to tell.
102
- async function listedPackageManager(list, { directory, where }) {
103
- let listings
85
+ // Null where one is no plain directory in git (a symlink, or under one); the root's failure is GitHub's.
86
+ async function listingsOf(list, dirs) {
104
87
  try {
105
- listings = await Promise.all((directory ? [directory, ...ancestorsOf(directory)] : [undefined]).map(list))
88
+ return await Promise.all(dirs.map(list))
106
89
  } catch {
107
- // No plain directory in git (a symlink, or under one); the root's failure is GitHub's.
108
90
  await list(undefined)
109
- return undefined
91
+ return null
110
92
  }
93
+ }
94
+
95
+ async function splitPath(list, path, where) {
96
+ const pattern = entryPattern(where, path)
97
+ const matches = pattern?.matches ?? ((file) => file === posix.basename(path))
98
+ const above = ancestorsOf(path)
99
+ const listings = await listingsOf(list, above)
100
+ const names = (listings?.[0] ?? []).filter((entry) => entry.type === 'blob' && entry.mode !== SYMLINK && matches(entry.path)).map((entry) => entry.path).toSorted()
101
+ if (names.length === 0 && pattern) throw new Error(`${where}: ${JSON.stringify(path)} matches no file`)
102
+ if (names.length === 0) return { directory: path }
103
+ const files = names.every((file) => file.endsWith('.sol')) ? ['foundry.toml', 'soldeer.toml'] : ['package.json']
104
+ const at = listings.findIndex((listing) => listing.some((entry) => files.includes(entry.path) && entry.type === 'blob'))
105
+ const directory = at === -1 ? undefined : above[at]
106
+ const paths = names.map((file) => posix.join(posix.dirname(path), file))
107
+ return { directory, entries: directory === undefined ? paths : paths.map((file) => posix.relative(directory, file)) }
108
+ }
109
+
110
+ async function listedPackageManager(list, { directory, where }) {
111
+ const listings = await listingsOf(list, directory ? [directory, ...ancestorsOf(directory)] : [undefined])
112
+ if (listings === null) return { listed: undefined, configured: false }
113
+ const configured = listsFile(listings[0], CONFIG_FILE)
111
114
  const listed = lockfilesListed(listings)
112
- if (listed.length === 0) throw noLockfile(where, posix.resolve('/', directory || '.'))
113
- return listed.length === 1 ? listed[0] : undefined
115
+ if (listed.length === 0 && !configured) throw noLockfile(where, posix.resolve('/', directory || '.'))
116
+ return { listed: listed.length === 1 ? listed[0] : undefined, configured }
114
117
  }
115
118
 
116
- // The files of `directory` alone, when it holds the lockfile, is installed from itself and stands
117
- // alone; else null.
118
119
  async function subtreeEntries(client, list, { github, directory, packageManager, where }) {
119
120
  const names = async (path) => (await list(path)).map((entry) => entry.path)
120
121
  let listing
@@ -125,78 +126,97 @@ async function subtreeEntries(client, list, { github, directory, packageManager,
125
126
  }
126
127
  if (!lockfilesListed([listing]).includes(packageManager)) return null
127
128
  const above = ancestorsOf(directory)
128
- // Installed from a root above it (a workspace's), it is built from there.
129
- if (!(await installedAlone(packageManager, listing.map((entry) => entry.path), () => Promise.all(above.map(names))))) return null
129
+ const aboveNames = () => Promise.all(above.map(names))
130
+ if (!(await installedAlone(packageManager, listing.map((entry) => entry.path), aboveNames))) return null
131
+ if (packageManagerOf('buildGitHubBundle', packageManager).kind === 'js' && (await aboveNames()).some((dir) => dir.includes(YARNCLEAN))) return null
130
132
  // Its tree id, as the listing above it holds it, held to that directory's own.
131
133
  const tree = (await list(above[0])).find((entry) => entry.path === posix.basename(directory) && entry.type === 'tree')?.sha
132
- const files = tree === undefined ? null : await treeFiles(client, { github, tree, where })
134
+ const files = tree === undefined ? null : await treeFiles(client, { github, tree, directory, where })
133
135
  return files === null || refersAbove(files, lockfileOf(packageManager)) ? null : files
134
136
  }
135
137
 
136
- // The repo as buildGitHubBundle downloads it, for `name`: at `sha`, else the commit `tag` names (an
137
- // annotated tag followed to it), else the default branch's head; `directory` alone where it stands
138
- // alone for the package manager installing it, else the whole repo. That package manager is
139
- // `packageManager` if given, else the one the listings tell, before the tree is fetched, else the
140
- // tree; `options` are checked for it once it is known.
141
- // -> { vfs, host, cwd, subtree, sha, where, packageManager, kind }
142
- async function repoTree(name, { github, sha, tag, directory, client, packageManager }, options) {
138
+ async function repoTree(name, { github, sha, tag, directory, path, client, packageManager }, options) {
143
139
  checkAhead(name, packageManager, options)
144
- checkRepo(name, { github, sha, tag, directory })
140
+ checkRepo(name, { github, sha, tag, directory, path })
145
141
  client ??= createClient({ token: null })
146
142
  sha ??= tag === undefined ? (await client.getRepoHead({ repo: github })).oid : (await client.getRepoTag({ repo: github, tag })).oid
147
143
  const where = `${name}: ${github}@${sha}`
148
- const list = lister(client, { github, sha })
149
- let found = packageManager
150
- if (found === undefined) {
151
- found = await listedPackageManager(list, { directory, where })
152
- if (found !== undefined) checkAhead(name, found, options)
144
+ const list = memoized((dir) => client.listRepoDir({ repo: github, sha, directory: dir }))
145
+ if (path) {
146
+ const split = await splitPath(list, path, where)
147
+ directory = split.directory
148
+ if (split.entries !== undefined) {
149
+ options = { ...options, entries: [...split.entries, ...(options.entries ?? [])] }
150
+ checkAhead(name, packageManager, options)
151
+ }
152
+ }
153
+ const told = packageManager === undefined ? await listedPackageManager(list, { directory, where }) : { listed: packageManager }
154
+ let found = told.listed
155
+ // Unchecked where stasis.config.json may name the lockfiles in place of the one listed.
156
+ const checked = packageManager ?? (told.configured ? undefined : found)
157
+ if (checked !== packageManager) checkAhead(name, checked, options)
158
+ const open = (files, at) => {
159
+ const vfs = vfsFromEntries(files)
160
+ return { vfs, host: vfsHost(vfs), cwd: posix.resolve('/', at) }
161
+ }
162
+ let subtree = directory && found !== undefined ? await subtreeEntries(client, list, { github, directory, packageManager: found, where }) : null
163
+ let opened = open(subtree ?? await repoFiles(client, { github, sha, where }), subtree ? '.' : directory ?? '.')
164
+ // Lockfiles it names may climb out of the directory: the whole repo, which holds the same config there.
165
+ const lockfiles = configuredLockfiles(where, opened.cwd, opened.host)
166
+ if (lockfiles !== undefined && subtree !== null) {
167
+ subtree = null
168
+ opened = open(await repoFiles(client, { github, sha, where }), directory)
153
169
  }
154
- const subtree = directory && found !== undefined ? await subtreeEntries(client, list, { github, directory, packageManager: found, where }) : null
155
- const vfs = vfsFromEntries(subtree ?? await repoFiles(client, { github, sha, where }))
156
- const host = vfsHost(vfs)
157
- const cwd = posix.resolve('/', subtree ? '.' : directory ?? '.')
158
- if (packageManager === undefined) {
159
- const detected = detectPackageManager(where, host, cwd, { os: options.os })
160
- if (detected !== found) checkAhead(name, detected, options)
161
- found = detected
170
+ options = { ...options, entries: expandEntries(where, opened.host, opened.cwd, options.entries) }
171
+ if (lockfiles === undefined && packageManager === undefined) {
172
+ found = detectPackageManager(where, opened.host, opened.cwd, { os: options.os })
173
+ if (found !== checked) checkAhead(name, found, options)
162
174
  }
163
- return { vfs, host, cwd, subtree, sha, where, packageManager: found, kind: KINDS[packageManagerOf(name, found).kind] }
175
+ // With lockfiles, none detected: a package manager given is the build's to refuse.
176
+ const packageManagers = lockfiles === undefined || packageManager !== undefined ? [found] : lockfilesManagers(where, lockfiles)
177
+ return { ...opened, client, subtree, sha, where, packageManager: lockfiles === undefined ? found : packageManager, packageManagers, directory, entries: options.entries }
164
178
  }
165
179
 
166
- // A GitHub repo at a full commit, or the one `tag` names, the default branch's head without either,
167
- // as buildVfsBundle builds it: `directory` downloaded alone where it stands alone, else the whole
168
- // repo, and without a `packageManager`, the one whose lockfile installs it, where only one's does
169
- // (repoTree). Without `entries`, they are the ones suggestedEntries suggests. The bundle's paths are
170
- // relative to `directory`, or for a JS bundle, to the innermost package directory at or above it
171
- // holding every file it bundles (buildJsBundle's innermostRoot), which is the project's root where
172
- // one is outside the others; `repo` names that directory and the commit. Nothing is read from disk:
173
- // the tree's bytes come from GitHub, or from the cache setCacheDir names, held to the git tree id
174
- // either way (@preventive/upstream), and are unpacked into a Vfs that buildVfsBundle reads alone.
175
- // `cache` is buildVfsBundle's, for the npm packages the tree installs: the repo's tree is cached
176
- // where setCacheDir says, whatever it is.
177
- export async function buildGitHubBundle({ github, sha, tag, directory, client, packageManager, ...options } = {}) {
180
+ // Nothing is read from disk: the tree comes from GitHub or setCacheDir's cache, held to its git tree id.
181
+ export async function buildGitHubBundle({ github, sha, tag, directory, path, client, packageManager, ...options } = {}) {
178
182
  checkTarget('buildGitHubBundle', options)
179
183
  checkCache('buildGitHubBundle', options.cache)
180
- // Its own, for a Soldeer git dependency too.
181
- client ??= createClient({ token: null })
182
- const tree = await repoTree('buildGitHubBundle', { github, sha, tag, directory, client, packageManager }, options)
183
- let { entries } = options
184
+ const tree = await debugTimed('Fetched the repo', () => repoTree('buildGitHubBundle', { github, sha, tag, directory, path, client, packageManager }, options))
185
+ let { entries } = tree
184
186
  if (entries === undefined) {
185
- // As suggestedEntries suggests them, from the tree at hand.
186
- entries = tree.kind.entries(tree.host, tree.cwd, options)
187
- if (entries.length === 0) throw new Error(`${tree.where}: no entries given, and ${directory || 'the repo root'} ${tree.kind.none}`)
187
+ const suggested = defaultEntries('buildGitHubBundle', tree.host, tree.cwd, tree.packageManagers, options)
188
+ entries = suggested.entries
189
+ if (entries.length === 0) throw new Error(`${tree.where}: no entries given, and ${tree.directory || 'the repo root'} ${suggested.none}`)
188
190
  }
189
191
  const repo = { github, commit: tree.sha }
190
- const built = await buildVfsBundle({ ...options, entries, packageManager: tree.packageManager, vfs: tree.vfs, cwd: tree.cwd, innermostRoot: true, repo, client })
192
+ const built = await buildVfsBundle({ ...options, entries, packageManager: tree.packageManager, vfs: tree.vfs, cwd: tree.cwd, innermostRoot: true, repo, client: tree.client })
191
193
  // The Vfs is the repo's tree, or the subtree at `directory`: a layout known, so its root is `''`.
192
- const at = posix.join(tree.subtree ? directory : '.', posix.relative('/', built.root)).replace(/^\.$/u, '')
193
- built.bundle.repo = { ...repo, ...(isValidRepoField('directory', at) ? { directory: at } : {}) }
194
+ const at = posix.join(tree.subtree ? tree.directory : '.', posix.relative('/', built.root)).replace(/^\.$/u, '')
195
+ built.bundle.repo = { ...repo, ...(isValidRepoField('directory', at) && { directory: at }) }
196
+ // Onto buildVfsBundle's own, not a copy, which would read its lockfile, built only when read.
197
+ built.directory = tree.directory
198
+ // The lockfile records the repo too, as the bundle does: a GitHub build's alone does.
199
+ const { get } = Object.getOwnPropertyDescriptor(built, 'lockfile')
200
+ Object.defineProperty(built, 'lockfile', {
201
+ enumerable: true,
202
+ configurable: true,
203
+ get() {
204
+ const lockfile = get.call(this)
205
+ if (lockfile !== undefined) lockfile.repo = built.bundle.repo
206
+ return lockfile
207
+ },
208
+ })
194
209
  return built
195
210
  }
196
211
 
197
- // suggestedEntries of a GitHub repo, `{ github, sha, tag, directory, client, packageManager }`, as
198
- // buildGitHubBundle downloads it.
212
+ export async function repoPrismaClients({ github, sha, directory, client }) {
213
+ const tree = await repoTree('repoPrismaClients', { github, sha, directory: directory || undefined, client }, {})
214
+ const outputs = await prismaClientsOf({ vfs: tree.vfs, cwd: tree.cwd, packageManager: tree.packageManager, client })
215
+ const from = (path) => posix.relative(tree.cwd, path)
216
+ return outputs.map((output) => ({ ...output, dir: from(output.dir), schema: from(output.schema) })).filter(({ dir }) => !posixPathEscapes(dir))
217
+ }
218
+
199
219
  export async function suggestedRepoEntries(repo, resolution) {
200
220
  const tree = await repoTree('suggestedEntries', repo, resolution)
201
- return tree.kind.entries(tree.host, tree.cwd, resolution)
221
+ return tree.entries ?? defaultEntries('suggestedEntries', tree.host, tree.cwd, tree.packageManagers, resolution).entries
202
222
  }