-
-
Notifications
You must be signed in to change notification settings - Fork 241
Expand file tree
/
Copy pathcli.ts
More file actions
162 lines (142 loc) · 4.69 KB
/
cli.ts
File metadata and controls
162 lines (142 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env node
import { bootstrapRuntime } from './runtime/bootstrap-runtime.ts';
import { buildCliToolCatalog } from './cli/cli-tool-catalog.ts';
import { buildYargsApp } from './cli/yargs-app.ts';
import { getSocketPath, getWorkspaceKey, resolveWorkspaceRoot } from './daemon/socket-path.ts';
import { startMcpServer } from './server/start-mcp-server.ts';
import { listCliWorkflowIdsFromManifest } from './runtime/tool-catalog.ts';
import { flushAndCloseSentry, initSentry, recordBootstrapDurationMetric } from './utils/sentry.ts';
import { coerceLogLevel, setLogLevel, type LogLevel } from './utils/logger.ts';
import { hydrateSentryDisabledEnvFromProjectConfig } from './utils/sentry-config.ts';
function findTopLevelCommand(argv: string[]): string | undefined {
const flagsWithValue = new Set(['--socket', '--log-level', '--style']);
let skipNext = false;
for (const token of argv) {
if (skipNext) {
skipNext = false;
continue;
}
if (token.startsWith('-')) {
if (flagsWithValue.has(token)) {
skipNext = true;
}
continue;
}
return token;
}
return undefined;
}
async function buildLightweightYargsApp(): Promise<ReturnType<typeof import('yargs').default>> {
const yargs = (await import('yargs')).default;
const { hideBin } = await import('yargs/helpers');
return yargs(hideBin(process.argv))
.scriptName('')
.strict()
.help()
.option('socket', {
type: 'string',
describe: 'Override daemon unix socket path',
hidden: true,
})
.option('log-level', {
type: 'string',
describe: 'Set log verbosity level',
choices: ['none', 'error', 'warn', 'info', 'debug'] as const,
coerce: coerceLogLevel,
default: 'none',
})
.option('style', {
type: 'string',
describe: 'Output verbosity (minimal hides next steps)',
choices: ['normal', 'minimal'] as const,
default: 'normal',
})
.middleware((argv) => {
const level = argv['log-level'] as LogLevel | undefined;
if (level) {
setLogLevel(level);
}
});
}
async function runInitCommand(): Promise<void> {
const { registerInitCommand } = await import('./cli/commands/init.ts');
const app = await buildLightweightYargsApp();
registerInitCommand(app, { workspaceRoot: process.cwd() });
await app.parseAsync();
}
async function runSetupCommand(): Promise<void> {
const { registerSetupCommand } = await import('./cli/commands/setup.ts');
const app = await buildLightweightYargsApp();
registerSetupCommand(app);
await app.parseAsync();
}
async function main(): Promise<void> {
const cliBootstrapStartedAt = Date.now();
const earlyCommand = findTopLevelCommand(process.argv.slice(2));
if (earlyCommand === 'mcp') {
await startMcpServer();
return;
}
if (earlyCommand === 'init') {
await runInitCommand();
return;
}
if (earlyCommand === 'setup') {
await runSetupCommand();
return;
}
await hydrateSentryDisabledEnvFromProjectConfig();
initSentry({ mode: 'cli' });
// CLI mode uses disableSessionDefaults to show all tool parameters as flags
const result = await bootstrapRuntime({
runtime: 'cli',
configOverrides: {
disableSessionDefaults: true,
},
});
// Compute workspace context for daemon routing
const workspaceRoot = resolveWorkspaceRoot({
cwd: result.runtime.cwd,
projectConfigPath: result.configPath,
});
const defaultSocketPath = getSocketPath({
cwd: result.runtime.cwd,
projectConfigPath: result.configPath,
});
const workspaceKey = getWorkspaceKey({
cwd: result.runtime.cwd,
projectConfigPath: result.configPath,
});
const cliExposedWorkflowIds = await listCliWorkflowIdsFromManifest({
excludeWorkflows: ['session-management', 'workflow-discovery'],
});
const topLevelCommand = findTopLevelCommand(process.argv.slice(2));
const discoveryMode = topLevelCommand === 'xcode-ide' ? 'quick' : 'none';
// CLI uses a manifest-resolved catalog plus daemon-backed xcode-ide dynamic tools.
const catalog = await buildCliToolCatalog({
socketPath: defaultSocketPath,
workspaceRoot,
cliExposedWorkflowIds,
discoveryMode,
});
const yargsApp = buildYargsApp({
catalog,
runtimeConfig: result.runtime.config,
defaultSocketPath,
workspaceRoot,
workspaceKey,
workflowNames: cliExposedWorkflowIds,
cliExposedWorkflowIds,
});
recordBootstrapDurationMetric('cli', Date.now() - cliBootstrapStartedAt);
await yargsApp.parseAsync();
}
main()
.then(async () => {
await flushAndCloseSentry(2000);
})
.catch(async (err) => {
console.error(err instanceof Error ? err.message : String(err));
await flushAndCloseSentry(2000);
process.exit(1);
});