@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 104 lines 4.02 KB
Raw
1 import * as Context from "effect/Context";
2 import * as Effect from "effect/Effect";
3
4 /**
5 * What the engine does with a resource's **physical** cloud object when the
6 * resource is deleted — orphaned (its declaration was removed or renamed
7 * away), destroyed (`alchemy destroy`), or superseded by a replacement's old
8 * generation.
9 *
10 * - `"destroy"` (the default for most resource types) — call the provider's
11 * `delete`.
12 * - `"retain"` — skip `provider.delete` entirely. The state row is still
13 * dropped: alchemy forgets the resource, the cloud object lives on.
14 *
15 * The policy is a *decoration* on the declaration, applied with
16 * {@link retain} / {@link destroy}, and it is persisted on the resource's
17 * state row. Because it isn't a prop, changing it produces no diff — the
18 * resource plans as a `noop` and the deploy re-commits the row's policy in
19 * place (see the noop branch of `Apply.ts`). So a policy change does take
20 * effect from the very next deploy, even though the plan shows no changes.
21 *
22 * A few resource types default to `"retain"` because their contents are
23 * irreplaceable (e.g. `GitHub.Repository`, `Cloudflare.Zone`); those opt into
24 * deletion with `destroy()`.
25 */
26 export class RemovalPolicy extends Context.Service<RemovalPolicy, "retain" | "destroy">()(
27 "RemovalPolicy",
28 ) {}
29
30 /**
31 * Retain the physical cloud resource when the resource is deleted: the
32 * provider's `delete` is never called, and the state row is dropped so
33 * alchemy stops tracking it.
34 *
35 * Applies to every resource declared inside the piped effect, so it can
36 * decorate a single resource or a whole scope.
37 *
38 * @example Retain one resource
39 * ```typescript
40 * const bucket = yield* R2.Bucket("Uploads").pipe(RemovalPolicy.retain());
41 * ```
42 *
43 * @example Retain only in production
44 * ```typescript
45 * const stack = yield* Stack;
46 * const bucket = yield* R2.Bucket("Uploads").pipe(
47 * RemovalPolicy.retain(stack.stage === "prod"),
48 * );
49 * ```
50 *
51 * @param enabled `true` (the default) retains; `false` selects `destroy`.
52 * May also be an `Effect` producing the boolean.
53 */
54 export const retain: {
55 (
56 enabled?: boolean,
57 ): <R, Req = never>(enabled: Effect.Effect<R, never, Req>) => Effect.Effect<R, never, Req>;
58 <Req = never>(
59 enabled: Effect.Effect<boolean, never, Req>,
60 ): <R, Req2 = never>(a: Effect.Effect<R, never, Req2>) => Effect.Effect<R, never, Req | Req2>;
61 } = ((enabled: boolean | Effect.Effect<boolean, never, any> = true) =>
62 (eff: Effect.Effect<any, never, any>) =>
63 eff.pipe(
64 typeof enabled === "boolean"
65 ? Effect.provideService(RemovalPolicy, enabled ? "retain" : "destroy")
66 : Effect.provideServiceEffect(
67 RemovalPolicy,
68 enabled.pipe(Effect.map((a) => (a ? "retain" : "destroy"))),
69 ),
70 )) as any;
71
72 /**
73 * Destroy the physical cloud resource when the resource is deleted — the
74 * default for most resource types, and the way to opt out for the ones that
75 * default to `retain` (e.g. `GitHub.Repository`, `Cloudflare.Zone`).
76 *
77 * @example Opt a retain-by-default resource into deletion
78 * ```typescript
79 * yield* GitHub.Repository("Preview", {
80 * owner: "my-org",
81 * name: "ephemeral-preview",
82 * }).pipe(RemovalPolicy.destroy());
83 * ```
84 *
85 * @param enabled `true` (the default) destroys; `false` selects `retain`.
86 * May also be an `Effect` producing the boolean.
87 */
88 export const destroy: {
89 (
90 enabled?: boolean,
91 ): <R, Req = never>(enabled: Effect.Effect<R, never, Req>) => Effect.Effect<R, never, Req>;
92 <Req = never>(
93 enabled: Effect.Effect<boolean, never, Req>,
94 ): <R, Req2 = never>(a: Effect.Effect<R, never, Req2>) => Effect.Effect<R, never, Req | Req2>;
95 } = ((enabled: boolean | Effect.Effect<boolean, never, any> = true) =>
96 (eff: Effect.Effect<any, never, any>) =>
97 eff.pipe(
98 typeof enabled === "boolean"
99 ? Effect.provideService(RemovalPolicy, enabled ? "destroy" : "retain")
100 : Effect.provideServiceEffect(
101 RemovalPolicy,
102 enabled.pipe(Effect.map((a) => (a ? "destroy" : "retain"))),
103 ),
104 )) as any;