recording.ts
This file
Path
infra/recording.ts
114 lines · 3.1 KiB · TypeScript
Last changed by
Merge build/comments-ops into mainoscabrielbefore adoptione4c407e
History of this fileTake it
Merge build/comments-ops into mainoscabrielbefore adoptione4c407e
lines 1–4why, one step back1// Usage: bun infra/recording.ts on | off | status2// The deploy lock: while `infra/.recording` exists, `infra/deploy.ts` refuses3// every deploy, since a deploy restarts the platform's Durable Objects.4// Only `off` lifts it: no flag or environment variable does.Add the live-run tooling: smoke --loop, seed --as, a recording lock for deploys, and a Playwright check of the deployed weboscabrielbefore adoption030c4d6
lines 5–114why, one step back5import { BunRuntime, BunServices } from "@effect/platform-bun";6import {7 Console,8 Context,9 DateTime,10 Effect,11 FileSystem,12 Schema,13 Stdio,14} from "effect";1516export const lockPath = "infra/.recording";1718export const LockFile = Context.Reference<string>("infra/RecordingLockFile", {19 defaultValue: () =>20 decodeURIComponent(new URL(".recording", import.meta.url).pathname),21});2223export class RecordingInProgress extends Schema.TaggedError<RecordingInProgress>()(24 "RecordingInProgress",25 { file: Schema.String }26) {27 override get message() {28 return `${this.file} is there: a take is being recorded (H7), so nothing deploys until a person runs bun infra/recording.ts off`;29 }30}3132export class RecordingUsage extends Schema.TaggedError<RecordingUsage>()(33 "RecordingUsage",34 { given: Schema.String }35) {36 override get message() {37 return `${this.given === "" ? "no command" : `${this.given} is no command`}: bun infra/recording.ts on | off | status`;38 }39}4041const isOn = Effect.gen(function* () {42 const fs = yield* FileSystem.FileSystem;4344 // A lock whose presence can't be read is no reason to deploy blind.45 return yield* fs.exists(yield* LockFile).pipe(Effect.orDie);46});4748export const notRecording: Effect.Effect<49 void,50 RecordingInProgress,51 FileSystem.FileSystem52> = Effect.gen(function* () {53 const file = yield* LockFile;5455 return yield* (yield* isOn)56 ? Effect.fail(new RecordingInProgress({ file }))57 : Effect.void;58});5960export const recording = (61 argv: readonly string[]62): Effect.Effect<void, RecordingUsage, FileSystem.FileSystem> =>63 Effect.gen(function* () {64 const fs = yield* FileSystem.FileSystem;65 const file = yield* LockFile;66 const [command = ""] = argv;6768 if (command === "on") {69 const since = DateTime.formatIso(yield* DateTime.now);7071 yield* fs72 .writeFileString(73 file,74 `recording since ${since}; bun infra/recording.ts off lifts it\n`75 )76 .pipe(Effect.orDie);7778 return yield* Console.log(79 `recording: on since ${since}; every deploy is refused until bun infra/recording.ts off`80 );81 }8283 if (command === "off") {84 const was = yield* isOn;8586 yield* fs.remove(file, { force: true }).pipe(Effect.orDie);8788 return yield* Console.log(89 was90 ? "recording: off; deploys run again"91 : "recording: off already; deploys run"92 );93 }9495 if (command === "status") {96 return yield* Console.log(97 (yield* isOn)98 ? `recording: on (${(yield* fs.readFileString(file).pipe(Effect.orDie)).trim()})`99 : "recording: off; deploys run"100 );101 }102103 return yield* new RecordingUsage({ given: command });104 });105106if (import.meta.main) {107 BunRuntime.runMain(108 Effect.gen(function* () {109 const args = yield* (yield* Stdio.Stdio).args;110111 return yield* recording(args);112 }).pipe(Effect.provide(BunServices.layer))113 );114}