@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 79 lines 2.92 KB
Raw
1 import * as Auth from "@distilled.cloud/aws/Auth";
2 import type * as Alchemist from "alchemy/Alchemist";
3 import { loadConfigProvider } from "alchemy/Util/ConfigProvider";
4 import * as ConfigProvider from "effect/ConfigProvider";
5 import * as EffectConsole from "effect/Console";
6 import * as Effect from "effect/Effect";
7 import * as Layer from "effect/Layer";
8 import * as Option from "effect/Option";
9 import {
10 bootstrap as bootstrapAws,
11 destroyBootstrap as destroyBootstrapAws,
12 } from "../AssetsBucket.ts";
13 import { getAccountId, silentConsole } from "../AuthProvider.ts";
14 import * as AWSCredentials from "../Credentials.ts";
15 import { AWSEnvironment } from "../Environment.ts";
16 import * as AWSRegion from "../Region.ts";
17
18 export interface AwsTarget extends Alchemist.Target {
19 readonly profile: string;
20 readonly region?: string;
21 }
22
23 const environment = Effect.fn(function* (target: AwsTarget) {
24 // SSO (`sso_account_id`) or console-login (`login_session`) profiles;
25 // distilled resolves both.
26 const profile = yield* Auth.loadProfile(target.profile);
27 const region = target.region ?? profile.region ?? "us-east-1";
28 // The credentials effect runs later, inside the built layer, where these
29 // services are no longer in context — capture them here so the route's
30 // requirements stay visible in its type.
31 const credentialServices =
32 yield* Effect.context<Effect.Services<ReturnType<typeof Auth.loadProfileCredentials>>>();
33 const credentials = Auth.loadProfileCredentials(target.profile).pipe(
34 Effect.provideService(EffectConsole.Console, silentConsole),
35 Effect.provide(credentialServices),
36 );
37 // A console-login profile records no account id; ask STS.
38 const accountId = profile.sso_account_id ?? (yield* Effect.flatMap(credentials, getAccountId));
39 const aws = Layer.provideMerge(
40 Layer.mergeAll(AWSRegion.fromEnvironment, AWSCredentials.fromEnvironment),
41 Layer.succeed(
42 AWSEnvironment,
43 Effect.succeed({
44 accountId,
45 region,
46 credentials,
47 profile: target.profile,
48 }),
49 ),
50 );
51 return {
52 accountId,
53 region,
54 layer: Layer.provide(
55 aws,
56 ConfigProvider.layer(yield* loadConfigProvider(Option.fromNullishOr(target.envFile))),
57 ),
58 };
59 });
60
61 /** Provision the AWS deployment assets bucket. */
62 export const bootstrap = Effect.fn("Alchemist.provider.aws.bootstrap")(function* (
63 target: AwsTarget,
64 ) {
65 const env = yield* environment(target);
66 const result = yield* Effect.provide(bootstrapAws(), env.layer);
67 return { accountId: env.accountId, region: env.region, ...result };
68 });
69
70 /** Destroy every Alchemy bootstrap bucket in the region. */
71 export const teardown = Effect.fn("Alchemist.provider.aws.teardown")(function* (target: AwsTarget) {
72 const env = yield* environment(target);
73 const result = yield* Effect.provide(destroyBootstrapAws(), env.layer);
74 return {
75 accountId: env.accountId,
76 region: env.region,
77 destroyed: result.bucketNames,
78 };
79 });