2026-05-02 16:08:04 +00:00
|
|
|
import type { Argv } from "yargs"
|
|
|
|
|
import { Effect, Schema } from "effect"
|
|
|
|
|
import { AppRuntime, type AppServices } from "@/effect/app-runtime"
|
|
|
|
|
import { InstanceStore } from "@/project/instance-store"
|
2026-05-02 23:54:13 +00:00
|
|
|
import { InstanceRef } from "@/effect/instance-ref"
|
2026-05-02 16:08:04 +00:00
|
|
|
import { cmd } from "./cmd/cmd"
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* User-visible command failure. Throw via `fail("...")` from an effectCmd handler
|
|
|
|
|
* to surface a printed message + non-zero exit. Recognised by the global error
|
|
|
|
|
* formatter in `src/cli/error.ts` (FormatError), so the existing top-level
|
|
|
|
|
* catch + cleanup in `src/index.ts` runs normally.
|
|
|
|
|
*/
|
|
|
|
|
export class CliError extends Schema.TaggedErrorClass<CliError>()("CliError", {
|
|
|
|
|
message: Schema.String,
|
|
|
|
|
exitCode: Schema.optional(Schema.Number),
|
|
|
|
|
}) {}
|
|
|
|
|
|
|
|
|
|
export const fail = (message: string, exitCode = 1) => Effect.fail(new CliError({ message, exitCode }))
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Effect-native CLI command builder. Wraps yargs `cmd()` so the handler body is
|
|
|
|
|
* an `Effect` with `InstanceRef` provided and any `AppServices` yieldable.
|
|
|
|
|
*
|
2026-05-02 23:54:13 +00:00
|
|
|
* The handler is wrapped in `Effect.ensuring(store.dispose(ctx))` so the loaded
|
|
|
|
|
* InstanceContext is disposed (runDisposers + IPC `server.instance.disposed`)
|
|
|
|
|
* on every Exit — success, typed failure, defect, or interruption. Matches the
|
|
|
|
|
* legacy `bootstrap()` finally-disposal semantics without per-handler boilerplate.
|
|
|
|
|
*
|
2026-05-02 16:08:04 +00:00
|
|
|
* Errors propagate to the existing top-level handler in `src/index.ts`; use
|
|
|
|
|
* `fail("...")` for user-visible domain failures (clean exit, formatted message).
|
|
|
|
|
*
|
|
|
|
|
* Handlers are typically `Effect.fn("Cli.<name>")(function*(args) { ... })`,
|
|
|
|
|
* which adds a named tracing span per CLI invocation. Once all commands use
|
|
|
|
|
* `effectCmd`, swapping the underlying `cmd()` factory for effect/cli's
|
|
|
|
|
* `Command.make(...)` won't touch any handler bodies.
|
|
|
|
|
*/
|
|
|
|
|
export const effectCmd = <Args, A>(opts: {
|
|
|
|
|
command: string | readonly string[]
|
2026-05-02 21:55:13 +00:00
|
|
|
aliases?: string | readonly string[]
|
2026-05-02 16:08:04 +00:00
|
|
|
describe: string | false
|
|
|
|
|
builder?: (yargs: Argv) => Argv<Args>
|
|
|
|
|
/** Defaults to process.cwd(). Override for commands that take a directory positional. */
|
|
|
|
|
directory?: (args: Args) => string
|
|
|
|
|
handler: (args: Args) => Effect.Effect<A, CliError, AppServices | InstanceStore.Service>
|
|
|
|
|
}) =>
|
|
|
|
|
cmd<{}, Args>({
|
|
|
|
|
command: opts.command,
|
2026-05-02 21:55:13 +00:00
|
|
|
aliases: opts.aliases,
|
2026-05-02 16:08:04 +00:00
|
|
|
describe: opts.describe,
|
|
|
|
|
builder: opts.builder as never,
|
|
|
|
|
async handler(rawArgs) {
|
|
|
|
|
// yargs typing wraps Args in ArgumentsCamelCase<WithDoubleDash<...>>; cast at the boundary.
|
|
|
|
|
const args = rawArgs as unknown as Args
|
|
|
|
|
const directory = opts.directory?.(args) ?? process.cwd()
|
2026-05-02 23:54:13 +00:00
|
|
|
await AppRuntime.runPromise(
|
|
|
|
|
InstanceStore.Service.use((store) =>
|
|
|
|
|
store.provide(
|
|
|
|
|
{ directory },
|
|
|
|
|
Effect.gen(function* () {
|
|
|
|
|
const ctx = yield* InstanceRef
|
|
|
|
|
const body = opts.handler(args)
|
|
|
|
|
return ctx ? yield* body.pipe(Effect.ensuring(store.dispose(ctx))) : yield* body
|
|
|
|
|
}),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
)
|
2026-05-02 16:08:04 +00:00
|
|
|
},
|
|
|
|
|
})
|