Skip to content

Instantly share code, notes, and snippets.

@jkpl
Created January 15, 2024 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkpl/d02d2a2c3bf935590e976aa24dc1c34d to your computer and use it in GitHub Desktop.
Save jkpl/d02d2a2c3bf935590e976aa24dc1c34d to your computer and use it in GitHub Desktop.
CDK stack dependencies to Mermaid
#!/usr/bin/env node
/*
Convert a CDK app manifest file to a stack dependency tree in Mermaid format.
*/
const fs = require('fs');
function dependencyTreeInMermaid(data) {
const names = {};
const links = [];
for (const [key, value] of Object.entries(data.artifacts)) {
if (value.type === 'aws:cloudformation:stack') {
names[key] = value.displayName.split('/').pop();
for (const dependency of value.dependencies) {
if (!dependency.endsWith('.assets')) {
links.push([key, dependency]);
}
}
}
}
const linksStr = links.map(([key, value]) => {
const keyName = names[key];
const valueName = names[value];
return ` ${keyName} --> ${valueName}`;
}).join('\n')
return [
'flowchart TD',
linksStr,
].join('\n')
}
function readJsonFile(fileName) {
const file = fs.readFileSync(fileName, 'utf8');
return JSON.parse(file);
}
function main() {
const jsonFile = readJsonFile(process.argv[2]);
console.log(dependencyTreeInMermaid(jsonFile));
}
if (typeof require !== 'undefined' && require.main === module) {
main();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment