-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathcommand.tsx
More file actions
180 lines (163 loc) · 5.75 KB
/
command.tsx
File metadata and controls
180 lines (163 loc) · 5.75 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
import { getErrorMessage } from '../../errors';
import { COMMAND_DESCRIPTIONS } from '../../tui/copy';
import { requireProject } from '../../tui/guards';
import { DeployScreen } from '../../tui/screens/deploy/DeployScreen';
import { handleDeploy } from './actions';
import type { DeployOptions } from './types';
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: { autoConfirm?: boolean; diffMode?: boolean } = {}): void {
requireProject();
const { unmount } = render(
<DeployScreen
isInteractive={false}
autoConfirm={options.autoConfirm}
diffMode={options.diffMode}
onExit={() => {
unmount();
process.exit(0);
}}
/>
);
}
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);
}
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');
}
if (options.json) {
console.log(JSON.stringify(result));
} else if (result.success) {
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.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}`);
}
} else {
console.error(result.error);
if (result.logPath) {
console.error(`Log: ${result.logPath}`);
}
}
process.exit(result.success ? 0 : 1);
}
export const registerDeploy = (program: Command) => {
program
.command('deploy')
.alias('p')
.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('--plan', 'Preview deployment without deploying (alias for --dry-run) [non-interactive]')
.option('--diff', 'Show CDK diff without deploying [non-interactive]')
.action(
async (cliOptions: {
target?: string;
yes?: boolean;
verbose?: boolean;
json?: boolean;
dryRun?: boolean;
plan?: boolean;
diff?: boolean;
}) => {
try {
requireProject();
const isDryRun = cliOptions.dryRun ?? cliOptions.plan;
if (cliOptions.json || cliOptions.target || isDryRun || cliOptions.yes || cliOptions.verbose) {
// CLI mode - any flag triggers non-interactive mode
const options = {
...cliOptions,
plan: isDryRun,
target: cliOptions.target ?? 'default',
progress: !cliOptions.json,
};
await handleDeployCLI(options as DeployOptions);
} else if (cliOptions.diff) {
// Diff-only: use TUI with diff mode
handleDeployTUI({ diffMode: true });
} else {
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);
}
}
);
};