@vzn/vx-migrate 0.0.636 → 0.0.638
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +298 -44
- package/package.json +3 -3
- package/src/adopt.ts +25 -8
- package/src/adoption-plugin.ts +2 -2
- package/src/manifest-graph.ts +65 -0
- package/src/manifests.ts +52 -0
- package/src/mapping-cache.ts +5 -9
- package/src/migrate-nx.ts +63 -22
- package/src/migrate-scripts.ts +1261 -0
- package/src/migrate-turbo.ts +6 -7
- package/src/migrate-vite-task.ts +5 -4
- package/src/migrate.ts +474 -173
- package/src/nx/export-graph.ts +15 -4
- package/src/nx/index.ts +82 -23
- package/src/nx/nx-boundaries.ts +191 -0
- package/src/nx/nx-deps.ts +3 -3
- package/src/nx/nx-map.ts +37 -41
- package/src/nx/nx-upstream.ts +3 -2
- package/src/nx-cache/index.ts +5 -11
- package/src/package-fs.ts +152 -0
- package/src/packages.ts +806 -0
- package/src/paths.ts +2 -2
- package/src/plugin-gaps.ts +20 -5
- package/src/remote-cache.ts +118 -0
- package/src/shared-outputs.ts +63 -19
- package/src/tracked-outputs.ts +54 -35
- package/src/turbo/index.ts +32 -20
- package/src/turbo/turbo-map.ts +96 -56
- package/src/turbo-cache/index.ts +14 -20
- package/src/vite-task/vite-task-map.ts +13 -12
- package/src/workspace-notes.ts +12 -6
- package/src/workspace-plugins.ts +41 -11
package/src/nx/nx-map.ts
CHANGED
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
// consumers: `bunx @vzn/vx-migrate --from nx` writes it to files, and the
|
|
3
3
|
// `nx()` plugin runs it live. Static by design: targets that Nx plugins
|
|
4
4
|
// infer at runtime are whatever the graph says. Named inputs expand from
|
|
5
|
-
// nx.json when readable; the graph's dependency edges are ignored (vx
|
|
6
|
-
//
|
|
7
|
-
// report line counting edges with no
|
|
5
|
+
// nx.json when readable; the graph's dependency edges are ignored (vx's
|
|
6
|
+
// package edges come from a lockfile plugin, which installs what the
|
|
7
|
+
// manifests name) except for one report line counting edges with no
|
|
8
|
+
// manifest counterpart.
|
|
8
9
|
//
|
|
9
10
|
// Executors: `nx:run-commands`, a plain `command` and `nx:run-script` are
|
|
10
11
|
// shell; `nx:noop` is a group; an executor the workspace translates
|
|
@@ -16,14 +17,10 @@
|
|
|
16
17
|
// default configuration is folded into the base task.
|
|
17
18
|
|
|
18
19
|
import path from 'node:path'
|
|
19
|
-
import {
|
|
20
|
-
|
|
21
|
-
type GeneratedProject,
|
|
22
|
-
type GeneratedTask,
|
|
23
|
-
type ProjectMeta,
|
|
24
|
-
UserError,
|
|
25
|
-
} from '@vzn/vx'
|
|
20
|
+
import { type Duration, type GeneratedProject, type GeneratedTask, UserError } from '@vzn/vx'
|
|
21
|
+
import type { PackageMeta } from '../packages.js'
|
|
26
22
|
import { withoutTakenBack } from '../glob-grammar.js'
|
|
23
|
+
import { manifestGraph, type ManifestGraph } from '../manifest-graph.js'
|
|
27
24
|
import { mapRunCommands, shellQuote } from '../nx-command.js'
|
|
28
25
|
import { scriptCommand, yarnPnp } from '../script-command.js'
|
|
29
26
|
import {
|
|
@@ -251,8 +248,8 @@ export interface NxExecutorTarget {
|
|
|
251
248
|
/** What a translated target runs as. The command runs in the project dir. */
|
|
252
249
|
export interface NxExecutorTranslation {
|
|
253
250
|
readonly command: string
|
|
254
|
-
/** `exec.timeout`,
|
|
255
|
-
readonly timeout?:
|
|
251
|
+
/** `exec.timeout`, a duration with its unit (`'10m'`). */
|
|
252
|
+
readonly timeout?: Duration
|
|
256
253
|
/** Defined in the task's environment, as a run-commands `env` is. */
|
|
257
254
|
readonly env?: Readonly<Record<string, string>>
|
|
258
255
|
}
|
|
@@ -298,7 +295,7 @@ function normRel(p: string): string {
|
|
|
298
295
|
|
|
299
296
|
export async function mapNxWorkspace(
|
|
300
297
|
root: string,
|
|
301
|
-
metas: readonly
|
|
298
|
+
metas: readonly PackageMeta[],
|
|
302
299
|
graph: NxGraph,
|
|
303
300
|
opts: MapNxOptions,
|
|
304
301
|
): Promise<NxMapping> {
|
|
@@ -324,11 +321,11 @@ export async function mapNxWorkspace(
|
|
|
324
321
|
syncTasks: new Map(),
|
|
325
322
|
}
|
|
326
323
|
|
|
327
|
-
const metaByRel = new Map<string,
|
|
324
|
+
const metaByRel = new Map<string, PackageMeta>()
|
|
328
325
|
for (const meta of metas) metaByRel.set(normRel(relPosix(root, meta.dir)), meta)
|
|
329
|
-
const metaByNode = new Map<string,
|
|
330
|
-
const nodeByMeta = new Map<
|
|
331
|
-
const nodeNameOf = new Map<
|
|
326
|
+
const metaByNode = new Map<string, PackageMeta>()
|
|
327
|
+
const nodeByMeta = new Map<PackageMeta, NxNode>()
|
|
328
|
+
const nodeNameOf = new Map<PackageMeta, string>()
|
|
332
329
|
for (const [nodeName, node] of Object.entries(nodeMap)) {
|
|
333
330
|
const meta = metaByRel.get(normRel(node?.data?.root ?? ''))
|
|
334
331
|
if (meta) {
|
|
@@ -343,16 +340,16 @@ export async function mapNxWorkspace(
|
|
|
343
340
|
// workspace root, but Nx routinely defines root targets. Synthesize
|
|
344
341
|
// a meta for any unmatched node whose root resolves inside the
|
|
345
342
|
// workspace, so its targets migrate too.
|
|
346
|
-
const allMetas:
|
|
343
|
+
const allMetas: PackageMeta[] = [...metas]
|
|
347
344
|
const taken = new Set(metas.map((m) => m.name))
|
|
348
345
|
for (const [nodeName, node] of Object.entries(nodeMap)) {
|
|
349
346
|
if (metaByNode.has(nodeName)) continue
|
|
350
347
|
const relRoot = normRel(node?.data?.root ?? '')
|
|
351
348
|
if (node?.data?.targets === undefined) continue
|
|
352
349
|
const dir = relRoot === '' || relRoot === '.' ? root : path.join(root, relRoot)
|
|
353
|
-
let pkg:
|
|
350
|
+
let pkg: PackageMeta['packageJson'] = { name: nodeName }
|
|
354
351
|
try {
|
|
355
|
-
pkg = (await Bun.file(path.join(dir, 'package.json')).json()) as
|
|
352
|
+
pkg = (await Bun.file(path.join(dir, 'package.json')).json()) as PackageMeta['packageJson']
|
|
356
353
|
} catch {
|
|
357
354
|
// No manifest at the node root — keep the synthetic one.
|
|
358
355
|
}
|
|
@@ -362,7 +359,7 @@ export async function mapNxWorkspace(
|
|
|
362
359
|
const name = [pkg.name, nodeName].find((n) => n && !taken.has(n))
|
|
363
360
|
if (name === undefined) continue
|
|
364
361
|
taken.add(name)
|
|
365
|
-
const synthetic:
|
|
362
|
+
const synthetic: PackageMeta = {
|
|
366
363
|
name,
|
|
367
364
|
dir,
|
|
368
365
|
packageJson: pkg,
|
|
@@ -388,7 +385,7 @@ export async function mapNxWorkspace(
|
|
|
388
385
|
|
|
389
386
|
// Once per project, not per task and variant: `path.relative` was a
|
|
390
387
|
// fifth of the mapping at 1,000 projects (item 608).
|
|
391
|
-
const relOf = new Map<
|
|
388
|
+
const relOf = new Map<PackageMeta, string>()
|
|
392
389
|
for (const meta of allMetas) {
|
|
393
390
|
if (nodeByMeta.get(meta)?.data?.targets) relOf.set(meta, normRel(relPosix(root, meta.dir)))
|
|
394
391
|
}
|
|
@@ -399,7 +396,7 @@ export async function mapNxWorkspace(
|
|
|
399
396
|
? null
|
|
400
397
|
: await listDotenv(root, [...relOf.values()])
|
|
401
398
|
|
|
402
|
-
const mapped: Array<{ meta:
|
|
399
|
+
const mapped: Array<{ meta: PackageMeta; tasks: GeneratedTask[] }> = []
|
|
403
400
|
let releasePublish = 0
|
|
404
401
|
// A configuration variant's task (`build:production`) and what it runs.
|
|
405
402
|
const configured = new WeakMap<GeneratedTask, string>()
|
|
@@ -521,11 +518,11 @@ export async function mapNxWorkspace(
|
|
|
521
518
|
nxClosureMemo.set(from, (r = seen))
|
|
522
519
|
return r
|
|
523
520
|
}
|
|
524
|
-
// What vx's `^` already reaches: a manifest path
|
|
525
|
-
// another project, orders `^x` and folds its
|
|
526
|
-
// on the first `^` edge.
|
|
527
|
-
let pkgGraph:
|
|
528
|
-
const packageGraph = () => (pkgGraph ??=
|
|
521
|
+
// What vx's `^` already reaches: a manifest path (the lockfile plugin's
|
|
522
|
+
// edges), direct or through another project, orders `^x` and folds its
|
|
523
|
+
// key (item 931). Built once, on the first `^` edge.
|
|
524
|
+
let pkgGraph: ManifestGraph | undefined
|
|
525
|
+
const packageGraph = () => (pkgGraph ??= manifestGraph(metas))
|
|
529
526
|
let reach: ((name: string) => ReadonlySet<string>) | undefined
|
|
530
527
|
const reached = (name: string): ReadonlySet<string> => {
|
|
531
528
|
if (reach === undefined) {
|
|
@@ -776,7 +773,7 @@ function allTargetNames(nodeMap: Readonly<Record<string, NxNode>>): string[] {
|
|
|
776
773
|
}
|
|
777
774
|
|
|
778
775
|
function buildTask(
|
|
779
|
-
meta:
|
|
776
|
+
meta: PackageMeta,
|
|
780
777
|
projectRel: string,
|
|
781
778
|
scripts: Record<string, unknown>,
|
|
782
779
|
projectName: string,
|
|
@@ -785,7 +782,7 @@ function buildTask(
|
|
|
785
782
|
variant: Variant,
|
|
786
783
|
nodeName: string,
|
|
787
784
|
upstream: NxUpstream,
|
|
788
|
-
metaByNode: ReadonlyMap<string,
|
|
785
|
+
metaByNode: ReadonlyMap<string, PackageMeta>,
|
|
789
786
|
nodeMap: Readonly<Record<string, NxNode>>,
|
|
790
787
|
taskNameFor: TaskNameFor,
|
|
791
788
|
opts: MapOpts,
|
|
@@ -874,9 +871,8 @@ function buildTask(
|
|
|
874
871
|
const readyWhen = mapped?.readyWhen
|
|
875
872
|
const persistent = readyWhen !== undefined || persistentTarget(target)
|
|
876
873
|
// vx cleans outputs before a run and Nx never does. With git's file list
|
|
877
|
-
// known, a wildcard-first output (`**/*.d.ts`) stays cached:
|
|
878
|
-
// committed file
|
|
879
|
-
// (`spareTrackedOutputs`), and what the clean removes is what the task
|
|
874
|
+
// known, a wildcard-first output (`**/*.d.ts`) stays cached: core never
|
|
875
|
+
// cleans a committed file, so what the clean removes is what the task
|
|
880
876
|
// writes (owner, 2026-10-03). Without it, the sources cannot be told apart.
|
|
881
877
|
const wild =
|
|
882
878
|
opts.tracked === undefined
|
|
@@ -1023,7 +1019,7 @@ interface MappedCommand {
|
|
|
1023
1019
|
/** The `.env` files the command loads, relative to the project dir: key inputs. */
|
|
1024
1020
|
readonly envInputs: readonly string[]
|
|
1025
1021
|
/** A translated executor's `exec.timeout`. */
|
|
1026
|
-
readonly timeout?:
|
|
1022
|
+
readonly timeout?: Duration
|
|
1027
1023
|
/** The Nx targets a leading `nx <target> <project>` ran: dependency edges (nx-command.ts). */
|
|
1028
1024
|
readonly nxCalls?: readonly { readonly project: string; readonly target: string }[]
|
|
1029
1025
|
}
|
|
@@ -1169,7 +1165,7 @@ function mapCommand(
|
|
|
1169
1165
|
function translation(
|
|
1170
1166
|
t: unknown,
|
|
1171
1167
|
at: string,
|
|
1172
|
-
): { command: string; env: Record<string, unknown>; timeout:
|
|
1168
|
+
): { command: string; env: Record<string, unknown>; timeout: Duration | undefined } {
|
|
1173
1169
|
if (!isRecord(t)) throw new UserError(`${at}: must return { command } or undefined`)
|
|
1174
1170
|
for (const key of Object.keys(t))
|
|
1175
1171
|
if (key !== 'command' && key !== 'timeout' && key !== 'env')
|
|
@@ -1179,13 +1175,13 @@ function translation(
|
|
|
1179
1175
|
const timeout = t.timeout
|
|
1180
1176
|
if (
|
|
1181
1177
|
timeout !== undefined &&
|
|
1182
|
-
!(typeof timeout === '
|
|
1178
|
+
!(typeof timeout === 'string' && /^[1-9]\d*(ms|s|m|h|d)$/.test(timeout))
|
|
1183
1179
|
)
|
|
1184
|
-
throw new UserError(`${at}: timeout must be a
|
|
1180
|
+
throw new UserError(`${at}: timeout must be a duration with its unit, like '30s' or '10m'`)
|
|
1185
1181
|
const env = t.env ?? {}
|
|
1186
1182
|
if (!isRecord(env) || Object.values(env).some((v) => typeof v !== 'string'))
|
|
1187
1183
|
throw new UserError(`${at}: env must map names to strings`)
|
|
1188
|
-
return { command: t.command, env, timeout }
|
|
1184
|
+
return { command: t.command, env, timeout: timeout as Duration | undefined }
|
|
1189
1185
|
}
|
|
1190
1186
|
|
|
1191
1187
|
/**
|
|
@@ -1354,7 +1350,7 @@ function passConfiguration(
|
|
|
1354
1350
|
nodeName: string,
|
|
1355
1351
|
configuration: string,
|
|
1356
1352
|
nxEdges: (from: string, target: string) => readonly string[],
|
|
1357
|
-
metaByNode: ReadonlyMap<string,
|
|
1353
|
+
metaByNode: ReadonlyMap<string, PackageMeta>,
|
|
1358
1354
|
emittedIds: ReadonlySet<string>,
|
|
1359
1355
|
taskNameFor: TaskNameFor,
|
|
1360
1356
|
): void {
|
|
@@ -1394,7 +1390,7 @@ function narrowToNxGraph(
|
|
|
1394
1390
|
pkgName: string,
|
|
1395
1391
|
nxClosure: (node: string) => ReadonlySet<string>,
|
|
1396
1392
|
nxEdges: (from: string, target: string) => readonly string[],
|
|
1397
|
-
metaByNode: ReadonlyMap<string,
|
|
1393
|
+
metaByNode: ReadonlyMap<string, PackageMeta>,
|
|
1398
1394
|
emittedIds: ReadonlySet<string>,
|
|
1399
1395
|
directDeps: (pkg: string) => readonly string[],
|
|
1400
1396
|
): void {
|
|
@@ -1436,7 +1432,7 @@ function followNxGraph(
|
|
|
1436
1432
|
nodeName: string,
|
|
1437
1433
|
pkgName: string,
|
|
1438
1434
|
nxEdges: (from: string, target: string) => readonly string[],
|
|
1439
|
-
metaByNode: ReadonlyMap<string,
|
|
1435
|
+
metaByNode: ReadonlyMap<string, PackageMeta>,
|
|
1440
1436
|
emittedIds: ReadonlySet<string>,
|
|
1441
1437
|
reached: (name: string) => ReadonlySet<string>,
|
|
1442
1438
|
): void {
|
package/src/nx/nx-upstream.ts
CHANGED
|
@@ -16,7 +16,8 @@
|
|
|
16
16
|
// goes on through it. Nx allows a project cycle and vx's task graph does
|
|
17
17
|
// not: inside a cycle a twin carries its peers' files as workspace globs
|
|
18
18
|
// and edges only out of the cycle.
|
|
19
|
-
import type { GeneratedTask
|
|
19
|
+
import type { GeneratedTask } from '@vzn/vx'
|
|
20
|
+
import type { PackageMeta } from '../packages.js'
|
|
20
21
|
import { emptyNxInputs, expandNxInputs, type NxInputs } from './nx-inputs.js'
|
|
21
22
|
import { nxProjectOutputs } from './nx-outputs.js'
|
|
22
23
|
import { matchNxProjects } from './nx-deps.js'
|
|
@@ -70,7 +71,7 @@ export function planNxUpstream(
|
|
|
70
71
|
nodes: Readonly<Record<string, GraphNode>>,
|
|
71
72
|
dependencies: unknown,
|
|
72
73
|
workspaceNamed: Readonly<Record<string, unknown[]>> | null,
|
|
73
|
-
metaByNode: ReadonlyMap<string,
|
|
74
|
+
metaByNode: ReadonlyMap<string, PackageMeta>,
|
|
74
75
|
): NxUpstream {
|
|
75
76
|
const isProject = (n: string): boolean => typeof nodes[n]?.data?.root === 'string'
|
|
76
77
|
const projectTags = Object.entries(nodes).map(([name, n]) => ({
|
package/src/nx-cache/index.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
// The wire is Nx's; the bytes are vx's own artifacts under vx's own keys,
|
|
6
6
|
// so the server is storage — an Nx binary cannot read them.
|
|
7
7
|
//
|
|
8
|
-
// Nothing is on by default: declare `nxCache()` in
|
|
8
|
+
// Nothing is on by default: declare `nxCache()` in `.vx/workspace.ts` (the
|
|
9
9
|
// local store is the floor beneath it) and give it a server (options, or Nx's own
|
|
10
10
|
// environment variables so a self-hosted setup carries over). Without one
|
|
11
11
|
// the plugin DECLINES and the run stays local.
|
|
@@ -13,8 +13,6 @@
|
|
|
13
13
|
// Imports core only through the public `@vzn/vx` specifier.
|
|
14
14
|
import {
|
|
15
15
|
definePlugin,
|
|
16
|
-
LayeredCache,
|
|
17
|
-
type CacheLayer,
|
|
18
16
|
type RemoteCacheLayer,
|
|
19
17
|
type VxPlugin,
|
|
20
18
|
refuseUnknownOptions,
|
|
@@ -95,7 +93,7 @@ export function resolveNxCacheConfig(
|
|
|
95
93
|
* `get` of the same hash, which no caller makes. Bodies stream: `get` returns the `fetch` Response and `put`
|
|
96
94
|
* sends the Blob core hands it. `put` treats `409` as success:
|
|
97
95
|
* the record is immutable and content-addressed, so "already there" is the
|
|
98
|
-
* outcome wanted. An auth failure (401/403) throws ONCE —
|
|
96
|
+
* outcome wanted. An auth failure (401/403) throws ONCE — core
|
|
99
97
|
* reports it — and then turns the layer off for the rest of the process
|
|
100
98
|
* (a write's 403 turns off writes alone: the read-only token);
|
|
101
99
|
* the requests already in flight when it lands degrade in silence rather
|
|
@@ -220,7 +218,7 @@ const NX_CACHE_KEYS: PluginOptionKinds<NxCacheOptions> = {
|
|
|
220
218
|
}
|
|
221
219
|
|
|
222
220
|
/**
|
|
223
|
-
* Declare in
|
|
221
|
+
* Declare in `.vx/workspace.ts`; the local store stays the floor beneath it:
|
|
224
222
|
*
|
|
225
223
|
* ```ts
|
|
226
224
|
* plugins: [nxCache({ server: 'https://cache.example.com', accessToken: process.env.CACHE_TOKEN })]
|
|
@@ -231,13 +229,9 @@ const NX_CACHE_KEYS: PluginOptionKinds<NxCacheOptions> = {
|
|
|
231
229
|
export function nxCache(options: NxCacheOptions = {}): VxPlugin {
|
|
232
230
|
refuseUnknownOptions('nxCache()', options, NX_CACHE_KEYS)
|
|
233
231
|
return definePlugin(import.meta, {
|
|
234
|
-
cache(
|
|
232
|
+
cache(): RemoteCacheLayer | undefined {
|
|
235
233
|
const config = resolveNxCacheConfig(options)
|
|
236
|
-
|
|
237
|
-
return new LayeredCache(ctx.localCache, new NxRemoteCache(config), {
|
|
238
|
-
policy: ctx.policy,
|
|
239
|
-
onRemoteError: (err) => ctx.warn(`vx/nx-cache: ${err.message}`),
|
|
240
|
-
})
|
|
234
|
+
return config === undefined ? undefined : new NxRemoteCache(config)
|
|
241
235
|
},
|
|
242
236
|
})
|
|
243
237
|
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// What packages.ts needs of the file system and of globs, as core has
|
|
2
|
+
// them: core's copies are not on the façade, and these left core with the
|
|
3
|
+
// package-manager discovery (M, 2026-10-14).
|
|
4
|
+
|
|
5
|
+
import { readlinkSync, realpathSync } from 'node:fs'
|
|
6
|
+
import path from 'node:path'
|
|
7
|
+
import { UserError } from '@vzn/vx'
|
|
8
|
+
|
|
9
|
+
/** An `EACCES`, `EPERM` or `EROFS`: not this user's to read or write. */
|
|
10
|
+
export function isPermissionError(err: unknown): err is NodeJS.ErrnoException {
|
|
11
|
+
if (!(err instanceof Error)) return false
|
|
12
|
+
const code = (err as NodeJS.ErrnoException).code
|
|
13
|
+
return code === 'EACCES' || code === 'EPERM' || code === 'EROFS'
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** `EMFILE` or `ENFILE`: out of file descriptors, never "no file here". */
|
|
17
|
+
export function isOutOfFds(err: unknown): err is NodeJS.ErrnoException {
|
|
18
|
+
if (!(err instanceof Error)) return false
|
|
19
|
+
const code = (err as NodeJS.ErrnoException).code
|
|
20
|
+
return code === 'EMFILE' || code === 'ENFILE'
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** `Bun.Glob`'s own alphabet, the class included: member globs follow npm/pnpm. */
|
|
24
|
+
export const BUN_GLOB_WILDCARDS = /[*?[\]{}]/
|
|
25
|
+
|
|
26
|
+
/** An extglob group (`!(…)`, `@(…)`, …), which `Bun.Glob` does not read. */
|
|
27
|
+
export const EXTGLOB = /[!@+*?]\(/
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The spellings npm and pnpm accept that a matcher fed the raw string
|
|
31
|
+
* turns into nothing: a leading `./`, an inner `/./`, a doubled `//`, and a
|
|
32
|
+
* trailing `/` on a pattern. Applied after an optional `!`.
|
|
33
|
+
*/
|
|
34
|
+
export function normalizeBunGlob(glob: string): string {
|
|
35
|
+
const neg = glob.startsWith('!')
|
|
36
|
+
let g = neg ? glob.slice(1) : glob
|
|
37
|
+
g = g.replace(/\/{2,}/g, '/').replace(/(^|\/)(\.\/)+/g, '$1')
|
|
38
|
+
if (g === '.') g = ''
|
|
39
|
+
if (BUN_GLOB_WILDCARDS.test(g) && g.endsWith('/')) g = `${g.replace(/\/+$/, '')}/**`
|
|
40
|
+
return neg ? `!${g}` : g
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* `pattern` with its first brace group expanded, recursively, while that
|
|
45
|
+
* group holds a `/`: `Bun.Glob`'s scan finds nothing for one.
|
|
46
|
+
*/
|
|
47
|
+
export function slashBraceExpansions(pattern: string): string[] {
|
|
48
|
+
if (pattern.includes('\\')) return [pattern]
|
|
49
|
+
for (let open = pattern.indexOf('{'); open !== -1; open = pattern.indexOf('{', open + 1)) {
|
|
50
|
+
let depth = 0
|
|
51
|
+
const cuts: number[] = []
|
|
52
|
+
let close = -1
|
|
53
|
+
for (let i = open; i < pattern.length && close === -1; i++) {
|
|
54
|
+
const c = pattern[i]
|
|
55
|
+
if (c === '{') depth++
|
|
56
|
+
else if (c === '}' && --depth === 0) close = i
|
|
57
|
+
else if (c === ',' && depth === 1) cuts.push(i)
|
|
58
|
+
}
|
|
59
|
+
if (close === -1) return [pattern]
|
|
60
|
+
if (cuts.length === 0 || !pattern.slice(open + 1, close).includes('/')) continue
|
|
61
|
+
const head = pattern.slice(0, open)
|
|
62
|
+
const tail = pattern.slice(close + 1)
|
|
63
|
+
const bounds = [open, ...cuts, close]
|
|
64
|
+
const out: string[] = []
|
|
65
|
+
for (let k = 0; k + 1 < bounds.length; k++) {
|
|
66
|
+
out.push(...slashBraceExpansions(head + pattern.slice(bounds[k]! + 1, bounds[k + 1]) + tail))
|
|
67
|
+
}
|
|
68
|
+
return [...new Set(out)]
|
|
69
|
+
}
|
|
70
|
+
return [pattern]
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* `realpathSync`, for a path holding a backslash too: Bun's answers ENOENT
|
|
75
|
+
* for any such path (1.4.2), so its links are read one by one.
|
|
76
|
+
*/
|
|
77
|
+
export function realpathOf(p: string): string {
|
|
78
|
+
try {
|
|
79
|
+
return realpathSync(p)
|
|
80
|
+
} catch (err) {
|
|
81
|
+
if (!p.includes('\\')) throw err
|
|
82
|
+
// Not `path.resolve`: it folds `link/..` lexically, and the kernel
|
|
83
|
+
// takes `..` from where the link leads.
|
|
84
|
+
const abs = path.isAbsolute(p) ? p : `${process.cwd()}${path.sep}${p}`
|
|
85
|
+
let at = path.parse(abs).root
|
|
86
|
+
const pending = abs.slice(at.length).split(path.sep).filter(Boolean).reverse()
|
|
87
|
+
let hops = 0
|
|
88
|
+
while (pending.length > 0) {
|
|
89
|
+
const next = path.join(at, pending.pop()!)
|
|
90
|
+
let link: string
|
|
91
|
+
try {
|
|
92
|
+
link = readlinkSync(next)
|
|
93
|
+
} catch (e) {
|
|
94
|
+
if ((e as NodeJS.ErrnoException).code !== 'EINVAL') throw e
|
|
95
|
+
at = next
|
|
96
|
+
continue
|
|
97
|
+
}
|
|
98
|
+
// Linux's own bound on links followed in one lookup (MAXSYMLINKS).
|
|
99
|
+
if (++hops > 40) {
|
|
100
|
+
throw Object.assign(
|
|
101
|
+
new Error(`ELOOP: too many symbolic links encountered, realpath '${p}'`),
|
|
102
|
+
{
|
|
103
|
+
code: 'ELOOP',
|
|
104
|
+
},
|
|
105
|
+
)
|
|
106
|
+
}
|
|
107
|
+
if (path.isAbsolute(link)) at = path.parse(link).root
|
|
108
|
+
pending.push(...link.split('/').filter(Boolean).reverse())
|
|
109
|
+
}
|
|
110
|
+
return at
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* The git top-level holding `dir`, or null outside git: the nearest `.git`
|
|
116
|
+
* up from it. A submodule's `.git` file points into its superproject's
|
|
117
|
+
* `.git/modules`, so the walk goes on to the superproject's.
|
|
118
|
+
*/
|
|
119
|
+
export async function gitTop(dir: string): Promise<string | null> {
|
|
120
|
+
for (let d = dir; ; d = d.slice(0, d.lastIndexOf('/'))) {
|
|
121
|
+
if (await Bun.file(`${d}/.git/HEAD`).exists()) return d || '/'
|
|
122
|
+
const file = Bun.file(`${d}/.git`)
|
|
123
|
+
if (await file.exists()) {
|
|
124
|
+
const gitdir = await file.text().catch(() => '')
|
|
125
|
+
if (!/(^|[\s/])\.git\/modules\//.test(gitdir)) return d || '/'
|
|
126
|
+
}
|
|
127
|
+
if (d === '') return null
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** The files one discovery has read, by absolute path: the bytes, or null when none is there. */
|
|
132
|
+
export type LoadReads = Map<string, Promise<Uint8Array | null>>
|
|
133
|
+
|
|
134
|
+
/** `file`'s bytes, read at most once per `reads`; null when there is no file there. */
|
|
135
|
+
export function readOnce(reads: LoadReads | undefined, file: string): Promise<Uint8Array | null> {
|
|
136
|
+
const held = reads?.get(file)
|
|
137
|
+
if (held !== undefined) return held
|
|
138
|
+
const f = Bun.file(file)
|
|
139
|
+
const pending = f
|
|
140
|
+
.exists()
|
|
141
|
+
.then((there) => (there ? f.bytes().catch((err: unknown) => unreadable(err, file)) : null))
|
|
142
|
+
reads?.set(file, pending)
|
|
143
|
+
return pending
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** A read the file system refused, named as a read; anything else rethrown. */
|
|
147
|
+
export function unreadable(err: unknown, file: string): never {
|
|
148
|
+
if (isPermissionError(err)) {
|
|
149
|
+
throw new UserError(`${file}: not readable by this user (${err.code})`)
|
|
150
|
+
}
|
|
151
|
+
throw err
|
|
152
|
+
}
|