Skip to content
Source
Sign inSign up

manage.ts

77 lines2.7 KB

1import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
2import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
3import * as NodeServices from "@effect/platform-node/NodeServices";
4import * as ChildProcess from "effect/process/ChildProcess";
5import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
6import * as Schema from "effect/Schema";
7import * as Console from "effect/Console";
8import * as Layer from "effect/Layer";
9import * as Effect from "effect/Effect";
10import * as Flag from "effect/cli/Flag";
11import * as Argument from "effect/cli/Argument";
12import * as Command from "effect/cli/Command";
13​
14import * as WriterRegistry from "../src/WriterRegistry.ts";
15​
16const decodeDeployment = Schema.decodeEffect(
17 Schema.fromJsonString(Schema.Struct({ url: Schema.String })),
18);
19​
20const name = Argument.String("name").pipe(Argument.withDescription("Lowercase writer name"));
21const 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​
59const 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​
70Command.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);