|
| 1 | +import * as Commonmark from "commonmark"; |
| 2 | +import * as fs from "fs"; |
| 3 | +import * as path from "path"; |
| 4 | +import { Node, Edge, Graph, Link, LinkType, NodeShape, ArrowType } from "./types"; |
| 5 | +import { FileLink } from "./types"; |
| 6 | + |
| 7 | +const graph = getGQMFileLinks(); |
| 8 | + |
| 9 | +export function getGQMFileLinks() { |
| 10 | + const graph: Graph = { |
| 11 | + nodes: [], |
| 12 | + edges: [], |
| 13 | + }; |
| 14 | + |
| 15 | + const goalsPath = "../../measuring/goals/"; |
| 16 | + const questionsPath = "../../measuring/questions/"; |
| 17 | + const metricsPath = "../../measuring/metrics/"; |
| 18 | + |
| 19 | + const goalFileLinks: FileLink[] = getFileLinks(LinkType.GOAL, goalsPath); |
| 20 | + appendToGraph(graph, goalFileLinks); |
| 21 | + |
| 22 | + const questionFileLinks: FileLink[] = getFileLinks( |
| 23 | + LinkType.QUESTION, |
| 24 | + questionsPath |
| 25 | + ); |
| 26 | + appendToGraph(graph, questionFileLinks); |
| 27 | + |
| 28 | + const metricFileLinks: FileLink[] = getFileLinks(LinkType.METRIC, metricsPath); |
| 29 | + appendToGraph(graph, metricFileLinks); |
| 30 | + |
| 31 | + return graph; |
| 32 | +} |
| 33 | + |
| 34 | +export function appendToGraph(graph: Graph, goalFileLinks: FileLink[]) { |
| 35 | + goalFileLinks.forEach((fileLink) => { |
| 36 | + const node: Node = { |
| 37 | + id: fileLink.file, |
| 38 | + shape: NodeShape.RECT, |
| 39 | + label: fileLink.label, |
| 40 | + }; |
| 41 | + if (node.label) { |
| 42 | + graph.nodes.push(node); |
| 43 | + } |
| 44 | + |
| 45 | + fileLink.links.forEach((link) => { |
| 46 | + const edge: Edge = { |
| 47 | + from: fileLink.file, |
| 48 | + to: link.name, |
| 49 | + arrowType: "arrow", |
| 50 | + }; |
| 51 | + if(edge.from && edge.to){ |
| 52 | + graph.edges.push(edge); |
| 53 | + } |
| 54 | + }); |
| 55 | + }); |
| 56 | + return graph |
| 57 | +} |
| 58 | + |
| 59 | +export function getFileLinks(linkType: LinkType, filePath: string) { |
| 60 | + const parser = new Commonmark.Parser(); |
| 61 | + |
| 62 | + const goalFiles = fs.readdirSync(filePath); |
| 63 | + let fileLinks: FileLink[] = []; |
| 64 | + goalFiles.forEach((fileName) => { |
| 65 | + if (fileName.endsWith("template.md")) return; |
| 66 | + const data = fs.readFileSync(`${filePath}/${fileName}`, "utf-8"); |
| 67 | + const parsed = parser.parse(data); |
| 68 | + const label = getHeading(parsed); |
| 69 | + const links = getLinks(parsed); |
| 70 | + |
| 71 | + const fileLink: FileLink = { |
| 72 | + linkType: linkType, |
| 73 | + file: fileName, |
| 74 | + label: label, |
| 75 | + links, |
| 76 | + }; |
| 77 | + fileLinks.push(fileLink); |
| 78 | + }); |
| 79 | + return fileLinks; |
| 80 | +} |
| 81 | + |
| 82 | +export function getHeading(parsed: Commonmark.Node) { |
| 83 | + const walker = parsed.walker(); |
| 84 | + let event, node; |
| 85 | + let heading: string = "No Heading"; |
| 86 | + while ((event = walker.next())) { |
| 87 | + node = event.node; |
| 88 | + if (event.entering && node.type === "heading") { |
| 89 | + heading = node.lastChild?.literal as string; |
| 90 | + break; |
| 91 | + } |
| 92 | + } |
| 93 | + return heading.trim(); |
| 94 | +} |
| 95 | + |
| 96 | +export function getLinks(parsed: Commonmark.Node) { |
| 97 | + const walker = parsed.walker(); |
| 98 | + let event, node; |
| 99 | + const links: Link[] = []; |
| 100 | + while ((event = walker.next())) { |
| 101 | + node = event.node; |
| 102 | + if (event.entering && node.type === "link") { |
| 103 | + const destination = node.destination as string; |
| 104 | + const text = node.firstChild?.literal as string; |
| 105 | + const link: Link = { |
| 106 | + url: destination, |
| 107 | + text: text, |
| 108 | + name: path.parse(destination).base, |
| 109 | + }; |
| 110 | + |
| 111 | + if (link.url.indexOf('.md') > -1 && link.url.indexOf('use_gqm') === -1) { |
| 112 | + links.push(link); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + return links; |
| 117 | +} |
| 118 | + |
| 119 | +export function getNodeShapeSyntax(node: Node) { |
| 120 | + switch (node.shape) { |
| 121 | + case 'rect': |
| 122 | + return `[${node.label}]`; |
| 123 | + case 'circ': |
| 124 | + return `((${node.label}))`; |
| 125 | + case 'roundrect': |
| 126 | + return `((${node.label}))`; |
| 127 | + case 'diamond': |
| 128 | + return `{${node.label}}`; |
| 129 | + default: |
| 130 | + return `[${node.label}]`; |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +export function generateMermaidDiagram(graph: Graph) { |
| 135 | + const nodes = graph.nodes; |
| 136 | + const edges = graph.edges; |
| 137 | + |
| 138 | + let mermaidSyntax = "```mermaid\ngraph TB\n"; |
| 139 | + |
| 140 | + nodes.forEach((node) => { |
| 141 | + const nodeSyntax = getNodeShapeSyntax(node); |
| 142 | + mermaidSyntax += `${node.id}${nodeSyntax}\n`; |
| 143 | + }); |
| 144 | + |
| 145 | + edges.forEach((edge) => { |
| 146 | + const arrowSyntax: string = ArrowType.ARROW; |
| 147 | + mermaidSyntax += `${edge.from}${arrowSyntax}${edge.to}\n`; |
| 148 | + }); |
| 149 | + mermaidSyntax += "\n```"; |
| 150 | + return mermaidSyntax; |
| 151 | +} |
| 152 | + |
| 153 | +console.log(generateMermaidDiagram(graph)) |
0 commit comments