Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

bin.ts

70 lines2.5 KB

1import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import { Argument, Command } from "effect/unstable/cli";
6import * as CliError from "effect/unstable/cli/CliError";
7​
8import * as NetService from "@t3tools/shared/Net";
9import packageJson from "../package.json" with { type: "json" };
10import { authCommand } from "./cli/auth.ts";
11import { connectCommand } from "./cli/connect.ts";
12import { pairCommand } from "./cli/pair.ts";
13import { hasCloudPublicConfig } from "./cloud/publicConfig.ts";
14import { sharedServerCommandFlags } from "./cli/config.ts";
15import { projectCommand } from "./cli/project.ts";
16import { runServerCommand, serveCommand, startCommand } from "./cli/server.ts";
17import { serviceCommand } from "./cli/service.ts";
18import { servicePreflightCommand } from "./cli/servicePreflight.ts";
19​
20const CliRuntimeLayer = Layer.mergeAll(NodeServices.layer, NetService.layer);
21​
22const connectPublicConfigMissingMessage =
23 "T3 Connect commands are unavailable: this build is missing T3 Connect public configuration.";
24​
25class ConnectPublicConfigMissingError extends CliError.UserError {
26 override get message() {
27 return connectPublicConfigMissingMessage;
28 }
30​
31const connectUnavailableCommand = Command.make("connect", {
32 command: Argument.string("command").pipe(Argument.variadic),
33}).pipe(
34 Command.withDescription("T3 Connect is unavailable in builds without public configuration."),
35 Command.withHidden,
36 Command.withHandler(() =>
37 Effect.fail(
38 new CliError.ShowHelp({
39 commandPath: ["t3", "connect"],
40 errors: [new ConnectPublicConfigMissingError({ cause: connectPublicConfigMissingMessage })],
41 }),
42 ),
43 ),
44);
45​
46export const makeCli = ({ cloudEnabled = hasCloudPublicConfig } = {}) =>
47 Command.make("t3", { ...sharedServerCommandFlags }).pipe(
48 Command.withDescription("Run the T3 Code server."),
49 Command.withHandler((flags) => runServerCommand(flags)),
50 Command.withSubcommands([
51 startCommand,
52 serveCommand,
53 pairCommand,
54 authCommand,
55 projectCommand,
56 serviceCommand,
57 servicePreflightCommand,
58 cloudEnabled ? connectCommand : connectUnavailableCommand,
59 ]),
60 );
61​
62export const cli = makeCli();
63​
64if (import.meta.main) {
65 Command.run(cli, { version: packageJson.version }).pipe(
66 Effect.scoped,
67 Effect.provide(CliRuntimeLayer),
68 NodeRuntime.runMain,
69 );