-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
list-stacks.ts
98 lines (82 loc) · 2.49 KB
/
list-stacks.ts
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
import '@jsii/check-node/run';
import { Environment } from '@aws-cdk/cx-api';
import { DefaultSelection, ExtendedStackSelection, StackCollection } from './api/cxapp/cloud-assembly';
import { CdkToolkit } from './cdk-toolkit';
/**
* Options for List Stacks
*/
export interface ListStacksOptions {
/**
* Stacks to list
*
* @default - All stacks are listed
*/
readonly selectors: string[];
}
/**
* Type to store stack dependencies recursively
*/
export type DependencyDetails = {
id: string;
dependencies: DependencyDetails[];
};
/**
* Type to store stack and their dependencies
*/
export type StackDetails = {
id: string;
name: string;
environment: Environment;
dependencies: DependencyDetails[];
};
/**
* List Stacks
*
* @param toolkit cdk toolkit
* @param options list stacks options
* @returns StackDetails[]
*/
export async function listStacks(toolkit: CdkToolkit, options: ListStacksOptions): Promise<StackDetails[]> {
const assembly = await toolkit.assembly();
const stacks = await assembly.selectStacks({
patterns: options.selectors,
}, {
extend: ExtendedStackSelection.Upstream,
defaultBehavior: DefaultSelection.AllStacks,
});
function calculateStackDependencies(collectionOfStacks: StackCollection): StackDetails[] {
const allData: StackDetails[] = [];
for (const stack of collectionOfStacks.stackArtifacts) {
const data: StackDetails = {
id: stack.displayName ?? stack.id,
name: stack.stackName,
environment: stack.environment,
dependencies: [],
};
for (const dependencyId of stack.dependencies.map(x => x.id)) {
if (dependencyId.includes('.assets')) {
continue;
}
const depStack = assembly.stackById(dependencyId);
if (depStack.stackArtifacts[0].dependencies.length > 0 &&
depStack.stackArtifacts[0].dependencies.filter((dep) => !(dep.id).includes('.assets')).length > 0) {
const stackWithDeps = calculateStackDependencies(depStack);
for (const stackDetail of stackWithDeps) {
data.dependencies.push({
id: stackDetail.id,
dependencies: stackDetail.dependencies,
});
}
} else {
data.dependencies.push({
id: depStack.stackArtifacts[0].displayName ?? depStack.stackArtifacts[0].id,
dependencies: [],
});
}
}
allData.push(data);
}
return allData;
}
return calculateStackDependencies(stacks);
}