Skip to content
Myr

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 file

Take it

Merge build/comments-ops into mainoscabrielbefore adoptione4c407e

lines 1–3why, one step back
1// The append-only ledger of what this build made; teardown acts on nothing
2// 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 back
4import { 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 back
5import 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 back
6

Add 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 back
7const 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 back
8 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 back
12};
1314const Made = Schema.Struct({15 at: Schema.DateTimeUtcFromString,

Merge build/comments-ops into mainoscabrielbefore adoptione4c407e

line 16why, one step back
16 // 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 back
17 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 back
18 ...resourceFields,

Merge branch 'build/913-import-any-branch-and-examples'oscabrielbefore adoptiona4543f6

line 19why, one step back
19 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 back
20});
21

Add 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 back
22const 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});31

Name the swarm, seed and release scripts, and record every throwaway resource a live run makes in one ledgeroscabrielbefore adoptiond7064cb

lines 32–40why, one step back
32export 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});40

Add 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 back
41const 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 back
42

Add 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 back
43const 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;54

Merge build/comments-ops into mainoscabrielbefore adoptione4c407e

lines 55–56why, one step back
55// A `deleted` line marks the latest unmarked entry before it naming the same
56// 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 back
57const 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};80

Name the swarm, seed and release scripts, and record every throwaway resource a live run makes in one ledgeroscabrielbefore adoptiond7064cb

lines 81–87why, one step back
81export 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;87

Merge build/comments-ops into mainoscabrielbefore adoptione4c407e

line 88why, one step back
88 // 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 back
89 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 back
90
91 yield* fs.writeFileString(file, `${line}\n`, { flag: "a" });92 });93

Add 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 back
94export 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 });109

Name the swarm, seed and release scripts, and record every throwaway resource a live run makes in one ledgeroscabrielbefore adoptiond7064cb

lines 110–125why, one step back
110export 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);125

Add 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 back
126 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 back
128 );
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 back
130
131export 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 back
138
139export const throwawayName = (by: Created["by"], run: string) => `${by}-${run}`;