teardown.ts
This file
Path
infra/teardown.ts
137 lines · 3.8 KiB · TypeScript
Last changed by
Merge build/comments-ops into mainoscabrielbefore adoptione4c407e
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138// Usage: bunx varlock run --path infra/ -- bun infra/teardown.ts// [--stage <s>] [--keep <project>]… [--apply]import { BunRuntime, BunServices } from "@effect/platform-bun";import { Fetch, FetchFailed } from "@repo/artifacts/fetch";import { RepoHost } from "@repo/artifacts/repo-host";import { RepoHostRest } from "@repo/artifacts/repo-host/rest";import { Config, Context, Effect, Layer, Option, Redacted, Scope, Stdio, SynchronizedRef,} from "effect";import { FetchHttpClient } from "effect/http";
import { teardown, TeardownAccount } from "./lib/teardown.ts";
const API = "https://api.cloudflare.com/client/v4";
const Credentials = Config.all({ accountId: Config.String("CLOUDFLARE_ACCOUNT_ID"), token: Config.Redacted("CLOUDFLARE_API_TOKEN"),});
// Never a query or a header, so no credential reaches the output.const placeOf = (request: Request) => { const url = new URL(request.url);
return `${url.origin}${url.pathname}`;};
const BunFetch = Layer.effect( Fetch, Effect.gen(function* () { const send = yield* FetchHttpClient.Fetch;
return Fetch.of({ fetch: (request) => Effect.tryPromise({ catch: (cause) => new FetchFailed({ reason: cause instanceof Error ? `the request got no response: ${cause.name}` : "the request got no response", url: placeOf(request), }), try: () => send(request), }), }); }));
const LiveAccount = Layer.effect( TeardownAccount, Effect.gen(function* () { const { accountId, token } = yield* Credentials; const fetch = yield* Fetch; const scope = yield* Scope.Scope;
const hosts = yield* SynchronizedRef.make< ReadonlyMap<string, RepoHost["Service"]> >(new Map());
const repos = (namespace: string) => SynchronizedRef.modifyEffect(hosts, (all) => Option.match(Option.fromNullishOr(all.get(namespace)), { onNone: () => Layer.buildWithScope( RepoHostRest.layer({ accountId, namespace, token }), scope ).pipe( Effect.provideService(Fetch, fetch), Effect.map((context) => { const host = Context.get(context, RepoHost);
return [host, new Map(all).set(namespace, host)] as const; }) ), onSome: (host) => Effect.succeed([host, all] as const), }) );
const namespaceCall = (name: string, method: "DELETE" | "GET") => Effect.gen(function* () { const response = yield* fetch .fetch( new Request( `${API}/accounts/${encodeURIComponent(accountId)}/artifacts/namespaces/${encodeURIComponent(name)}`, { headers: { accept: "application/json", authorization: `Bearer ${Redacted.value(token)}`, }, method, } ) ) .pipe(Effect.orDie);
if (response.status === 200 || response.status === 404) { return response.status === 200; }
return yield* Effect.die( `${method} namespace ${name} answered ${response.status}` ); });
return TeardownAccount.of({ namespaceExists: (name) => namespaceCall(name, "GET"), removeNamespace: (name) => namespaceCall(name, "DELETE"), repos, }); }));
if (import.meta.main) { BunRuntime.runMain( Effect.gen(function* () { const args = yield* (yield* Stdio.Stdio).args;
return yield* teardown(args); }).pipe( Effect.provide( Layer.mergeAll( LiveAccount.pipe(Layer.provide(BunFetch)), BunServices.layer ) ) ) );}