@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 3.02 KB
Raw
1 import { pathToFileURL } from "node:url";
2 import * as Context from "effect/Context";
3 import * as Data from "effect/Data";
4 import * as Effect from "effect/Effect";
5 import * as FileSystem from "effect/FileSystem";
6 import * as Path from "effect/Path";
7 import * as Predicate from "effect/Predicate";
8 import { emit } from "../Events.ts";
9 import type { ConfiguredStack } from "../Stack.ts";
10 import { UserFacingError } from "../UserFacingError.ts";
11
12 export class StackEntrypointError extends Data.TaggedError("StackEntrypointError")<{
13 readonly message: string;
14 }> {
15 readonly [UserFacingError] = true;
16 }
17
18 export const DEFAULT_ENTRYPOINT = "alchemy.run.ts";
19
20 /**
21 * The absolute path of the stack entrypoint, or the user-facing error for
22 * a missing one. Kept free of the engine so the `alchemy dev` supervisor can
23 * check the entry before it starts an exec child.
24 */
25 export const resolveStackEntrypoint = Effect.fn(function* (main: string) {
26 const fs = yield* FileSystem.FileSystem;
27 const path = yield* Path.Path;
28 const absolutePath = path.resolve(main);
29 if (!(yield* fs.exists(absolutePath))) {
30 return yield* new StackEntrypointError({
31 message: `Stack entrypoint '${main}' does not exist in '${path.dirname(absolutePath)}'. Run this command from an Alchemy project or pass --config <path>.`,
32 });
33 }
34 return absolutePath;
35 });
36
37 export interface StackModuleLoader {
38 readonly import: (url: string) => Promise<{ readonly default?: unknown }>;
39 }
40
41 export const StackModuleLoader = Context.Reference<StackModuleLoader>(
42 "Alchemy/Alchemist/StackModuleLoader",
43 { defaultValue: () => ({ import: (url) => import(url) }) },
44 );
45
46 /**
47 * Import a stack entrypoint and return its default export, the stack value
48 * every stack operation takes.
49 */
50 export const importStack = Effect.fn("Alchemist.importStack")(function* (main: string) {
51 yield* emit({ type: "plan.phase", phase: "importing-module" });
52 const absolutePath = yield* resolveStackEntrypoint(main);
53 const loader = yield* StackModuleLoader;
54 const module = yield* Effect.promise(() => loader.import(pathToFileURL(absolutePath).href));
55 if (
56 !Effect.isEffect(module.default) ||
57 !Predicate.hasProperty(module.default, "stackName") ||
58 typeof module.default.stackName !== "string"
59 ) {
60 return yield* new StackEntrypointError({
61 message: `Stack entrypoint '${main}' must export a default stack definition (export default Alchemy.Stack({...})).`,
62 });
63 }
64 // oxlint-disable-next-line effecttsgo/unsafe-effect-type-assertion -- default export shape validated at runtime above
65 return module.default as ConfiguredStack<unknown, unknown>;
66 });
67
68 /**
69 * Which project and credentials an operation acts under. The optional fields
70 * fall back to the same defaults the CLI uses.
71 */
72 export interface Target {
73 /** Stack entrypoint. @default "alchemy.run.ts" */
74 readonly entrypoint?: string;
75 /** Profile whose credentials the run uses. Falls back to the active profile. */
76 readonly profile?: string;
77 /** `.env` file layered under the process environment. */
78 readonly envFile?: string;
79 }