|
| 1 | +import type { Graph, Node, Relation } from '../graph/models'; |
| 2 | +import type { OptionValues } from '../../setting/model'; |
| 3 | +import type { DirAndNodesTree } from './directoryTreeBuilder'; |
| 4 | +import { fileNameToMermaidId, fileNameToMermaidName } from './mermaidUtils'; |
| 5 | + |
| 6 | +type Options = Omit<OptionValues, 'watchMetrics'> & { |
| 7 | + rootDir: string; |
| 8 | +}; |
| 9 | + |
| 10 | +const indent = ' '; |
| 11 | +const CLASSNAME_DIR = 'dir'; |
| 12 | +const CLASSNAME_HIGHLIGHT = 'highlight'; |
| 13 | +const CLASSNAME_CREATED = 'created'; |
| 14 | +const CLASSNAME_MODIFIED = 'modified'; |
| 15 | +const CLASSNAME_DELETED = 'deleted'; |
| 16 | + |
| 17 | +export function writeFlowchartDirection( |
| 18 | + write: (arg: string) => void, |
| 19 | + options: Pick<Options, 'LR' | 'TB'>, |
| 20 | +) { |
| 21 | + if (options.LR) { |
| 22 | + write(`flowchart LR\n`); |
| 23 | + } else if (options.TB) { |
| 24 | + write(`flowchart TB\n`); |
| 25 | + } else { |
| 26 | + write(`flowchart\n`); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +export function writeClassDefinitions( |
| 31 | + write: (arg: string) => void, |
| 32 | + graph: Graph, |
| 33 | + options: Pick<Options, 'abstraction' | 'highlight'>, |
| 34 | +) { |
| 35 | + if (options.abstraction) { |
| 36 | + write(`${indent}classDef ${CLASSNAME_DIR} fill:#0000,stroke:#999\n`); |
| 37 | + } |
| 38 | + |
| 39 | + if (options.highlight) { |
| 40 | + write(`${indent}classDef ${CLASSNAME_HIGHLIGHT} fill:yellow,color:black\n`); |
| 41 | + } |
| 42 | + |
| 43 | + const changeStatusClassDefs = [ |
| 44 | + { |
| 45 | + status: 'created' as const, |
| 46 | + className: CLASSNAME_CREATED, |
| 47 | + style: 'fill:cyan,stroke:#999,color:black', |
| 48 | + }, |
| 49 | + { |
| 50 | + status: 'modified' as const, |
| 51 | + className: CLASSNAME_MODIFIED, |
| 52 | + style: 'fill:yellow,stroke:#999,color:black', |
| 53 | + }, |
| 54 | + { |
| 55 | + status: 'deleted' as const, |
| 56 | + className: CLASSNAME_DELETED, |
| 57 | + style: |
| 58 | + 'fill:dimgray,stroke:#999,color:black,stroke-dasharray: 4 4,stroke-width:2px;', |
| 59 | + }, |
| 60 | + ]; |
| 61 | + |
| 62 | + changeStatusClassDefs.forEach(({ status, className, style }) => { |
| 63 | + if (graph.nodes.some(node => node.changeStatus === status)) { |
| 64 | + write(`${indent}classDef ${className} ${style}\n`); |
| 65 | + } |
| 66 | + }); |
| 67 | +} |
| 68 | + |
| 69 | +export function writeRelations(write: (arg: string) => void, graph: Graph) { |
| 70 | + graph.relations |
| 71 | + .map(relation => ({ |
| 72 | + ...relation, |
| 73 | + from: { |
| 74 | + ...relation.from, |
| 75 | + mermaidId: fileNameToMermaidId(relation.from.path), |
| 76 | + }, |
| 77 | + to: { |
| 78 | + ...relation.to, |
| 79 | + mermaidId: fileNameToMermaidId(relation.to.path), |
| 80 | + }, |
| 81 | + })) |
| 82 | + .forEach(relation => { |
| 83 | + const connectionStyle = getConnectionStyle(relation); |
| 84 | + write( |
| 85 | + ` ${relation.from.mermaidId}${connectionStyle}${relation.to.mermaidId}`, |
| 86 | + ); |
| 87 | + write('\n'); |
| 88 | + }); |
| 89 | +} |
| 90 | + |
| 91 | +function getConnectionStyle(relation: Relation): string { |
| 92 | + if (relation.kind === 'rename_to') { |
| 93 | + return `-.->|"rename to"|`; |
| 94 | + } else if (relation.changeStatus === 'deleted') { |
| 95 | + return `-.->`; |
| 96 | + } else { |
| 97 | + return `-->`; |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +export function writeFileNodesWithSubgraph( |
| 102 | + write: (arg: string) => void, |
| 103 | + trees: DirAndNodesTree[], |
| 104 | +) { |
| 105 | + trees.forEach(tree => addGraph(write, tree)); |
| 106 | +} |
| 107 | + |
| 108 | +function addGraph( |
| 109 | + write: (arg: string) => void, |
| 110 | + tree: DirAndNodesTree, |
| 111 | + indentNumber = 0, |
| 112 | + parent?: string, |
| 113 | +) { |
| 114 | + const currentIndent = indent.repeat(indentNumber + 1); |
| 115 | + const displayName = parent |
| 116 | + ? tree.currentDir.replace(parent, '') |
| 117 | + : tree.currentDir; |
| 118 | + |
| 119 | + writeSubgraphStart(write, currentIndent, tree.currentDir, displayName); |
| 120 | + writeNodes(write, currentIndent, tree.nodes); |
| 121 | + writeChildGraphs(write, tree, indentNumber); |
| 122 | + writeSubgraphEnd(write, currentIndent); |
| 123 | +} |
| 124 | + |
| 125 | +function writeSubgraphStart( |
| 126 | + write: (arg: string) => void, |
| 127 | + currentIndent: string, |
| 128 | + currentDir: string, |
| 129 | + displayName: string, |
| 130 | +) { |
| 131 | + write( |
| 132 | + `${currentIndent}subgraph ${fileNameToMermaidId(currentDir)}["${fileNameToMermaidName(displayName)}"]\n`, |
| 133 | + ); |
| 134 | +} |
| 135 | + |
| 136 | +function writeNodes( |
| 137 | + write: (arg: string) => void, |
| 138 | + currentIndent: string, |
| 139 | + nodes: Node[], |
| 140 | +) { |
| 141 | + nodes |
| 142 | + .map(node => ({ ...node, mermaidId: fileNameToMermaidId(node.path) })) |
| 143 | + .forEach(node => { |
| 144 | + const classString = getNodeClassString(node); |
| 145 | + write( |
| 146 | + `${currentIndent}${indent}${node.mermaidId}["${fileNameToMermaidName(node.name)}"]${classString}\n`, |
| 147 | + ); |
| 148 | + }); |
| 149 | +} |
| 150 | + |
| 151 | +function getNodeClassString(node: Node & { mermaidId: string }): string { |
| 152 | + if (node.highlight) { |
| 153 | + return `:::${CLASSNAME_HIGHLIGHT}`; |
| 154 | + } |
| 155 | + |
| 156 | + if (node.isDirectory) { |
| 157 | + return `:::${CLASSNAME_DIR}`; |
| 158 | + } |
| 159 | + |
| 160 | + const statusClassMap = { |
| 161 | + created: CLASSNAME_CREATED, |
| 162 | + modified: CLASSNAME_MODIFIED, |
| 163 | + deleted: CLASSNAME_DELETED, |
| 164 | + not_modified: undefined, |
| 165 | + } as const; |
| 166 | + |
| 167 | + const className = statusClassMap[node.changeStatus]; |
| 168 | + return className ? `:::${className}` : ''; |
| 169 | +} |
| 170 | + |
| 171 | +function writeChildGraphs( |
| 172 | + write: (arg: string) => void, |
| 173 | + tree: DirAndNodesTree, |
| 174 | + indentNumber: number, |
| 175 | +) { |
| 176 | + tree.children.forEach(child => |
| 177 | + addGraph(write, child, indentNumber + 1, tree.currentDir), |
| 178 | + ); |
| 179 | +} |
| 180 | + |
| 181 | +function writeSubgraphEnd(write: (arg: string) => void, currentIndent: string) { |
| 182 | + write(`${currentIndent}end\n`); |
| 183 | +} |
0 commit comments