created.ts
This file
Path
infra/created.ts
139 lines · 4.0 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–3why, one step back1// The append-only ledger of what this build made; teardown acts on nothing2// else. It is committed and holds names only, never a token. A deletion is a3// later `deleted` line folded into its entry on read, never a rewrite.Name the swarm, seed and release scripts, and record every throwaway resource a live run makes in one ledgeroscabrielbefore adoptiond7064cb
line 4why, one step back4import { Context, Effect, FileSystem, Schema } from "effect";Add bun infra/teardown.ts: it prints which of the ledger's throwaways still exist, and deletes exactly those only with --applyoscabrielbefore adoptionf2215bd
line 5why, one step back5import type { DateTime, PlatformError } from "effect";Name the swarm, seed and release scripts, and record every throwaway resource a live run makes in one ledgeroscabrielbefore adoptiond7064cb
line 6why, one step back6Add bun infra/teardown.ts: it prints which of the ledger's throwaways still exist, and deletes exactly those only with --applyoscabrielbefore adoptionf2215bd
line 7why, one step back7const resourceFields = {Name the swarm, seed and release scripts, and record every throwaway resource a live run makes in one ledgeroscabrielbefore adoptiond7064cb
lines 8–11why, one step back8 kind: Schema.Literals(["namespace", "repo", "project"]),9 name: Schema.NonEmptyString,10 namespace: Schema.optionalKey(Schema.NonEmptyString),11 stage: Schema.NonEmptyString,Add bun infra/teardown.ts: it prints which of the ledger's throwaways still exist, and deletes exactly those only with --applyoscabrielbefore adoptionf2215bd
lines 12–15why, one step back12};1314const Made = Schema.Struct({15 at: Schema.DateTimeUtcFromString,Merge build/comments-ops into mainoscabrielbefore adoptione4c407e
line 16why, one step back16 // An `import` is kept, not a throwaway: teardown never selects it.Merge branch 'build/913-import-any-branch-and-examples'oscabrielbefore adoptiona4543f6
line 17why, one step back17 by: Schema.Literals(["smoke", "swarm", "seed", "import"]),Add bun infra/teardown.ts: it prints which of the ledger's throwaways still exist, and deletes exactly those only with --applyoscabrielbefore adoptionf2215bd
line 18why, one step back18 ...resourceFields,Merge branch 'build/913-import-any-branch-and-examples'oscabrielbefore adoptiona4543f6
line 19why, one step back19 note: Schema.optionalKey(Schema.NonEmptyString),Name the swarm, seed and release scripts, and record every throwaway resource a live run makes in one ledgeroscabrielbefore adoptiond7064cb
lines 20–21why, one step back20});21Add bun infra/teardown.ts: it prints which of the ledger's throwaways still exist, and deletes exactly those only with --applyoscabrielbefore adoptionf2215bd
lines 22–31why, one step back22const Deleted = Schema.Struct({23 deletedAt: Schema.DateTimeUtcFromString,24 ...resourceFields,25});2627export const Created = Schema.Struct({28 ...Made.fields,29 deletedAt: Schema.optionalKey(Schema.DateTimeUtcFromString),30});31Name the swarm, seed and release scripts, and record every throwaway resource a live run makes in one ledgeroscabrielbefore adoptiond7064cb
lines 32–40why, one step back32export type Created = typeof Created.Type;3334export const ledgerPath = "infra/created.jsonl";3536export const LedgerFile = Context.Reference<string>("infra/LedgerFile", {37 defaultValue: () =>38 decodeURIComponent(new URL("created.jsonl", import.meta.url).pathname),39});40Add bun infra/teardown.ts: it prints which of the ledger's throwaways still exist, and deletes exactly those only with --applyoscabrielbefore adoptionf2215bd
line 41why, one step back41const MadeLine = Schema.fromJsonString(Made);Name the swarm, seed and release scripts, and record every throwaway resource a live run makes in one ledgeroscabrielbefore adoptiond7064cb
line 42why, one step back42Add bun infra/teardown.ts: it prints which of the ledger's throwaways still exist, and deletes exactly those only with --applyoscabrielbefore adoptionf2215bd
lines 43–54why, one step back43const DeletedLine = Schema.fromJsonString(Deleted);4445const Line = Schema.fromJsonString(Schema.Union([Made, Deleted]));4647type Resource = Pick<Created, "kind" | "name" | "namespace" | "stage">;4849const sameResource = (one: Resource, other: Resource): boolean =>50 one.kind === other.kind &&51 one.name === other.name &&52 one.namespace === other.namespace &&53 one.stage === other.stage;54Merge build/comments-ops into mainoscabrielbefore adoptione4c407e
lines 55–56why, one step back55// A `deleted` line marks the latest unmarked entry before it naming the same56// resource; one matching nothing changes nothing.Add bun infra/teardown.ts: it prints which of the ledger's throwaways still exist, and deletes exactly those only with --applyoscabrielbefore adoptionf2215bd
lines 57–80why, one step back57const folded = (58 lines: readonly (typeof Made.Type | typeof Deleted.Type)[]59): readonly Created[] => {60 const entries: Created[] = [];6162 for (const line of lines) {63 if ("deletedAt" in line) {64 const index = entries.findLastIndex(65 (entry) => entry.deletedAt === undefined && sameResource(line, entry)66 );6768 const marked = entries[index];6970 if (marked !== undefined) {71 entries[index] = { ...marked, deletedAt: line.deletedAt };72 }73 } else {74 entries.push(line);75 }76 }7778 return entries;79};80Name the swarm, seed and release scripts, and record every throwaway resource a live run makes in one ledgeroscabrielbefore adoptiond7064cb
lines 81–87why, one step back81export const recordCreated = (82 entry: Created83): Effect.Effect<void, PlatformError.PlatformError, FileSystem.FileSystem> =>84 Effect.gen(function* () {85 const fs = yield* FileSystem.FileSystem;86 const file = yield* LedgerFile;87Merge build/comments-ops into mainoscabrielbefore adoptione4c407e
line 88why, one step back88 // MadeLine drops `deletedAt`: it is never written on an entry's own line.Add bun infra/teardown.ts: it prints which of the ledger's throwaways still exist, and deletes exactly those only with --applyoscabrielbefore adoptionf2215bd
line 89why, one step back89 const line = yield* Schema.encodeEffect(MadeLine)(entry).pipe(Effect.orDie);Name the swarm, seed and release scripts, and record every throwaway resource a live run makes in one ledgeroscabrielbefore adoptiond7064cb
lines 90–93why, one step back9091 yield* fs.writeFileString(file, `${line}\n`, { flag: "a" });92 });93Add bun infra/teardown.ts: it prints which of the ledger's throwaways still exist, and deletes exactly those only with --applyoscabrielbefore adoptionf2215bd
lines 94–109why, one step back94export const recordDeleted = (95 entry: Created,96 at: DateTime.Utc97): Effect.Effect<void, PlatformError.PlatformError, FileSystem.FileSystem> =>98 Effect.gen(function* () {99 const fs = yield* FileSystem.FileSystem;100 const file = yield* LedgerFile;101102 const line = yield* Schema.encodeEffect(DeletedLine)({103 ...entry,104 deletedAt: at,105 }).pipe(Effect.orDie);106107 yield* fs.writeFileString(file, `${line}\n`, { flag: "a" });108 });109Name the swarm, seed and release scripts, and record every throwaway resource a live run makes in one ledgeroscabrielbefore adoptiond7064cb
lines 110–125why, one step back110export const readLedger: Effect.Effect<111 readonly Created[],112 PlatformError.PlatformError | Schema.SchemaError,113 FileSystem.FileSystem114> = Effect.gen(function* () {115 const fs = yield* FileSystem.FileSystem;116 const file = yield* LedgerFile;117118 if (!(yield* fs.exists(file))) {119 return [];120 }121122 const lines = (yield* fs.readFileString(file))123 .split("\n")124 .filter((line) => line.length > 0);125Add bun infra/teardown.ts: it prints which of the ledger's throwaways still exist, and deletes exactly those only with --applyoscabrielbefore adoptionf2215bd
lines 126–127why, one step back126 return folded(127 yield* Effect.forEach(lines, (line) => Schema.decodeEffect(Line)(line))Name the swarm, seed and release scripts, and record every throwaway resource a live run makes in one ledgeroscabrielbefore adoptiond7064cb
lines 128–129why, one step back128 );129});Add bun infra/teardown.ts: it prints which of the ledger's throwaways still exist, and deletes exactly those only with --applyoscabrielbefore adoptionf2215bd
lines 130–137why, one step back130131export const isRecorded = (132 ledger: readonly Created[],133 resource: Resource134): boolean =>135 ledger.some(136 (entry) => entry.deletedAt === undefined && sameResource(resource, entry)137 );Name the swarm, seed and release scripts, and record every throwaway resource a live run makes in one ledgeroscabrielbefore adoptiond7064cb
lines 138–139why, one step back138139export const throwawayName = (by: Created["by"], run: string) => `${by}-${run}`;