Skip to content

Commit

Permalink
rolling back pageres changes in favour of a separate cli for CI-mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ljacobsson committed Oct 24, 2020
1 parent bb71529 commit 3442ecd
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 2,207 deletions.
14 changes: 4 additions & 10 deletions graph/Vis.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const path = require("path");
const open = require("open");
const ColorHash = require("color-hash");
const { yamlParse, yamlDump } = require("yaml-cfn");
const Pageres = require('pageres');

const colorHash = new ColorHash();
let nodes = [];
Expand Down Expand Up @@ -158,7 +157,7 @@ function pathToDescriptor(path) {
return path.split(".").slice(-1)[0];
}

async function renderTemplate(template, isJson, filePath, outputType, imagePath) {
async function renderTemplate(template, isJson, filePath, ciMode) {
useJson = isJson;
const { nodes, edges } = makeGraph(template, "root");
const fileContent = `
Expand All @@ -167,7 +166,7 @@ async function renderTemplate(template, isJson, filePath, outputType, imagePath)
var edges = new vis.DataSet(${JSON.stringify(edges)});
var nested = ${JSON.stringify(nested.sort())};
var types = ${JSON.stringify(Array.from(types).sort())};
var showSidebar = ${outputType === "html"};
var showSidebar = ${!ciMode};
`;
const uiPath = filePath || path.join(tempDirectory, "cfn-diagram");
if (!fs.existsSync(uiPath)) {
Expand All @@ -182,14 +181,9 @@ async function renderTemplate(template, isJson, filePath, outputType, imagePath)
path.join(uiPath, "icons.js")
);
fs.writeFileSync(path.join(uiPath, "data.js"), fileContent);
if (outputType === "html") {
if (!ciMode) {
open(path.join(uiPath, "index.html"));
} else {
await new Pageres({filename: "cfn-diagram"})
.src(`file://${path.join(uiPath, "index.html")}`, ['1024x768'], {crop: true})
.dest(imagePath)
.run()
}
}
}

module.exports = {
Expand Down
46 changes: 25 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const actionOption = {
EdgeLabels: "Edge labels: On",
};

let ciMode = false;

program.version(package.version, "-v, --vers", "output the current version");

program
Expand All @@ -29,9 +31,16 @@ program
"Path to template",
"template.yaml"
)
.option(
"-c, --ci-mode",
"Disable interactivity",
false
)
.option("-o, --output-file [outputFile]", "Output file", "template.drawio")
.description("Generates a draw.io diagram from a CloudFormation template")
.action(async (cmd) => {
ciMode = cmd.ciMode;

await generate(cmd);
});
program
Expand All @@ -43,44 +52,34 @@ program
"template.yaml"
)
.option(
"-o, --output-path [outputPath]",
"Output file",
`${path.join(tempDirectory, "cfn-diagram")}`
)
.description("Generates a vis.js diagram from a CloudFormation template")
.action(async (cmd) => {
const template = getTemplate(cmd);
await Vis.renderTemplate(template.template, template.isJson, cmd.outputPath, "html");
});
program
.command("image")
.alias("i")
.option(
"-t, --template-file [templateFile]",
"Path to template",
"template.yaml"
"-c, --ci-mode",
"Disable interactivity",
false
)
.option(
"-o, --output-path [outputPath]",
"Output path",
`./`
"Output file",
`${path.join(tempDirectory, "cfn-diagram")}`
)
.description("Generates a vis.js diagram from a CloudFormation template")
.action(async (cmd) => {
ciMode = cmd.ciMode;
const template = getTemplate(cmd);
await Vis.renderTemplate(template.template, template.isJson, `${path.join(tempDirectory, "cfn-diagram")}`, "image", cmd.outputPath);
await Vis.renderTemplate(template.template, template.isJson, cmd.outputPath, ciMode);
});
program

program
.command("generate")
.alias("g")
.option(
"-t, --template-file [templateFile]",
"Path to template",
"template.yaml"
)
)
.option("-o, --output-file [outputFile]", "Output file", "template.drawio")
.description("[Deprected] Same as draw.io. Kept for backward compatability")
.action(async (cmd) => {
console.log("Command is deprecated and will be removed. Please use `cfn-dia draw.io` instead.")
await generate(cmd);
});

Expand All @@ -98,6 +97,11 @@ async function generate(cmd) {
let edgeMode = { answer: "On" };
let actionChoice = {};
console.log("Writing diagram to ./template.drawio");

if (ciMode) {
return;
}

console.log("Press CTRL+C to exit");
while (true) {
filterConfig.resourceNamesToInclude = resourceNames.answer;
Expand Down
Loading

0 comments on commit 3442ecd

Please sign in to comment.