@packall/core 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Archive.d.ts +32 -0
- package/dist/Archive.d.ts.map +1 -0
- package/dist/Bundle.d.ts +123 -0
- package/dist/Bundle.d.ts.map +1 -0
- package/dist/DependencyRange.d.ts +26 -0
- package/dist/DependencyRange.d.ts.map +1 -0
- package/dist/Download.d.ts +35 -0
- package/dist/Download.d.ts.map +1 -0
- package/dist/Errors.d.ts +124 -0
- package/dist/Errors.d.ts.map +1 -0
- package/dist/InputFile.d.ts +46 -0
- package/dist/InputFile.d.ts.map +1 -0
- package/dist/Integrity.d.ts +48 -0
- package/dist/Integrity.d.ts.map +1 -0
- package/dist/Layout.d.ts +53 -0
- package/dist/Layout.d.ts.map +1 -0
- package/dist/Manifest.d.ts +82 -0
- package/dist/Manifest.d.ts.map +1 -0
- package/dist/Options.d.ts +100 -0
- package/dist/Options.d.ts.map +1 -0
- package/dist/Platform.d.ts +69 -0
- package/dist/Platform.d.ts.map +1 -0
- package/dist/Progress.d.ts +107 -0
- package/dist/Progress.d.ts.map +1 -0
- package/dist/Registry.d.ts +106 -0
- package/dist/Registry.d.ts.map +1 -0
- package/dist/Resolve.d.ts +105 -0
- package/dist/Resolve.d.ts.map +1 -0
- package/dist/Spec.d.ts +62 -0
- package/dist/Spec.d.ts.map +1 -0
- package/dist/index.d.ts +36 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1766 -0
- package/dist/index.js.map +1 -0
- package/package.json +38 -0
- package/src/Archive.ts +92 -0
- package/src/Bundle.ts +570 -0
- package/src/DependencyRange.ts +115 -0
- package/src/Download.ts +132 -0
- package/src/Errors.ts +216 -0
- package/src/InputFile.ts +229 -0
- package/src/Integrity.ts +122 -0
- package/src/Layout.ts +103 -0
- package/src/Manifest.ts +133 -0
- package/src/Options.ts +123 -0
- package/src/Platform.ts +182 -0
- package/src/Progress.ts +106 -0
- package/src/Registry.ts +121 -0
- package/src/Resolve.ts +528 -0
- package/src/Spec.ts +186 -0
- package/src/index.ts +44 -0
package/src/Bundle.ts
ADDED
|
@@ -0,0 +1,570 @@
|
|
|
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 type { BundlerError } from "./Errors.js"
|
|
26
|
+
import { OutputError } from "./Errors.js"
|
|
27
|
+
import {
|
|
28
|
+
importGuide,
|
|
29
|
+
MANIFEST_FILE,
|
|
30
|
+
packagePath,
|
|
31
|
+
perSpecArchiveName,
|
|
32
|
+
README_FILE,
|
|
33
|
+
singleArchiveName
|
|
34
|
+
} from "./Layout.js"
|
|
35
|
+
import { buildManifest, serializeManifest } from "./Manifest.js"
|
|
36
|
+
import type { BundleOptions, Layout } from "./Options.js"
|
|
37
|
+
import * as Progress from "./Progress.js"
|
|
38
|
+
import { Registry } from "./Registry.js"
|
|
39
|
+
import type { PackageKey, Resolution, ResolvedPackage } from "./Resolve.js"
|
|
40
|
+
import { indexPackages, resolve } from "./Resolve.js"
|
|
41
|
+
import type { PackageSpec } from "./Spec.js"
|
|
42
|
+
import { formatSpec } from "./Spec.js"
|
|
43
|
+
|
|
44
|
+
/** One thing written to the output directory. */
|
|
45
|
+
export interface BundleArtifact {
|
|
46
|
+
readonly kind: "archive" | "directory"
|
|
47
|
+
readonly path: string
|
|
48
|
+
readonly bytes: number
|
|
49
|
+
readonly packageCount: number
|
|
50
|
+
/** Which spec this artifact covers. Absent for `single` and `dir`. */
|
|
51
|
+
readonly spec?: string | undefined
|
|
52
|
+
readonly version?: string | undefined
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The outcome of a bundle run. */
|
|
56
|
+
export interface BundleResult {
|
|
57
|
+
readonly resolution: Resolution
|
|
58
|
+
readonly artifacts: ReadonlyArray<BundleArtifact>
|
|
59
|
+
/** Total bytes downloaded from the registry. */
|
|
60
|
+
readonly downloadedBytes: number
|
|
61
|
+
/** Packages the registry advertised no usable checksum for. */
|
|
62
|
+
readonly unverified: ReadonlyArray<string>
|
|
63
|
+
readonly dryRun: boolean
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Everything `bundle` needs beyond the user-facing options. */
|
|
67
|
+
export interface BundleContext extends BundleOptions {
|
|
68
|
+
/** Recorded in the manifest so a bundle can be traced to the tool that built it. */
|
|
69
|
+
readonly toolVersion: string
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* A summary of what a resolution would produce, for deciding whether the
|
|
74
|
+
* chosen layout is still the right one.
|
|
75
|
+
*/
|
|
76
|
+
export interface PlanSummary {
|
|
77
|
+
/** Distinct packages in the deduplicated union. */
|
|
78
|
+
readonly uniquePackages: number
|
|
79
|
+
/** How many archives `per-spec` layout would emit. */
|
|
80
|
+
readonly perSpecArchives: number
|
|
81
|
+
/**
|
|
82
|
+
* Total package entries across all per-spec archives, counting shared
|
|
83
|
+
* dependencies once per archive they appear in. The gap between this and
|
|
84
|
+
* `uniquePackages` is exactly what `single` layout would save.
|
|
85
|
+
*/
|
|
86
|
+
readonly perSpecEntries: number
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Computes the plan summary for a resolution. */
|
|
90
|
+
export const summarize = (resolution: Resolution): PlanSummary => {
|
|
91
|
+
let perSpecArchives = 0
|
|
92
|
+
let perSpecEntries = 0
|
|
93
|
+
for (const root of resolution.roots) {
|
|
94
|
+
for (const version of root.versions) {
|
|
95
|
+
perSpecArchives += 1
|
|
96
|
+
perSpecEntries += (root.closures.get(version) ?? []).length
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
uniquePackages: resolution.packages.length,
|
|
101
|
+
perSpecArchives,
|
|
102
|
+
perSpecEntries
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Replaces the layout on a set of options. */
|
|
107
|
+
export const withLayout = <T extends BundleOptions>(options: T, layout: Layout): T => ({
|
|
108
|
+
...options,
|
|
109
|
+
layout
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
type BundleEnv = Registry | Progress.Progress | FileSystem.FileSystem | Path.Path
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Checks the registry is reachable, then resolves every spec.
|
|
116
|
+
*
|
|
117
|
+
* Split out from `bundle` so a caller can inspect the plan — how many packages,
|
|
118
|
+
* how many archives — and act on it before any bytes move. The CLI uses this to
|
|
119
|
+
* offer a different layout when a run turns out to be much larger than
|
|
120
|
+
* expected.
|
|
121
|
+
*/
|
|
122
|
+
export const plan = (
|
|
123
|
+
specs: ReadonlyArray<PackageSpec>,
|
|
124
|
+
options: BundleOptions
|
|
125
|
+
): Effect.Effect<Resolution, BundlerError, Registry | Progress.Progress> =>
|
|
126
|
+
Effect.gen(function*() {
|
|
127
|
+
const registry = yield* Registry
|
|
128
|
+
|
|
129
|
+
yield* Progress.emit({ _tag: "PhaseStarted", phase: "preflight" })
|
|
130
|
+
yield* registry.preflight
|
|
131
|
+
yield* Progress.emit({ _tag: "PhaseCompleted", phase: "preflight" })
|
|
132
|
+
|
|
133
|
+
return yield* resolve(specs, options)
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Resolves, downloads and packages a set of specs.
|
|
138
|
+
*
|
|
139
|
+
* Under `dryRun` this stops after resolution and reports the plan — useful for
|
|
140
|
+
* checking what a command *would* pull before committing to a multi-gigabyte
|
|
141
|
+
* download over a slow VPN.
|
|
142
|
+
*/
|
|
143
|
+
export const bundle = (
|
|
144
|
+
specs: ReadonlyArray<PackageSpec>,
|
|
145
|
+
options: BundleContext
|
|
146
|
+
): Effect.Effect<BundleResult, BundlerError | PlatformError, BundleEnv> =>
|
|
147
|
+
Effect.gen(function*() {
|
|
148
|
+
const resolution = yield* plan(specs, options)
|
|
149
|
+
return yield* bundleResolved(resolution, options)
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Downloads and packages an already-computed resolution.
|
|
154
|
+
*
|
|
155
|
+
* Everything from here on touches the disk, and all of it happens inside
|
|
156
|
+
* `Effect.scoped` — so the staging tree's lifetime is exactly this call,
|
|
157
|
+
* including when it fails partway through or the user hits Ctrl-C.
|
|
158
|
+
*/
|
|
159
|
+
export const bundleResolved = (
|
|
160
|
+
resolution: Resolution,
|
|
161
|
+
options: BundleContext
|
|
162
|
+
): Effect.Effect<BundleResult, BundlerError | PlatformError, BundleEnv> =>
|
|
163
|
+
Effect.gen(function*() {
|
|
164
|
+
if (options.dryRun) {
|
|
165
|
+
return {
|
|
166
|
+
resolution,
|
|
167
|
+
artifacts: [],
|
|
168
|
+
downloadedBytes: 0,
|
|
169
|
+
unverified: [],
|
|
170
|
+
dryRun: true
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return yield* Effect.scoped(runBundle(resolution, options))
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
const runBundle = (
|
|
177
|
+
resolution: Resolution,
|
|
178
|
+
options: BundleContext
|
|
179
|
+
): Effect.Effect<
|
|
180
|
+
BundleResult,
|
|
181
|
+
BundlerError | PlatformError,
|
|
182
|
+
BundleEnv | Scope
|
|
183
|
+
> =>
|
|
184
|
+
Effect.gen(function*() {
|
|
185
|
+
const fs = yield* FileSystem.FileSystem
|
|
186
|
+
const path = yield* Path.Path
|
|
187
|
+
const registry = yield* Registry
|
|
188
|
+
|
|
189
|
+
yield* prepareOutputDir(resolution, options)
|
|
190
|
+
|
|
191
|
+
const staging = yield* fs.makeTempDirectoryScoped({ prefix: "packall-" })
|
|
192
|
+
const packagesDir = path.join(staging, "packages")
|
|
193
|
+
yield* fs.makeDirectory(packagesDir, { recursive: true })
|
|
194
|
+
|
|
195
|
+
const report = yield* downloadAll(resolution.packages, packagesDir, {
|
|
196
|
+
concurrency: options.concurrency,
|
|
197
|
+
verifyIntegrity: options.verifyIntegrity
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
yield* Progress.emit({ _tag: "PhaseStarted", phase: "archive" })
|
|
201
|
+
|
|
202
|
+
const registryInfo = {
|
|
203
|
+
kind: registry.kind,
|
|
204
|
+
url: registry.registryFor(resolution.roots[0]?.spec.name ?? "")
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const index = indexPackages(resolution)
|
|
208
|
+
|
|
209
|
+
const artifacts =
|
|
210
|
+
options.layout === "single"
|
|
211
|
+
? yield* emitSingle({ resolution, options, staging, packagesDir, report, registryInfo })
|
|
212
|
+
: options.layout === "dir"
|
|
213
|
+
? yield* emitDirectory({ resolution, options, packagesDir, report, registryInfo })
|
|
214
|
+
: yield* emitPerSpec({
|
|
215
|
+
resolution,
|
|
216
|
+
options,
|
|
217
|
+
staging,
|
|
218
|
+
packagesDir,
|
|
219
|
+
report,
|
|
220
|
+
registryInfo,
|
|
221
|
+
index
|
|
222
|
+
})
|
|
223
|
+
|
|
224
|
+
yield* Progress.emit({ _tag: "PhaseCompleted", phase: "archive" })
|
|
225
|
+
yield* Progress.emit({ _tag: "PhaseStarted", phase: "done" })
|
|
226
|
+
|
|
227
|
+
return {
|
|
228
|
+
resolution,
|
|
229
|
+
artifacts,
|
|
230
|
+
downloadedBytes: report.totalBytes,
|
|
231
|
+
unverified: report.unverified,
|
|
232
|
+
dryRun: false
|
|
233
|
+
}
|
|
234
|
+
})
|
|
235
|
+
|
|
236
|
+
/* -------------------------------------------------------------------------- */
|
|
237
|
+
/* Emitters */
|
|
238
|
+
/* -------------------------------------------------------------------------- */
|
|
239
|
+
|
|
240
|
+
interface EmitInput {
|
|
241
|
+
readonly resolution: Resolution
|
|
242
|
+
readonly options: BundleContext
|
|
243
|
+
readonly packagesDir: string
|
|
244
|
+
readonly report: { readonly sizes: ReadonlyMap<string, number> }
|
|
245
|
+
readonly registryInfo: { readonly kind: string; readonly url: string }
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** One tarball containing the deduplicated union of every closure. */
|
|
249
|
+
const emitSingle = (
|
|
250
|
+
input: EmitInput & { readonly staging: string }
|
|
251
|
+
): Effect.Effect<
|
|
252
|
+
ReadonlyArray<BundleArtifact>,
|
|
253
|
+
BundlerError | PlatformError,
|
|
254
|
+
FileSystem.FileSystem | Path.Path | Progress.Progress
|
|
255
|
+
> =>
|
|
256
|
+
Effect.gen(function*() {
|
|
257
|
+
const path = yield* Path.Path
|
|
258
|
+
|
|
259
|
+
const archiveName = singleArchiveName(input.options.archiveName ?? "bundle")
|
|
260
|
+
// Declined at the prompt, and it is the run's only output.
|
|
261
|
+
if (input.options.skipExisting?.has(archiveName) === true) return []
|
|
262
|
+
|
|
263
|
+
yield* writeBundleMetadata({
|
|
264
|
+
dir: input.packagesDir,
|
|
265
|
+
resolution: input.resolution,
|
|
266
|
+
included: input.resolution.packages,
|
|
267
|
+
options: input.options,
|
|
268
|
+
registryInfo: input.registryInfo,
|
|
269
|
+
sizes: input.report.sizes
|
|
270
|
+
})
|
|
271
|
+
|
|
272
|
+
const outPath = path.join(input.options.outDir, archiveName)
|
|
273
|
+
|
|
274
|
+
const result = yield* createArchive({
|
|
275
|
+
cwd: input.packagesDir,
|
|
276
|
+
entries: yield* topLevelEntries(input.packagesDir),
|
|
277
|
+
outPath
|
|
278
|
+
})
|
|
279
|
+
|
|
280
|
+
return [
|
|
281
|
+
{
|
|
282
|
+
kind: "archive" as const,
|
|
283
|
+
path: result.path,
|
|
284
|
+
bytes: result.bytes,
|
|
285
|
+
packageCount: input.resolution.packages.length
|
|
286
|
+
}
|
|
287
|
+
]
|
|
288
|
+
})
|
|
289
|
+
|
|
290
|
+
/** The raw npm-layout tree, no archive. */
|
|
291
|
+
const emitDirectory = (
|
|
292
|
+
input: EmitInput
|
|
293
|
+
): Effect.Effect<
|
|
294
|
+
ReadonlyArray<BundleArtifact>,
|
|
295
|
+
BundlerError | PlatformError,
|
|
296
|
+
FileSystem.FileSystem | Path.Path
|
|
297
|
+
> =>
|
|
298
|
+
Effect.gen(function*() {
|
|
299
|
+
const fs = yield* FileSystem.FileSystem
|
|
300
|
+
|
|
301
|
+
yield* writeBundleMetadata({
|
|
302
|
+
dir: input.packagesDir,
|
|
303
|
+
resolution: input.resolution,
|
|
304
|
+
included: input.resolution.packages,
|
|
305
|
+
options: input.options,
|
|
306
|
+
registryInfo: input.registryInfo,
|
|
307
|
+
sizes: input.report.sizes
|
|
308
|
+
})
|
|
309
|
+
|
|
310
|
+
// The staging tree is about to be deleted with its scope, so this is a
|
|
311
|
+
// real copy rather than a move — `copy` also works across devices, which
|
|
312
|
+
// matters because the temp dir often lives on a different filesystem.
|
|
313
|
+
yield* fs.copy(input.packagesDir, input.options.outDir, { overwrite: true }).pipe(
|
|
314
|
+
Effect.mapError(
|
|
315
|
+
(cause) => new OutputError(input.options.outDir, "could not write output tree", { cause })
|
|
316
|
+
)
|
|
317
|
+
)
|
|
318
|
+
|
|
319
|
+
const bytes = [...input.report.sizes.values()].reduce((a, b) => a + b, 0)
|
|
320
|
+
|
|
321
|
+
return [
|
|
322
|
+
{
|
|
323
|
+
kind: "directory" as const,
|
|
324
|
+
path: input.options.outDir,
|
|
325
|
+
bytes,
|
|
326
|
+
packageCount: input.resolution.packages.length
|
|
327
|
+
}
|
|
328
|
+
]
|
|
329
|
+
})
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* One tarball per resolved root *version*.
|
|
333
|
+
*
|
|
334
|
+
* Under `--all-versions react@^18` this produces `react-18.0.0.tgz`,
|
|
335
|
+
* `react-18.1.0.tgz` and so on, each independently importable — which is the
|
|
336
|
+
* whole reason to prefer this layout.
|
|
337
|
+
*/
|
|
338
|
+
const emitPerSpec = (
|
|
339
|
+
input: EmitInput & {
|
|
340
|
+
readonly staging: string
|
|
341
|
+
readonly index: ReadonlyMap<PackageKey, ResolvedPackage>
|
|
342
|
+
}
|
|
343
|
+
): Effect.Effect<
|
|
344
|
+
ReadonlyArray<BundleArtifact>,
|
|
345
|
+
BundlerError | PlatformError,
|
|
346
|
+
FileSystem.FileSystem | Path.Path | Progress.Progress
|
|
347
|
+
> =>
|
|
348
|
+
Effect.gen(function*() {
|
|
349
|
+
const fs = yield* FileSystem.FileSystem
|
|
350
|
+
const path = yield* Path.Path
|
|
351
|
+
const artifacts: Array<BundleArtifact> = []
|
|
352
|
+
|
|
353
|
+
let counter = 0
|
|
354
|
+
for (const root of input.resolution.roots) {
|
|
355
|
+
for (const version of root.versions) {
|
|
356
|
+
// Declined at the prompt: the existing file stays, and this archive is
|
|
357
|
+
// simply never built. Skipping the staging work too, not just the write.
|
|
358
|
+
if (input.options.skipExisting?.has(perSpecArchiveName(root.spec.name, version)) === true) {
|
|
359
|
+
continue
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
const closure = root.closures.get(version) ?? []
|
|
363
|
+
const included = closure
|
|
364
|
+
.map((key) => input.index.get(key))
|
|
365
|
+
.filter((pkg): pkg is ResolvedPackage => pkg !== undefined)
|
|
366
|
+
|
|
367
|
+
const rootDir = path.join(input.staging, "roots", String(counter++))
|
|
368
|
+
yield* fs.makeDirectory(rootDir, { recursive: true })
|
|
369
|
+
|
|
370
|
+
for (const pkg of included) {
|
|
371
|
+
const relative = packagePath(pkg.name, pkg.version)
|
|
372
|
+
const parts = relative.split("/")
|
|
373
|
+
const source = path.join(input.packagesDir, ...parts)
|
|
374
|
+
const target = path.join(rootDir, ...parts)
|
|
375
|
+
yield* fs.makeDirectory(path.dirname(target), { recursive: true })
|
|
376
|
+
yield* linkOrCopy(source, target)
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
yield* writeBundleMetadata({
|
|
380
|
+
dir: rootDir,
|
|
381
|
+
resolution: {
|
|
382
|
+
...input.resolution,
|
|
383
|
+
roots: [{ ...root, versions: [version] }]
|
|
384
|
+
},
|
|
385
|
+
included,
|
|
386
|
+
options: input.options,
|
|
387
|
+
registryInfo: input.registryInfo,
|
|
388
|
+
sizes: input.report.sizes
|
|
389
|
+
})
|
|
390
|
+
|
|
391
|
+
const outPath = path.join(
|
|
392
|
+
input.options.outDir,
|
|
393
|
+
perSpecArchiveName(root.spec.name, version)
|
|
394
|
+
)
|
|
395
|
+
|
|
396
|
+
const result = yield* createArchive({
|
|
397
|
+
cwd: rootDir,
|
|
398
|
+
entries: yield* topLevelEntries(rootDir),
|
|
399
|
+
outPath
|
|
400
|
+
})
|
|
401
|
+
|
|
402
|
+
artifacts.push({
|
|
403
|
+
kind: "archive",
|
|
404
|
+
path: result.path,
|
|
405
|
+
bytes: result.bytes,
|
|
406
|
+
packageCount: included.length,
|
|
407
|
+
spec: formatSpec(root.spec),
|
|
408
|
+
version
|
|
409
|
+
})
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
return artifacts
|
|
414
|
+
})
|
|
415
|
+
|
|
416
|
+
/* -------------------------------------------------------------------------- */
|
|
417
|
+
/* Helpers */
|
|
418
|
+
/* -------------------------------------------------------------------------- */
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* Hard-links a file, falling back to a copy.
|
|
422
|
+
*
|
|
423
|
+
* Hard links make `per-spec` layout nearly free in the staging tree. They fail
|
|
424
|
+
* across devices and on some Windows configurations, so the copy fallback is
|
|
425
|
+
* not optional.
|
|
426
|
+
*/
|
|
427
|
+
const linkOrCopy = (
|
|
428
|
+
source: string,
|
|
429
|
+
target: string
|
|
430
|
+
): Effect.Effect<void, PlatformError, FileSystem.FileSystem> =>
|
|
431
|
+
Effect.gen(function*() {
|
|
432
|
+
const fs = yield* FileSystem.FileSystem
|
|
433
|
+
yield* fs.link(source, target).pipe(
|
|
434
|
+
Effect.catch(() => fs.copyFile(source, target))
|
|
435
|
+
)
|
|
436
|
+
})
|
|
437
|
+
|
|
438
|
+
/** Writes the manifest and import guide into a staging directory. */
|
|
439
|
+
const writeBundleMetadata = (input: {
|
|
440
|
+
readonly dir: string
|
|
441
|
+
readonly resolution: Resolution
|
|
442
|
+
readonly included: ReadonlyArray<ResolvedPackage>
|
|
443
|
+
readonly options: BundleContext
|
|
444
|
+
readonly registryInfo: { readonly kind: string; readonly url: string }
|
|
445
|
+
readonly sizes: ReadonlyMap<string, number>
|
|
446
|
+
}): Effect.Effect<void, PlatformError, FileSystem.FileSystem | Path.Path> =>
|
|
447
|
+
Effect.gen(function*() {
|
|
448
|
+
const fs = yield* FileSystem.FileSystem
|
|
449
|
+
const path = yield* Path.Path
|
|
450
|
+
|
|
451
|
+
const createdAt = new Date()
|
|
452
|
+
const manifest = buildManifest({
|
|
453
|
+
resolution: input.resolution,
|
|
454
|
+
included: input.included,
|
|
455
|
+
options: input.options,
|
|
456
|
+
registry: input.registryInfo,
|
|
457
|
+
toolVersion: input.options.toolVersion,
|
|
458
|
+
sizes: input.sizes,
|
|
459
|
+
createdAt
|
|
460
|
+
})
|
|
461
|
+
|
|
462
|
+
yield* fs.writeFileString(path.join(input.dir, MANIFEST_FILE), serializeManifest(manifest))
|
|
463
|
+
yield* fs.writeFileString(
|
|
464
|
+
path.join(input.dir, README_FILE),
|
|
465
|
+
importGuide({
|
|
466
|
+
packageCount: input.included.length,
|
|
467
|
+
createdAt: createdAt.toISOString(),
|
|
468
|
+
toolVersion: input.options.toolVersion
|
|
469
|
+
})
|
|
470
|
+
)
|
|
471
|
+
})
|
|
472
|
+
|
|
473
|
+
/** Sorted top-level entries of a directory, used as the tar entry list. */
|
|
474
|
+
const topLevelEntries = (
|
|
475
|
+
dir: string
|
|
476
|
+
): Effect.Effect<ReadonlyArray<string>, PlatformError, FileSystem.FileSystem> =>
|
|
477
|
+
Effect.gen(function*() {
|
|
478
|
+
const fs = yield* FileSystem.FileSystem
|
|
479
|
+
const entries = yield* fs.readDirectory(dir)
|
|
480
|
+
return [...entries].sort()
|
|
481
|
+
})
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* Creates the output directory, refusing to clobber an existing non-empty one
|
|
485
|
+
* unless `--force` was passed.
|
|
486
|
+
*
|
|
487
|
+
* Silently overwriting somebody's previous bundle is the kind of thing a tool
|
|
488
|
+
* only gets forgiven for once.
|
|
489
|
+
*/
|
|
490
|
+
/**
|
|
491
|
+
* The files a run will write, known before anything is downloaded.
|
|
492
|
+
*
|
|
493
|
+
* `dir` layout is the odd one: it merges a package tree into `outDir`, and
|
|
494
|
+
* package paths are `name/-/name-version.tgz` — the same package at the same
|
|
495
|
+
* version is the same bytes, so an overlap there is idempotent rather than
|
|
496
|
+
* destructive. Only the two summary files are genuinely replaced, and those are
|
|
497
|
+
* the ones worth guarding.
|
|
498
|
+
*/
|
|
499
|
+
export interface PlannedOutput {
|
|
500
|
+
readonly file: string
|
|
501
|
+
/** The root this file belongs to, when one file corresponds to one spec. */
|
|
502
|
+
readonly name?: string | undefined
|
|
503
|
+
readonly version?: string | undefined
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
export const plannedOutputs = (
|
|
507
|
+
resolution: Resolution,
|
|
508
|
+
options: BundleContext
|
|
509
|
+
): ReadonlyArray<PlannedOutput> => {
|
|
510
|
+
if (options.layout === "dir") return [{ file: MANIFEST_FILE }, { file: README_FILE }]
|
|
511
|
+
if (options.layout === "single") {
|
|
512
|
+
return [{ file: singleArchiveName(options.archiveName ?? "bundle") }]
|
|
513
|
+
}
|
|
514
|
+
return resolution.roots.flatMap((root) =>
|
|
515
|
+
root.versions.map((version) => ({
|
|
516
|
+
file: perSpecArchiveName(root.spec.name, version),
|
|
517
|
+
name: root.spec.name,
|
|
518
|
+
version
|
|
519
|
+
}))
|
|
520
|
+
)
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/**
|
|
524
|
+
* Fails if the run would overwrite something, unless `--force`.
|
|
525
|
+
*
|
|
526
|
+
* The guard is on the *files this run writes*, not on whether the directory has
|
|
527
|
+
* anything in it. An output directory accumulating bundles is the normal way to
|
|
528
|
+
* use this — bundling `esbuild` into an `out/` that already holds `tsdown` is
|
|
529
|
+
* not a conflict, and refusing it made `--out` a single-use directory and the
|
|
530
|
+
* default `.` unusable.
|
|
531
|
+
*
|
|
532
|
+
* Checked before anything is downloaded. Discovering the collision *after* a
|
|
533
|
+
* twenty-minute download would be a uniquely irritating way to fail.
|
|
534
|
+
*/
|
|
535
|
+
const prepareOutputDir = (
|
|
536
|
+
resolution: Resolution,
|
|
537
|
+
options: BundleContext
|
|
538
|
+
): Effect.Effect<void, OutputError | PlatformError, FileSystem.FileSystem | Path.Path> =>
|
|
539
|
+
Effect.gen(function*() {
|
|
540
|
+
const fs = yield* FileSystem.FileSystem
|
|
541
|
+
const path = yield* Path.Path
|
|
542
|
+
const outDir = options.outDir
|
|
543
|
+
|
|
544
|
+
if (!options.force) {
|
|
545
|
+
const clashes: Array<string> = []
|
|
546
|
+
for (const planned of plannedOutputs(resolution, options)) {
|
|
547
|
+
if (options.skipExisting?.has(planned.file) === true) continue
|
|
548
|
+
if (options.overwrite?.has(planned.file) === true) continue
|
|
549
|
+
const exists = yield* fs
|
|
550
|
+
.exists(path.join(outDir, planned.file))
|
|
551
|
+
.pipe(Effect.orElseSucceed(() => false))
|
|
552
|
+
if (exists) clashes.push(planned.file)
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
if (clashes.length > 0) {
|
|
556
|
+
return yield* Effect.fail(
|
|
557
|
+
new OutputError(
|
|
558
|
+
outDir,
|
|
559
|
+
`would overwrite ${clashes.length} existing file${clashes.length === 1 ? "" : "s"} ` +
|
|
560
|
+
`(${clashes.slice(0, 3).join(", ")}${clashes.length > 3 ? ", …" : ""}). ` +
|
|
561
|
+
`Pass --force true to replace ${clashes.length === 1 ? "it" : "them"}, or choose a different --out.`
|
|
562
|
+
)
|
|
563
|
+
)
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
yield* fs.makeDirectory(outDir, { recursive: true }).pipe(
|
|
568
|
+
Effect.mapError((cause) => new OutputError(outDir, "could not create output directory", { cause }))
|
|
569
|
+
)
|
|
570
|
+
})
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Classification of the right-hand side of a dependency entry.
|
|
3
|
+
*
|
|
4
|
+
* `"dependencies": { "x": "^1.0.0" }` is the easy case. Real package.json files
|
|
5
|
+
* also contain aliases, git URLs, tarball URLs, `file:` links and
|
|
6
|
+
* `workspace:` protocols — none of which can be fetched from a registry.
|
|
7
|
+
*
|
|
8
|
+
* Rather than crashing or silently producing a bundle that will not install,
|
|
9
|
+
* we classify each edge and let the caller report precisely what was skipped
|
|
10
|
+
* and why.
|
|
11
|
+
*/
|
|
12
|
+
import semver from "semver"
|
|
13
|
+
import type { Selector } from "./Spec.js"
|
|
14
|
+
|
|
15
|
+
/** What a dependency edge points at. */
|
|
16
|
+
export type DependencyTarget =
|
|
17
|
+
/** A normal registry dependency. `name` may differ from the key under an alias. */
|
|
18
|
+
| {
|
|
19
|
+
readonly _tag: "Registry"
|
|
20
|
+
readonly name: string
|
|
21
|
+
readonly selector: Selector
|
|
22
|
+
/** Set when the edge is an `npm:` alias, e.g. `"lodash4": "npm:lodash@^4"`. */
|
|
23
|
+
readonly aliasOf?: string | undefined
|
|
24
|
+
}
|
|
25
|
+
/** Something we cannot fetch from a registry. */
|
|
26
|
+
| { readonly _tag: "Unsupported"; readonly name: string; readonly raw: string; readonly reason: string }
|
|
27
|
+
|
|
28
|
+
const UNSUPPORTED_PROTOCOLS: ReadonlyArray<readonly [string, string]> = [
|
|
29
|
+
["file:", "local file path"],
|
|
30
|
+
["link:", "local link"],
|
|
31
|
+
["workspace:", "workspace protocol"],
|
|
32
|
+
["portal:", "portal protocol"],
|
|
33
|
+
["patch:", "patch protocol"],
|
|
34
|
+
["git:", "git dependency"],
|
|
35
|
+
["git+", "git dependency"],
|
|
36
|
+
["github:", "GitHub shorthand"],
|
|
37
|
+
["gitlab:", "GitLab shorthand"],
|
|
38
|
+
["bitbucket:", "Bitbucket shorthand"],
|
|
39
|
+
["http:", "remote tarball URL"],
|
|
40
|
+
["https:", "remote tarball URL"]
|
|
41
|
+
]
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Classifies one `name -> range` entry.
|
|
45
|
+
*
|
|
46
|
+
* An empty range, `*`, and `latest` all mean "any published version"; npm
|
|
47
|
+
* treats them interchangeably and so do we.
|
|
48
|
+
*/
|
|
49
|
+
export const parseDependencyTarget = (name: string, raw: string): DependencyTarget => {
|
|
50
|
+
const text = raw.trim()
|
|
51
|
+
|
|
52
|
+
if (text.length === 0 || text === "*" || text === "x" || text === "latest") {
|
|
53
|
+
return { _tag: "Registry", name, selector: { _tag: "Range", range: "*" } }
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (text.startsWith("npm:")) {
|
|
57
|
+
return parseAlias(name, text)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
for (const [prefix, reason] of UNSUPPORTED_PROTOCOLS) {
|
|
61
|
+
if (text.startsWith(prefix)) {
|
|
62
|
+
return { _tag: "Unsupported", name, raw: text, reason }
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Bare `owner/repo` is GitHub shorthand, but only when it is not a valid
|
|
67
|
+
// range — `>=1.0.0/2` is not a thing, so this ordering is safe.
|
|
68
|
+
if (semver.validRange(text, { loose: true }) === null && /^[\w.-]+\/[\w.-]+/.test(text)) {
|
|
69
|
+
return { _tag: "Unsupported", name, raw: text, reason: "git shorthand" }
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const exact = semver.valid(text, { loose: true })
|
|
73
|
+
if (exact !== null) {
|
|
74
|
+
return { _tag: "Registry", name, selector: { _tag: "Exact", version: exact } }
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (semver.validRange(text, { loose: true }) !== null) {
|
|
78
|
+
return { _tag: "Registry", name, selector: { _tag: "Range", range: text } }
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Whatever is left is most plausibly a dist-tag published by a private
|
|
82
|
+
// registry, e.g. `"@acme/sdk": "stable"`.
|
|
83
|
+
if (/^[a-zA-Z0-9][a-zA-Z0-9._-]*$/.test(text)) {
|
|
84
|
+
return { _tag: "Registry", name, selector: { _tag: "Tag", tag: text } }
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return { _tag: "Unsupported", name, raw: text, reason: "unrecognised version specifier" }
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Parses `npm:<name>[@<range>]`.
|
|
92
|
+
*
|
|
93
|
+
* The nested name may itself be scoped, so the `@` split has the same
|
|
94
|
+
* "not at index 0" caveat as top-level spec parsing.
|
|
95
|
+
*/
|
|
96
|
+
const parseAlias = (key: string, text: string): DependencyTarget => {
|
|
97
|
+
const body = text.slice("npm:".length)
|
|
98
|
+
if (body.length === 0) {
|
|
99
|
+
return { _tag: "Unsupported", name: key, raw: text, reason: "empty npm: alias" }
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const at = body.lastIndexOf("@")
|
|
103
|
+
const target = at > 0 ? body.slice(0, at) : body
|
|
104
|
+
const rangeText = at > 0 ? body.slice(at + 1) : ""
|
|
105
|
+
|
|
106
|
+
if (target.length === 0) {
|
|
107
|
+
return { _tag: "Unsupported", name: key, raw: text, reason: "empty npm: alias target" }
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const inner = parseDependencyTarget(target, rangeText)
|
|
111
|
+
if (inner._tag === "Unsupported") {
|
|
112
|
+
return { ...inner, name: key, raw: text }
|
|
113
|
+
}
|
|
114
|
+
return { _tag: "Registry", name: target, selector: inner.selector, aliasOf: key }
|
|
115
|
+
}
|