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

main
ts 91 lines 3.44 KB
Raw
1 import { exec } from "alchemy/Util/exec";
2 import * as Command from "effect/cli/Command";
3 import * as Flag from "effect/cli/Flag";
4 import * as Effect from "effect/Effect";
5 import * as FileSystem from "effect/FileSystem";
6 import * as Path from "effect/Path";
7 import { ChildProcess } from "effect/process";
8 import * as Stream from "effect/Stream";
9 import { generate, loadGeneratorConfig } from "./ORM/Generate.ts";
10 import { CliError } from "./ORM/internal.ts";
11
12 const generateCommand = Command.make(
13 "generate",
14 {
15 config: Flag.String("config").pipe(Flag.withDefault("./prisma.config.ts")),
16 watch: Flag.Boolean("watch").pipe(Flag.withDefault(false)),
17 },
18 Effect.fn(function* ({ config, watch }) {
19 if (!watch) {
20 const result = yield* generate(config);
21 yield* Effect.logInfo(`Generated ${result.files.length} files in ${result.directory}`);
22 return;
23 }
24 const fs = yield* FileSystem.FileSystem;
25 const path = yield* Path.Path;
26 const loaded = yield* loadGeneratorConfig(config);
27 // alchemy's own CLI, found through module resolution so it works from the
28 // workspace and from an installed project alike.
29 const manifest = yield* Effect.sync(() => import.meta.resolve("alchemy/package.json"));
30 const bin = path.join(
31 path.dirname(yield* path.fromFileUrl(new URL(manifest))),
32 "bin",
33 "cli.js",
34 );
35 const run = Effect.gen(function* () {
36 const result = yield* exec(
37 ChildProcess.make(process.execPath, [
38 bin,
39 "provider",
40 "prisma",
41 "generate",
42 "--config",
43 loaded.absolute,
44 "--no-input",
45 ]),
46 );
47 if (result.exitCode !== 0)
48 return yield* new CliError({ message: `${result.stdout}\n${result.stderr}` });
49 yield* Effect.logInfo(result.stdout);
50 }).pipe(
51 Effect.scoped,
52 Effect.catch((error) => Effect.logError(error)),
53 );
54 yield* run;
55 yield* Effect.logInfo(`Watching ${loaded.directory}; each generation uses a fresh process.`);
56 yield* fs.watch(loaded.directory, { recursive: true }).pipe(
57 Stream.filter((event) => {
58 const file = path.resolve(loaded.directory, event.path);
59 const relative = path.relative(loaded.directory, file);
60 const output = path.dirname(loaded.output);
61 return (
62 !relative
63 .split(path.sep)
64 .some((part) => part === "node_modules" || part === ".git" || part === ".alchemy") &&
65 !(file === output || file.startsWith(output + path.sep)) &&
66 /\.(?:ts|mts|cts|js|mjs|cjs|prisma|psl)$/.test(file)
67 );
68 }),
69 Stream.filterEffect((event) =>
70 Effect.gen(function* () {
71 const file = path.resolve(loaded.directory, event.path);
72 if (!(yield* fs.exists(file))) return true;
73 const contents = yield* fs.readFileString(file);
74 return (
75 !contents.startsWith("// Generated by alchemy prisma generate.") &&
76 !contents.includes("This file is automatically generated by 'prisma contract emit'")
77 );
78 }).pipe(Effect.orElseSucceed(() => false)),
79 ),
80 Stream.debounce("200 millis"),
81 Stream.runForEach(() => run),
82 );
83 }),
84 ).pipe(
85 Command.withDescription("Generate Effect bindings and standalone schemas from a Prisma contract"),
86 );
87
88 export const prismaCommand = Command.make("prisma").pipe(
89 Command.withDescription("Prisma contract tooling"),
90 Command.withSubcommands([generateCommand]),
91 );