The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { Command, Flag } from "effect/cli"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Option from "effect/Option"; |
| 4 | import * as Alchemist from "../../Alchemist/index.ts"; |
| 5 | import * as Contributions from "../../Contributions.ts"; |
| 6 | import { Interaction } from "../../Interaction.ts"; |
| 7 | import { setExitCode } from "./errors.ts"; |
| 8 | import { config, envFile, profile } from "./flags.ts"; |
| 9 | import { instrumentCommand } from "./instrument.ts"; |
| 10 | |
| 11 | /** Optional repeatable filter for checking a subset of registered providers. */ |
| 12 | const checkedProviders = Flag.String("provider").pipe( |
| 13 | Flag.withDescription( |
| 14 | "Check only this provider (repeatable; defaults to every provider the stack registers)", |
| 15 | ), |
| 16 | Flag.atLeast(0), |
| 17 | ); |
| 18 | |
| 19 | const checkEnvCommand = Command.make( |
| 20 | "check-env", |
| 21 | { provider: checkedProviders, main: config, envFile, profile }, |
| 22 | instrumentCommand("provider.check-env")( |
| 23 | Effect.fn(function* ({ provider: requested, main, envFile, profile }) { |
| 24 | const cli = yield* Interaction; |
| 25 | const result = yield* Alchemist.Provider.checkEnvironment({ |
| 26 | entrypoint: main, |
| 27 | envFile: Option.getOrUndefined(envFile), |
| 28 | profile, |
| 29 | providers: requested, |
| 30 | }); |
| 31 | for (const check of result.checks) { |
| 32 | if (check.status === "no-contract") { |
| 33 | yield* cli.output.info({ |
| 34 | message: check.provider, |
| 35 | detail: "No CI environment contract", |
| 36 | }); |
| 37 | } else if (check.status === "satisfied") { |
| 38 | yield* cli.output.success(check.provider); |
| 39 | } else { |
| 40 | yield* cli.output.error({ |
| 41 | message: check.provider, |
| 42 | detail: `Missing: ${check.missing |
| 43 | .map(({ alternatives }) => alternatives.join(" | ")) |
| 44 | .join(", ")}`, |
| 45 | }); |
| 46 | } |
| 47 | } |
| 48 | if (!result.satisfied) yield* setExitCode(1); |
| 49 | }), |
| 50 | ), |
| 51 | ).pipe( |
| 52 | Command.withDescription( |
| 53 | "Verify the required environment variables for the stack's providers are set (CI preflight; exits 1 when any are missing)", |
| 54 | ), |
| 55 | ); |
| 56 | |
| 57 | /** |
| 58 | * `alchemy provider`: `check-env` plus each provider's provider subcommand. A |
| 59 | * provider whose CLI fails to load never blocks another's: it runs as a |
| 60 | * command failing with its error. |
| 61 | */ |
| 62 | export const providerCommand = async (providers: ReadonlyArray<Contributions.Contributions>) => { |
| 63 | const subcommands = await Effect.runPromise( |
| 64 | Effect.forEach(providers, (provider) => { |
| 65 | const cli = provider.cli?.provider; |
| 66 | if (cli === undefined) return Effect.succeed([]); |
| 67 | return Contributions.resolve(provider, "cli.provider", cli.load).pipe( |
| 68 | Effect.catch((error) => |
| 69 | Effect.succeed<Contributions.AnyCommand>( |
| 70 | Command.make(provider.name, {}, () => Effect.fail(error)).pipe( |
| 71 | Command.withDescription(cli.description), |
| 72 | ), |
| 73 | ), |
| 74 | ), |
| 75 | Effect.map((command) => [command]), |
| 76 | ); |
| 77 | }).pipe(Effect.map((commands) => commands.flat())), |
| 78 | ); |
| 79 | return Command.make("provider", {}).pipe( |
| 80 | Command.withDescription("Manage cloud provider prerequisites and utilities"), |
| 81 | Command.withSubcommands([checkEnvCommand, ...subcommands]), |
| 82 | ); |
| 83 | }; |