forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeployStatus.tsx
More file actions
222 lines (200 loc) · 6.64 KB
/
Copy pathDeployStatus.tsx
File metadata and controls
222 lines (200 loc) · 6.64 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
221
222
import type { DeployMessage } from '../../cdk/toolkit-lib';
import { GradientText } from './StepProgress';
import { Box, Text } from 'ink';
import React, { useMemo } from 'react';
interface DeployStatusProps {
messages: DeployMessage[];
isComplete: boolean;
hasError: boolean;
hasPostDeployError?: boolean;
postDeployWarnings?: string[];
/** Root CloudFormation resource failure detail (logical id, type, reason + console link). */
failureDetail?: string | null;
}
const PROGRESS_BAR_WIDTH = 20;
// CDK message code for resource events
const CDK_CODE_RESOURCE_EVENT = 'CDK_TOOLKIT_I5502';
/**
* Extract resource progress from messages.
* Progress is pre-extracted at the source (in createSwitchableIoHost).
*/
function extractProgress(messages: DeployMessage[]): { current: number; total: number } | null {
// Search from end to find most recent message with progress
for (let i = messages.length - 1; i >= 0; i--) {
const msg = messages[i];
if (msg?.progress) {
return { current: msg.progress.completed, total: msg.progress.total };
}
}
return null;
}
/**
* Progress bar component.
*/
function ProgressBar({ current, total }: { current: number; total: number }) {
// CDK toolkit can briefly emit completed > total during graph expansion.
// Clamp here so the bar never asks String.repeat for a negative count.
const safeTotal = total > 0 ? total : 0;
const safeCurrent = Math.max(0, Math.min(current, safeTotal));
const percent = safeTotal > 0 ? safeCurrent / safeTotal : 0;
const filled = Math.min(PROGRESS_BAR_WIDTH, Math.max(0, Math.round(percent * PROGRESS_BAR_WIDTH)));
const empty = PROGRESS_BAR_WIDTH - filled;
return (
<Box>
<Text color="cyan">[</Text>
<Text color="green">{'█'.repeat(filled)}</Text>
<Text color="gray">{'░'.repeat(empty)}</Text>
<Text color="cyan">]</Text>
<Text>
{' '}
{safeCurrent}/{safeTotal}
</Text>
</Box>
);
}
type ResourceStatus =
| 'CREATE_IN_PROGRESS'
| 'CREATE_COMPLETE'
| 'CREATE_FAILED'
| 'UPDATE_IN_PROGRESS'
| 'UPDATE_COMPLETE'
| 'UPDATE_FAILED'
| 'DELETE_IN_PROGRESS'
| 'DELETE_COMPLETE'
| 'DELETE_FAILED';
interface ParsedResource {
resourceType: string;
status: ResourceStatus;
}
/**
* Get color for a resource status.
*/
function getStatusColor(status: ResourceStatus): string | undefined {
if (status.endsWith('_COMPLETE')) return 'green';
if (status.endsWith('_FAILED')) return 'red';
if (status.endsWith('_IN_PROGRESS')) return 'cyan';
return undefined;
}
/**
* Extract resource type and status from a CDK resource event message.
* Only processes I5502 (resource event) messages.
*/
function parseResourceMessage(msg: DeployMessage): ParsedResource | null {
// Only process resource event messages
if (msg.code !== CDK_CODE_RESOURCE_EVENT) {
return null;
}
const text = msg.message;
// Skip CLEANUP messages - they're confusing
if (text.includes('CLEANUP')) {
return null;
}
// Format: "StackName | STATUS | AWS::Service::Resource | LogicalId"
const resourceMatch = /(AWS::\S+)/.exec(text);
const statusMatch =
/(CREATE_IN_PROGRESS|CREATE_COMPLETE|CREATE_FAILED|UPDATE_IN_PROGRESS|UPDATE_COMPLETE|UPDATE_FAILED|DELETE_IN_PROGRESS|DELETE_COMPLETE|DELETE_FAILED)/.exec(
text
);
if (resourceMatch?.[1] && statusMatch) {
const shortType = resourceMatch[1].replace(/^AWS::/, '');
return { resourceType: shortType, status: statusMatch[1] as ResourceStatus };
}
return null;
}
/**
* Render a resource line with color-coded status.
*/
function ResourceLine({ resource }: { resource: ParsedResource }) {
const color = getStatusColor(resource.status);
return (
<Text color={color}>
{resource.resourceType} {resource.status}
</Text>
);
}
/**
* Deploy status component showing deployment progress in a contained box.
* During deployment: shows last N resource events (type + status only)
* After completion: shows success/failure state
*/
export function DeployStatus({
messages,
isComplete,
hasError,
hasPostDeployError,
postDeployWarnings,
failureDetail,
}: DeployStatusProps) {
// Parse and filter messages to only meaningful resource updates
const parsedResources = messages
.map(msg => ({ original: msg, parsed: parseResourceMessage(msg) }))
.filter((m): m is { original: DeployMessage; parsed: ParsedResource } => m.parsed !== null)
.slice(-8);
// Extract progress for the bar
const progress = useMemo(() => extractProgress(messages), [messages]);
// When complete, show final status
if (isComplete) {
const hasWarning = hasPostDeployError && !hasError;
const borderColor = hasError ? 'red' : hasWarning ? 'yellow' : 'green';
const textColor = borderColor;
const bannerText = hasError
? '✗ Deploy to AWS Failed'
: hasWarning
? '⚠ Deploy to AWS Complete (with warnings)'
: '✓ Deploy to AWS Complete';
return (
<Box flexDirection="column" borderStyle="round" borderColor={borderColor} paddingX={1} minWidth={50}>
<Text bold color={textColor}>
{bannerText}
</Text>
{progress && (
<Box marginTop={1}>
<ProgressBar current={progress.total} total={progress.total} />
</Box>
)}
{hasError && (
<Box flexDirection="column" marginTop={1}>
{parsedResources.slice(-3).map((m, i) => (
<ResourceLine key={`${m.original.code}-${i}`} resource={m.parsed} />
))}
</Box>
)}
{hasError && failureDetail && (
<Box flexDirection="column" marginTop={1}>
{failureDetail.split('\n').map((line, i) => (
<Text key={i} color="red">
{line}
</Text>
))}
</Box>
)}
{hasWarning && postDeployWarnings && postDeployWarnings.length > 0 && (
<Box flexDirection="column" marginTop={1}>
{postDeployWarnings.map((w, i) => (
<Text key={i} color="yellow">
{w}
</Text>
))}
</Box>
)}
</Box>
);
}
return (
<Box flexDirection="column" borderStyle="round" borderColor="gray" paddingX={1} minWidth={50}>
<GradientText text="Deploying to AWS" />
{progress && (
<Box marginTop={1}>
<ProgressBar current={progress.current} total={progress.total} />
</Box>
)}
{parsedResources.length > 0 && (
<Box flexDirection="column" marginTop={1}>
{parsedResources.map((m, i) => (
<ResourceLine key={`${m.original.code}-${i}`} resource={m.parsed} />
))}
</Box>
)}
</Box>
);
}