import { exec } from "alchemy/Util/exec";
import * as Command from "effect/cli/Command";
import * as Flag from "effect/cli/Flag";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";
import { ChildProcess } from "effect/process";
import * as Stream from "effect/Stream";
import { generate, loadGeneratorConfig } from "./ORM/Generate.ts";
import { CliError } from "./ORM/internal.ts";
const generateCommand = Command.make(
"generate",
{
config: Flag.String("config").pipe(Flag.withDefault("./prisma.config.ts")),
watch: Flag.Boolean("watch").pipe(Flag.withDefault(false)),
},
Effect.fn(function* ({ config, watch }) {
if (!watch) {
const result = yield* generate(config);
yield* Effect.logInfo(`Generated ${result.files.length} files in ${result.directory}`);
return;
}
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const loaded = yield* loadGeneratorConfig(config);
// alchemy's own CLI, found through module resolution so it works from the
// workspace and from an installed project alike.
const manifest = yield* Effect.sync(() => import.meta.resolve("alchemy/package.json"));
const bin = path.join(
path.dirname(yield* path.fromFileUrl(new URL(manifest))),
"bin",
"cli.js",
);
const run = Effect.gen(function* () {
const result = yield* exec(
ChildProcess.make(process.execPath, [
bin,
"provider",
"prisma",
"generate",
"--config",
loaded.absolute,
"--no-input",
]),
);
if (result.exitCode !== 0)
return yield* new CliError({ message: `${result.stdout}\n${result.stderr}` });
yield* Effect.logInfo(result.stdout);
}).pipe(
Effect.scoped,
Effect.catch((error) => Effect.logError(error)),
);
yield* run;
yield* Effect.logInfo(`Watching ${loaded.directory}; each generation uses a fresh process.`);
yield* fs.watch(loaded.directory, { recursive: true }).pipe(
Stream.filter((event) => {
const file = path.resolve(loaded.directory, event.path);
const relative = path.relative(loaded.directory, file);
const output = path.dirname(loaded.output);
return (
!relative
.split(path.sep)
.some((part) => part === "node_modules" || part === ".git" || part === ".alchemy") &&
!(file === output || file.startsWith(output + path.sep)) &&
/\.(?:ts|mts|cts|js|mjs|cjs|prisma|psl)$/.test(file)
);
}),
Stream.filterEffect((event) =>
Effect.gen(function* () {
const file = path.resolve(loaded.directory, event.path);
if (!(yield* fs.exists(file))) return true;
const contents = yield* fs.readFileString(file);
return (
!contents.startsWith("// Generated by alchemy prisma generate.") &&
!contents.includes("This file is automatically generated by 'prisma contract emit'")
);
}).pipe(Effect.orElseSucceed(() => false)),
),
Stream.debounce("200 millis"),
Stream.runForEach(() => run),
);
}),
).pipe(
Command.withDescription("Generate Effect bindings and standalone schemas from a Prisma contract"),
);
export const prismaCommand = Command.make("prisma").pipe(
Command.withDescription("Prisma contract tooling"),
Command.withSubcommands([generateCommand]),
);