The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 2 | import * as NodeRuntime from "@effect/platform-node/NodeRuntime"; |
| 3 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 4 | import * as ChildProcess from "effect/process/ChildProcess"; |
| 5 | import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; |
| 6 | import * as Schema from "effect/Schema"; |
| 7 | import * as Console from "effect/Console"; |
| 8 | import * as Layer from "effect/Layer"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Flag from "effect/cli/Flag"; |
| 11 | import * as Argument from "effect/cli/Argument"; |
| 12 | import * as Command from "effect/cli/Command"; |
| 13 | |
| 14 | import * as WriterRegistry from "../src/WriterRegistry.ts"; |
| 15 | |
| 16 | const decodeDeployment = Schema.decodeEffect( |
| 17 | Schema.fromJsonString(Schema.Struct({ url: Schema.String })), |
| 18 | ); |
| 19 | |
| 20 | const name = Argument.String("name").pipe(Argument.withDescription("Lowercase writer name")); |
| 21 | const updateWriter = Effect.fn("TaskCache.manageWriter")(function* ( |
| 22 | name: string, |
| 23 | revoke: boolean, |
| 24 | initialize = false, |
| 25 | ) { |
| 26 | const registry = yield* WriterRegistry.WriterRegistry; |
| 27 | let endpoint: string | undefined; |
| 28 | if (!revoke && WriterRegistry.isWriterName(name)) { |
| 29 | const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; |
| 30 | const output = yield* spawner.string( |
| 31 | ChildProcess.make( |
| 32 | "node", |
| 33 | [ |
| 34 | `${import.meta.dirname}/../node_modules/alchemy/bin/cli.js`, |
| 35 | "state", |
| 36 | "read", |
| 37 | "T3TaskCache/prod/output", |
| 38 | "--backend", |
| 39 | "cloudflare", |
| 40 | "--no-input", |
| 41 | ], |
| 42 | { cwd: `${import.meta.dirname}/..`, stderr: "inherit" }, |
| 43 | ), |
| 44 | ); |
| 45 | const deployed = yield* decodeDeployment(output); |
| 46 | endpoint = new URL("/projects/t3code", deployed.url).href; |
| 47 | } |
| 48 | const file = yield* registry.update(name, revoke, endpoint, initialize); |
| 49 | yield* Console.log( |
| 50 | file === undefined |
| 51 | ? `Removed ${name} from the local writer registry.` |
| 52 | : `Private credential written to ${file}.`, |
| 53 | ); |
| 54 | yield* Console.log( |
| 55 | "Run alchemy deploy --stage prod from infra/task-cache to apply the writer changes.", |
| 56 | ); |
| 57 | }); |
| 58 | |
| 59 | const command = Command.make("task-cache").pipe( |
| 60 | Command.withSubcommands([ |
| 61 | Command.make( |
| 62 | "add-writer", |
| 63 | { name, newRegistry: Flag.Boolean("new-registry").pipe(Flag.withDefault(false)) }, |
| 64 | ({ name, newRegistry }) => updateWriter(name, false, newRegistry), |
| 65 | ), |
| 66 | Command.make("revoke-writer", { name }, ({ name }) => updateWriter(name, true)), |
| 67 | ]), |
| 68 | ); |
| 69 | |
| 70 | Command.run(command, { version: "1.0.0" }).pipe( |
| 71 | Effect.provide( |
| 72 | WriterRegistry.layer.pipe( |
| 73 | Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), |
| 74 | ), |
| 75 | ), |
| 76 | NodeRuntime.runMain, |
| 77 | ); |