push-check.ts
This file
Path
apps/cli/src/hook/push-check.ts
152 lines · 4.4 KiB · TypeScript
Last changed by
Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153// A port, so the hook never imports the check command's modules. Offline, the check cannot read the// claim's base itself, so it is passed: main would be the wrong start for work committed on main.import { Envelope } from "@repo/protocol/envelope";import type { Refusal } from "@repo/protocol/refusal";import { Context, Duration, Effect, Layer, Option, Schema, Stream,} from "effect";import { ChildProcess, ChildProcessSpawner } from "effect/process";
export class PushCheckFailed extends Schema.TaggedError<PushCheckFailed>()( "PushCheckFailed", { reason: Schema.String }) { override get message() { return `the local check gave no answer: ${this.reason}`; }}
export class PushCheck extends Context.Service< PushCheck, { readonly run: ( root: string, base: string ) => Effect.Effect<readonly Refusal[], PushCheckFailed>; }>()("cli/PushCheck") {}
const CHECK = ["check", "--shape-only", "--offline", "--json"] as const;
const LIMIT = Duration.minutes(1);
const decodeEnvelope = Schema.decodeUnknownEffect( Schema.fromJsonString(Envelope(Schema.Unknown)));
const text = <E>(bytes: Stream.Stream<Uint8Array, E>) => bytes.pipe(Stream.decodeText(), Stream.mkString);
/** A refusal ends with the refusal's exit code, but the envelope says all, so only the envelope is read. */export const pushCheckWith = ( command: readonly [string, ...string[]]): Layer.Layer<PushCheck, never, ChildProcessSpawner.ChildProcessSpawner> => Layer.effect( PushCheck, Effect.gen(function* () { const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; const [executable, ...leading] = command;
const run = (root: string, base: string) => Effect.scoped( Effect.gen(function* () { const child = yield* spawner.spawn( ChildProcess.make( executable, [...leading, ...CHECK, "--base", base], { cwd: root, stderr: "ignore", stdin: "ignore", } ) );
const [stdout] = yield* Effect.all( [text(child.stdout), child.exitCode], { concurrency: "unbounded" } );
return stdout; }) ).pipe( Effect.timeoutOrElse({ duration: LIMIT, orElse: () => Effect.fail( new PushCheckFailed({ reason: `it ran past ${Duration.format(LIMIT)}`, }) ), }), Effect.catchTag("PlatformError", (failure) => Effect.fail(new PushCheckFailed({ reason: failure.message })) ), Effect.flatMap((stdout) => decodeEnvelope(stdout.trim()).pipe( Effect.mapError( () => new PushCheckFailed({ reason: "it printed no envelope on stdout", }) ) ) ), Effect.map((envelope): readonly Refusal[] => envelope.ok ? [] : envelope.refusals ) );
return { run }; }) );
const Started = Schema.Struct({ argv: Schema.Array(Schema.String), execPath: Schema.NonEmptyString,});
const GlobalScope = Schema.Struct({ process: Started });
/** A compiled binary is its own executable: its entry lives in Bun's embedded `/$bunfs/` or `B:/~BUN/`. */export const selfCommand = ( started: typeof Started.Type): readonly [string, ...string[]] => { const [, entry] = started.argv;
return entry === undefined || entry.startsWith("/$bunfs/") || entry.startsWith("B:/~BUN/") ? [started.execPath] : [started.execPath, entry];};
export const PushCheckChild: Layer.Layer< PushCheck, never, ChildProcessSpawner.ChildProcessSpawner> = Layer.unwrap( Effect.sync(() => { // Unknown on purpose: the runtime is whatever started this process. const scope: unknown = globalThis;
return Option.match(Schema.decodeUnknownOption(GlobalScope)(scope), { onNone: () => Layer.succeed(PushCheck)({ run: () => Effect.fail( new PushCheckFailed({ reason: "this runtime does not say how the CLI was started", }) ), }), onSome: ({ process }) => pushCheckWith(selfCommand(process)), }); }));