command.ts
This file
Path
apps/cli/src/tui/command.ts
99 lines · 2.9 KiB · TypeScript
Last changed by
Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100import { Client, Description, TodayOf } from "@repo/protocol/contracts";import { ProjectName } from "@repo/protocol/ids";import type { NotDecider, UnknownProject } from "@repo/protocol/refusal";import { Context, Effect, Option, Schema } from "effect";import { Command, Flag } from "effect/cli";
import { endRefused, Output } from "../output.ts";import { views } from "../render.ts";import { payloadFor, runTodayTui, TuiRefused } from "./run.ts";
const COMMAND = "today";
const encodeToday = Schema.encodeUnknownEffect(TodayOf.successSchema);
const reported = (json: boolean) => { const once = (refusal: NotDecider | UnknownProject) => Effect.flatMap(Effect.service(Output), (output) => endRefused(output, { command: COMMAND, json, refusals: [refusal] }) );
return { NotDecider: once, UnknownProject: once };};
const print = (input: { readonly json: boolean; readonly project: Option.Option<ProjectName>;}) => Effect.gen(function* () { const client = yield* Client; const output = yield* Output;
const page = yield* client .today(payloadFor(input.project)) .pipe(Effect.catchTags(reported(input.json)));
const data = yield* Effect.orDie(encodeToday(page));
const view = Option.getOrElse(views.today(page), () => { const text = JSON.stringify(data, null, 2);
return { markdown: ["```json", text, "```"].join("\n"), text }; });
return yield* output.success({ command: COMMAND, data, json: input.json, view, }); });
const full = (input: { readonly json: boolean; readonly project: Option.Option<ProjectName>;}) => input.json ? Effect.fail( new TuiRefused({ reason: "--json asks for one document for a script" }) ) : runTodayTui({ project: input.project }).pipe( Effect.catchTags(reported(false)) );
export const today = Command.make( COMMAND, { json: Flag.Boolean("json").pipe( Flag.withDefault(false), Flag.withDescription( "Print one versioned JSON envelope on stdout, and nothing else there" ) ), project: Flag.String("project").pipe( Flag.withSchema(ProjectName), Flag.optional, Flag.withDescription( "The project whose decisions to show; every project you decide for when left out" ) ), tui: Flag.Boolean("tui").pipe( Flag.withDefault(false), Flag.withDescription( "Open Today full-screen and take its decisions with the keyboard; for a person at a terminal" ) ), }, ({ json, project, tui }) => Effect.gen(function* () { if (tui) { return yield* full({ json, project }); }
return yield* print({ json, project }); }).pipe(Effect.withSpan(`cli.${COMMAND}`))).pipe( Command.withDescription( `${Context.get(TodayOf.annotations, Description)} It only reads, unless --tui opens it full-screen to take decisions at a terminal.` ));