forked from ljacobsson/cfn-diagram
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* mermaid support wip * feat: adding support for mermaid output * feat: adding support for mermaid output Co-authored-by: ljacobsson <lars@mathem.se>
- Loading branch information
1 parent
68d047d
commit 523fefb
Showing
7 changed files
with
1,527 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
const Vis = require("../../graph/Vis"); | ||
const program = require("commander"); | ||
const template = require("../../shared/templateParser"); | ||
const fs = require("fs"); | ||
program | ||
.command("mermaid") | ||
.alias("m") | ||
.option( | ||
"-t, --template-file [templateFile]", | ||
"Path to template or cdk.json file", | ||
"template.yaml or cdk.json" | ||
) | ||
.option("-all --render-all", "If set, all nested stacks will be rendered. By default only root template is rendered", false) | ||
.option( | ||
"-o, --output-path [outputPath]", | ||
"Name of output file" | ||
) | ||
.option("-co, --cdk-output [cdkOutputPath]", "CDK synth output path", `cdk.out`) | ||
.option("-s, --skip-synth", "Skips CDK synth", false) | ||
.description("Generates a mermaid graph from a template") | ||
.action(async (cmd) => { | ||
ciMode = cmd.ciMode; | ||
const templateObj = template.get(cmd); | ||
const graph = await Vis.makeGraph( | ||
templateObj.template, | ||
"root", | ||
false, | ||
cmd.renderAll | ||
); | ||
|
||
const groups = {}; | ||
for (const edge of graph.edges) { | ||
const owner = edge.from.split(".")[0]; | ||
|
||
if (edge.to.startsWith(`${owner}.`) && edge.from.startsWith(`${owner}.`)) { | ||
if (!groups[owner]) { | ||
groups[owner] = []; | ||
} | ||
groups[owner].push(edge); | ||
} else { | ||
if (!groups["crossgroup"]) { | ||
groups["crossgroup"] = []; | ||
} | ||
groups["crossgroup"].push(edge); | ||
} | ||
} | ||
const uniqueRelations = []; | ||
let mermaidString = `\`\`\`mermaid\n\tflowchart TB;\n`; | ||
for (const groupKey in groups) { | ||
const group = groups[groupKey]; | ||
if (groupKey !== "crossgroup") { | ||
mermaidString += `\t\tsubgraph ${groupKey !== "root" ? groupKey : " "}\n`; | ||
} | ||
|
||
mermaidString += `${group.map(p => { | ||
const fromResource = graph.nodes.find(n => n.id === p.from); | ||
const toResource = graph.nodes.find(n => n.id === p.to); | ||
const from = createShape(fromResource); | ||
const to = createShape(toResource); | ||
const relation = `\t\t${from}-->${to}`; | ||
if (!uniqueRelations.includes(relation)) { | ||
uniqueRelations.push(relation); | ||
return relation; | ||
} | ||
}).filter(p => p).join("\n")} | ||
` | ||
if (groupKey !== "crossgroup") { | ||
mermaidString += `\tend\n`; | ||
} | ||
|
||
} | ||
|
||
mermaidString += `\n\`\`\``; | ||
if (cmd.outputPath) { | ||
fs.writeFileSync(cmd.outputPath, mermaidString); | ||
console.log(`Wrote Mermaid diagram to ${cmd.outputPath}`); | ||
} else { | ||
console.log(mermaidString) | ||
} | ||
}); | ||
|
||
function createShape(resource, cmd) { | ||
const label = resource.label.replace(/[^a-z0-9\n]/gmi, "").replace(/\s+/g, ""); | ||
const id = resource.id.replace(/[^a-z0-9\n]/gmi, "").replace(/\s+/g, "");; | ||
const type = resource.type.replace("AWS::", ""); | ||
switch (resource.type) { | ||
case "AWS::Serverless::Function": | ||
case "AWS::Lambda::Function": | ||
return `${id}[[${label}<br/>${type}]]`; | ||
case "AWS::Serverless::SimpleTable": | ||
case "AWS::DynamoDB::Table": | ||
case "AWS::RDS::DBInstance": | ||
case "AWS::RDS::DBCluster": | ||
return `${id}[(${label}<br/>${type})]`; | ||
} | ||
return `${id}[${label}<br/>${type}]`; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -227,4 +227,5 @@ async function renderTemplate( | |
module.exports = { | ||
renderTemplate, | ||
reset, | ||
makeGraph | ||
}; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.