Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

manage.ts

72 lines2.5 KB

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