The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as Layer from "effect/Layer"; |
| 3 | import * as Interaction from "../Interaction.ts"; |
| 4 | import type { Plan } from "../Plan.ts"; |
| 5 | import { Cli, type PlanDisplayOptions } from "../Report.ts"; |
| 6 | import { renderApply } from "../Report.ts"; |
| 7 | import { PlatformServices } from "../Util/PlatformServices.ts"; |
| 8 | import { renderPlanning } from "./commands/render.ts"; |
| 9 | import { renderNukeDelete, renderNukeScan, reviewNuke } from "./components/view/Nuke.tsx"; |
| 10 | import { stackOutputsView } from "./components/view/StackOutputs.tsx"; |
| 11 | import { selectCliServices } from "./selectCli.ts"; |
| 12 | |
| 13 | /** |
| 14 | * The CLI's terminal UI for scripts that drive `alchemy/Alchemist` |
| 15 | * themselves: the Sigil UI on an interactive terminal, plain log lines |
| 16 | * otherwise, exactly like the CLI. |
| 17 | * |
| 18 | * ```ts |
| 19 | * const plan = yield* Alchemist.plan({ stack, operation: "deploy", stage }).pipe( |
| 20 | * SigilRenderer.planning({ operation: "Deploy", stage }), |
| 21 | * ); |
| 22 | * if (!(yield* SigilRenderer.approve(plan, { stage }))) return; |
| 23 | * const output = yield* Alchemist.apply(plan).pipe(SigilRenderer.apply(plan, { stage })); |
| 24 | * yield* SigilRenderer.outputs(output); |
| 25 | * ``` |
| 26 | */ |
| 27 | |
| 28 | /** A deploy or destroy plan, or a drift scan, from `alchemy/Alchemist`. */ |
| 29 | export interface RenderablePlan { |
| 30 | readonly native: Plan; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * The CLI's terminal and renderer services, in place of `Alchemist.layer()`'s |
| 35 | * non-interactive terminal. Provide it after `Alchemist.layer()`: |
| 36 | * `Effect.provide([Alchemist.layer(), SigilRenderer.layer()])`. |
| 37 | */ |
| 38 | export const layer = () => |
| 39 | Layer.provideMerge(selectCliServices(), Interaction.layer()).pipe( |
| 40 | Layer.provide(PlatformServices), |
| 41 | ); |
| 42 | |
| 43 | /** Planning progress (import, services, state, per-resource diffing) around a `plan` call. */ |
| 44 | export const planning = renderPlanning; |
| 45 | |
| 46 | /** |
| 47 | * Show the plan and ask to apply it; `true` when approved. Fails with |
| 48 | * `NonInteractiveTerminal` when there is no terminal to ask on. |
| 49 | */ |
| 50 | export const approve = (plan: RenderablePlan, options?: PlanDisplayOptions) => |
| 51 | Effect.flatMap(Cli, (cli) => cli.approvePlan(plan.native, options)); |
| 52 | |
| 53 | /** Show the plan without asking, as `alchemy plan` does. */ |
| 54 | export const display = (plan: RenderablePlan, options?: PlanDisplayOptions) => |
| 55 | Effect.flatMap(Cli, (cli) => cli.displayPlan(plan.native, options)); |
| 56 | |
| 57 | /** Per-resource apply rows around an `apply` call. */ |
| 58 | export const apply = (plan: RenderablePlan, options?: PlanDisplayOptions) => |
| 59 | renderApply(plan.native, options); |
| 60 | |
| 61 | /** Print a deployed stack's outputs. */ |
| 62 | export const outputs = (output: unknown) => |
| 63 | Effect.flatMap(Interaction.Interaction, (terminal) => |
| 64 | terminal.output.print(stackOutputsView(output)), |
| 65 | ); |
| 66 | |
| 67 | /** `alchemy unsafe nuke`'s scan progress, review prompt and deletion view. */ |
| 68 | export const Nuke = { |
| 69 | /** Live scan progress around `Alchemist.Nuke.scan`. */ |
| 70 | scan: renderNukeScan, |
| 71 | /** Show the inventory and ask to delete it; `true` when approved (or `yes`/`dryRun`). */ |
| 72 | review: reviewNuke, |
| 73 | /** Per-resource deletion rows around `Alchemist.Nuke.execute`. */ |
| 74 | delete: renderNukeDelete, |
| 75 | }; |