forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
52 lines (48 loc) · 1.57 KB
/
Copy pathindex.ts
File metadata and controls
52 lines (48 loc) · 1.57 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
/**
* Shared test utilities for AgentCore CLI tests.
* Import these helpers instead of duplicating code in each test file.
*/
export { runCLI, spawnAndCollect, cleanSpawnEnv, type RunResult } from './cli-runner.js';
export { exists } from './fs-helpers.js';
export { hasCommand, hasAwsCredentials, prereqs } from './prereqs.js';
export { createTestProject, type TestProject, type CreateTestProjectOptions } from './project-factory.js';
export { readProjectConfig } from './config-reader.js';
/**
* Retry an async function up to `times` attempts with a delay between retries.
*/
export async function retry<T>(fn: () => Promise<T>, times: number, delayMs: number): Promise<T> {
let lastError: unknown;
for (let i = 0; i < times; i++) {
try {
return await fn();
} catch (err) {
lastError = err;
if (i < times - 1) {
await new Promise(resolve => setTimeout(resolve, delayMs));
}
}
}
throw lastError;
}
/**
* Strip ANSI escape codes from a string.
*/
export function stripAnsi(str: string): string {
// eslint-disable-next-line no-control-regex
return str.replace(/\x1B\[[0-9;]*[a-zA-Z]/g, '');
}
/**
* Parse JSON from CLI output, handling ANSI codes and whitespace.
* @throws Error if output is not valid JSON
*/
export function parseJsonOutput(output: string): unknown {
const cleaned = stripAnsi(output).trim();
if (!cleaned) {
throw new Error('Empty output, cannot parse JSON');
}
try {
return JSON.parse(cleaned);
} catch (_e) {
throw new Error(`Failed to parse JSON from output: ${cleaned.slice(0, 100)}...`);
}
}