Skip to content

Commit 4f6ef89

Browse files
ljacobssonljacobsson
andauthored
feat: adding --watch command to ascii-art diagram (#46)
Co-authored-by: ljacobsson <lars@mathem.se>
1 parent 1d22e04 commit 4f6ef89

File tree

4 files changed

+97
-4
lines changed

4 files changed

+97
-4
lines changed

commands/asciiart/index.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const mxGenerator = require("../../graph/MxGenerator");
33
const templateParser = require("../../shared/templateParser");
44
const filterConfig = require("../../resources/FilterConfig");
55
const mxGraphToAsciiArt = require("./mxgraph-to-asciiart");
6+
const fs = require("fs");
67
program
78
.command("ascii-art")
89
.alias("a")
@@ -17,17 +18,41 @@ program
1718
)
1819
.option("-co, --cdk-output [outputPath]", "CDK synth output path", `cdk.out`)
1920
.option("-s, --skip-synth", "Skips CDK synth", false)
21+
.option(
22+
"-w, --watch",
23+
"Watch for changes in template and rerender diagram on change",
24+
false
25+
)
2026
.option(
2127
"-e, --exclude-types [excludeTypes...]",
2228
"List of resource types to exclude when using CI mode"
2329
)
2430
.description("Generates an ascii-art diagram from a CloudFormation template")
2531
.action(async (cmd) => {
32+
const xml = await render(cmd);
33+
if (cmd.watch) {
34+
fs.watchFile(cmd.templateFile, (a, b) => {
35+
render(cmd);
36+
});
37+
}
38+
});
39+
async function render(cmd) {
40+
try {
2641
const template = templateParser.get(cmd).template;
42+
43+
if (!template.Resources) {
44+
console.log("Can't find resources");
45+
return;
46+
}
2747
filterConfig.resourceNamesToInclude = Object.keys(template.Resources);
2848
filterConfig.resourceTypesToInclude = Object.keys(template.Resources).map(
2949
(p) => template.Resources[p].Type
3050
);
51+
mxGenerator.reset();
3152
const xml = await mxGenerator.renderTemplate(template);
3253
mxGraphToAsciiArt.render(xml);
33-
});
54+
return xml;
55+
} catch (err) {
56+
console.log(err.message);
57+
}
58+
}

graph/ui/icons.js

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/IconMap.js

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ const icons = {
1111
icon: "mxgraph.aws4.api_gateway",
1212
serviceType: "networkingcontentdelivery",
1313
},
14+
"AWS::Serverless::HttpApi": {
15+
icon: "mxgraph.aws4.api_gateway",
16+
serviceType: "networkingcontentdelivery",
17+
},
18+
"AWS::Serverless::StateMachine": {
19+
icon: "mxgraph.aws4.step_functions",
20+
serviceType: "applicationintegration",
21+
},
1422
"AWS::Lambda::Permission": {
1523
icon: "mxgraph.aws4.policy",
1624
serviceType: "securityidentitycomplicance",
@@ -94,8 +102,59 @@ const icons = {
94102
"AWS::WAFv2::WebACLAssociation": {
95103
icon: "mxgraph.aws4.waf",
96104
serviceType: "securityidentitycomplicance",
97-
}
98-
105+
},
106+
"AWS::ApiGatewayV2::Api": {
107+
icon: "mxgraph.aws4.api_gateway",
108+
serviceType: "networkingcontentdelivery",
109+
},
110+
"AWS::ApiGatewayV2::ApiGatewayManagedOverrides": {
111+
icon: "mxgraph.aws4.api_gateway",
112+
serviceType: "networkingcontentdelivery",
113+
},
114+
"AWS::ApiGatewayV2::ApiMapping": {
115+
icon: "mxgraph.aws4.api_gateway",
116+
serviceType: "networkingcontentdelivery",
117+
},
118+
"AWS::ApiGatewayV2::Authorizer": {
119+
icon: "mxgraph.aws4.api_gateway",
120+
serviceType: "networkingcontentdelivery",
121+
},
122+
"AWS::ApiGatewayV2::Deployment": {
123+
icon: "mxgraph.aws4.api_gateway",
124+
serviceType: "networkingcontentdelivery",
125+
},
126+
"AWS::ApiGatewayV2::DomainName": {
127+
icon: "mxgraph.aws4.api_gateway",
128+
serviceType: "networkingcontentdelivery",
129+
},
130+
"AWS::ApiGatewayV2::Integration": {
131+
icon: "mxgraph.aws4.api_gateway",
132+
serviceType: "networkingcontentdelivery",
133+
},
134+
"AWS::ApiGatewayV2::IntegrationResponse": {
135+
icon: "mxgraph.aws4.api_gateway",
136+
serviceType: "networkingcontentdelivery",
137+
},
138+
"AWS::ApiGatewayV2::Model": {
139+
icon: "mxgraph.aws4.api_gateway",
140+
serviceType: "networkingcontentdelivery",
141+
},
142+
"AWS::ApiGatewayV2::Route": {
143+
icon: "mxgraph.aws4.api_gateway",
144+
serviceType: "networkingcontentdelivery",
145+
},
146+
"AWS::ApiGatewayV2::RouteResponse": {
147+
icon: "mxgraph.aws4.api_gateway",
148+
serviceType: "networkingcontentdelivery",
149+
},
150+
"AWS::ApiGatewayV2::Stage": {
151+
icon: "mxgraph.aws4.api_gateway",
152+
serviceType: "networkingcontentdelivery",
153+
},
154+
"AWS::ApiGatewayV2::VpcLink": {
155+
icon: "mxgraph.aws4.api_gateway",
156+
serviceType: "networkingcontentdelivery",
157+
},
99158
};
100159

101160
const colors = {
@@ -244,7 +303,7 @@ const serviceTranslation = {
244303
kms: "key_management_service",
245304
stepfunctions: "step_functions",
246305
events: "eventbridge",
247-
logs: "cloudwatch"
306+
logs: "cloudwatch",
248307
};
249308

250309
function getIcon(type) {

shared/templateParser.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ function get(cmd) {
3131
template = fromCDK(cmd);
3232
}
3333
templateCache.templates["root"] = template;
34+
if (!template.Resources) return { isJson, template };
3435
Object.keys(template.Resources)
3536
.filter((p) => template.Resources[p].Type === "AWS::CloudFormation::Stack")
3637
.map((p) => {

0 commit comments

Comments
 (0)