main.ts
This file
Path
apps/cli/src/main.ts
187 lines · 5.7 KiB · TypeScript
Last changed by
Merge cli/default-platform into mainoscabrielbefore adoptionb9fc54b
History of this fileTake it
The walk stopped after 200 commits: lines older than c3d7b1b point at it.
Merge build/613-answer-held-back-and-guards: an answer is judged by the sealed suite it was pinned to, lands with a Held-back: trailer, and can't carry layer content or a stored valueoscabrielbefore adoptionc3d7b1b
lines 1–2why, one step back1import { Brand } from "@repo/brand";2import { ApprovalNeeded, MissingValue, Reported } from "@repo/protocol/cli";Merge cli/exit-codes into mainoscabrielbefore adoptionc19a862
line 3why, one step back3import { ExitCode } from "@repo/protocol/exit-code";Merge build/613-answer-held-back-and-guards: an answer is judged by the sealed suite it was pinned to, lands with a Held-back: trailer, and can't carry layer content or a stored valueoscabrielbefore adoptionc3d7b1b
lines 4–20why, one step back4import { Unauthenticated } from "@repo/protocol/principal";5import { Refusal } from "@repo/protocol/refusal";6import { Console, Effect, Exit, Layer, Option, Runtime, Schema } from "effect";7import { CliConfig, CliError, Command, GlobalFlag } from "effect/cli";89import { credential } from "./credential-helper.ts";10import { CredentialsFile } from "./credentials.ts";11import { hook } from "./hook.ts";12import { refuseStrangeFlags } from "./local/env-set.ts";13import { holdStdout, Output, OutputLive } from "./output.ts";14import { CurrentMode, Policy } from "./policy.ts";15import {16 isTransportFailure,17 RemoteClient,18 transportMessage,19} from "./remote.ts";20import { commands, mcp, UnknownCommand } from "./surfaces.ts";Merge build/1104 into mainoscabrielbefore adoption82cc398
line 21why, one step back21import { cliVersion } from "./version.ts";Merge build/613-answer-held-back-and-guards: an answer is judged by the sealed suite it was pinned to, lands with a Held-back: trailer, and can't carry layer content or a stored valueoscabrielbefore adoptionc3d7b1b
lines 22–24why, one step back2223export const banner = [24 "Start here:",Merge cli/init-and-ask-open into mainoscabrielbefore adoption6be06d3
line 25why, one step back25 ` ${Brand.cli} front list the open asks across every project`,Merge build/613-answer-held-back-and-guards: an answer is judged by the sealed suite it was pinned to, lands with a Held-back: trailer, and can't carry layer content or a stored valueoscabrielbefore adoptionc3d7b1b
lines 26–36why, one step back26 ` ${Brand.cli} claim take one; you get a fork to push your answer to`,27 ` ${Brand.cli} check run the gate's checks locally before you push`,28].join("\n");2930const tree = [...commands, mcp, hook, credential];3132export const root = Command.make(Brand.cli).pipe(33 Command.withDescription(banner),34 Command.withSubcommands(tree)35);36Merge cli/exit-codes into mainoscabrielbefore adoptionc19a862
line 37why, one step back37const USAGE = ExitCode.usage;Merge build/613-answer-held-back-and-guards: an answer is judged by the sealed suite it was pinned to, lands with a Held-back: trailer, and can't carry layer content or a stored valueoscabrielbefore adoptionc3d7b1b
lines 38–53why, one step back3839export class Misused extends Schema.TaggedError<Misused>()("Misused", {}) {40 override readonly [Runtime.errorExitCode] = USAGE;41 override readonly [Runtime.errorReported] = false;42}4344const isRefusal = Schema.is(Refusal);4546const isReported = Schema.is(Reported);4748const isUnauthenticated = Schema.is(Unauthenticated);4950const isSharedUsage = Schema.is(51 Schema.Union([ApprovalNeeded, MissingValue, UnknownCommand])52);53Merge cli/init-and-ask-open into mainoscabrielbefore adoption6be06d3
lines 54–63why, one step back54const isMissingValue = Schema.is(MissingValue);5556/** `asks` wants one project; outside a clone, the cross-project list is the answer. */57const hinted = (failure: Failure) =>58 isMissingValue(failure) &&59 failure.command === "asks" &&60 failure.flag === "--project"61 ? `${failure.message}; ${Brand.cli} front lists every project's`62 : undefined;63Merge build/613-answer-held-back-and-guards: an answer is judged by the sealed suite it was pinned to, lands with a Held-back: trailer, and can't carry layer content or a stored valueoscabrielbefore adoptionc3d7b1b
lines 64–84why, one step back64const signIn = [65 "Unauthenticated: the platform does not know who you are",66 ` next: ${Brand.cli} login`,67 " retryable: no",68].join("\n");6970type Failure = Command.Error<typeof root> | CliError.CliError;7172const isUsage = (failure: Failure) =>73 isSharedUsage(failure) || Runtime.getErrorExitCode(failure) === USAGE;7475const usageLine = (failure: Failure): Option.Option<string> => {76 if (CliError.isCliError(failure)) {77 return Option.some(78 Schema.is(CliError.ShowHelp)(failure)79 ? failure.errors.map((error) => error.message).join("; ")80 : failure.message81 );82 }8384 if (isUsage(failure) || Schema.isSchemaError(failure)) {Merge cli/init-and-ask-open into mainoscabrielbefore adoption6be06d3
line 85why, one step back85 return Option.some(hinted(failure) ?? failure.message);Merge build/613-answer-held-back-and-guards: an answer is judged by the sealed suite it was pinned to, lands with a Held-back: trailer, and can't carry layer content or a stored valueoscabrielbefore adoptionc3d7b1b
lines 86–87why, one step back86 }87Merge cli/default-platform into mainoscabrielbefore adoptionb9fc54b
line 88why, one step back88 return Option.none();Merge build/613-answer-held-back-and-guards: an answer is judged by the sealed suite it was pinned to, lands with a Held-back: trailer, and can't carry layer content or a stored valueoscabrielbefore adoptionc3d7b1b
lines 89–92why, one step back89};9091const noteFor = (failure: Failure) => {92 if (isUnauthenticated(failure)) {Merge cli/exit-codes into mainoscabrielbefore adoptionc19a862
line 93why, one step back93 return { exitCode: ExitCode.noPerm, note: signIn };Merge build/613-answer-held-back-and-guards: an answer is judged by the sealed suite it was pinned to, lands with a Held-back: trailer, and can't carry layer content or a stored valueoscabrielbefore adoptionc3d7b1b
lines 94–96why, one step back94 }9596 if (isTransportFailure(failure)) {Merge cli/exit-codes into mainoscabrielbefore adoptionc19a862
lines 97–100why, one step back97 return {98 exitCode: ExitCode.unavailable,99 note: `could not reach the platform: ${transportMessage(failure)}`,100 };Merge build/613-answer-held-back-and-guards: an answer is judged by the sealed suite it was pinned to, lands with a Held-back: trailer, and can't carry layer content or a stored valueoscabrielbefore adoptionc3d7b1b
lines 101–102why, one step back101 }102Merge cli/exit-codes into mainoscabrielbefore adoptionc19a862
line 103why, one step back103 return { exitCode: ExitCode.failed, note: String(failure) };Merge build/613-answer-held-back-and-guards: an answer is judged by the sealed suite it was pinned to, lands with a Held-back: trailer, and can't carry layer content or a stored valueoscabrielbefore adoptionc3d7b1b
lines 104–105why, one step back104};105Merge cli/exit-codes into mainoscabrielbefore adoptionc19a862
lines 106–115why, one step back106const printed = (failure: Failure): Option.Option<Reported> => {107 if (isReported(failure)) {108 return Option.some(failure);109 }110111 return isRefusal(failure)112 ? Option.some(Reported.of([failure]))113 : Option.none();114};115Merge build/613-answer-held-back-and-guards: an answer is judged by the sealed suite it was pinned to, lands with a Held-back: trailer, and can't carry layer content or a stored valueoscabrielbefore adoptionc3d7b1b
lines 116–120why, one step back116const settle = (117 failure: Failure118): Effect.Effect<never, Reported | Misused, Output> =>119 Option.match(usageLine(failure), {120 onNone: () =>Merge cli/exit-codes into mainoscabrielbefore adoptionc19a862
lines 121–130why, one step back121 Option.match(printed(failure), {122 onNone: () => {123 const { exitCode, note } = noteFor(failure);124125 return Output.use((output) => output.note(note)).pipe(126 Effect.andThen(Effect.fail(new Reported({ exitCode })))127 );128 },129 onSome: Effect.fail,130 }),Merge build/613-answer-held-back-and-guards: an answer is judged by the sealed suite it was pinned to, lands with a Held-back: trailer, and can't carry layer content or a stored valueoscabrielbefore adoptionc3d7b1b
lines 131–139why, one step back131 onSome: (line) =>132 Output.use((output) => output.note(line)).pipe(133 Effect.andThen(Effect.fail(new Misused()))134 ),135 });136137const isMisuse = (failure: Failure) =>138 Schema.is(CliError.ShowHelp)(failure) && failure.errors.length > 0;139Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f
line 140why, one step back140/** The wizard prompts, so only a person at a terminal gets it. */Merge build/613-answer-held-back-and-guards: an answer is judged by the sealed suite it was pinned to, lands with a Held-back: trailer, and can't carry layer content or a stored valueoscabrielbefore adoptionc3d7b1b
lines 141–146why, one step back141const builtInsFor = (policy: Option.Option<Policy["Service"]>) =>142 Option.exists(policy, ({ mode }) => mode !== "human")143 ? GlobalFlag.BuiltIns.filter((flag) => flag !== GlobalFlag.Wizard)144 : GlobalFlag.BuiltIns;145146/**Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f
lines 147–148why, one step back147 * A defect is not caught: the runtime prints it. `env set`'s words are screened148 * before effect/cli parses them, so a value typed where a flag goes is never printed back.Merge build/613-answer-held-back-and-guards: an answer is judged by the sealed suite it was pinned to, lands with a Held-back: trailer, and can't carry layer content or a stored valueoscabrielbefore adoptionc3d7b1b
lines 149–159why, one step back149 */150export const program = (argv: readonly string[]) =>151 Effect.gen(function* () {152 yield* refuseStrangeFlags(argv);153154 const policy = yield* Effect.serviceOption(Policy);155 const stdout = holdStdout(yield* Console.Console);156157 const exit = yield* Effect.exit(158 Command.runWith(root.pipe(Command.provideEffectDiscard(stdout.open)), {159 renderErrors: false,Merge build/1104 into mainoscabrielbefore adoption82cc398
line 160why, one step back160 version: cliVersion,Merge build/613-answer-held-back-and-guards: an answer is judged by the sealed suite it was pinned to, lands with a Held-back: trailer, and can't carry layer content or a stored valueoscabrielbefore adoptionc3d7b1b
lines 161–187why, one step back161 })(argv).pipe(162 Effect.provideService(163 CliConfig.CliConfig,164 CliConfig.make({ builtIns: builtInsFor(policy) })165 ),166 Effect.provideService(Console.Console, stdout.console)167 )168 );169170 if (!Option.exists(Exit.findErrorOption(exit), isMisuse)) {171 yield* stdout.release;172 }173174 return yield* exit;175 }).pipe(176 Effect.catchTag("ShowHelp", (help) =>177 help.errors.length === 0 ? Effect.void : Effect.fail(help)178 ),179 Effect.matchEffect({180 onFailure: (failure) => settle(failure),181 onSuccess: () => Effect.void,182 })183 );184185export const CliServices = Layer.mergeAll(OutputLive, RemoteClient).pipe(186 Layer.provideMerge(Layer.mergeAll(CurrentMode, CredentialsFile))187);