@packall/core 0.1.0 → 0.2.0

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 (95) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +58 -0
  3. package/dist/archive-6sqTn1C4.js +274 -0
  4. package/dist/archive-6sqTn1C4.js.map +1 -0
  5. package/dist/archive.d.ts +47 -15
  6. package/dist/archive.d.ts.map +1 -1
  7. package/dist/bundle.d.ts +6 -3
  8. package/dist/bundle.d.ts.map +1 -1
  9. package/dist/dependency-range.d.ts.map +1 -1
  10. package/dist/download.d.ts +2 -1
  11. package/dist/download.d.ts.map +1 -1
  12. package/dist/enums/artifact-kind.d.ts.map +1 -1
  13. package/dist/enums/edge-kind.d.ts.map +1 -1
  14. package/dist/enums/input-file-kind.d.ts.map +1 -1
  15. package/dist/enums/layout.d.ts.map +1 -1
  16. package/dist/enums/lockfile-format.d.ts.map +1 -1
  17. package/dist/enums/phase.d.ts.map +1 -1
  18. package/dist/errors.d.ts.map +1 -1
  19. package/dist/index.d.ts +12 -10
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +169 -344
  22. package/dist/index.js.map +1 -1
  23. package/dist/input-file.d.ts.map +1 -1
  24. package/dist/integrity.d.ts +37 -5
  25. package/dist/integrity.d.ts.map +1 -1
  26. package/dist/layout.d.ts +41 -7
  27. package/dist/layout.d.ts.map +1 -1
  28. package/dist/locked-resolve.d.ts.map +1 -1
  29. package/dist/lockfile/bun.d.ts.map +1 -1
  30. package/dist/lockfile/detect.d.ts.map +1 -1
  31. package/dist/lockfile/json.d.ts.map +1 -1
  32. package/dist/lockfile/names.d.ts.map +1 -1
  33. package/dist/lockfile/npm.d.ts.map +1 -1
  34. package/dist/lockfile/parse.d.ts.map +1 -1
  35. package/dist/lockfile/pnpm.d.ts.map +1 -1
  36. package/dist/lockfile/tree-builder.d.ts.map +1 -1
  37. package/dist/lockfile/types.d.ts.map +1 -1
  38. package/dist/manifest.d.ts.map +1 -1
  39. package/dist/node/archiver.d.ts +5 -0
  40. package/dist/node/archiver.d.ts.map +1 -0
  41. package/dist/node/index.d.ts +13 -0
  42. package/dist/node/index.d.ts.map +1 -0
  43. package/dist/node/index.js +55 -0
  44. package/dist/node/index.js.map +1 -0
  45. package/dist/options.d.ts +3 -3
  46. package/dist/options.d.ts.map +1 -1
  47. package/dist/platform.d.ts +5 -10
  48. package/dist/platform.d.ts.map +1 -1
  49. package/dist/progress.d.ts.map +1 -1
  50. package/dist/registry.d.ts.map +1 -1
  51. package/dist/resolve.d.ts.map +1 -1
  52. package/dist/schemas/lenient.d.ts.map +1 -1
  53. package/dist/schemas/package-json.d.ts.map +1 -1
  54. package/dist/spec.d.ts.map +1 -1
  55. package/dist/types/value-of.d.ts.map +1 -1
  56. package/dist/utils/is-record.d.ts.map +1 -1
  57. package/package.json +11 -10
  58. package/src/archive.ts +95 -92
  59. package/src/bundle.ts +736 -616
  60. package/src/dependency-range.ts +121 -115
  61. package/src/download.ts +155 -133
  62. package/src/enums/artifact-kind.ts +5 -5
  63. package/src/enums/edge-kind.ts +13 -13
  64. package/src/enums/input-file-kind.ts +9 -9
  65. package/src/enums/layout.ts +20 -20
  66. package/src/enums/lockfile-format.ts +6 -6
  67. package/src/enums/phase.ts +8 -8
  68. package/src/errors.ts +200 -198
  69. package/src/index.ts +126 -113
  70. package/src/input-file.ts +246 -234
  71. package/src/integrity.ts +165 -121
  72. package/src/layout.ts +63 -29
  73. package/src/locked-resolve.ts +471 -461
  74. package/src/lockfile/bun.ts +214 -207
  75. package/src/lockfile/detect.ts +80 -80
  76. package/src/lockfile/json.ts +91 -92
  77. package/src/lockfile/names.ts +9 -9
  78. package/src/lockfile/npm.ts +328 -308
  79. package/src/lockfile/parse.ts +32 -32
  80. package/src/lockfile/pnpm.ts +230 -225
  81. package/src/lockfile/tree-builder.ts +116 -116
  82. package/src/lockfile/types.ts +52 -52
  83. package/src/manifest.ts +129 -133
  84. package/src/node/archiver.ts +85 -0
  85. package/src/node/index.ts +13 -0
  86. package/src/options.ts +84 -84
  87. package/src/platform.ts +97 -101
  88. package/src/progress.ts +105 -106
  89. package/src/registry.ts +116 -115
  90. package/src/resolve.ts +560 -537
  91. package/src/schemas/lenient.ts +67 -60
  92. package/src/schemas/package-json.ts +11 -10
  93. package/src/spec.ts +123 -119
  94. package/src/types/value-of.ts +1 -1
  95. package/src/utils/is-record.ts +1 -1
