The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type * as Context from "effect/Context"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Nuke from "../Nuke.ts"; |
| 4 | import type { ProviderMode } from "../ProviderMode.ts"; |
| 5 | import type { ConfiguredStack } from "../Stack.ts"; |
| 6 | import { buildProviders, type ConfigOptions } from "./Operations.ts"; |
| 7 | |
| 8 | export interface ScanOptions extends ConfigOptions { |
| 9 | /** The stack whose providers are scanned; nothing reads its state. */ |
| 10 | readonly stack: ConfiguredStack<unknown, unknown>; |
| 11 | /** Scan the emulators `alchemy dev` runs instead of the real cloud. */ |
| 12 | readonly dev?: boolean; |
| 13 | /** Provider-id globs to include. Omitted means every provider. */ |
| 14 | readonly include?: ReadonlyArray<string>; |
| 15 | /** Provider-id globs to exclude. Applied after `include`. */ |
| 16 | readonly exclude?: ReadonlyArray<string>; |
| 17 | /** Providers scanned in parallel. @default 16 */ |
| 18 | readonly concurrency?: number | "unbounded"; |
| 19 | /** Timeout for each provider's `list`. @default 120 */ |
| 20 | readonly providerTimeoutSeconds?: number; |
| 21 | } |
| 22 | |
| 23 | /** One discovered cloud object, bound to the provider that can delete it. */ |
| 24 | export type Resource = Nuke.Target; |
| 25 | |
| 26 | /** Everything a stack's providers can see; only {@link execute} deletes it. */ |
| 27 | export interface NukeScan { |
| 28 | /** `local` when the scan ran with `dev: true`, otherwise `live`. */ |
| 29 | readonly mode: ProviderMode; |
| 30 | readonly resources: ReadonlyArray<Resource>; |
| 31 | /** Providers whose `list` failed; they contribute no resources. */ |
| 32 | readonly failures: ReadonlyArray<Nuke.ProviderFailure>; |
| 33 | /** |
| 34 | * The built provider context; lives in the caller's `Scope`. |
| 35 | * @internal {@link execute} deletes under it. |
| 36 | */ |
| 37 | readonly context: Context.Context<never>; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * List everything the stack's registered providers can see in the account. |
| 42 | * The stack is imported only for its providers: nothing here reads state or |
| 43 | * is scoped to a stage. |
| 44 | */ |
| 45 | export const scan = Effect.fn("Alchemist.Nuke.scan")(function* (options: ScanOptions) { |
| 46 | const mode: ProviderMode = options.dev ? "local" : "live"; |
| 47 | const context = yield* buildProviders(options.stack, options); |
| 48 | const { resources, failures } = yield* Nuke.list({ |
| 49 | context, |
| 50 | mode, |
| 51 | include: options.include, |
| 52 | exclude: options.exclude, |
| 53 | concurrency: options.concurrency, |
| 54 | timeoutSeconds: options.providerTimeoutSeconds, |
| 55 | }); |
| 56 | return { mode, resources, failures, context } satisfies NukeScan; |
| 57 | }); |
| 58 | |
| 59 | export interface ExecuteOptions { |
| 60 | /** The scan whose resources are deleted. */ |
| 61 | readonly scan: NukeScan; |
| 62 | /** The subset of `scan.resources` to delete. @default every planned resource */ |
| 63 | readonly resources?: ReadonlyArray<Resource>; |
| 64 | /** |
| 65 | * `coordinated` deletes in passes until one makes no progress; |
| 66 | * `independent` retries each resource on its own. |
| 67 | * @default { type: "coordinated" } |
| 68 | */ |
| 69 | readonly strategy?: Nuke.Strategy; |
| 70 | /** Providers deleted in parallel. @default 16 */ |
| 71 | readonly concurrency?: number | "unbounded"; |
| 72 | /** Timeout for each `delete`. @default 120 */ |
| 73 | readonly providerTimeoutSeconds?: number; |
| 74 | } |
| 75 | |
| 76 | export type Result = Nuke.Result; |
| 77 | |
| 78 | /** |
| 79 | * Permanently delete scanned resources. Progress is emitted through |
| 80 | * events: `apply.resource.status` and `nuke.*` events. |
| 81 | */ |
| 82 | export const execute = Effect.fn("Alchemist.Nuke.execute")(function* (options: ExecuteOptions) { |
| 83 | return yield* Nuke.destroy({ |
| 84 | targets: options.resources ?? options.scan.resources, |
| 85 | context: options.scan.context, |
| 86 | strategy: options.strategy ?? { type: "coordinated" }, |
| 87 | concurrency: options.concurrency, |
| 88 | timeoutSeconds: options.providerTimeoutSeconds, |
| 89 | }); |
| 90 | }); |