The best way to control Claude Code, Codex and more from any computer or phone

main
ts 760 lines 26.4 KB
Raw
1 import { gunzipSync, gzipSync } from "node:zlib";
2 import { exec } from "alchemy/Util/exec";
3 import { sha256 } from "alchemy/Util/sha256";
4 import * as Config from "effect/Config";
5 import * as Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import * as FileSystem from "effect/FileSystem";
8 import * as Option from "effect/Option";
9 import * as Path from "effect/Path";
10 import * as ChildProcess from "effect/process/ChildProcess";
11 import * as Schema from "effect/Schema";
12 import * as SchemaIssue from "effect/SchemaIssue";
13 import * as SchemaTransformation from "effect/SchemaTransformation";
14 import { packTar, unpackTar, type TarHeader } from "modern-tar";
15 import {
16 GroupName,
17 MANIFEST_FILE,
18 ManifestJson,
19 type Manifest,
20 type ManifestPackage,
21 } from "../Manifest.ts";
22 import { manifestArtifactName, tarballUrl } from "../Protocol.ts";
23
24 export class WorkspaceError extends Data.TaggedError("WorkspaceError")<{
25 readonly message: string;
26 }> {}
27
28 const GROUP_SPEC = /^([^[=]+?)\s*(?:\[([^\]]*)\])?\s*=\s*(.+?)\s*$/;
29
30 /**
31 * A `--group` flag, `NAME=GLOB` or `NAME[Collapsed]=GLOB`, e.g.
32 * `Alchemy=./packages/*` or `Distilled[Collapsed]=./submodules/distilled/packages/*`.
33 * `Collapsed` is the only attribute.
34 */
35 export const Group = Schema.String.pipe(
36 Schema.decodeTo(
37 Schema.Struct({
38 name: GroupName,
39 pattern: Schema.String,
40 /** Render the group collapsed in the install comment. */
41 collapsed: Schema.Boolean,
42 }),
43 SchemaTransformation.transformEffect({
44 decode: (spec, options) => {
45 const match = spec.match(GROUP_SPEC);
46 const attributes = (match?.[2] ?? "")
47 .split(",")
48 .map((attribute) => attribute.trim())
49 .filter((attribute) => attribute.length > 0);
50 return match && attributes.every((attribute) => attribute === "Collapsed")
51 ? Effect.succeed({
52 name: match[1]!,
53 pattern: match[3]!,
54 collapsed: attributes.includes("Collapsed"),
55 })
56 : Effect.fail(
57 new SchemaIssue.InvalidValue(
58 { expected: "NAME=GLOB or NAME[Collapsed]=GLOB" },
59 spec,
60 options,
61 ),
62 );
63 },
64 encode: (group) =>
65 Effect.succeed(`${group.name}${group.collapsed ? "[Collapsed]" : ""}=${group.pattern}`),
66 }),
67 ),
68 );
69 export type Group = typeof Group.Type;
70
71 /** The subset of `package.json` the CLI reads. Extra keys are preserved on the raw object. */
72 export const PackageJson = Schema.Struct({
73 name: Schema.optionalKey(Schema.String),
74 version: Schema.optionalKey(Schema.String),
75 private: Schema.optionalKey(Schema.Boolean),
76 });
77
78 export const DEPENDENCY_SECTIONS = [
79 "dependencies",
80 "devDependencies",
81 "peerDependencies",
82 "optionalDependencies",
83 ] as const;
84
85 const DependencyMap = Schema.optionalKey(Schema.Record(Schema.String, Schema.String));
86
87 export const DependencySections = Schema.Struct({
88 dependencies: DependencyMap,
89 devDependencies: DependencyMap,
90 peerDependencies: DependencyMap,
91 optionalDependencies: DependencyMap,
92 });
93
94 /**
95 * Order packages so every package comes after the packed packages it
96 * depends on, grouped into levels that can be packed concurrently. Fails on
97 * a cycle, since a tarball cannot link to a dependency that links back.
98 */
99 export const dependencyLevels = Effect.fn("dependencyLevels")(function* (
100 dependencies: ReadonlyMap<string, ReadonlySet<string>>,
101 ) {
102 const remaining = new Map(
103 [...dependencies].map(([name, deps]) => [
104 name,
105 new Set([...deps].filter((dep) => dependencies.has(dep))),
106 ]),
107 );
108 const levels: string[][] = [];
109 while (remaining.size > 0) {
110 const ready = [...remaining]
111 .filter(([, deps]) => deps.size === 0)
112 .map(([name]) => name)
113 .sort();
114 if (ready.length === 0) {
115 return yield* new WorkspaceError({
116 message: `Dependency cycle among packed packages: ${[...remaining.keys()].sort().join(", ")}`,
117 });
118 }
119 for (const name of ready) remaining.delete(name);
120 for (const deps of remaining.values()) {
121 for (const name of ready) deps.delete(name);
122 }
123 levels.push(ready);
124 }
125 return levels;
126 });
127
128 export interface WorkspacePackage {
129 readonly name: string;
130 readonly version: string;
131 /** Relative to the workspace root, POSIX separators. */
132 readonly dir: string;
133 readonly absDir: string;
134 readonly group: string;
135 }
136
137 /** Shared inputs that invalidate every configured package. */
138 export const REBUILD_ALL_PATHS = [
139 "package.json",
140 "pnpm-lock.yaml",
141 "pnpm-workspace.yaml",
142 "package-lock.json",
143 "yarn.lock",
144 "bun.lock",
145 "bun.lockb",
146 "tsconfig.json",
147 "tsconfig.base.json",
148 "turbo.json",
149 ".github/workflows/pkg.yml",
150 ".github/workflows/pr-package.yml",
151 ];
152
153 /** Select dependents first, then dependencies, without pulling in unrelated siblings. */
154 export const selectPackages = (
155 packages: ReadonlyArray<WorkspacePackage>,
156 dependencies: ReadonlyMap<string, ReadonlySet<string>>,
157 changedFiles: ReadonlyArray<string>,
158 rebuildAllPaths: ReadonlyArray<string> = [],
159 ) => {
160 if (
161 changedFiles.some((file) =>
162 [...REBUILD_ALL_PATHS, ...rebuildAllPaths].some((pattern) =>
163 pattern.endsWith("/**")
164 ? file === pattern.slice(0, -3) || file.startsWith(pattern.slice(0, -2))
165 : file === pattern,
166 ),
167 )
168 )
169 return [...packages];
170
171 const selected = new Set(
172 packages
173 .filter((pkg) =>
174 changedFiles.some(
175 (file) =>
176 file === pkg.dir || file.startsWith(`${pkg.dir}/`) || pkg.dir.startsWith(`${file}/`),
177 ),
178 )
179 .map((pkg) => pkg.name),
180 );
181 // A changed gitlink selects the packages nested inside that submodule.
182 let size: number;
183 do {
184 size = selected.size;
185 for (const [name, deps] of dependencies) {
186 if ([...deps].some((dep) => selected.has(dep))) selected.add(name);
187 }
188 } while (selected.size !== size);
189 const pending = [...selected];
190 for (const name of pending) {
191 for (const dep of dependencies.get(name) ?? []) {
192 if (!selected.has(dep)) {
193 selected.add(dep);
194 pending.push(dep);
195 }
196 }
197 }
198 return packages.filter((pkg) => selected.has(pkg.name));
199 };
200
201 /**
202 * Expand one level of `{a,b,c}` alternatives into plain patterns, so
203 * `./packages/{alchemy,pkg}` lists exactly those two directories.
204 */
205 export const expandBraces = (pattern: string): string[] => {
206 const match = pattern.match(/^(.*?)\{([^{}]*)\}(.*)$/);
207 if (!match) return [pattern];
208 return match[2]!
209 .split(",")
210 .map((alternative) => alternative.trim())
211 .filter((alternative) => alternative.length > 0)
212 .flatMap((alternative) => expandBraces(`${match[1]}${alternative}${match[3]}`));
213 };
214
215 /**
216 * Expand a directory glob. `*` is supported as a whole path segment and
217 * `{a,b}` as a list of alternatives, which covers `./packages/*` and
218 * `./submodules/x/packages/{core,aws}`. Matches are directories only.
219 */
220 const expand = Effect.fn("expandGlob")(function* (cwd: string, glob: string) {
221 const results: string[] = [];
222 for (const pattern of expandBraces(glob)) {
223 results.push(...(yield* expandPattern(cwd, pattern)));
224 }
225 return [...new Set(results)];
226 });
227
228 const expandPattern = Effect.fn("expandPattern")(function* (cwd: string, pattern: string) {
229 const fs = yield* FileSystem.FileSystem;
230 const path = yield* Path.Path;
231 if (pattern.includes("**")) {
232 return yield* new WorkspaceError({
233 message: `Unsupported pattern ${JSON.stringify(pattern)}: only a single "*" segment is supported`,
234 });
235 }
236 const segments = pattern.split("/").filter((s) => s !== "" && s !== ".");
237 let current: string[] = [cwd];
238 for (const segment of segments) {
239 const next: string[] = [];
240 for (const base of current) {
241 if (segment === "*") {
242 const entries = yield* fs.readDirectory(base);
243 for (const entry of entries.sort()) {
244 const candidate = path.join(base, entry);
245 const stat = yield* fs.stat(candidate);
246 if (stat.type === "Directory") next.push(candidate);
247 }
248 } else if (segment.includes("*")) {
249 return yield* new WorkspaceError({
250 message: `Unsupported pattern ${JSON.stringify(pattern)}: "*" must be a whole path segment`,
251 });
252 } else {
253 const candidate = path.join(base, segment);
254 if (yield* fs.exists(candidate)) next.push(candidate);
255 }
256 }
257 current = next;
258 }
259 return current;
260 });
261
262 /**
263 * Discover publishable packages under each group's pattern, in the order the
264 * groups and their directories were given, which is the order they are
265 * listed in. Private packages and directories without a named
266 * `package.json` are skipped. A package name appearing under two groups is
267 * an error.
268 */
269 export const discover = Effect.fn("discoverPackages")(function* (
270 cwd: string,
271 groups: ReadonlyArray<Group>,
272 ) {
273 const fs = yield* FileSystem.FileSystem;
274 const path = yield* Path.Path;
275 const decode = Schema.decodeUnknownEffect(Schema.fromJsonString(PackageJson));
276 const found = new Map<string, WorkspacePackage>();
277 for (const group of groups) {
278 for (const absDir of yield* expand(cwd, group.pattern)) {
279 const manifestPath = path.join(absDir, "package.json");
280 if (!(yield* fs.exists(manifestPath))) continue;
281 const manifest = yield* decode(yield* fs.readFileString(manifestPath));
282 if (manifest.private || manifest.name === undefined) continue;
283 const existing = found.get(manifest.name);
284 if (existing !== undefined) {
285 return yield* new WorkspaceError({
286 message: `Package ${manifest.name} found in both ${existing.dir} and ${path.relative(cwd, absDir)}`,
287 });
288 }
289 found.set(manifest.name, {
290 name: manifest.name,
291 version: manifest.version ?? "0.0.0",
292 dir: path.relative(cwd, absDir).split(path.sep).join("/"),
293 absDir,
294 group: group.name,
295 });
296 }
297 }
298 return [...found.values()];
299 });
300
301 /** Display group of workspace dependencies packed without being listed by `--group`. */
302 export const TRANSITIVE_GROUP: Group = {
303 name: "Transitive Dependencies",
304 pattern: "",
305 collapsed: true,
306 };
307
308 /** Sections a consumer installs, so a workspace dependency in one has to be published too. */
309 const INSTALLED_SECTIONS = ["dependencies", "peerDependencies", "optionalDependencies"] as const;
310
311 /**
312 * Add every `workspace:` dependency the discovered packages install,
313 * transitively, so a listed package never links to a version that was
314 * not published alongside it. Each dependency is resolved the way Node
315 * would, through the `node_modules` link the package manager created,
316 * walking up no further than `root`. Added packages join
317 * {@link TRANSITIVE_GROUP}.
318 */
319 export const discoverWorkspaceDependencies = Effect.fn("discoverWorkspaceDependencies")(function* (
320 cwd: string,
321 root: string,
322 packages: ReadonlyArray<WorkspacePackage>,
323 ) {
324 const fs = yield* FileSystem.FileSystem;
325 const path = yield* Path.Path;
326 const decode = Schema.decodeUnknownEffect(
327 Schema.fromJsonString(Schema.Struct({ ...PackageJson.fields, ...DependencySections.fields })),
328 );
329 // Links resolve to real paths, so compare everything by real path.
330 const realCwd = yield* fs.realPath(cwd);
331 const realRoot = yield* fs.realPath(root);
332 const found = new Map(packages.map((pkg) => [pkg.name, pkg]));
333 const realDirs = new Map<string, string>();
334 for (const pkg of packages) realDirs.set(pkg.name, yield* fs.realPath(pkg.absDir));
335 const added: WorkspacePackage[] = [];
336
337 const resolve = Effect.fn(function* (from: string, name: string) {
338 let dir = from;
339 while (true) {
340 const link = path.join(dir, "node_modules", name);
341 if (yield* fs.exists(link)) return yield* fs.realPath(link);
342 const relative = path.relative(realRoot, dir);
343 if (relative === "" || relative.startsWith("..")) break;
344 dir = path.dirname(dir);
345 }
346 return yield* new WorkspaceError({
347 message: `Cannot resolve workspace dependency ${name} from ${from}: install dependencies before packing`,
348 });
349 });
350
351 const pending = [...packages];
352 for (const dependent of pending) {
353 const manifest = yield* decode(
354 yield* fs.readFileString(path.join(dependent.absDir, "package.json")),
355 );
356 const names = INSTALLED_SECTIONS.flatMap((section) =>
357 Object.entries(manifest[section] ?? {})
358 .filter(([, spec]) => spec.startsWith("workspace:"))
359 .map(([name]) => name),
360 );
361 for (const name of [...new Set(names)].sort()) {
362 if (found.has(name)) continue;
363 const absDir = yield* resolve(realDirs.get(dependent.name)!, name);
364 const dependency = yield* decode(yield* fs.readFileString(path.join(absDir, "package.json")));
365 if (dependency.private) {
366 return yield* new WorkspaceError({
367 message: `${dependent.name} depends on private workspace package ${name}, which cannot be published`,
368 });
369 }
370 const pkg: WorkspacePackage = {
371 name,
372 version: dependency.version ?? "0.0.0",
373 dir: path.relative(realCwd, absDir).split(path.sep).join("/"),
374 absDir,
375 group: TRANSITIVE_GROUP.name,
376 };
377 found.set(name, pkg);
378 realDirs.set(name, absDir);
379 added.push(pkg);
380 pending.push(pkg);
381 }
382 }
383 return added;
384 });
385
386 export class GitError extends Data.TaggedError("GitError")<{
387 readonly args: ReadonlyArray<string>;
388 readonly cwd: string;
389 readonly exitCode: number;
390 readonly stderr: string;
391 }> {
392 override get message() {
393 return `git ${this.args.join(" ")} in ${this.cwd} exited with ${this.exitCode}: ${this.stderr.trim()}`;
394 }
395 }
396
397 /** Run `git` in `cwd` and return trimmed stdout. */
398 export const git = Effect.fn("git")(function* (cwd: string, args: ReadonlyArray<string>) {
399 const { exitCode, stdout, stderr } = yield* exec(
400 ChildProcess.make("git", [...args], { cwd, shell: false }),
401 ).pipe(Effect.scoped);
402 if (exitCode !== 0) {
403 return yield* new GitError({ args, cwd, exitCode, stderr });
404 }
405 return stdout.trim();
406 });
407
408 /** Absolute path of the repository (or submodule) that owns `cwd`. */
409 export const toplevel = (cwd: string) => git(cwd, ["rev-parse", "--show-toplevel"]);
410
411 /** Full HEAD SHA of the repository that owns `cwd`. */
412 const gitHead = (cwd: string) => git(cwd, ["rev-parse", "HEAD"]);
413
414 export class PackError extends Data.TaggedError("PackError")<{
415 readonly dir: string;
416 readonly message: string;
417 }> {}
418
419 const PnpmPackOutput = Schema.fromJsonString(Schema.Struct({ filename: Schema.String }));
420
421 /**
422 * Rewrite every dependency on a package in `links` to that package's
423 * tarball URL. Returns the rewritten manifest text and the rewrites made.
424 */
425 export const rewriteDependencies = (manifestText: string, links: ReadonlyMap<string, string>) =>
426 Effect.gen(function* () {
427 const manifest = yield* Schema.decodeEffect(
428 Schema.fromJsonString(
429 Schema.StructWithRest(DependencySections, [Schema.Record(Schema.String, Schema.Unknown)]),
430 ),
431 )(manifestText);
432 // Preserve source key order after validation: it contributes to the tarball hash.
433 const rewritten = JSON.parse(manifestText);
434 const rewrites: Array<{ section: string; name: string; url: string }> = [];
435 for (const section of DEPENDENCY_SECTIONS) {
436 const deps = manifest[section];
437 if (deps === undefined) continue;
438 const next: Record<string, string> = { ...deps };
439 for (const name of Object.keys(deps)) {
440 const url = links.get(name);
441 if (url === undefined) continue;
442 next[name] = url;
443 rewrites.push({ section, name, url });
444 }
445 rewritten[section] = next;
446 }
447 return { text: `${JSON.stringify(rewritten, null, 2)}\n`, rewrites };
448 });
449
450 const EPOCH = new Date(0);
451
452 /**
453 * Normalize tar headers so identical inputs produce identical bytes:
454 * fixed mtime, no ownership, entries sorted by path.
455 */
456 const normalize = (header: TarHeader, size: number): TarHeader => ({
457 name: header.name,
458 size,
459 mode: header.mode,
460 type: header.type ?? "file",
461 mtime: EPOCH,
462 uid: 0,
463 gid: 0,
464 uname: "",
465 gname: "",
466 ...(header.linkname !== undefined ? { linkname: header.linkname } : {}),
467 });
468
469 export interface PackedTarball {
470 readonly file: string;
471 readonly sha256: string;
472 readonly size: number;
473 readonly rewrites: ReadonlyArray<{
474 section: string;
475 name: string;
476 url: string;
477 }>;
478 }
479
480 /**
481 * Pack one package with pnpm, rewrite its dependencies on already-packed
482 * packages to their tarball URLs, and repack reproducibly into `outDir/file`.
483 */
484 export const packPackage = Effect.fn("packPackage")(function* (options: {
485 readonly absDir: string;
486 /** Tarball URL of every already-packed dependency, by package name. */
487 readonly links: ReadonlyMap<string, string>;
488 readonly outDir: string;
489 readonly file: string;
490 }) {
491 const fs = yield* FileSystem.FileSystem;
492 const path = yield* Path.Path;
493 const tmp = yield* fs.makeTempDirectoryScoped({ prefix: "pkg-pack-" });
494
495 const { exitCode, stdout, stderr } = yield* exec(
496 ChildProcess.make("pnpm", ["pack", "--json", "--pack-destination", tmp], {
497 cwd: options.absDir,
498 shell: false,
499 }),
500 );
501 if (exitCode !== 0) {
502 return yield* new PackError({
503 dir: options.absDir,
504 message: `pnpm pack exited with ${exitCode}: ${stderr.trim() || stdout.trim()}`,
505 });
506 }
507 // pnpm may print progress lines before the JSON document.
508 const json = stdout.slice(stdout.indexOf("{"));
509 const { filename } = yield* Schema.decodeEffect(PnpmPackOutput)(json);
510
511 const packed = yield* fs.readFile(path.join(tmp, path.basename(filename)));
512 const inflated = yield* Effect.sync(() => gunzipSync(packed));
513 const entries = yield* Effect.promise(() => unpackTar(inflated));
514
515 let rewrites: PackedTarball["rewrites"] = [];
516 const normalized: Array<{ header: TarHeader; data: Uint8Array }> = [];
517 for (const entry of entries.sort((a, b) => a.header.name.localeCompare(b.header.name))) {
518 let data = entry.data ?? new Uint8Array();
519 if (entry.header.name === "package/package.json") {
520 const text = yield* Effect.sync(() => new TextDecoder().decode(data));
521 const result = yield* rewriteDependencies(text, options.links).pipe(
522 Effect.mapError((e) => new PackError({ dir: options.absDir, message: String(e) })),
523 );
524 rewrites = result.rewrites;
525 data = yield* Effect.sync(() => new TextEncoder().encode(result.text));
526 }
527 normalized.push({ header: normalize(entry.header, data.byteLength), data });
528 }
529
530 const tar = yield* Effect.promise(() => packTar(normalized));
531 const bytes = yield* Effect.sync(() => gzipSync(tar, { level: 9 }));
532 yield* fs.writeFile(path.join(options.outDir, options.file), bytes);
533 return {
534 file: options.file,
535 sha256: yield* sha256(bytes),
536 size: bytes.byteLength,
537 rewrites,
538 } satisfies PackedTarball;
539 });
540
541 const PullRequestEvent = Schema.fromJsonString(
542 Schema.Struct({
543 pull_request: Schema.optionalKey(
544 Schema.Struct({
545 head: Schema.Struct({ sha: Schema.String }),
546 base: Schema.Struct({ sha: Schema.String }),
547 }),
548 ),
549 }),
550 );
551
552 /**
553 * Pull request metadata from the GitHub Actions event payload.
554 * `undefined` outside pull request events.
555 * On that event the default checkout is a synthetic merge commit, which the
556 * registry would reject because it does not match the run's head.
557 */
558 const pullRequest = Effect.gen(function* () {
559 const event = yield* Config.option(Config.String("GITHUB_EVENT_NAME"));
560 const eventPath = yield* Config.option(Config.String("GITHUB_EVENT_PATH"));
561 if (Option.getOrUndefined(event) !== "pull_request" || Option.isNone(eventPath)) {
562 return undefined;
563 }
564 const fs = yield* FileSystem.FileSystem;
565 const payload = yield* fs
566 .readFileString(eventPath.value)
567 .pipe(Effect.flatMap(Schema.decodeUnknownEffect(PullRequestEvent)));
568 return payload.pull_request;
569 });
570
571 export interface PackOptions {
572 readonly cwd: string;
573 readonly groups: ReadonlyArray<Group>;
574 readonly registry: string;
575 readonly out: string;
576 readonly since?: string;
577 readonly all?: boolean;
578 readonly rebuildAllPaths?: ReadonlyArray<string>;
579 }
580
581 /**
582 * Tarball file name for a package: `@` dropped and the scope separator
583 * replaced by `+`, a character package names cannot contain, so `@a/b-c`
584 * and `@a-b/c` get distinct files.
585 */
586 export const tarballFile = (name: string) => `${name.replace(/^@/, "").replace("/", "+")}.tgz`;
587
588 /**
589 * Pack every discovered package into `out` with a manifest describing each
590 * tarball. Nothing here talks to the registry.
591 */
592 export const pack = Effect.fn("pack")(function* (options: PackOptions) {
593 const fs = yield* FileSystem.FileSystem;
594 const path = yield* Path.Path;
595
596 const root = yield* toplevel(options.cwd);
597 const head = yield* gitHead(root);
598 const pr = yield* pullRequest;
599 const prHead = pr?.head.sha;
600 if (prHead !== undefined && prHead !== head) {
601 return yield* new WorkspaceError({
602 message:
603 `HEAD is ${head} but the pull request head is ${prHead}. ` +
604 "Check out github.event.pull_request.head.sha before packing so tarballs are addressed by a commit that exists on the pull request.",
605 });
606 }
607 let packages = yield* discover(options.cwd, options.groups);
608 const implied = yield* discoverWorkspaceDependencies(options.cwd, root, packages);
609 if (implied.length > 0) {
610 yield* Effect.logInfo(
611 `Including ${implied.length} workspace dependenc${implied.length === 1 ? "y" : "ies"} not listed by --group: ${implied.map((pkg) => pkg.name).join(", ")}`,
612 );
613 packages = [...packages, ...implied];
614 }
615
616 // Dependencies between packed packages are rewritten to the dependency's
617 // immutable tarball URL, so a package is packed only after everything it
618 // depends on has a hash.
619 const byName = new Map(packages.map((pkg) => [pkg.name, pkg]));
620 const dependencies = new Map<string, Set<string>>();
621 for (const pkg of packages) {
622 const manifest = yield* fs
623 .readFileString(path.join(pkg.absDir, "package.json"))
624 .pipe(Effect.flatMap(Schema.decodeUnknownEffect(Schema.fromJsonString(DependencySections))));
625 dependencies.set(
626 pkg.name,
627 new Set(
628 DEPENDENCY_SECTIONS.flatMap((section) => Object.keys(manifest[section] ?? {})).filter(
629 (name) => name !== pkg.name && byName.has(name),
630 ),
631 ),
632 );
633 }
634 const since = options.all ? undefined : (options.since ?? pr?.base.sha);
635 if (since !== undefined) {
636 // Disable rename detection so moves affect both the old and new packages.
637 const changed = (yield* git(root, [
638 "diff",
639 "--name-only",
640 "--no-renames",
641 "-z",
642 since,
643 head,
644 "--",
645 ]))
646 .split("\0")
647 .filter(Boolean);
648 const selected = selectPackages(
649 packages.map((pkg) => ({
650 ...pkg,
651 dir: path.relative(root, pkg.absDir).split(path.sep).join("/"),
652 })),
653 dependencies,
654 changed,
655 options.rebuildAllPaths,
656 );
657 const names = new Set(selected.map((pkg) => pkg.name));
658 packages = packages.filter((pkg) => names.has(pkg.name));
659 for (const name of dependencies.keys()) {
660 if (!names.has(name)) dependencies.delete(name);
661 }
662 yield* Effect.logInfo(
663 `Selected ${packages.length} package(s) changed since ${since}, including dependents and dependencies.`,
664 );
665 }
666 const levels = yield* dependencyLevels(dependencies);
667
668 // The output directory is replaced wholesale, so it has to be a proper
669 // subdirectory of the workspace: `--out .` would otherwise delete it.
670 const outDir = path.resolve(options.cwd, options.out);
671 const relative = path.relative(options.cwd, outDir);
672 if (relative === "" || relative.startsWith("..") || path.isAbsolute(relative)) {
673 return yield* new WorkspaceError({
674 message: `--out must be a subdirectory of ${options.cwd}, got ${outDir}`,
675 });
676 }
677 yield* fs.remove(outDir, { recursive: true, force: true });
678 yield* fs.makeDirectory(outDir, { recursive: true });
679
680 const links = new Map<string, string>();
681 const packedByName = new Map<string, ManifestPackage>();
682 for (const level of levels) {
683 const packedLevel = yield* Effect.forEach(
684 level,
685 Effect.fn(function* (name) {
686 const pkg = byName.get(name)!;
687 const packed = yield* packPackage({
688 absDir: pkg.absDir,
689 links,
690 outDir,
691 file: tarballFile(pkg.name),
692 }).pipe(Effect.scoped);
693 const lines = [
694 `${pkg.name}@${pkg.version} ${packed.sha256.slice(0, 12)} ${packed.size} bytes`,
695 ...packed.rewrites.map((r) => ` ${r.section}.${r.name} -> ${r.url}`),
696 ];
697 yield* Effect.logInfo(lines.join("\n"));
698 return {
699 name: pkg.name,
700 version: pkg.version,
701 dir: pkg.dir,
702 group: pkg.group,
703 file: packed.file,
704 sha256: packed.sha256,
705 size: packed.size,
706 } satisfies ManifestPackage;
707 }),
708 { concurrency: 4 },
709 );
710 for (const entry of packedLevel) {
711 links.set(entry.name, tarballUrl(options.registry, entry.name, entry.sha256));
712 packedByName.set(entry.name, entry);
713 }
714 }
715 // Packing ran in dependency order; the manifest keeps the listed order.
716 const entries = packages.map((pkg) => packedByName.get(pkg.name)!);
717
718 // One entry per distinct group name, in the order the groups were given,
719 // then the transitive group when it has members.
720 const groups = [
721 ...new Map(
722 [
723 ...options.groups,
724 ...(entries.some((entry) => entry.group === TRANSITIVE_GROUP.name)
725 ? [TRANSITIVE_GROUP]
726 : []),
727 ].map((group) => [group.name, { name: group.name, collapsed: group.collapsed }]),
728 ).values(),
729 ];
730 const manifest: Manifest = {
731 version: 1,
732 groups,
733 registry: options.registry.replace(/\/+$/, ""),
734 head,
735 packages: entries,
736 };
737 const manifestText = `${yield* Schema.encodeEffect(ManifestJson)(manifest)}\n`;
738 yield* fs.writeFileString(path.join(outDir, MANIFEST_FILE), manifestText);
739 yield* Effect.logInfo(
740 `Packed ${entries.length} package(s) into ${path.relative(options.cwd, outDir) || "."}`,
741 );
742
743 // The workflow uploads the manifest as an artifact under this name; that
744 // upload is what proves to the registry that this run vouched for it.
745 // Only a JavaScript action receives the runtime token needed to upload,
746 // so the CLI cannot do it itself.
747 const artifact = manifestArtifactName(yield* sha256(manifestText));
748 const stepOutput = yield* Config.option(Config.String("GITHUB_OUTPUT"));
749 if (Option.isSome(stepOutput)) {
750 yield* fs.writeFileString(
751 stepOutput.value,
752 `artifact-name=${artifact}\npackage-count=${entries.length}\n`,
753 {
754 flag: "a",
755 },
756 );
757 }
758 yield* Effect.logInfo(`Manifest artifact name: ${artifact}`);
759 return manifest;
760 });