|
2 | 2 |
|
3 | 3 | import { Command } from "commander"; |
4 | 4 | import { VERSION } from "../version"; |
5 | | -import { createCli } from "trpc-cli"; |
6 | | -import { router } from "@/node/orpc/router"; |
7 | | -import { Config } from "@/node/config"; |
8 | | -import { ServiceContainer } from "@/node/services/serviceContainer"; |
9 | | -import { migrateLegacyMuxHome } from "@/common/constants/paths"; |
10 | | -import type { BrowserWindow } from "electron"; |
11 | | -import type { ORPCContext } from "@/node/orpc/context"; |
12 | 5 |
|
13 | | -// Minimal BrowserWindow stub for services that expect one (same as server.ts) |
14 | | -const mockWindow: BrowserWindow = { |
15 | | - isDestroyed: () => false, |
16 | | - setTitle: () => undefined, |
17 | | - webContents: { |
18 | | - send: () => undefined, |
19 | | - openDevTools: () => undefined, |
20 | | - }, |
21 | | -} as unknown as BrowserWindow; |
22 | | - |
23 | | -async function createServiceContext(): Promise<ORPCContext> { |
24 | | - migrateLegacyMuxHome(); |
25 | | - |
26 | | - const config = new Config(); |
27 | | - const serviceContainer = new ServiceContainer(config); |
28 | | - await serviceContainer.initialize(); |
29 | | - serviceContainer.windowService.setMainWindow(mockWindow); |
30 | | - |
31 | | - return { |
32 | | - projectService: serviceContainer.projectService, |
33 | | - workspaceService: serviceContainer.workspaceService, |
34 | | - providerService: serviceContainer.providerService, |
35 | | - terminalService: serviceContainer.terminalService, |
36 | | - windowService: serviceContainer.windowService, |
37 | | - updateService: serviceContainer.updateService, |
38 | | - tokenizerService: serviceContainer.tokenizerService, |
39 | | - serverService: serviceContainer.serverService, |
40 | | - menuEventService: serviceContainer.menuEventService, |
41 | | - }; |
42 | | -} |
43 | | - |
44 | | -async function main() { |
45 | | - const program = new Command(); |
46 | | - |
47 | | - program |
48 | | - .name("mux") |
49 | | - .description("mux - coder multiplexer") |
50 | | - .version(`mux ${VERSION.git_describe} (${VERSION.git_commit})`, "-v, --version"); |
51 | | - |
52 | | - program |
53 | | - .command("server") |
54 | | - .description("Start the HTTP/WebSocket oRPC server") |
55 | | - .allowUnknownOption() // server.ts handles its own options via commander |
56 | | - .action(() => { |
57 | | - // Remove 'server' from args since server.ts has its own commander instance |
58 | | - process.argv.splice(2, 1); |
59 | | - // eslint-disable-next-line @typescript-eslint/no-require-imports |
60 | | - require("./server"); |
61 | | - }); |
62 | | - |
63 | | - program |
64 | | - .command("version") |
65 | | - .description("Show version information") |
66 | | - .action(() => { |
67 | | - console.log(`mux ${VERSION.git_describe} (${VERSION.git_commit})`); |
68 | | - }); |
69 | | - |
70 | | - // Only initialize services if the 'api' subcommand is being used |
71 | | - if (process.argv[2] === "api") { |
72 | | - const context = await createServiceContext(); |
73 | | - program.addCommand( |
74 | | - (createCli({ router: router(), context }).buildProgram() as Command) |
75 | | - .name("api") |
76 | | - .description("Interact with the oRPC API directly") |
77 | | - ); |
78 | | - } |
79 | | - |
80 | | - // Default action: launch desktop app when no subcommand given |
81 | | - program.action(() => { |
| 6 | +const program = new Command(); |
| 7 | + |
| 8 | +program |
| 9 | + .name("mux") |
| 10 | + .description("mux - coder multiplexer") |
| 11 | + .version(`mux ${VERSION.git_describe} (${VERSION.git_commit})`, "-v, --version"); |
| 12 | + |
| 13 | +// Subcommands with their own CLI parsers - disable help interception so --help passes through |
| 14 | +program |
| 15 | + .command("server") |
| 16 | + .description("Start the HTTP/WebSocket oRPC server") |
| 17 | + .helpOption(false) |
| 18 | + .allowUnknownOption() |
| 19 | + .allowExcessArguments() |
| 20 | + .action(() => { |
| 21 | + process.argv.splice(2, 1); |
82 | 22 | // eslint-disable-next-line @typescript-eslint/no-require-imports |
83 | | - require("../desktop/main"); |
| 23 | + require("./server"); |
| 24 | + }); |
| 25 | + |
| 26 | +program |
| 27 | + .command("api") |
| 28 | + .description("Interact with the oRPC API via a running server") |
| 29 | + .helpOption(false) |
| 30 | + .allowUnknownOption() |
| 31 | + .allowExcessArguments() |
| 32 | + .action(async () => { |
| 33 | + process.argv.splice(2, 1); |
| 34 | + // eslint-disable-next-line no-restricted-syntax -- dynamic import needed for ESM-only trpc-cli |
| 35 | + const { runApiCli } = await import("./api"); |
| 36 | + await runApiCli(program); |
84 | 37 | }); |
85 | 38 |
|
86 | | - program.parse(); |
87 | | -} |
| 39 | +program |
| 40 | + .command("version") |
| 41 | + .description("Show version information") |
| 42 | + .action(() => { |
| 43 | + console.log(`mux ${VERSION.git_describe} (${VERSION.git_commit})`); |
| 44 | + }); |
88 | 45 |
|
89 | | -main().catch((error) => { |
90 | | - console.error("CLI initialization failed:", error); |
91 | | - process.exit(1); |
| 46 | +// Default action: launch desktop app when no subcommand given |
| 47 | +program.action(() => { |
| 48 | + // eslint-disable-next-line @typescript-eslint/no-require-imports |
| 49 | + require("../desktop/main"); |
92 | 50 | }); |
| 51 | + |
| 52 | +program.parse(); |
0 commit comments