|
| 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 getLinkUrl(linkType: LinkType, file: string) { |
| 10 | + const measuringUrl = "https://github.com/InnerSourceCommons/managing-inner-source-projects/blob/main/measuring/"; |
| 11 | + const url = `${measuringUrl}/${linkType.toLowerCase()}s/${file}` |
| 12 | + return url; |
| 13 | +} |
| 14 | + |
| 15 | +export function getGQMFileLinks() { |
| 16 | + const graph: Graph = { |
| 17 | + nodes: [], |
| 18 | + edges: [], |
| 19 | + }; |
| 20 | + |
| 21 | + const goalsPath = "../../measuring/goals/"; |
| 22 | + const questionsPath = "../../measuring/questions/"; |
| 23 | + const metricsPath = "../../measuring/metrics/"; |
| 24 | + |
| 25 | + const goalFileLinks: FileLink[] = getFileLinks(LinkType.GOAL, goalsPath); |
| 26 | + appendToGraph(graph, goalFileLinks); |
| 27 | + |
| 28 | + const questionFileLinks: FileLink[] = getFileLinks( |
| 29 | + LinkType.QUESTION, |
| 30 | + questionsPath |
| 31 | + ); |
| 32 | + appendToGraph(graph, questionFileLinks); |
| 33 | + |
| 34 | + const metricFileLinks: FileLink[] = getFileLinks(LinkType.METRIC, metricsPath); |
| 35 | + appendToGraph(graph, metricFileLinks); |
| 36 | + |
| 37 | + return graph; |
| 38 | +} |
| 39 | + |
| 40 | +export function appendToGraph(graph: Graph, fileLinks: FileLink[]) { |
| 41 | + fileLinks.forEach((fileLink) => { |
| 42 | + const node: Node = { |
| 43 | + id: fileLink.file, |
| 44 | + type: fileLink.linkType, |
| 45 | + shape: NodeShape.RECT, |
| 46 | + label: fileLink.label, |
| 47 | + }; |
| 48 | + if (node.label) { |
| 49 | + graph.nodes.push(node); |
| 50 | + } |
| 51 | + |
| 52 | + fileLink.links.forEach((link) => { |
| 53 | + const edge: Edge = { |
| 54 | + from: fileLink.file, |
| 55 | + to: link.name, |
| 56 | + arrowType: "arrow", |
| 57 | + }; |
| 58 | + if(edge.from && edge.to){ |
| 59 | + graph.edges.push(edge); |
| 60 | + } |
| 61 | + }); |
| 62 | + }); |
| 63 | + return graph |
| 64 | +} |
| 65 | + |
| 66 | +export function getFileLinks(linkType: LinkType, filePath: string) { |
| 67 | + const parser = new Commonmark.Parser(); |
| 68 | + |
| 69 | + const goalFiles = fs.readdirSync(filePath); |
| 70 | + let fileLinks: FileLink[] = []; |
| 71 | + goalFiles.forEach((fileName) => { |
| 72 | + if (fileName.endsWith("template.md")) return; |
| 73 | + const data = fs.readFileSync(`${filePath}/${fileName}`, "utf-8"); |
| 74 | + const parsed = parser.parse(data); |
| 75 | + const label = getHeading(parsed); |
| 76 | + const links = getLinks(parsed); |
| 77 | + |
| 78 | + const fileLink: FileLink = { |
| 79 | + linkType: linkType, |
| 80 | + file: fileName, |
| 81 | + label: label, |
| 82 | + links, |
| 83 | + }; |
| 84 | + fileLinks.push(fileLink); |
| 85 | + }); |
| 86 | + return fileLinks; |
| 87 | +} |
| 88 | + |
| 89 | +export function getHeading(parsed: Commonmark.Node) { |
| 90 | + const walker = parsed.walker(); |
| 91 | + let event, node; |
| 92 | + let heading: string = "No Heading"; |
| 93 | + while ((event = walker.next())) { |
| 94 | + node = event.node; |
| 95 | + if (event.entering && node.type === "heading") { |
| 96 | + heading = node.lastChild?.literal as string; |
| 97 | + break; |
| 98 | + } |
| 99 | + } |
| 100 | + return heading.trim(); |
| 101 | +} |
| 102 | + |
| 103 | +export function getLinks(parsed: Commonmark.Node) { |
| 104 | + const walker = parsed.walker(); |
| 105 | + let event, node; |
| 106 | + const links: Link[] = []; |
| 107 | + while ((event = walker.next())) { |
| 108 | + node = event.node; |
| 109 | + if (event.entering && node.type === "link") { |
| 110 | + const destination = node.destination as string; |
| 111 | + const text = node.firstChild?.literal as string; |
| 112 | + const link: Link = { |
| 113 | + url: destination, |
| 114 | + text: text, |
| 115 | + name: path.parse(destination).base, |
| 116 | + }; |
| 117 | + |
| 118 | + if (link.url.indexOf('.md') > -1 && link.url.indexOf('use_gqm') === -1) { |
| 119 | + links.push(link); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + return links; |
| 124 | +} |
| 125 | + |
| 126 | +export function getNodeShapeSyntax(node: Node) { |
| 127 | + const nodeUrl = getLinkUrl(node.type, node.id) |
| 128 | + const nodeLabel = `<a href='${nodeUrl}'>${node.label}</a>`; |
| 129 | + switch (node.shape) { |
| 130 | + case 'rect': |
| 131 | + return `[${nodeLabel}]`; |
| 132 | + case 'circ': |
| 133 | + return `((${nodeLabel}))`; |
| 134 | + case 'roundrect': |
| 135 | + return `((${nodeLabel}))`; |
| 136 | + case 'diamond': |
| 137 | + return `{${nodeLabel}}`; |
| 138 | + default: |
| 139 | + return `[${nodeLabel}]`; |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +export function generateMermaidDiagram(graph: Graph) { |
| 144 | + const nodes = graph.nodes; |
| 145 | + const edges = graph.edges; |
| 146 | + |
| 147 | + let mermaidSyntax = `\`\`\`mermaid\n |
| 148 | + graph LR;\n |
| 149 | + subgraph GQM[Goals, Questions, Metrics]\n |
| 150 | + `; |
| 151 | + |
| 152 | + nodes.forEach((node) => { |
| 153 | + const nodeSyntax = getNodeShapeSyntax(node) |
| 154 | + mermaidSyntax += ` ${node.id}${nodeSyntax}\n` |
| 155 | + }); |
| 156 | + |
| 157 | + edges.forEach((edge) => { |
| 158 | + const arrowSyntax: string = ArrowType.ARROW; |
| 159 | + mermaidSyntax += `${edge.from}${arrowSyntax}${edge.to}\n`; |
| 160 | + }); |
| 161 | + |
| 162 | + const goalsList = nodes.filter(n => n.type == LinkType.GOAL).map(n => `${n.id}`).join(','); |
| 163 | + const questionsList = nodes.filter(n => n.type == LinkType.QUESTION).map(n => `${n.id}`).join(','); |
| 164 | + const metricsList = nodes.filter(n => n.type == LinkType.METRIC).map(n => `${n.id}`).join(','); |
| 165 | + |
| 166 | + mermaidSyntax += " end"; |
| 167 | + mermaidSyntax += ` |
| 168 | + subgraph Legend |
| 169 | + direction TB |
| 170 | +
|
| 171 | + goal[Goal] |
| 172 | + question[Question] |
| 173 | + metric[Metric] |
| 174 | +
|
| 175 | + classDef goals stroke:green,stroke-width:2px; |
| 176 | + class goal,${goalsList} goals |
| 177 | +
|
| 178 | + classDef questions stroke:orange,stroke-width:2px; |
| 179 | + class question,${questionsList} questions |
| 180 | +
|
| 181 | + classDef metrics stroke:purple,stroke-width:2px; |
| 182 | + class metric,${metricsList} metrics |
| 183 | + end |
| 184 | + `; |
| 185 | + mermaidSyntax += "\n```"; |
| 186 | + return mermaidSyntax; |
| 187 | +} |
| 188 | + |
| 189 | +console.log(generateMermaidDiagram(graph)) |
0 commit comments