@exodus/stasis 1.0.0-beta.12 → 1.0.0-beta.14

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.
@@ -5,10 +5,11 @@ 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
15
  // Node's own conditions for require() and for import, which the build's are added to; with
@@ -124,15 +125,35 @@ export const KINDS = {
124
125
 
125
126
  const EMPTY = vfsHost(new Vfs())
126
127
 
128
+ // What a stasis.config.json's empty `entries` says of the directory holding it.
129
+ const CONFIGURED_NONE = 'has a stasis.config.json whose entries name none'
130
+
131
+ // -> { entries, none }: the entries a build of the project in `dir` (read through `host`) takes
132
+ // where none are given, as paths from `dir`, and what having none says of `dir`. Those its
133
+ // stasis.config.json's `entries` names, checked with `options` as the build checks them
134
+ // (configuredBuilds), where it has `entries`, whatever it names; else those of `kind` (KINDS'),
135
+ // with `options` then checked on their own, as the build checks them, which nothing completes.
136
+ export function defaultEntries(name, host, dir, kind, options) {
137
+ const configured = configuredBuilds(name, { cwd: dir, host, options })
138
+ if (configured !== null) return { entries: configured.entries, none: CONFIGURED_NONE }
139
+ checkVfsOptions(name, { ...options, entries: [kind.standIn], cwd: '/', host: EMPTY, fetched: false })
140
+ return { entries: kind.entries(host, dir, options), none: kind.none }
141
+ }
142
+
127
143
  // The build's checks of `options` before anything is fetched, over an empty tree, which never
128
144
  // 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
145
+ // of the kind the default ones are. But for what one of them needs of another, which the options of
146
+ // an entry stasis.config.json names may set (`partial`), left to the build, which knows whether it
147
+ // names it; and `lazy`, which the package manager given has to take. -> the PACKAGE_MANAGERS entry of
148
+ // the one given
130
149
  export function checkAhead(name, packageManager, options) {
131
150
  checkVersion(name, { packageManager, ...options })
151
+ if (options.lazy !== undefined && typeof options.lazy !== 'boolean') throw new TypeError(`${name}: lazy must be a boolean`)
132
152
  const pm = packageManager === undefined ? undefined : packageManagerOf(name, packageManager)
153
+ if (options.lazy && pm !== undefined) checkLazy(name, packageManager)
133
154
  const refusal = (entries) => {
134
155
  try {
135
- checkKind(name, checkVfsOptions(name, { ...options, entries, cwd: '/', host: EMPTY, fetched: false }), packageManager === undefined ? undefined : [packageManager])
156
+ checkKind(name, checkVfsOptions(name, { ...options, entries, cwd: '/', host: EMPTY, fetched: false, partial: true }), packageManager === undefined ? undefined : [packageManager])
136
157
  return null
137
158
  } catch (error) {
138
159
  return error
@@ -6,9 +6,10 @@ 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'
9
+ import { buildVfsBundle, debugTimed } from '../cmd/bundle.js'
10
+ import { KINDS, checkAhead, defaultEntries } from './entries.js'
11
11
  import { checkCache, checkTarget, detectPackageManager, installedAlone, lockfileOf, lockfilesListed, 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
@@ -79,14 +80,45 @@ async function repoFiles(client, { github, sha, where }) {
79
80
  return files
80
81
  }
81
82
 
82
- // Checked as the Bundle checks them, before anything is fetched; a tag's name is upstream's to check.
83
- function checkRepo(name, { github, sha, tag, directory }) {
83
+ // Checked as the Bundle checks them, before anything is fetched; a tag's name is upstream's to check,
84
+ // and a `path` is checked as a `directory`, which it may be.
85
+ function checkRepo(name, { github, sha, tag, directory, path }) {
84
86
  if (github === undefined) throw new Error(`${name}: github is required`)
85
87
  if (tag !== undefined && (typeof tag !== 'string' || tag === '')) throw new TypeError(`${name}: tag must be a non-empty string`)
86
88
  if (sha !== undefined && tag !== undefined) throw new Error(`${name}: sha and tag both name the commit: give one`)
89
+ if (directory !== undefined && path !== undefined) throw new Error(`${name}: directory and path both name the directory: give one`)
87
90
  for (const [key, value] of Object.entries({ github, commit: sha, directory: directory || undefined })) {
88
91
  if (value !== undefined && !isValidRepoField(key, value)) throw new Error(`${name}: invalid ${key}: ${JSON.stringify(value)}`)
89
92
  }
93
+ if (path && !isValidRepoField('directory', path)) throw new Error(`${name}: invalid path: ${JSON.stringify(path)}`)
94
+ }
95
+
96
+ // The files that make a directory a project's root, for an entry of each kind: a JS project's
97
+ // package.json, and the foundry.toml or soldeer.toml Soldeer installs from.
98
+ const PROJECT_FILES = { js: ['package.json'], sol: ['foundry.toml', 'soldeer.toml'] }
99
+ // A symlink's mode in a git tree.
100
+ const SYMLINK = '120000'
101
+
102
+ // `path` in the repo, by `list`'s listings, as `{ directory, entry }`: a file (no symlink) is an
103
+ // entry of the nearest directory at or above it holding one of PROJECT_FILES of its kind, the repo's
104
+ // root (undefined) where none does, and a path from there; anything else, or what the listings can't
105
+ // tell (a symlink on the way), is the directory, with no entry.
106
+ async function splitPath(list, path) {
107
+ const above = ancestorsOf(path)
108
+ let listings
109
+ try {
110
+ listings = await Promise.all(above.map(list))
111
+ } catch {
112
+ // No plain directory in git (a symlink, or under one); the root's failure is GitHub's.
113
+ await list(undefined)
114
+ return { directory: path }
115
+ }
116
+ const name = posix.basename(path)
117
+ if (!listings[0].some((entry) => entry.path === name && entry.type === 'blob' && entry.mode !== SYMLINK)) return { directory: path }
118
+ const files = PROJECT_FILES[path.endsWith('.sol') ? 'sol' : 'js']
119
+ const at = listings.findIndex((listing) => listing.some((entry) => files.includes(entry.path) && entry.type === 'blob'))
120
+ const directory = at === -1 ? undefined : above[at]
121
+ return { directory, entry: directory === undefined ? path : posix.relative(directory, path) }
90
122
  }
91
123
 
92
124
  // listRepoDir's listing of a directory of the repo at `sha`, each listed once.
@@ -116,7 +148,7 @@ async function listedPackageManager(list, { directory, where }) {
116
148
  }
117
149
 
118
150
  // The files of `directory` alone, when it holds the lockfile, is installed from itself and stands
119
- // alone; else null.
151
+ // alone, nor does a .yarnclean above it clean the node_modules it installs; else null.
120
152
  async function subtreeEntries(client, list, { github, directory, packageManager, where }) {
121
153
  const names = async (path) => (await list(path)).map((entry) => entry.path)
122
154
  let listing
@@ -129,6 +161,7 @@ async function subtreeEntries(client, list, { github, directory, packageManager,
129
161
  const above = ancestorsOf(directory)
130
162
  // Installed from a root above it (a workspace's), it is built from there.
131
163
  if (!(await installedAlone(packageManager, listing.map((entry) => entry.path), () => Promise.all(above.map(names))))) return null
164
+ if (packageManagerOf('buildGitHubBundle', packageManager).kind === 'js' && (await Promise.all(above.map(names))).some((dir) => dir.includes(YARNCLEAN))) return null
132
165
  // Its tree id, as the listing above it holds it, held to that directory's own.
133
166
  const tree = (await list(above[0])).find((entry) => entry.path === posix.basename(directory) && entry.type === 'tree')?.sha
134
167
  const files = tree === undefined ? null : await treeFiles(client, { github, tree, directory, where })
@@ -139,15 +172,24 @@ async function subtreeEntries(client, list, { github, directory, packageManager,
139
172
  // annotated tag followed to it), else the default branch's head; `directory` alone where it stands
140
173
  // alone for the package manager installing it, else the whole repo. That package manager is
141
174
  // `packageManager` if given, else the one the listings tell, before the tree is fetched, else the
142
- // tree; `options` are checked for it once it is known.
143
- // -> { vfs, host, cwd, subtree, sha, where, packageManager, kind }
144
- async function repoTree(name, { github, sha, tag, directory, client, packageManager }, options) {
175
+ // tree; `options` are checked for it once it is known. A `path` is the `directory`, or a file in
176
+ // it which is an entry ahead of `options.entries` (splitPath).
177
+ // -> { vfs, host, cwd, subtree, sha, where, packageManager, kind, directory, entries }
178
+ async function repoTree(name, { github, sha, tag, directory, path, client, packageManager }, options) {
145
179
  checkAhead(name, packageManager, options)
146
- checkRepo(name, { github, sha, tag, directory })
180
+ checkRepo(name, { github, sha, tag, directory, path })
147
181
  client ??= createClient({ token: null })
148
182
  sha ??= tag === undefined ? (await client.getRepoHead({ repo: github })).oid : (await client.getRepoTag({ repo: github, tag })).oid
149
183
  const where = `${name}: ${github}@${sha}`
150
184
  const list = lister(client, { github, sha })
185
+ if (path) {
186
+ const split = await splitPath(list, path)
187
+ directory = split.directory
188
+ if (split.entry !== undefined) {
189
+ options = { ...options, entries: [split.entry, ...(options.entries ?? [])] }
190
+ checkAhead(name, packageManager, options)
191
+ }
192
+ }
151
193
  let found = packageManager
152
194
  if (found === undefined) {
153
195
  found = await listedPackageManager(list, { directory, where })
@@ -162,43 +204,50 @@ async function repoTree(name, { github, sha, tag, directory, client, packageMana
162
204
  if (detected !== found) checkAhead(name, detected, options)
163
205
  found = detected
164
206
  }
165
- return { vfs, host, cwd, subtree, sha, where, packageManager: found, kind: KINDS[packageManagerOf(name, found).kind] }
207
+ return { vfs, host, cwd, subtree, sha, where, packageManager: found, kind: KINDS[packageManagerOf(name, found).kind], directory, entries: options.entries }
166
208
  }
167
209
 
168
210
  // A GitHub repo at a full commit, or the one `tag` names, the default branch's head without either,
169
211
  // as buildVfsBundle builds it: `directory` downloaded alone where it stands alone, else the whole
170
212
  // repo, and without a `packageManager`, the one whose lockfile installs it, where only one's does
171
- // (repoTree). Without `entries`, they are the ones suggestedEntries suggests. The bundle's paths are
213
+ // (repoTree). Without `entries`, they are the ones suggestedEntries suggests: those its
214
+ // stasis.config.json's `entries` names, where it has `entries`. The bundle's paths are
172
215
  // relative to `directory`, or for a JS bundle, to the innermost package directory at or above it
173
216
  // holding every file it bundles (buildJsBundle's innermostRoot), which is the project's root where
174
217
  // one is outside the others; `repo` names that directory and the commit. Nothing is read from disk:
175
218
  // the tree's bytes come from GitHub, or from the cache setCacheDir names, held to the git tree id
176
219
  // either way (@preventive/upstream), and are unpacked into a Vfs that buildVfsBundle reads alone.
177
- // `cache` is buildVfsBundle's, for the npm packages the tree installs: the repo's tree is cached
178
- // where setCacheDir says, whatever it is.
179
- export async function buildGitHubBundle({ github, sha, tag, directory, client, packageManager, ...options } = {}) {
220
+ // `cache` and `lazy` are buildVfsBundle's, for the npm packages the tree installs: the repo's tree is
221
+ // cached where setCacheDir says, whatever it is, and fetched whole.
222
+ // `path`, in place of `directory`, is a path in the repo at the commit: a directory is the
223
+ // `directory`, and a file an entry ahead of `entries`, of the nearest directory at or above it with
224
+ // a package.json (with a foundry.toml or soldeer.toml, for a .sol file), else of the repo's root,
225
+ // which is then the `directory` (splitPath).
226
+ // -> buildVfsBundle's, and the `directory` bundled
227
+ export async function buildGitHubBundle({ github, sha, tag, directory, path, client, packageManager, ...options } = {}) {
180
228
  checkTarget('buildGitHubBundle', options)
181
229
  checkCache('buildGitHubBundle', options.cache)
182
230
  // Its own, for a Soldeer git dependency too.
183
231
  client ??= createClient({ token: null })
184
- const tree = await repoTree('buildGitHubBundle', { github, sha, tag, directory, client, packageManager }, options)
185
- let { entries } = options
232
+ const tree = await debugTimed('Fetched the repo', () => repoTree('buildGitHubBundle', { github, sha, tag, directory, path, client, packageManager }, options))
233
+ let { entries } = tree
186
234
  if (entries === undefined) {
187
235
  // As suggestedEntries suggests them, from the tree at hand.
188
- entries = tree.kind.entries(tree.host, tree.cwd, options)
189
- if (entries.length === 0) throw new Error(`${tree.where}: no entries given, and ${directory || 'the repo root'} ${tree.kind.none}`)
236
+ const suggested = defaultEntries('buildGitHubBundle', tree.host, tree.cwd, tree.kind, options)
237
+ entries = suggested.entries
238
+ if (entries.length === 0) throw new Error(`${tree.where}: no entries given, and ${tree.directory || 'the repo root'} ${suggested.none}`)
190
239
  }
191
240
  const repo = { github, commit: tree.sha }
192
241
  const built = await buildVfsBundle({ ...options, entries, packageManager: tree.packageManager, vfs: tree.vfs, cwd: tree.cwd, innermostRoot: true, repo, client })
193
242
  // The Vfs is the repo's tree, or the subtree at `directory`: a layout known, so its root is `''`.
194
- const at = posix.join(tree.subtree ? directory : '.', posix.relative('/', built.root)).replace(/^\.$/u, '')
243
+ const at = posix.join(tree.subtree ? tree.directory : '.', posix.relative('/', built.root)).replace(/^\.$/u, '')
195
244
  built.bundle.repo = { ...repo, ...(isValidRepoField('directory', at) ? { directory: at } : {}) }
196
- return built
245
+ return { ...built, directory: tree.directory }
197
246
  }
198
247
 
199
- // suggestedEntries of a GitHub repo, `{ github, sha, tag, directory, client, packageManager }`, as
200
- // buildGitHubBundle downloads it.
248
+ // suggestedEntries of a GitHub repo, `{ github, sha, tag, directory | path, client, packageManager }`,
249
+ // as buildGitHubBundle downloads it: a `path` to a file, that file.
201
250
  export async function suggestedRepoEntries(repo, resolution) {
202
251
  const tree = await repoTree('suggestedEntries', repo, resolution)
203
- return tree.kind.entries(tree.host, tree.cwd, resolution)
252
+ return tree.entries ?? defaultEntries('suggestedEntries', tree.host, tree.cwd, tree.kind, resolution).entries
204
253
  }
@@ -0,0 +1,420 @@
1
+ import { createRequire } from 'node:module'
2
+ import { posix } from 'node:path'
3
+
4
+ // Node's own fs.globSync and path.matchesGlob over a `host` (@exodus/stasis-core/host) in place of
5
+ // the disk, for a Vfs's: Node 24.22.2's and 26.13.0's internal/fs/glob.js, its globSync as it runs
6
+ // without `followSymlinks`, `maxDepth` or an array `exclude`, over the minimatch 10 they vendor, an
7
+ // optional dependency loaded where a pattern is first matched. Unlike Node's, matching never folds
8
+ // case, whatever the OS: the tree is git's, whose names differ by case alone.
9
+
10
+ const { join, resolve } = posix
11
+ const require = createRequire(import.meta.url)
12
+
13
+ const MINIMATCH = 'minimatch'
14
+ const MINIMATCH_MAJOR = '10'
15
+ // Node's createMatcher's, as on Linux.
16
+ const OPTIONS = { nocase: false, windowsPathsNoEscape: true, nonegate: true, nocomment: true, optimizationLevel: 2, platform: 'linux', nocaseMagicOnly: true }
17
+
18
+ let minimatch
19
+ // minimatch, or a refusal naming `what` needs it where it isn't installed.
20
+ export function loadMinimatch(what) {
21
+ if (minimatch) return minimatch
22
+ const needs = `${what} needs the optional '${MINIMATCH}' ${MINIMATCH_MAJOR} dependency`
23
+ let version
24
+ try {
25
+ version = require(`${MINIMATCH}/package.json`).version
26
+ } catch (cause) {
27
+ throw new Error(`${needs}; install it (e.g. \`npm i ${MINIMATCH}@${MINIMATCH_MAJOR}\`)`, { cause })
28
+ }
29
+ // Another package's, found where the optional one isn't installed.
30
+ if (version.split('.')[0] !== MINIMATCH_MAJOR) throw new Error(`${needs}, not ${version}`)
31
+ minimatch = require(MINIMATCH)
32
+ return minimatch
33
+ }
34
+
35
+ const lazyMinimatch = () => loadMinimatch("A glob over a Vfs (stasis.config.json's extra)")
36
+
37
+ // Throws where globbing a Vfs can't load minimatch, before anything else is done.
38
+ export const checkGlob = () => void lazyMinimatch()
39
+
40
+ const createMatcher = (pattern) => new (lazyMinimatch().Minimatch)(pattern, OPTIONS)
41
+
42
+ // What `host` holds at `path`, its last link not followed, as far as the walk asks of lstat's
43
+ // Dirent; null where nothing is.
44
+ function lstatOf(host, path) {
45
+ let link
46
+ try {
47
+ link = host.readlink(path)
48
+ } catch {
49
+ return null
50
+ }
51
+ const directory = link === null ? host.stat(path)?.isDirectory() : false
52
+ if (directory === undefined) return null
53
+ return { isDirectory: () => directory, isSymbolicLink: () => link !== null }
54
+ }
55
+
56
+ class Cache {
57
+ #host
58
+ #cache = new Map()
59
+ #statsCache = new Map()
60
+ #readdirCache = new Map()
61
+
62
+ constructor(host) {
63
+ this.#host = host
64
+ }
65
+ statSync(path) {
66
+ const cached = this.#statsCache.get(path)
67
+ if (cached) {
68
+ return cached
69
+ }
70
+ const val = lstatOf(this.#host, path)
71
+ this.#statsCache.set(path, val)
72
+ return val
73
+ }
74
+ addToStatCache(path, val) {
75
+ this.#statsCache.set(path, val)
76
+ }
77
+ readdirSync(path) {
78
+ const cached = this.#readdirCache.get(path)
79
+ if (cached) {
80
+ return cached
81
+ }
82
+ let val
83
+ try {
84
+ val = this.#host.readdir(path)
85
+ } catch {
86
+ val = []
87
+ }
88
+ this.#readdirCache.set(path, val)
89
+ return val
90
+ }
91
+ add(path, pattern) {
92
+ let cache = this.#cache.get(path)
93
+ if (!cache) {
94
+ cache = new Set()
95
+ this.#cache.set(path, cache)
96
+ }
97
+ const originalSize = cache.size
98
+ pattern.indexes.forEach((index) => cache.add(pattern.cacheKey(index)))
99
+ return cache.size !== originalSize + pattern.indexes.size
100
+ }
101
+ seen(path, pattern, index) {
102
+ return this.#cache.get(path)?.has(pattern.cacheKey(index))
103
+ }
104
+ }
105
+
106
+ class Pattern {
107
+ #pattern
108
+ #globStrings
109
+ indexes
110
+ symlinks
111
+ last
112
+
113
+ constructor(pattern, globStrings, indexes, symlinks) {
114
+ this.#pattern = pattern
115
+ this.#globStrings = globStrings
116
+ this.indexes = indexes
117
+ this.symlinks = symlinks
118
+ this.last = pattern.length - 1
119
+ }
120
+
121
+ isLast(isDirectory) {
122
+ return this.indexes.has(this.last) ||
123
+ (this.at(-1) === '' && isDirectory &&
124
+ this.indexes.has(this.last - 1) && this.at(-2) === lazyMinimatch().GLOBSTAR)
125
+ }
126
+ isFirst() {
127
+ return this.indexes.has(0)
128
+ }
129
+ get hasSeenSymlinks() {
130
+ return [...this.indexes].some((i) => !this.symlinks.has(i))
131
+ }
132
+ at(index) {
133
+ return this.#pattern.at(index)
134
+ }
135
+ child(indexes, symlinks = new Set()) {
136
+ return new Pattern(this.#pattern, this.#globStrings, indexes, symlinks)
137
+ }
138
+ test(index, path) {
139
+ if (index > this.#pattern.length) {
140
+ return false
141
+ }
142
+ const pattern = this.#pattern[index]
143
+ if (pattern === lazyMinimatch().GLOBSTAR) {
144
+ return true
145
+ }
146
+ if (typeof pattern === 'string') {
147
+ return pattern === path
148
+ }
149
+ if (typeof pattern?.test === 'function') {
150
+ return pattern.test(path)
151
+ }
152
+ return false
153
+ }
154
+
155
+ cacheKey(index) {
156
+ let key = ''
157
+ for (let i = index; i < this.#globStrings.length; i++) {
158
+ key += this.#globStrings[i]
159
+ if (i !== this.#globStrings.length - 1) {
160
+ key += '/'
161
+ }
162
+ }
163
+ return key
164
+ }
165
+ }
166
+
167
+ // Node's, but for `exclude`: given each path's absolute path (where Node gives its Dirent with
168
+ // `withFileTypes`), and in #addSubpattern, the path from the root (where Node's lstat takes it, as
169
+ // spelled from the root, from the process's cwd).
170
+ class Glob {
171
+ #root
172
+ #exclude
173
+ #cache
174
+ #results = new Set()
175
+ #queue = []
176
+ #subpatterns = new Map()
177
+ #patterns
178
+
179
+ constructor(host, pattern, { cwd, exclude }) {
180
+ this.#root = cwd
181
+ this.#exclude = exclude
182
+ this.#cache = new Cache(host)
183
+ const matcher = createMatcher(pattern)
184
+ this.#patterns = matcher.set.map((set, i) => new Pattern(set, matcher.globParts[i], new Set().add(0), new Set()))
185
+ }
186
+
187
+ globSync() {
188
+ this.#queue.push({ path: '.', patterns: this.#patterns })
189
+ while (this.#queue.length > 0) {
190
+ const item = this.#queue.pop()
191
+ for (let i = 0; i < item.patterns.length; i++) {
192
+ this.#addSubpatterns(item.path, item.patterns[i])
193
+ }
194
+ this.#subpatterns.forEach((patterns, path) => this.#queue.push({ path, patterns }))
195
+ this.#subpatterns.clear()
196
+ }
197
+ return [...this.#results]
198
+ }
199
+ #isDirectorySync(stat, pattern) {
200
+ if (stat?.isDirectory()) {
201
+ return true
202
+ }
203
+ if (!stat?.isSymbolicLink()) {
204
+ return false
205
+ }
206
+ return pattern.hasSeenSymlinks
207
+ }
208
+ #addSubpattern(path, pattern) {
209
+ if (this.#exclude) {
210
+ const fullpath = resolve(this.#root, path)
211
+ if (this.#cache.statSync(fullpath) !== null && this.#exclude(fullpath)) {
212
+ return
213
+ }
214
+ }
215
+ if (!this.#subpatterns.has(path)) {
216
+ this.#subpatterns.set(path, [pattern])
217
+ } else {
218
+ this.#subpatterns.get(path).push(pattern)
219
+ }
220
+ }
221
+ #addSubpatterns(path, pattern) {
222
+ const seen = this.#cache.add(path, pattern)
223
+ if (seen) {
224
+ return
225
+ }
226
+ const fullpath = resolve(this.#root, path)
227
+ const stat = this.#cache.statSync(fullpath)
228
+ const last = pattern.last
229
+ const isDirectory = this.#isDirectorySync(stat, pattern)
230
+ const isLast = pattern.isLast(isDirectory)
231
+ const isFirst = pattern.isFirst()
232
+
233
+ if (isFirst && pattern.at(0) === '') {
234
+ // Absolute path, go to root
235
+ this.#addSubpattern('/', pattern.child(new Set().add(1)))
236
+ return
237
+ }
238
+ if (isFirst && pattern.at(0) === '..') {
239
+ // Start with .., go to parent
240
+ this.#addSubpattern('../', pattern.child(new Set().add(1)))
241
+ return
242
+ }
243
+ if (isFirst && pattern.at(0) === '.') {
244
+ // Start with ., proceed
245
+ this.#addSubpattern('.', pattern.child(new Set().add(1)))
246
+ return
247
+ }
248
+
249
+ if (isLast && typeof pattern.at(-1) === 'string') {
250
+ // Add result if it exists
251
+ const p = pattern.at(-1)
252
+ const found = this.#cache.statSync(join(fullpath, p))
253
+ if (found && (p || isDirectory)) {
254
+ this.#results.add(join(path, p))
255
+ }
256
+ if (pattern.indexes.size === 1 && pattern.indexes.has(last)) {
257
+ return
258
+ }
259
+ } else if (isLast && pattern.at(-1) === lazyMinimatch().GLOBSTAR &&
260
+ (path !== '.' || pattern.at(0) === '.' || (last === 0 && stat))) {
261
+ // If pattern ends with **, add to results
262
+ // if path is ".", add it only if pattern starts with "." or pattern is exactly "**"
263
+ this.#results.add(path)
264
+ }
265
+
266
+ if (!isDirectory) {
267
+ return
268
+ }
269
+
270
+ let children
271
+ const firstPattern = pattern.indexes.size === 1 && pattern.at(pattern.indexes.values().next().value)
272
+ if (typeof firstPattern === 'string') {
273
+ const child = this.#cache.statSync(join(fullpath, firstPattern))
274
+ if (child) {
275
+ child.name = firstPattern
276
+ children = [child]
277
+ } else {
278
+ return
279
+ }
280
+ } else {
281
+ children = this.#cache.readdirSync(fullpath)
282
+ }
283
+
284
+ for (let i = 0; i < children.length; i++) {
285
+ const entry = children[i]
286
+ const entryPath = join(path, entry.name)
287
+ const entryFullpath = join(fullpath, entry.name)
288
+ this.#cache.addToStatCache(entryFullpath, entry)
289
+ const entryIsDirectory = entry.isDirectory()
290
+
291
+ const subPatterns = new Set()
292
+ const nSymlinks = new Set()
293
+ for (const index of pattern.indexes) {
294
+ // For each child, check potential patterns
295
+ const current = pattern.at(index)
296
+ const nextIndex = index + 1
297
+ const next = pattern.at(nextIndex)
298
+ const fromSymlink = pattern.symlinks.has(index)
299
+
300
+ if (current === lazyMinimatch().GLOBSTAR) {
301
+ const isDot = entry.name[0] === '.'
302
+ const nextMatches = pattern.test(nextIndex, entry.name)
303
+
304
+ let nextNonGlobIndex = nextIndex
305
+ while (pattern.at(nextNonGlobIndex) === lazyMinimatch().GLOBSTAR) {
306
+ nextNonGlobIndex++
307
+ }
308
+
309
+ const matchesDot = isDot && pattern.test(nextNonGlobIndex, entry.name)
310
+
311
+ if ((isDot && !matchesDot) ||
312
+ (this.#exclude && this.#exclude(entryFullpath))) {
313
+ continue
314
+ }
315
+ if (!fromSymlink && entryIsDirectory) {
316
+ // If directory, add ** to its potential patterns
317
+ subPatterns.add(index)
318
+ } else if (!fromSymlink && index === last) {
319
+ // If ** is last, add to results
320
+ this.#results.add(entryPath)
321
+ }
322
+
323
+ // Any pattern after ** is also a potential pattern
324
+ // so we can already test it here
325
+ if (nextMatches && nextIndex === last && !isLast) {
326
+ // If next pattern is the last one, add to results
327
+ this.#results.add(entryPath)
328
+ } else if (nextMatches && entryIsDirectory) {
329
+ // Pattern matched, meaning two patterns forward
330
+ // are also potential patterns
331
+ // e.g **/b/c when entry is a/b - add c to potential patterns
332
+ subPatterns.add(index + 2)
333
+ }
334
+ if ((nextMatches || pattern.at(0) === '.') &&
335
+ entryIsDirectory && !fromSymlink) {
336
+ // If pattern after ** matches, or pattern starts with "."
337
+ // and entry is a directory, add to potential patterns
338
+ subPatterns.add(nextIndex)
339
+ }
340
+
341
+ if (entry.isSymbolicLink()) {
342
+ nSymlinks.add(index)
343
+ }
344
+
345
+ if (next === '..' && entryIsDirectory) {
346
+ // In case pattern is "**/..",
347
+ // both parent and current directory should be added to the queue
348
+ // if this is the last pattern, add to results instead
349
+ const parent = join(path, '..')
350
+ if (nextIndex < last) {
351
+ if (!this.#subpatterns.has(path) && !this.#cache.seen(path, pattern, nextIndex + 1)) {
352
+ this.#subpatterns.set(path, [pattern.child(new Set().add(nextIndex + 1))])
353
+ }
354
+ if (!this.#subpatterns.has(parent) && !this.#cache.seen(parent, pattern, nextIndex + 1)) {
355
+ this.#subpatterns.set(parent, [pattern.child(new Set().add(nextIndex + 1))])
356
+ }
357
+ } else {
358
+ if (!this.#cache.seen(path, pattern, nextIndex)) {
359
+ this.#cache.add(path, pattern.child(new Set().add(nextIndex)))
360
+ this.#results.add(path)
361
+ }
362
+ if (!this.#cache.seen(path, pattern, nextIndex) || !this.#cache.seen(parent, pattern, nextIndex)) {
363
+ this.#cache.add(parent, pattern.child(new Set().add(nextIndex)))
364
+ this.#results.add(parent)
365
+ }
366
+ }
367
+ }
368
+ }
369
+ if (typeof current === 'string') {
370
+ if (pattern.test(index, entry.name) && index !== last) {
371
+ // If current pattern matches entry name
372
+ // the next pattern is a potential pattern
373
+ subPatterns.add(nextIndex)
374
+ } else if (current === '.' && pattern.test(nextIndex, entry.name)) {
375
+ // If current pattern is ".", proceed to test next pattern
376
+ if (nextIndex === last) {
377
+ this.#results.add(entryPath)
378
+ } else {
379
+ subPatterns.add(nextIndex + 1)
380
+ }
381
+ }
382
+ }
383
+ if (typeof current === 'object' && pattern.test(index, entry.name)) {
384
+ // If current pattern is a regex that matches entry name (e.g *.js)
385
+ // add next pattern to potential patterns, or to results if it's the last pattern
386
+ if (index === last) {
387
+ this.#results.add(entryPath)
388
+ } else if (entryIsDirectory) {
389
+ subPatterns.add(nextIndex)
390
+ }
391
+ }
392
+ }
393
+ if (subPatterns.size > 0) {
394
+ // If there are potential patterns, add to queue
395
+ this.#addSubpattern(entryPath, pattern.child(subPatterns, nSymlinks))
396
+ }
397
+ }
398
+ }
399
+ }
400
+
401
+ // fs.globSync(pattern, { cwd, exclude }) over `host`, `cwd` an absolute path there: the matches as
402
+ // Node spells them, from `cwd` (or from `/`, of an absolute pattern), never walking into a path
403
+ // `exclude` takes, given its absolute path. As Node's, it leaves out such a path where its `**`
404
+ // meets it alone: one the segment after the `**` matches is still a match.
405
+ export const globSync = (host, pattern, { cwd, exclude } = {}) => new Glob(host, pattern, { cwd, exclude }).globSync()
406
+
407
+ // As Node's matchGlobPattern keeps them: the most recent 250 patterns' matchers.
408
+ const MATCHERS_KEPT = 250
409
+ const matchers = new Map()
410
+
411
+ // path.matchesGlob(path, pattern), never folding case.
412
+ export function matchesGlob(path, pattern) {
413
+ let matcher = matchers.get(pattern)
414
+ if (matcher === undefined) {
415
+ matcher = createMatcher(pattern)
416
+ matchers.set(pattern, matcher)
417
+ if (matchers.size >= MATCHERS_KEPT) matchers.delete(matchers.keys().next().value)
418
+ }
419
+ return matcher.match(path)
420
+ }