Open History to see the last commit
1import * as HostProcess from "@t3tools/shared/HostProcess";
2import * as Effect from "effect/Effect";
3import * as FileSystem from "effect/FileSystem";
4import * as Option from "effect/Option";
5import * as Schema from "effect/Schema";
6import { Command, GlobalFlag } from "effect/cli";
7import * as CliError from "effect/cli/CliError";
8
9import * as ServerConfig from "../config.ts";
10import { runServer } from "../server.ts";
11import { type CliServerFlags, resolveServerConfig, sharedServerCommandFlags } from "./config.ts";
12
13const encodeCommand = Schema.encodeEffect(Schema.fromJsonString(Schema.String));
14
15const runServerCommand = (
16 flags: CliServerFlags,
17 options?: {
18 readonly startupPresentation?: ServerConfig.StartupPresentation;
19 readonly forceAutoBootstrapProjectFromCwd?: boolean;
20 readonly rejectRunningServer?: boolean;
21 },
22) =>
23 Effect.gen(function* () {
24 const logLevel = yield* GlobalFlag.LogLevel;
25 const config = yield* resolveServerConfig(flags, logLevel, options);
26 return yield* runServer.pipe(Effect.provideService(ServerConfig.ServerConfig, config));
27 });
28
29export const runDefaultServerCommand = (flags: CliServerFlags) =>
30 Effect.gen(function* () {
31 if (Option.isSome(flags.cwd)) {
32 const cwd = flags.cwd.value.trim();
33 const fs = yield* FileSystem.FileSystem;
34 const platform = yield* HostProcess.Platform;
35 const explicitPath =
36 cwd === "." ||
37 cwd === ".." ||
38 cwd === "~" ||
39 /[/\\]/.test(cwd) ||
40 (platform === "win32" && /^[a-z]:/i.test(cwd));
41 if (
42 !explicitPath &&
43 (!(yield* fs.exists(cwd)) || (yield* fs.stat(cwd)).type !== "Directory")
44 ) {
45 return yield* new CliError.UserError({
46 cause: cwd,
47 userMessage: `Unknown command ${yield* encodeCommand(cwd)}. Use "t3 --help" for commands or an explicit path such as "t3 ./my-project" for a new directory.`,
48 });
49 }
50 }
51 return yield* runServerCommand(flags, { rejectRunningServer: true });
52 });
53
54export const startCommand = Command.make("start", { ...sharedServerCommandFlags }).pipe(
55 Command.withDescription("Run the T3 Code server."),
56 Command.withHandler((flags) => runServerCommand(flags, { rejectRunningServer: true })),
57);
58
59export const serveCommand = Command.make("serve", { ...sharedServerCommandFlags }).pipe(
60 Command.withDescription(
61 "Run the T3 Code server without opening a browser and print headless pairing details.",
62 ),
63 Command.withHandler((flags) =>
64 runServerCommand(flags, {
65 startupPresentation: "headless",
66 forceAutoBootstrapProjectFromCwd: false,
67 }),
68 ),
69);