-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathcommand.tsx
More file actions
220 lines (197 loc) · 7.23 KB
/
Copy pathcommand.tsx
File metadata and controls
220 lines (197 loc) · 7.23 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { ConfigIO, serializeResult } from '../../../lib';
import { getErrorMessage } from '../../errors';
import { withCommandRunTelemetry } from '../../telemetry/cli-command-run.js';
import { renderTUI } from '../../tui';
import { COMMAND_DESCRIPTIONS } from '../../tui/copy';
import { requireProject, requireTTY } from '../../tui/guards';
import { handleDeploy } from './actions';
import type { DeployOptions, DeployResult } from './types';
import { DEFAULT_DEPLOY_ATTRS, computeDeployAttrs } from './utils';
import { validateDeployOptions } from './validate';
import type { Command } from '@commander-js/extra-typings';
import { Text, render } from 'ink';
import React from 'react';
const SPINNER_FRAMES = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
function handleDeployTUI(options: { diffMode?: boolean } = {}): Promise<void> {
requireProject();
return renderTUI({
initialRoute: { name: 'deploy', diffMode: options.diffMode },
enterAltScreen: false,
actionOnBack: 'exit',
isInteractive: false,
});
}
async function handleDeployCLI(options: DeployOptions): Promise<void> {
const validation = validateDeployOptions(options);
if (!validation.valid) {
if (options.json) {
console.log(JSON.stringify({ success: false, error: validation.error }));
} else {
console.error(validation.error);
}
process.exit(1);
}
// Compute attrs upfront from project spec (available before deploy)
const mode = options.diff ? 'diff' : options.plan ? 'dry-run' : 'deploy';
const attrs = await new ConfigIO()
.readProjectSpec()
.then(spec => computeDeployAttrs(spec, mode))
.catch(() => ({ ...DEFAULT_DEPLOY_ATTRS, mode }) as const);
const { deployResult } = await withCommandRunTelemetry('deploy', attrs, async () => {
const result = await executeDeploy(options).catch(
(e): DeployResult => ({ success: false, error: e instanceof Error ? e : new Error(getErrorMessage(e)) })
);
if (!result.success) {
return { success: false as const, error: result.error, deployResult: result };
}
return { success: true as const, deployResult: result };
});
// ALL output happens here, after telemetry
if (!deployResult.success) {
if (options.json) {
console.log(JSON.stringify(serializeResult(deployResult)));
} else {
console.error(deployResult.error.message);
if (deployResult.logPath) {
console.error(`Log: ${deployResult.logPath}`);
}
}
process.exit(1);
}
printDeployResult(deployResult, options);
if (deployResult.postDeployWarnings && deployResult.postDeployWarnings.length > 0) {
process.exit(2);
}
process.exit(0);
}
async function executeDeploy(options: DeployOptions): Promise<DeployResult> {
let spinner: NodeJS.Timeout | undefined;
// Progress callback for --progress mode
const onProgress = options.progress
? (step: string, status: 'start' | 'success' | 'error') => {
if (spinner) {
clearInterval(spinner);
process.stdout.write('\r\x1b[K'); // Clear line
}
if (status === 'start') {
let i = 0;
process.stdout.write(`${SPINNER_FRAMES[0]} ${step}...`);
spinner = setInterval(() => {
i = (i + 1) % SPINNER_FRAMES.length;
process.stdout.write(`\r${SPINNER_FRAMES[i]} ${step}...`);
}, 80);
} else if (status === 'success') {
console.log(`✓ ${step}`);
} else {
console.log(`✗ ${step}`);
}
}
: undefined;
const onResourceEvent = options.verbose
? (message: string) => {
console.log(message);
}
: undefined;
const result = await handleDeploy({
target: options.target!,
autoConfirm: options.yes,
verbose: options.verbose ?? options.diff,
plan: options.plan,
diff: options.diff,
onProgress,
onResourceEvent,
});
if (spinner) {
clearInterval(spinner);
process.stdout.write('\r\x1b[K');
}
return result;
}
function printDeployResult(result: DeployResult & { success: true }, options: DeployOptions): void {
if (options.json) {
console.log(JSON.stringify(result));
return;
}
if (options.diff) {
console.log(`\n✓ Diff complete for '${result.targetName}' (stack: ${result.stackName})`);
} else if (options.plan) {
console.log(`\n✓ Dry run complete for '${result.targetName}' (stack: ${result.stackName})`);
console.log('\nRun `agentcore deploy` to deploy.');
} else {
console.log(`\n✓ Deployed to '${result.targetName}' (stack: ${result.stackName})`);
// Show stack outputs in non-JSON mode
if (result.outputs && Object.keys(result.outputs).length > 0) {
console.log('\nOutputs:');
for (const [key, value] of Object.entries(result.outputs)) {
console.log(` ${key}: ${value}`);
}
}
if (result.postDeployWarnings && result.postDeployWarnings.length > 0) {
console.log('\n⚠ Post-deploy warnings:');
for (const warning of result.postDeployWarnings) {
console.log(` ${warning}`);
}
}
if (result.notes && result.notes.length > 0) {
for (const note of result.notes) {
console.log(`\nNote: ${note}`);
}
}
if (result.nextSteps && result.nextSteps.length > 0) {
console.log(`Next: ${result.nextSteps.join(' | ')}`);
}
}
if (result.logPath) {
console.log(`\nLog: ${result.logPath}`);
}
}
export const registerDeploy = (program: Command) => {
program
.command('deploy')
.alias('dp')
.description(COMMAND_DESCRIPTIONS.deploy)
.option('--target <target>', 'Deployment target name (default: "default") [non-interactive]')
.option('-y, --yes', 'Auto-confirm prompts, read credentials from env [non-interactive]')
.option('-v, --verbose', 'Show resource-level deployment events [non-interactive]')
.option('--json', 'Output as JSON [non-interactive]')
.option('--dry-run', 'Preview deployment without deploying [non-interactive]')
.option('--diff', 'Show CDK diff without deploying [non-interactive]')
.action(
async (cliOptions: {
target?: string;
yes?: boolean;
verbose?: boolean;
json?: boolean;
dryRun?: boolean;
diff?: boolean;
}) => {
try {
requireProject();
if (cliOptions.json || cliOptions.target || cliOptions.dryRun || cliOptions.yes || cliOptions.verbose) {
// CLI mode - any flag triggers non-interactive mode
const options = {
...cliOptions,
plan: cliOptions.dryRun,
target: cliOptions.target ?? 'default',
progress: !cliOptions.json,
};
await handleDeployCLI(options as DeployOptions);
} else if (cliOptions.diff) {
// Diff-only: use TUI with diff mode
requireTTY();
await handleDeployTUI({ diffMode: true });
} else {
requireTTY();
await handleDeployTUI();
}
} catch (error) {
if (cliOptions.json) {
console.log(JSON.stringify({ success: false, error: getErrorMessage(error) }));
} else {
render(<Text color="red">Error: {getErrorMessage(error)}</Text>);
}
process.exit(1);
}
}
);
};