Skip to content

Commit

Permalink
feat: adding --watch command to ascii-art diagram (#46)
Browse files Browse the repository at this point in the history
Co-authored-by: ljacobsson <[email protected]>
  • Loading branch information
ljacobsson and ljacobsson authored Oct 11, 2021
1 parent 1d22e04 commit 4f6ef89
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 4 deletions.
27 changes: 26 additions & 1 deletion commands/asciiart/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const mxGenerator = require("../../graph/MxGenerator");
const templateParser = require("../../shared/templateParser");
const filterConfig = require("../../resources/FilterConfig");
const mxGraphToAsciiArt = require("./mxgraph-to-asciiart");
const fs = require("fs");
program
.command("ascii-art")
.alias("a")
Expand All @@ -17,17 +18,41 @@ program
)
.option("-co, --cdk-output [outputPath]", "CDK synth output path", `cdk.out`)
.option("-s, --skip-synth", "Skips CDK synth", false)
.option(
"-w, --watch",
"Watch for changes in template and rerender diagram on change",
false
)
.option(
"-e, --exclude-types [excludeTypes...]",
"List of resource types to exclude when using CI mode"
)
.description("Generates an ascii-art diagram from a CloudFormation template")
.action(async (cmd) => {
const xml = await render(cmd);
if (cmd.watch) {
fs.watchFile(cmd.templateFile, (a, b) => {
render(cmd);
});
}
});
async function render(cmd) {
try {
const template = templateParser.get(cmd).template;

if (!template.Resources) {
console.log("Can't find resources");
return;
}
filterConfig.resourceNamesToInclude = Object.keys(template.Resources);
filterConfig.resourceTypesToInclude = Object.keys(template.Resources).map(
(p) => template.Resources[p].Type
);
mxGenerator.reset();
const xml = await mxGenerator.renderTemplate(template);
mxGraphToAsciiArt.render(xml);
});
return xml;
} catch (err) {
console.log(err.message);
}
}
8 changes: 8 additions & 0 deletions graph/ui/icons.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 62 additions & 3 deletions resources/IconMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ const icons = {
icon: "mxgraph.aws4.api_gateway",
serviceType: "networkingcontentdelivery",
},
"AWS::Serverless::HttpApi": {
icon: "mxgraph.aws4.api_gateway",
serviceType: "networkingcontentdelivery",
},
"AWS::Serverless::StateMachine": {
icon: "mxgraph.aws4.step_functions",
serviceType: "applicationintegration",
},
"AWS::Lambda::Permission": {
icon: "mxgraph.aws4.policy",
serviceType: "securityidentitycomplicance",
Expand Down Expand Up @@ -94,8 +102,59 @@ const icons = {
"AWS::WAFv2::WebACLAssociation": {
icon: "mxgraph.aws4.waf",
serviceType: "securityidentitycomplicance",
}

},
"AWS::ApiGatewayV2::Api": {
icon: "mxgraph.aws4.api_gateway",
serviceType: "networkingcontentdelivery",
},
"AWS::ApiGatewayV2::ApiGatewayManagedOverrides": {
icon: "mxgraph.aws4.api_gateway",
serviceType: "networkingcontentdelivery",
},
"AWS::ApiGatewayV2::ApiMapping": {
icon: "mxgraph.aws4.api_gateway",
serviceType: "networkingcontentdelivery",
},
"AWS::ApiGatewayV2::Authorizer": {
icon: "mxgraph.aws4.api_gateway",
serviceType: "networkingcontentdelivery",
},
"AWS::ApiGatewayV2::Deployment": {
icon: "mxgraph.aws4.api_gateway",
serviceType: "networkingcontentdelivery",
},
"AWS::ApiGatewayV2::DomainName": {
icon: "mxgraph.aws4.api_gateway",
serviceType: "networkingcontentdelivery",
},
"AWS::ApiGatewayV2::Integration": {
icon: "mxgraph.aws4.api_gateway",
serviceType: "networkingcontentdelivery",
},
"AWS::ApiGatewayV2::IntegrationResponse": {
icon: "mxgraph.aws4.api_gateway",
serviceType: "networkingcontentdelivery",
},
"AWS::ApiGatewayV2::Model": {
icon: "mxgraph.aws4.api_gateway",
serviceType: "networkingcontentdelivery",
},
"AWS::ApiGatewayV2::Route": {
icon: "mxgraph.aws4.api_gateway",
serviceType: "networkingcontentdelivery",
},
"AWS::ApiGatewayV2::RouteResponse": {
icon: "mxgraph.aws4.api_gateway",
serviceType: "networkingcontentdelivery",
},
"AWS::ApiGatewayV2::Stage": {
icon: "mxgraph.aws4.api_gateway",
serviceType: "networkingcontentdelivery",
},
"AWS::ApiGatewayV2::VpcLink": {
icon: "mxgraph.aws4.api_gateway",
serviceType: "networkingcontentdelivery",
},
};

const colors = {
Expand Down Expand Up @@ -244,7 +303,7 @@ const serviceTranslation = {
kms: "key_management_service",
stepfunctions: "step_functions",
events: "eventbridge",
logs: "cloudwatch"
logs: "cloudwatch",
};

function getIcon(type) {
Expand Down
1 change: 1 addition & 0 deletions shared/templateParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function get(cmd) {
template = fromCDK(cmd);
}
templateCache.templates["root"] = template;
if (!template.Resources) return { isJson, template };
Object.keys(template.Resources)
.filter((p) => template.Resources[p].Type === "AWS::CloudFormation::Stack")
.map((p) => {
Expand Down

0 comments on commit 4f6ef89

Please sign in to comment.