package/src/bundle.ts CHANGED
@@ -1,616 +1,736 @@
1
- /**
2
- * End-to-end orchestration: specs in, tarballs out.
3
- *
4
- * Two properties this module is responsible for, both of them fixes for how
5
- * the original tool behaved:
6
- *
7
- * 1. **Nothing is written outside the output directory.** Every intermediate
8
- * file lives in a scoped temp directory obtained from
9
- * `makeTempDirectoryScoped`, so the scope closing on success, on failure,
10
- * or on Ctrl-C — takes the whole staging tree with it. The working
11
- * directory is never touched.
12
- *
13
- * 2. **Each package is downloaded exactly once**, no matter how many bundles
14
- * it ends up in. Per-spec archives are assembled by hard-linking out of one
15
- * shared download tree, so `per-spec` costs extra disk in the *output*, not
16
- * extra network.
17
- */
18
- import * as Effect from "effect/Effect"
19
- import * as FileSystem from "effect/FileSystem"
20
- import * as Path from "effect/Path"
21
- import type { PlatformError } from "effect/PlatformError"
22
- import type { Scope } from "effect/Scope"
23
- import { createArchive } from "./archive.js"
24
- import { downloadAll } from "./download.js"
25
- import { ArtifactKind } from "./enums/artifact-kind.js"
26
- import { Layout } from "./enums/layout.js"
27
- import { Phase } from "./enums/phase.js"
28
- import type { BundlerError } from "./errors.js"
29
- import { OutputError } from "./errors.js"
30
- import {
31
- importGuide,
32
- MANIFEST_FILE,
33
- packagePath,
34
- perSpecArchiveName,
35
- README_FILE,
36
- singleArchiveName
37
- } from "./layout.js"
38
- import type { LockedResolveOptions } from "./locked-resolve.js"
39
- import { resolveLocked } from "./locked-resolve.js"
40
- import type { LockedTree } from "./lockfile/types.js"
41
- import { buildManifest, serializeManifest } from "./manifest.js"
42
- import type { BundleOptions } from "./options.js"
43
- import * as Progress from "./progress.js"
44
- import { Registry } from "./registry.js"
45
- import type { PackageKey, Resolution, ResolvedPackage } from "./resolve.js"
46
- import { indexPackages, resolve } from "./resolve.js"
47
- import type { PackageSpec } from "./spec.js"
48
- import { formatSpec } from "./spec.js"
49
-
50
- /** One thing written to the output directory. */
51
- export type BundleArtifact = {
52
- readonly kind: ArtifactKind
53
- readonly path: string
54
- readonly bytes: number
55
- readonly packageCount: number
56
- /** Which spec this artifact covers. Absent for `single` and `dir`. */
57
- readonly spec?: string | undefined
58
- readonly version?: string | undefined
59
- }
60
-
61
- /** The outcome of a bundle run. */
62
- export type BundleResult = {
63
- readonly resolution: Resolution
64
- readonly artifacts: ReadonlyArray<BundleArtifact>
65
- /** Total bytes downloaded from the registry. */
66
- readonly downloadedBytes: number
67
- /** Packages the registry advertised no usable checksum for. */
68
- readonly unverified: ReadonlyArray<string>
69
- readonly dryRun: boolean
70
- }
71
-
72
- /** Everything `bundle` needs beyond the user-facing options. */
73
- export type BundleContext = BundleOptions & {
74
- /** Recorded in the manifest so a bundle can be traced to the tool that built it. */
75
- readonly toolVersion: string
76
- }
77
-
78
- /**
79
- * A summary of what a resolution would produce, for deciding whether the
80
- * chosen layout is still the right one.
81
- */
82
- export type PlanSummary = {
83
- /** Distinct packages in the deduplicated union. */
84
- readonly uniquePackages: number
85
- /** How many archives `per-spec` layout would emit. */
86
- readonly perSpecArchives: number
87
- /**
88
- * Total package entries across all per-spec archives, counting shared
89
- * dependencies once per archive they appear in. The gap between this and
90
- * `uniquePackages` is exactly what `single` layout would save.
91
- */
92
- readonly perSpecEntries: number
93
- }
94
-
95
- /**
96
- * The files a run will write, known before anything is downloaded.
97
- *
98
- * `dir` layout is the odd one: it merges a package tree into `outDir`, and
99
- * package paths are `name/-/name-version.tgz` — the same package at the same
100
- * version is the same bytes, so an overlap there is idempotent rather than
101
- * destructive. Only the two summary files are genuinely replaced, and those are
102
- * the ones worth guarding.
103
- */
104
- export type PlannedOutput = {
105
- readonly file: string
106
- /** The root this file belongs to, when one file corresponds to one spec. */
107
- readonly name?: string | undefined
108
- readonly version?: string | undefined
109
- }
110
-
111
- type BundleEnv = Registry | Progress.Progress | FileSystem.FileSystem | Path.Path
112
-
113
- type EmitInput = {
114
- readonly resolution: Resolution
115
- readonly options: BundleContext
116
- readonly packagesDir: string
117
- readonly report: { readonly sizes: ReadonlyMap<string, number> }
118
- readonly registryInfo: { readonly kind: string; readonly url: string }
119
- }
120
-
121
- /** Computes the plan summary for a resolution. */
122
- export const summarize = (resolution: Resolution): PlanSummary => {
123
- let perSpecArchives = 0
124
- let perSpecEntries = 0
125
- for (const root of resolution.roots) {
126
- for (const version of root.versions) {
127
- perSpecArchives += 1
128
- perSpecEntries += (root.closures.get(version) ?? []).length
129
- }
130
- }
131
- return {
132
- uniquePackages: resolution.packages.length,
133
- perSpecArchives,
134
- perSpecEntries
135
- }
136
- }
137
-
138
- /** Replaces the layout on a set of options. */
139
- export const withLayout = <T extends BundleOptions>(options: T, layout: Layout): T => ({
140
- ...options,
141
- layout
142
- })
143
-
144
- /**
145
- * Checks the registry is reachable, then resolves every spec.
146
- *
147
- * Split out from `bundle` so a caller can inspect the plan — how many packages,
148
- * how many archives — and act on it before any bytes move. The CLI uses this to
149
- * offer a different layout when a run turns out to be much larger than
150
- * expected.
151
- */
152
- export const plan = (
153
- specs: ReadonlyArray<PackageSpec>,
154
- options: BundleOptions
155
- ): Effect.Effect<Resolution, BundlerError, Registry | Progress.Progress> =>
156
- Effect.gen(function*() {
157
- yield* preflight
158
- return yield* resolve(specs, options)
159
- })
160
-
161
- /**
162
- * The lockfile counterpart of `plan`.
163
- *
164
- * Same preflight, same `Resolution` out; the difference is entirely in how the
165
- * package set is arrived at. Kept as a separate entry point rather than an
166
- * option on `plan` because the two take genuinely different inputs — a set of
167
- * specs to satisfy versus a graph to reproduce — and blurring that is how a
168
- * "pinned" run quietly starts resolving ranges again.
169
- */
170
- export const planLocked = (
171
- lockfile: LockedTree,
172
- options: BundleOptions & LockedResolveOptions
173
- ): Effect.Effect<Resolution, BundlerError, Registry | Progress.Progress> =>
174
- Effect.gen(function*() {
175
- yield* preflight
176
- return yield* resolveLocked(lockfile, options)
177
- })
178
-
179
- const preflight: Effect.Effect<void, BundlerError, Registry | Progress.Progress> = Effect.gen(
180
- function*() {
181
- const registry = yield* Registry
182
- yield* Progress.emit({ _tag: "PhaseStarted", phase: Phase.Preflight })
183
- yield* registry.preflight
184
- yield* Progress.emit({ _tag: "PhaseCompleted", phase: Phase.Preflight })
185
- }
186
- )
187
-
188
- /**
189
- * Resolves, downloads and packages a set of specs.
190
- *
191
- * Under `dryRun` this stops after resolution and reports the plan — useful for
192
- * checking what a command *would* pull before committing to a multi-gigabyte
193
- * download over a slow VPN.
194
- */
195
- export const bundle = (
196
- specs: ReadonlyArray<PackageSpec>,
197
- options: BundleContext
198
- ): Effect.Effect<BundleResult, BundlerError | PlatformError, BundleEnv> =>
199
- Effect.gen(function*() {
200
- const resolution = yield* plan(specs, options)
201
- return yield* bundleResolved(resolution, options)
202
- })
203
-
204
- /** `bundle`, pinned to a lockfile. */
205
- export const bundleLocked = (
206
- lockfile: LockedTree,
207
- options: BundleContext & LockedResolveOptions
208
- ): Effect.Effect<BundleResult, BundlerError | PlatformError, BundleEnv> =>
209
- Effect.gen(function*() {
210
- const resolution = yield* planLocked(lockfile, options)
211
- return yield* bundleResolved(resolution, options)
212
- })
213
-
214
- /**
215
- * Downloads and packages an already-computed resolution.
216
- *
217
- * Everything from here on touches the disk, and all of it happens inside
218
- * `Effect.scoped` — so the staging tree's lifetime is exactly this call,
219
- * including when it fails partway through or the user hits Ctrl-C.
220
- */
221
- export const bundleResolved = (
222
- resolution: Resolution,
223
- options: BundleContext
224
- ): Effect.Effect<BundleResult, BundlerError | PlatformError, BundleEnv> =>
225
- Effect.gen(function*() {
226
- if (options.dryRun) {
227
- return {
228
- resolution,
229
- artifacts: [],
230
- downloadedBytes: 0,
231
- unverified: [],
232
- dryRun: true
233
- }
234
- }
235
- return yield* Effect.scoped(runBundle(resolution, options))
236
- })
237
-
238
- /**
239
- * The files a run will write.
240
- *
241
- * Computed before anything is downloaded, so a collision is reported in the
242
- * first second rather than after a twenty-minute transfer.
243
- */
244
- export const plannedOutputs = (
245
- resolution: Resolution,
246
- options: BundleContext
247
- ): ReadonlyArray<PlannedOutput> => {
248
- if (options.layout === Layout.Dir) return [{ file: MANIFEST_FILE }, { file: README_FILE }]
249
- if (options.layout === Layout.Single) {
250
- return [{ file: singleArchiveName(options.archiveName ?? "bundle") }]
251
- }
252
- return resolution.roots.flatMap((root) =>
253
- root.versions.map((version) => ({
254
- file: perSpecArchiveName(root.spec.name, version),
255
- name: root.spec.name,
256
- version
257
- }))
258
- )
259
- }
260
-
261
- const runBundle = (
262
- resolution: Resolution,
263
- options: BundleContext
264
- ): Effect.Effect<
265
- BundleResult,
266
- BundlerError | PlatformError,
267
- BundleEnv | Scope
268
- > =>
269
- Effect.gen(function*() {
270
- const fs = yield* FileSystem.FileSystem
271
- const path = yield* Path.Path
272
- const registry = yield* Registry
273
-
274
- yield* prepareOutputDir(resolution, options)
275
-
276
- const staging = yield* fs.makeTempDirectoryScoped({ prefix: "packall-" })
277
- const packagesDir = path.join(staging, "packages")
278
- yield* fs.makeDirectory(packagesDir, { recursive: true })
279
-
280
- const report = yield* downloadAll(resolution.packages, packagesDir, {
281
- concurrency: options.concurrency,
282
- verifyIntegrity: options.verifyIntegrity
283
- })
284
-
285
- yield* Progress.emit({ _tag: "PhaseStarted", phase: Phase.Archive })
286
-
287
- const input: EmitInput = {
288
- resolution,
289
- options,
290
- packagesDir,
291
- report,
292
- registryInfo: {
293
- kind: registry.kind,
294
- url: registry.registryFor(resolution.roots[0]?.spec.name ?? "")
295
- }
296
- }
297
-
298
- const artifacts = yield* emit(input, staging)
299
-
300
- yield* Progress.emit({ _tag: "PhaseCompleted", phase: Phase.Archive })
301
- yield* Progress.emit({ _tag: "PhaseStarted", phase: Phase.Done })
302
-
303
- return {
304
- resolution,
305
- artifacts,
306
- downloadedBytes: report.totalBytes,
307
- unverified: report.unverified,
308
- dryRun: false
309
- }
310
- })
311
-
312
- /* -------------------------------------------------------------------------- */
313
- /* Emitters */
314
- /* -------------------------------------------------------------------------- */
315
-
316
- const emit = (
317
- input: EmitInput,
318
- staging: string
319
- ): Effect.Effect<
320
- ReadonlyArray<BundleArtifact>,
321
- BundlerError | PlatformError,
322
- FileSystem.FileSystem | Path.Path | Progress.Progress
323
- > => {
324
- switch (input.options.layout) {
325
- case Layout.Single:
326
- return emitSingle(input)
327
- case Layout.Dir:
328
- return emitDirectory(input)
329
- case Layout.PerSpec:
330
- return emitPerSpec(input, staging)
331
- }
332
- }
333
-
334
- /** One tarball containing the deduplicated union of every closure. */
335
- const emitSingle = (
336
- input: EmitInput
337
- ): Effect.Effect<
338
- ReadonlyArray<BundleArtifact>,
339
- BundlerError | PlatformError,
340
- FileSystem.FileSystem | Path.Path | Progress.Progress
341
- > =>
342
- Effect.gen(function*() {
343
- const path = yield* Path.Path
344
-
345
- const archiveName = singleArchiveName(input.options.archiveName ?? "bundle")
346
- // Declined at the prompt, and it is the run's only output.
347
- if (input.options.skipExisting?.has(archiveName) === true) return []
348
-
349
- yield* writeBundleMetadata({
350
- dir: input.packagesDir,
351
- resolution: input.resolution,
352
- included: input.resolution.packages,
353
- options: input.options,
354
- registryInfo: input.registryInfo,
355
- sizes: input.report.sizes
356
- })
357
-
358
- const outPath = path.join(input.options.outDir, archiveName)
359
-
360
- const result = yield* createArchive({
361
- cwd: input.packagesDir,
362
- entries: yield* topLevelEntries(input.packagesDir),
363
- outPath
364
- })
365
-
366
- return [
367
- {
368
- kind: ArtifactKind.Archive,
369
- path: result.path,
370
- bytes: result.bytes,
371
- packageCount: input.resolution.packages.length
372
- }
373
- ]
374
- })
375
-
376
- /** The raw npm-layout tree, no archive. */
377
- const emitDirectory = (
378
- input: EmitInput
379
- ): Effect.Effect<
380
- ReadonlyArray<BundleArtifact>,
381
- BundlerError | PlatformError,
382
- FileSystem.FileSystem | Path.Path
383
- > =>
384
- Effect.gen(function*() {
385
- const fs = yield* FileSystem.FileSystem
386
-
387
- yield* writeBundleMetadata({
388
- dir: input.packagesDir,
389
- resolution: input.resolution,
390
- included: input.resolution.packages,
391
- options: input.options,
392
- registryInfo: input.registryInfo,
393
- sizes: input.report.sizes
394
- })
395
-
396
- // The staging tree is about to be deleted with its scope, so this is a
397
- // real copy rather than a move — `copy` also works across devices, which
398
- // matters because the temp dir often lives on a different filesystem.
399
- yield* fs.copy(input.packagesDir, input.options.outDir, { overwrite: true }).pipe(
400
- Effect.mapError(
401
- (cause) => new OutputError(input.options.outDir, "could not write output tree", { cause })
402
- )
403
- )
404
-
405
- const bytes = [...input.report.sizes.values()].reduce((a, b) => a + b, 0)
406
-
407
- return [
408
- {
409
- kind: ArtifactKind.Directory,
410
- path: input.options.outDir,
411
- bytes,
412
- packageCount: input.resolution.packages.length
413
- }
414
- ]
415
- })
416
-
417
- /**
418
- * One tarball per resolved root *version*.
419
- *
420
- * Under `--all-versions react@^18` this produces `react-18.0.0.tgz`,
421
- * `react-18.1.0.tgz` and so on, each independently importable — which is the
422
- * whole reason to prefer this layout.
423
- */
424
- const emitPerSpec = (
425
- input: EmitInput,
426
- staging: string
427
- ): Effect.Effect<
428
- ReadonlyArray<BundleArtifact>,
429
- BundlerError | PlatformError,
430
- FileSystem.FileSystem | Path.Path | Progress.Progress
431
- > =>
432
- Effect.gen(function*() {
433
- const fs = yield* FileSystem.FileSystem
434
- const path = yield* Path.Path
435
- const index = indexPackages(input.resolution)
436
- const artifacts: Array<BundleArtifact> = []
437
-
438
- let counter = 0
439
- for (const root of input.resolution.roots) {
440
- for (const version of root.versions) {
441
- // Declined at the prompt: the existing file stays, and this archive is
442
- // simply never built. Skipping the staging work too, not just the write.
443
- if (input.options.skipExisting?.has(perSpecArchiveName(root.spec.name, version)) === true) {
444
- continue
445
- }
446
-
447
- const included = includedPackages(index, root.closures.get(version) ?? [])
448
-
449
- const rootDir = path.join(staging, "roots", String(counter++))
450
- yield* fs.makeDirectory(rootDir, { recursive: true })
451
-
452
- for (const pkg of included) {
453
- const parts = packagePath(pkg.name, pkg.version).split("/")
454
- const source = path.join(input.packagesDir, ...parts)
455
- const target = path.join(rootDir, ...parts)
456
- yield* fs.makeDirectory(path.dirname(target), { recursive: true })
457
- yield* linkOrCopy(source, target)
458
- }
459
-
460
- yield* writeBundleMetadata({
461
- dir: rootDir,
462
- resolution: {
463
- ...input.resolution,
464
- roots: [{ ...root, versions: [version] }]
465
- },
466
- included,
467
- options: input.options,
468
- registryInfo: input.registryInfo,
469
- sizes: input.report.sizes
470
- })
471
-
472
- const outPath = path.join(
473
- input.options.outDir,
474
- perSpecArchiveName(root.spec.name, version)
475
- )
476
-
477
- const result = yield* createArchive({
478
- cwd: rootDir,
479
- entries: yield* topLevelEntries(rootDir),
480
- outPath
481
- })
482
-
483
- artifacts.push({
484
- kind: ArtifactKind.Archive,
485
- path: result.path,
486
- bytes: result.bytes,
487
- packageCount: included.length,
488
- spec: formatSpec(root.spec),
489
- version
490
- })
491
- }
492
- }
493
-
494
- return artifacts
495
- })
496
-
497
- /* -------------------------------------------------------------------------- */
498
- /* Helpers */
499
- /* -------------------------------------------------------------------------- */
500
-
501
- const includedPackages = (
502
- index: ReadonlyMap<PackageKey, ResolvedPackage>,
503
- closure: ReadonlyArray<PackageKey>
504
- ): ReadonlyArray<ResolvedPackage> => closure.flatMap((key) => index.get(key) ?? [])
505
-
506
- /**
507
- * Hard-links a file, falling back to a copy.
508
- *
509
- * Hard links make `per-spec` layout nearly free in the staging tree. They fail
510
- * across devices and on some Windows configurations, so the copy fallback is
511
- * not optional.
512
- */
513
- const linkOrCopy = (
514
- source: string,
515
- target: string
516
- ): Effect.Effect<void, PlatformError, FileSystem.FileSystem> =>
517
- Effect.gen(function*() {
518
- const fs = yield* FileSystem.FileSystem
519
- yield* fs.link(source, target).pipe(
520
- Effect.catch(() => fs.copyFile(source, target))
521
- )
522
- })
523
-
524
- /** Writes the manifest and import guide into a staging directory. */
525
- const writeBundleMetadata = (input: {
526
- readonly dir: string
527
- readonly resolution: Resolution
528
- readonly included: ReadonlyArray<ResolvedPackage>
529
- readonly options: BundleContext
530
- readonly registryInfo: { readonly kind: string; readonly url: string }
531
- readonly sizes: ReadonlyMap<string, number>
532
- }): Effect.Effect<void, PlatformError, FileSystem.FileSystem | Path.Path> =>
533
- Effect.gen(function*() {
534
- const fs = yield* FileSystem.FileSystem
535
- const path = yield* Path.Path
536
-
537
- const createdAt = new Date()
538
- const manifest = buildManifest({
539
- resolution: input.resolution,
540
- included: input.included,
541
- options: input.options,
542
- registry: input.registryInfo,
543
- toolVersion: input.options.toolVersion,
544
- sizes: input.sizes,
545
- createdAt
546
- })
547
-
548
- yield* fs.writeFileString(path.join(input.dir, MANIFEST_FILE), serializeManifest(manifest))
549
- yield* fs.writeFileString(
550
- path.join(input.dir, README_FILE),
551
- importGuide({
552
- packageCount: input.included.length,
553
- createdAt: createdAt.toISOString(),
554
- toolVersion: input.options.toolVersion
555
- })
556
- )
557
- })
558
-
559
- /** Sorted top-level entries of a directory, used as the tar entry list. */
560
- const topLevelEntries = (
561
- dir: string
562
- ): Effect.Effect<ReadonlyArray<string>, PlatformError, FileSystem.FileSystem> =>
563
- Effect.gen(function*() {
564
- const fs = yield* FileSystem.FileSystem
565
- const entries = yield* fs.readDirectory(dir)
566
- return entries.toSorted()
567
- })
568
-
569
- /**
570
- * Fails if the run would overwrite something, unless `--force`.
571
- *
572
- * The guard is on the *files this run writes*, not on whether the directory has
573
- * anything in it. An output directory accumulating bundles is the normal way to
574
- * use this — bundling `esbuild` into an `out/` that already holds `tsdown` is
575
- * not a conflict, and refusing it made `--out` a single-use directory and the
576
- * default `.` unusable.
577
- *
578
- * Checked before anything is downloaded. Discovering the collision *after* a
579
- * twenty-minute download would be a uniquely irritating way to fail.
580
- */
581
- const prepareOutputDir = (
582
- resolution: Resolution,
583
- options: BundleContext
584
- ): Effect.Effect<void, OutputError | PlatformError, FileSystem.FileSystem | Path.Path> =>
585
- Effect.gen(function*() {
586
- const fs = yield* FileSystem.FileSystem
587
- const path = yield* Path.Path
588
- const outDir = options.outDir
589
-
590
- if (!options.force) {
591
- const clashes: Array<string> = []
592
- for (const planned of plannedOutputs(resolution, options)) {
593
- if (options.skipExisting?.has(planned.file) === true) continue
594
- if (options.overwrite?.has(planned.file) === true) continue
595
- const exists = yield* fs
596
- .exists(path.join(outDir, planned.file))
597
- .pipe(Effect.orElseSucceed(() => false))
598
- if (exists) clashes.push(planned.file)
599
- }
600
-
601
- if (clashes.length > 0) {
602
- return yield* Effect.fail(
603
- new OutputError(
604
- outDir,
605
- `would overwrite ${clashes.length} existing file${clashes.length === 1 ? "" : "s"} ` +
606
- `(${clashes.slice(0, 3).join(", ")}${clashes.length > 3 ? ", …" : ""}). ` +
607
- `Pass --force true to replace ${clashes.length === 1 ? "it" : "them"}, or choose a different --out.`
608
- )
609
- )
610
- }
611
- }
612
-
613
- yield* fs.makeDirectory(outDir, { recursive: true }).pipe(
614
- Effect.mapError((cause) => new OutputError(outDir, "could not create output directory", { cause }))
615
- )
616
- })
1
+ /**
2
+ * End-to-end orchestration: specs in, tarballs out.
3
+ *
4
+ * Two properties this module is responsible for see `notes/requirements.md`
5
+ * § FR-5 and § NFR-9 for why they are requirements, and the mechanisms below
6
+ * for why the code reads as it does:
7
+ *
8
+ * 1. **Nothing is written outside the output directory.** Every intermediate
9
+ * file lives in a scoped temp directory obtained from
10
+ * `makeTempDirectoryScoped`, so the scope closing on success, on failure,
11
+ * or on Ctrl-C — takes the whole staging tree with it. The working
12
+ * directory is never touched.
13
+ *
14
+ * 2. **Each package is downloaded exactly once**, no matter how many bundles
15
+ * it ends up in. Per-spec archives are assembled by hard-linking out of one
16
+ * shared download tree, so `per-spec` costs extra disk in the *output*, not
17
+ * extra network.
18
+ */
19
+ import type * as Crypto from "effect/Crypto";
20
+ import * as Effect from "effect/Effect";
21
+ import * as FileSystem from "effect/FileSystem";
22
+ import * as Path from "effect/Path";
23
+ import type { PlatformError } from "effect/PlatformError";
24
+ import type { Scope } from "effect/Scope";
25
+
26
+ import type { Archiver } from "./archive.js";
27
+ import { createArchive } from "./archive.js";
28
+ import { downloadAll } from "./download.js";
29
+ import { ArtifactKind } from "./enums/artifact-kind.js";
30
+ import { Layout } from "./enums/layout.js";
31
+ import { Phase } from "./enums/phase.js";
32
+ import type { BundlerError } from "./errors.js";
33
+ import { OutputError } from "./errors.js";
34
+ import {
35
+ importGuide,
36
+ MANIFEST_FILE,
37
+ packagePath,
38
+ perSpecArchiveName,
39
+ README_FILE,
40
+ singleArchiveName,
41
+ } from "./layout.js";
42
+ import type { LockedResolveOptions } from "./locked-resolve.js";
43
+ import { resolveLocked } from "./locked-resolve.js";
44
+ import type { LockedTree } from "./lockfile/types.js";
45
+ import { buildManifest, serializeManifest } from "./manifest.js";
46
+ import type { BundleOptions } from "./options.js";
47
+ import * as Progress from "./progress.js";
48
+ import { Registry } from "./registry.js";
49
+ import type { PackageKey, Resolution, ResolvedPackage, RootResolution } from "./resolve.js";
50
+ import { indexPackages, resolve } from "./resolve.js";
51
+ import type { PackageSpec } from "./spec.js";
52
+ import { formatSpec } from "./spec.js";
53
+
54
+ /** One thing written to the output directory. */
55
+ export type BundleArtifact = {
56
+ readonly kind: ArtifactKind;
57
+ readonly path: string;
58
+ readonly bytes: number;
59
+ readonly packageCount: number;
60
+ /** Which spec this artifact covers. Absent for `single` and `dir`. */
61
+ readonly spec?: string | undefined;
62
+ readonly version?: string | undefined;
63
+ };
64
+
65
+ /** The outcome of a bundle run. */
66
+ export type BundleResult = {
67
+ readonly resolution: Resolution;
68
+ readonly artifacts: ReadonlyArray<BundleArtifact>;
69
+ /** Total bytes downloaded from the registry. */
70
+ readonly downloadedBytes: number;
71
+ /** Packages the registry advertised no usable checksum for. */
72
+ readonly unverified: ReadonlyArray<string>;
73
+ readonly dryRun: boolean;
74
+ };
75
+
76
+ /** Everything `bundle` needs beyond the user-facing options. */
77
+ export type BundleContext = BundleOptions & {
78
+ /** Recorded in the manifest so a bundle can be traced to the tool that built it. */
79
+ readonly toolVersion: string;
80
+ };
81
+
82
+ /**
83
+ * A summary of what a resolution would produce, for deciding whether the
84
+ * chosen layout is still the right one.
85
+ */
86
+ export type PlanSummary = {
87
+ /** Distinct packages in the deduplicated union. */
88
+ readonly uniquePackages: number;
89
+ /** How many archives `per-spec` layout would emit. */
90
+ readonly perSpecArchives: number;
91
+ /**
92
+ * Total package entries across all per-spec archives, counting shared
93
+ * dependencies once per archive they appear in. The gap between this and
94
+ * `uniquePackages` is exactly what `single` layout would save.
95
+ */
96
+ readonly perSpecEntries: number;
97
+ };
98
+
99
+ /**
100
+ * The files a run will write, known before anything is downloaded.
101
+ *
102
+ * `dir` layout is the odd one: it merges a package tree into `outDir`, and
103
+ * package paths are `name/-/name-version.tgz` — the same package at the same
104
+ * version is the same bytes, so an overlap there is idempotent rather than
105
+ * destructive. Only the two summary files are genuinely replaced, and those are
106
+ * the ones worth guarding.
107
+ */
108
+ export type PlannedOutput = {
109
+ readonly file: string;
110
+ /** The root this file belongs to, when one file corresponds to one spec. */
111
+ readonly name?: string | undefined;
112
+ readonly version?: string | undefined;
113
+ };
114
+
115
+ type BundleEnv =
116
+ | Registry
117
+ | Progress.Progress
118
+ | FileSystem.FileSystem
119
+ | Path.Path
120
+ | Crypto.Crypto
121
+ | Archiver;
122
+
123
+ type EmitInput = {
124
+ readonly resolution: Resolution;
125
+ readonly options: BundleContext;
126
+ readonly packagesDir: string;
127
+ readonly report: { readonly sizes: ReadonlyMap<string, number> };
128
+ readonly registryInfo: { readonly kind: string; readonly url: string };
129
+ };
130
+
131
+ /** One archive `per-spec` layout will write: a root, at one of its versions. */
132
+ type PerSpecTarget = {
133
+ readonly root: RootResolution;
134
+ readonly version: string;
135
+ };
136
+
137
+ /**
138
+ * The archives `per-spec` emits one per distinct root package version.
139
+ *
140
+ * Roots are *specs*, and several specs can name one package: `react`,
141
+ * `react@latest` and `react@19.2.8` are three ways of asking for the same
142
+ * thing, and after resolution they are indistinguishable. Without this they
143
+ * were three archives — the same bytes, under the same file name, written over
144
+ * each other. The run then reported three artifacts and three times the size
145
+ * for one file on disk, and the duplication ratio it computes to decide whether
146
+ * to ask about the layout counted them too, so the tool would offer to
147
+ * deduplicate a bundle it had duplicated itself.
148
+ *
149
+ * Keyed by name and version rather than by the archive's file name. A scoped
150
+ * name is flattened to make a file name, so `@foo/bar` and `foo-bar` can spell
151
+ * the same one — and those are two different packages, which should collide
152
+ * loudly rather than being merged here.
153
+ *
154
+ * Roots only. The packages *inside* an archive were deduplicated by resolution
155
+ * long before this, which is why the same closure fetched from twenty specs is
156
+ * fetched once.
157
+ */
158
+ const perSpecTargets = (resolution: Resolution): ReadonlyArray<PerSpecTarget> => {
159
+ const seen = new Set<string>();
160
+
161
+ return resolution.roots.flatMap((root) =>
162
+ root.versions.flatMap((version) => {
163
+ const key = `${root.spec.name}@${version}`;
164
+ if (seen.has(key)) return [];
165
+
166
+ seen.add(key);
167
+ return { root, version };
168
+ }),
169
+ );
170
+ };
171
+
172
+ /** Computes the plan summary for a resolution. */
173
+ export const summarize = (resolution: Resolution): PlanSummary => {
174
+ let perSpecArchives = 0;
175
+ let perSpecEntries = 0;
176
+ for (const { root, version } of perSpecTargets(resolution)) {
177
+ perSpecArchives += 1;
178
+ perSpecEntries += (root.closures.get(version) ?? []).length;
179
+ }
180
+ return {
181
+ uniquePackages: resolution.packages.length,
182
+ perSpecArchives,
183
+ perSpecEntries,
184
+ };
185
+ };
186
+
187
+ /** Replaces the layout on a set of options. */
188
+ export const withLayout = <T extends BundleOptions>(options: T, layout: Layout): T => ({
189
+ ...options,
190
+ layout,
191
+ });
192
+
193
+ /**
194
+ * Checks the registry is reachable, then resolves every spec.
195
+ *
196
+ * Split out from `bundle` so a caller can inspect the plan — how many packages,
197
+ * how many archives — and act on it before any bytes move. The CLI uses this to
198
+ * offer a different layout when a run turns out to be much larger than
199
+ * expected.
200
+ */
201
+ export const plan = (
202
+ specs: ReadonlyArray<PackageSpec>,
203
+ options: BundleOptions,
204
+ ): Effect.Effect<Resolution, BundlerError, Registry | Progress.Progress> =>
205
+ Effect.gen(function* () {
206
+ yield* preflight;
207
+ return yield* resolve(specs, options);
208
+ });
209
+
210
+ /**
211
+ * The lockfile counterpart of `plan`.
212
+ *
213
+ * Same preflight, same `Resolution` out; the difference is entirely in how the
214
+ * package set is arrived at. Kept as a separate entry point rather than an
215
+ * option on `plan` because the two take genuinely different inputs — a set of
216
+ * specs to satisfy versus a graph to reproduce — and blurring that is how a
217
+ * "pinned" run quietly starts resolving ranges again.
218
+ */
219
+ export const planLocked = (
220
+ lockfile: LockedTree,
221
+ options: BundleOptions & LockedResolveOptions,
222
+ ): Effect.Effect<Resolution, BundlerError, Registry | Progress.Progress> =>
223
+ Effect.gen(function* () {
224
+ yield* preflight;
225
+ return yield* resolveLocked(lockfile, options);
226
+ });
227
+
228
+ const preflight: Effect.Effect<void, BundlerError, Registry | Progress.Progress> = Effect.gen(
229
+ function* () {
230
+ const registry = yield* Registry;
231
+ yield* Progress.emit({ _tag: "PhaseStarted", phase: Phase.Preflight });
232
+ yield* registry.preflight;
233
+ yield* Progress.emit({ _tag: "PhaseCompleted", phase: Phase.Preflight });
234
+ },
235
+ );
236
+
237
+ /**
238
+ * Resolves, downloads and packages a set of specs.
239
+ *
240
+ * Under `dryRun` this stops after resolution and reports the plan — useful for
241
+ * checking what a command *would* pull before committing to a multi-gigabyte
242
+ * download over a slow VPN.
243
+ */
244
+ export const bundle = (
245
+ specs: ReadonlyArray<PackageSpec>,
246
+ options: BundleContext,
247
+ ): Effect.Effect<BundleResult, BundlerError | PlatformError, BundleEnv> =>
248
+ Effect.gen(function* () {
249
+ const resolution = yield* plan(specs, options);
250
+ return yield* bundleResolved(resolution, options);
251
+ });
252
+
253
+ /** `bundle`, pinned to a lockfile. */
254
+ export const bundleLocked = (
255
+ lockfile: LockedTree,
256
+ options: BundleContext & LockedResolveOptions,
257
+ ): Effect.Effect<BundleResult, BundlerError | PlatformError, BundleEnv> =>
258
+ Effect.gen(function* () {
259
+ const resolution = yield* planLocked(lockfile, options);
260
+ return yield* bundleResolved(resolution, options);
261
+ });
262
+
263
+ /**
264
+ * Downloads and packages an already-computed resolution.
265
+ *
266
+ * Everything from here on touches the disk, and all of it happens inside
267
+ * `Effect.scoped` — so the staging tree's lifetime is exactly this call,
268
+ * including when it fails partway through or the user hits Ctrl-C.
269
+ */
270
+ export const bundleResolved = (
271
+ resolution: Resolution,
272
+ options: BundleContext,
273
+ ): Effect.Effect<BundleResult, BundlerError | PlatformError, BundleEnv> =>
274
+ Effect.gen(function* () {
275
+ if (options.dryRun) {
276
+ return {
277
+ resolution,
278
+ artifacts: [],
279
+ downloadedBytes: 0,
280
+ unverified: [],
281
+ dryRun: true,
282
+ };
283
+ }
284
+ return yield* Effect.scoped(runBundle(resolution, options));
285
+ });
286
+
287
+ /**
288
+ * The files a run will write.
289
+ *
290
+ * Computed before anything is downloaded, so a collision is reported in the
291
+ * first second rather than after a twenty-minute transfer.
292
+ */
293
+ export const plannedOutputs = (
294
+ resolution: Resolution,
295
+ options: BundleContext,
296
+ ): ReadonlyArray<PlannedOutput> => {
297
+ if (options.layout === Layout.Dir) return [{ file: MANIFEST_FILE }, { file: README_FILE }];
298
+ if (options.layout === Layout.Single) {
299
+ return [{ file: singleArchiveName(options.archiveName ?? "bundle") }];
300
+ }
301
+ return perSpecTargets(resolution).map(({ root, version }) => ({
302
+ file: perSpecArchiveName(root.spec.name, version),
303
+ name: root.spec.name,
304
+ version,
305
+ }));
306
+ };
307
+
308
+ const runBundle = (
309
+ resolution: Resolution,
310
+ options: BundleContext,
311
+ ): Effect.Effect<BundleResult, BundlerError | PlatformError, BundleEnv | Scope> =>
312
+ Effect.gen(function* () {
313
+ const fs = yield* FileSystem.FileSystem;
314
+ const path = yield* Path.Path;
315
+ const registry = yield* Registry;
316
+
317
+ yield* prepareOutputDir(resolution, options);
318
+
319
+ const staging = yield* fs.makeTempDirectoryScoped({ prefix: "packall-" });
320
+ const packagesDir = path.join(staging, "packages");
321
+ yield* fs.makeDirectory(packagesDir, { recursive: true });
322
+
323
+ const report = yield* downloadAll(resolution.packages, packagesDir, {
324
+ concurrency: options.concurrency,
325
+ verifyIntegrity: options.verifyIntegrity,
326
+ });
327
+
328
+ yield* Progress.emit({ _tag: "PhaseStarted", phase: Phase.Archive });
329
+
330
+ const input: EmitInput = {
331
+ resolution,
332
+ options,
333
+ packagesDir,
334
+ report,
335
+ registryInfo: {
336
+ kind: registry.kind,
337
+ url: registry.registryFor(resolution.roots[0]?.spec.name ?? ""),
338
+ },
339
+ };
340
+
341
+ const artifacts = yield* emit(input, staging);
342
+
343
+ yield* Progress.emit({ _tag: "PhaseCompleted", phase: Phase.Archive });
344
+ yield* Progress.emit({ _tag: "PhaseStarted", phase: Phase.Done });
345
+
346
+ return {
347
+ resolution,
348
+ artifacts,
349
+ downloadedBytes: report.totalBytes,
350
+ unverified: report.unverified,
351
+ dryRun: false,
352
+ };
353
+ });
354
+
355
+ /* -------------------------------------------------------------------------- */
356
+ /* Emitters */
357
+ /* -------------------------------------------------------------------------- */
358
+
359
+ const emit = (
360
+ input: EmitInput,
361
+ staging: string,
362
+ ): Effect.Effect<
363
+ ReadonlyArray<BundleArtifact>,
364
+ BundlerError | PlatformError,
365
+ FileSystem.FileSystem | Path.Path | Progress.Progress | Archiver
366
+ > => {
367
+ switch (input.options.layout) {
368
+ case Layout.Single:
369
+ return emitSingle(input);
370
+ case Layout.Dir:
371
+ return emitDirectory(input);
372
+ case Layout.PerSpec:
373
+ return emitPerSpec(input, staging);
374
+ }
375
+ };
376
+
377
+ /** One tarball containing the deduplicated union of every closure. */
378
+ const emitSingle = (
379
+ input: EmitInput,
380
+ ): Effect.Effect<
381
+ ReadonlyArray<BundleArtifact>,
382
+ BundlerError | PlatformError,
383
+ FileSystem.FileSystem | Path.Path | Progress.Progress | Archiver
384
+ > =>
385
+ Effect.gen(function* () {
386
+ const path = yield* Path.Path;
387
+
388
+ const archiveName = singleArchiveName(input.options.archiveName ?? "bundle");
389
+ // Declined at the prompt, and it is the run's only output.
390
+ if (input.options.skipExisting?.has(archiveName) === true) return [];
391
+
392
+ yield* writeBundleMetadata({
393
+ dir: input.packagesDir,
394
+ resolution: input.resolution,
395
+ included: input.resolution.packages,
396
+ options: input.options,
397
+ registryInfo: input.registryInfo,
398
+ sizes: input.report.sizes,
399
+ });
400
+
401
+ const outPath = path.join(input.options.outDir, archiveName);
402
+
403
+ const result = yield* createArchive({
404
+ cwd: input.packagesDir,
405
+ entries: yield* topLevelEntries(input.packagesDir),
406
+ outPath,
407
+ });
408
+
409
+ return [
410
+ {
411
+ kind: ArtifactKind.Archive,
412
+ path: result.path,
413
+ bytes: result.bytes,
414
+ packageCount: input.resolution.packages.length,
415
+ },
416
+ ];
417
+ });
418
+
419
+ /** The raw npm-layout tree, no archive. */
420
+ const emitDirectory = (
421
+ input: EmitInput,
422
+ ): Effect.Effect<
423
+ ReadonlyArray<BundleArtifact>,
424
+ BundlerError | PlatformError,
425
+ FileSystem.FileSystem | Path.Path
426
+ > =>
427
+ Effect.gen(function* () {
428
+ const fs = yield* FileSystem.FileSystem;
429
+
430
+ yield* writeBundleMetadata({
431
+ dir: input.packagesDir,
432
+ resolution: input.resolution,
433
+ included: input.resolution.packages,
434
+ options: input.options,
435
+ registryInfo: input.registryInfo,
436
+ sizes: input.report.sizes,
437
+ });
438
+
439
+ // The staging tree is about to be deleted with its scope, so this is a
440
+ // real copy rather than a move — `copy` also works across devices, which
441
+ // matters because the temp dir often lives on a different filesystem.
442
+ yield* fs.copy(input.packagesDir, input.options.outDir, { overwrite: true }).pipe(
443
+ Effect.mapError(
444
+ (cause) =>
445
+ new OutputError(input.options.outDir, "could not write output tree", {
446
+ cause,
447
+ }),
448
+ ),
449
+ );
450
+
451
+ const bytes = [...input.report.sizes.values()].reduce((a, b) => a + b, 0);
452
+
453
+ return [
454
+ {
455
+ kind: ArtifactKind.Directory,
456
+ path: input.options.outDir,
457
+ bytes,
458
+ packageCount: input.resolution.packages.length,
459
+ },
460
+ ];
461
+ });
462
+
463
+ /**
464
+ * One tarball per resolved root *version*.
465
+ *
466
+ * Under `--all-versions react@^18` this produces `react-18.0.0.tgz`,
467
+ * `react-18.1.0.tgz` and so on, each independently importable — which is the
468
+ * whole reason to prefer this layout.
469
+ */
470
+ const emitPerSpec = (
471
+ input: EmitInput,
472
+ staging: string,
473
+ ): Effect.Effect<
474
+ ReadonlyArray<BundleArtifact>,
475
+ BundlerError | PlatformError,
476
+ FileSystem.FileSystem | Path.Path | Progress.Progress | Archiver
477
+ > =>
478
+ Effect.gen(function* () {
479
+ const fs = yield* FileSystem.FileSystem;
480
+ const path = yield* Path.Path;
481
+ const index = indexPackages(input.resolution);
482
+ const artifacts: Array<BundleArtifact> = [];
483
+
484
+ let counter = 0;
485
+ for (const { root, version } of perSpecTargets(input.resolution)) {
486
+ // Declined at the prompt: the existing file stays, and this archive is
487
+ // simply never built. Skipping the staging work too, not just the write.
488
+ if (
489
+ input.options.skipExisting?.has(perSpecArchiveName(root.spec.name, version)) ===
490
+ true
491
+ ) {
492
+ continue;
493
+ }
494
+
495
+ const included = includedPackages(index, root.closures.get(version) ?? []);
496
+
497
+ const rootDir = path.join(staging, "roots", String(counter++));
498
+ yield* fs.makeDirectory(rootDir, { recursive: true });
499
+
500
+ for (const pkg of included) {
501
+ const parts = packagePath(pkg.name, pkg.version).split("/");
502
+ const source = path.join(input.packagesDir, ...parts);
503
+ const target = path.join(rootDir, ...parts);
504
+ yield* fs.makeDirectory(path.dirname(target), { recursive: true });
505
+ yield* linkOrCopy(source, target);
506
+ }
507
+
508
+ yield* writeBundleMetadata({
509
+ dir: rootDir,
510
+ resolution: {
511
+ ...input.resolution,
512
+ roots: [{ ...root, versions: [version] }],
513
+ },
514
+ included,
515
+ options: input.options,
516
+ registryInfo: input.registryInfo,
517
+ sizes: input.report.sizes,
518
+ });
519
+
520
+ const outPath = path.join(
521
+ input.options.outDir,
522
+ perSpecArchiveName(root.spec.name, version),
523
+ );
524
+
525
+ const result = yield* createArchive({
526
+ cwd: rootDir,
527
+ entries: yield* topLevelEntries(rootDir),
528
+ outPath,
529
+ });
530
+
531
+ artifacts.push({
532
+ kind: ArtifactKind.Archive,
533
+ path: result.path,
534
+ bytes: result.bytes,
535
+ packageCount: included.length,
536
+ spec: formatSpec(root.spec),
537
+ version,
538
+ });
539
+ }
540
+
541
+ return artifacts;
542
+ });
543
+
544
+ /* -------------------------------------------------------------------------- */
545
+ /* Helpers */
546
+ /* -------------------------------------------------------------------------- */
547
+
548
+ const includedPackages = (
549
+ index: ReadonlyMap<PackageKey, ResolvedPackage>,
550
+ closure: ReadonlyArray<PackageKey>,
551
+ ): ReadonlyArray<ResolvedPackage> => closure.flatMap((key) => index.get(key) ?? []);
552
+
553
+ /**
554
+ * Hard-links a file, falling back to a copy.
555
+ *
556
+ * Hard links make `per-spec` layout nearly free in the staging tree. They fail
557
+ * across devices and on some Windows configurations, so the copy fallback is
558
+ * not optional.
559
+ */
560
+ const linkOrCopy = (
561
+ source: string,
562
+ target: string,
563
+ ): Effect.Effect<void, PlatformError, FileSystem.FileSystem> =>
564
+ Effect.gen(function* () {
565
+ const fs = yield* FileSystem.FileSystem;
566
+ yield* fs.link(source, target).pipe(Effect.catch(() => fs.copyFile(source, target)));
567
+ });
568
+
569
+ /** Writes the manifest and import guide into a staging directory. */
570
+ const writeBundleMetadata = (input: {
571
+ readonly dir: string;
572
+ readonly resolution: Resolution;
573
+ readonly included: ReadonlyArray<ResolvedPackage>;
574
+ readonly options: BundleContext;
575
+ readonly registryInfo: { readonly kind: string; readonly url: string };
576
+ readonly sizes: ReadonlyMap<string, number>;
577
+ }): Effect.Effect<void, PlatformError, FileSystem.FileSystem | Path.Path> =>
578
+ Effect.gen(function* () {
579
+ const fs = yield* FileSystem.FileSystem;
580
+ const path = yield* Path.Path;
581
+
582
+ const createdAt = new Date();
583
+ const manifest = buildManifest({
584
+ resolution: input.resolution,
585
+ included: input.included,
586
+ options: input.options,
587
+ registry: input.registryInfo,
588
+ toolVersion: input.options.toolVersion,
589
+ sizes: input.sizes,
590
+ createdAt,
591
+ });
592
+
593
+ yield* fs.writeFileString(path.join(input.dir, MANIFEST_FILE), serializeManifest(manifest));
594
+ yield* fs.writeFileString(
595
+ path.join(input.dir, README_FILE),
596
+ importGuide({
597
+ packageCount: input.included.length,
598
+ createdAt: createdAt.toISOString(),
599
+ toolVersion: input.options.toolVersion,
600
+ }),
601
+ );
602
+ });
603
+
604
+ /** Sorted top-level entries of a directory, used as the tar entry list. */
605
+ const topLevelEntries = (
606
+ dir: string,
607
+ ): Effect.Effect<ReadonlyArray<string>, PlatformError, FileSystem.FileSystem> =>
608
+ Effect.gen(function* () {
609
+ const fs = yield* FileSystem.FileSystem;
610
+ const entries = yield* fs.readDirectory(dir);
611
+ return entries.toSorted();
612
+ });
613
+
614
+ /**
615
+ * Two different packages must not want the same file name.
616
+ *
617
+ * Most of what this used to catch is now impossible: an archive is named for
618
+ * the spec, `@types-react@19.2.18.tgz`, and an unscoped npm name may not begin
619
+ * with `@`, so it cannot spell a scoped one. What survives is **two scoped
620
+ * packages whose scope boundary falls in a different place**, because that
621
+ * boundary is the one character the file name drops:
622
+ *
623
+ * @a/b-c -> @a-b-c@1.0.0.tgz
624
+ * @a-b/c -> @a-b-c@1.0.0.tgz
625
+ *
626
+ * Pathological rather than plausible — it needs somebody to hold the `@a-b`
627
+ * scope as well as `@a`, and both packages in one run at one version. It is
628
+ * kept anyway because of what it costs to be wrong: they are *different
629
+ * packages*, so deduplicating them would be wrong, and writing both is worse —
630
+ * the second lands on the first and the run reports two artifacts for the one
631
+ * file that survived. A bundle short a package, called complete, found at the
632
+ * far end of an air gap by somebody who cannot go and fetch the missing one.
633
+ *
634
+ * Nothing else would catch it. The overwrite check below compares this run's
635
+ * planned files against what is *already on disk*, and both of these are new.
636
+ *
637
+ * So it refuses, and names both specs. Not silently renamed: an archive whose
638
+ * name nobody chose is a file somebody has to identify later.
639
+ *
640
+ * Unconditional, unlike the overwrite check: `--force` is permission to replace
641
+ * what was already in the directory, not to let one run overwrite itself.
642
+ */
643
+ const assertDistinctOutputs = (
644
+ resolution: Resolution,
645
+ options: BundleContext,
646
+ ): Effect.Effect<void, OutputError> =>
647
+ Effect.gen(function* () {
648
+ const owners = new Map<string, Array<string>>();
649
+
650
+ // Over `plannedOutputs` rather than the per-spec targets, so this asks
651
+ // about the layout that is actually running. `single` writes one archive
652
+ // and `dir` two summary files, neither of which can collide — and a check
653
+ // that refused those would make its own advice impossible to take.
654
+ for (const planned of plannedOutputs(resolution, options)) {
655
+ const claimed = owners.get(planned.file);
656
+ const spec =
657
+ planned.name === undefined ? planned.file : `${planned.name}@${planned.version}`;
658
+
659
+ if (claimed === undefined) owners.set(planned.file, [spec]);
660
+ else claimed.push(spec);
661
+ }
662
+
663
+ const collisions = [...owners].filter(([, specs]) => specs.length > 1);
664
+ if (collisions.length === 0) return;
665
+
666
+ const described = collisions
667
+ .map(([file, specs]) => `${specs.join(" and ")} would both be ${file}`)
668
+ .join("; ");
669
+
670
+ return yield* Effect.fail(
671
+ new OutputError(
672
+ options.outDir,
673
+ `two packages want the same archive name — ${described}. The \`/\` in a ` +
674
+ `scope becomes a \`-\` to make a file name, so \`@a/b-c\` and \`@a-b/c\` ` +
675
+ `meet. Use --layout single, or bundle them into separate --out ` +
676
+ `directories.`,
677
+ ),
678
+ );
679
+ });
680
+
681
+ /**
682
+ * Fails if the run would overwrite something, unless `--force`.
683
+ *
684
+ * The guard is on the *files this run writes*, not on whether the directory has
685
+ * anything in it. An output directory accumulating bundles is the normal way to
686
+ * use this — bundling `esbuild` into an `out/` that already holds `tsdown` is
687
+ * not a conflict, and refusing it made `--out` a single-use directory and the
688
+ * default `.` unusable.
689
+ *
690
+ * Checked before anything is downloaded. Discovering the collision *after* a
691
+ * twenty-minute download would be a uniquely irritating way to fail — which is
692
+ * also why `assertDistinctOutputs` runs from here rather than at emit time.
693
+ */
694
+ const prepareOutputDir = (
695
+ resolution: Resolution,
696
+ options: BundleContext,
697
+ ): Effect.Effect<void, OutputError | PlatformError, FileSystem.FileSystem | Path.Path> =>
698
+ Effect.gen(function* () {
699
+ const fs = yield* FileSystem.FileSystem;
700
+ const path = yield* Path.Path;
701
+ const outDir = options.outDir;
702
+
703
+ yield* assertDistinctOutputs(resolution, options);
704
+
705
+ if (!options.force) {
706
+ const clashes: Array<string> = [];
707
+ for (const planned of plannedOutputs(resolution, options)) {
708
+ if (options.skipExisting?.has(planned.file) === true) continue;
709
+ if (options.overwrite?.has(planned.file) === true) continue;
710
+ const exists = yield* fs
711
+ .exists(path.join(outDir, planned.file))
712
+ .pipe(Effect.orElseSucceed(() => false));
713
+ if (exists) clashes.push(planned.file);
714
+ }
715
+
716
+ if (clashes.length > 0) {
717
+ return yield* Effect.fail(
718
+ new OutputError(
719
+ outDir,
720
+ `would overwrite ${clashes.length} existing file${clashes.length === 1 ? "" : "s"} ` +
721
+ `(${clashes.slice(0, 3).join(", ")}${clashes.length > 3 ? ", …" : ""}). ` +
722
+ `Pass --force true to replace ${clashes.length === 1 ? "it" : "them"}, or choose a different --out.`,
723
+ ),
724
+ );
725
+ }
726
+ }
727
+
728
+ yield* fs
729
+ .makeDirectory(outDir, { recursive: true })
730
+ .pipe(
731
+ Effect.mapError(
732
+ (cause) =>
733
+ new OutputError(outDir, "could not create output directory", { cause }),
734
+ ),
735
+ );
736
+ });