@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, 04:45 UTC

main
ts 130 lines 3.88 KB
Raw
1 import * as Effect from "effect/Effect";
2 import { havePropsChanged, isResolved } from "../Diff.ts";
3 import * as Provider from "../Provider.ts";
4 import { Resource } from "../Resource.ts";
5 import { CommandExecutor, type CommandRunProps } from "./Command.ts";
6 import { hashDirectory, type MemoOptions } from "./Memo.ts";
7
8 export interface ExecProps extends CommandRunProps {
9 /**
10 * Controls which files are hashed to decide whether the command should
11 * re-run. By default every non-gitignored file in `cwd` is hashed, plus the
12 * nearest lockfile. Provide explicit globs to narrow the scope, or set
13 * `false` to disable memoization and re-run on every deploy.
14 *
15 * @see {@link MemoOptions}
16 * @default true
17 */
18 memo?: MemoOptions | boolean;
19 }
20
21 export interface Exec extends Resource<
22 "Command.Exec",
23 ExecProps,
24 {
25 /**
26 * Hash of the input files for this command, if memoization is enabled.
27 */
28 hash: {
29 input: string | undefined;
30 };
31 }
32 > {}
33
34 /**
35 * An `Exec` runs a shell command purely for its side effects — it has no
36 * output contract. Unlike `Build`, it does not produce or track an output
37 * asset; `reconcile` runs the command and the resource succeeds as long as the
38 * command exits with code `0` (a non-zero exit fails with a `CommandError`).
39 *
40 * Use it for one-off setup steps — running migrations, seeding data, code
41 * generation, or any command whose result lives outside Alchemy's state. By
42 * default the input files are content-hashed so the command only re-runs when
43 * its inputs (or `command`/`cwd`/`env`) change; set `memo: false` to re-run on
44 * every deploy.
45 *
46 * ### Running a Command
47 * **Example:** Run a One-Off Command
48 * ```typescript
49 * yield* Exec("codegen", {
50 * command: "npm run codegen",
51 * cwd: "./packages/api",
52 * });
53 * ```
54 *
55 * ### Running with Custom Environment
56 * **Example:** Run Database Migrations
57 * ```typescript
58 * yield* Exec("migrate", {
59 * command: "npm run db:migrate",
60 * env: {
61 * DATABASE_URL: Redacted.make("postgres://..."),
62 * },
63 * });
64 * ```
65 *
66 * ### Memoizing Re-Runs
67 * **Example:** Only Re-Run When Inputs Change
68 * ```typescript
69 * yield* Exec("codegen", {
70 * command: "npm run codegen",
71 * memo: { include: ["schema/**"] },
72 * });
73 * ```
74 *
75 * ### Bounding Command Runtime
76 * **Example:** Time Out a Migration
77 * ```typescript
78 * yield* Exec("migrate", {
79 * command: "npm run db:migrate",
80 * timeout: "5 minutes",
81 * });
82 * ```
83 *
84 * @resource
85 */
86 export const Exec = Resource<Exec>("Command.Exec");
87
88 export const ExecProvider = () =>
89 Provider.effect(
90 Exec,
91 Effect.gen(function* () {
92 const { run } = yield* CommandExecutor;
93
94 return {
95 list: () => Effect.succeed([]),
96 diff: Effect.fn(function* ({ olds, news, output }) {
97 if (!output || !isResolved(news)) return undefined;
98
99 // Always update if memoization is disabled or input hash is not available.
100 if (news.memo === false || !output.hash.input) return { action: "update" };
101
102 // Optimization: short-circuit if props have changed to avoid unnecessary file system operations.
103 if (havePropsChanged(olds, news)) return { action: "update" };
104
105 const newHash = yield* hashDirectory({
106 cwd: news.cwd,
107 memo: news.memo === true ? {} : news.memo,
108 });
109 return {
110 action: newHash === output.hash.input ? "noop" : "update",
111 };
112 }),
113 reconcile: Effect.fn(function* ({ news, session }) {
114 yield* run(news, session);
115 return {
116 hash: {
117 input:
118 news.memo === false
119 ? undefined
120 : yield* hashDirectory({
121 cwd: news.cwd,
122 memo: news.memo === true ? {} : news.memo,
123 }),
124 },
125 };
126 }),
127 delete: () => Effect.void,
128 };
129 }),
130 );