UNPKG

autouml

Version:

Autogenerate UML diagrams using d2

104 lines (103 loc) 3.72 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.compileRelations = void 0; var path = __importStar(require("path")); function normalize(s) { return s .replace(/\./g, "\\.") .replace(/\\(?!\.)/g, ".") .replace(/\/(?!\.)/g, ".") .split(/(?<!\\)\./g) .map(function (x) { return "\"".concat(x, "\""); }) .join("."); } function typeToString(t) { if (t.isPrimitive) { return t.name; } return "".concat(normalize(path.normalize(t.typeLocation.fileName)), ".").concat(t.typeLocation.namespaceNest .map(function (x) { return "\"".concat(x, "\""); }) .join(".")); } function getArrowStyle(t) { var tor = { text: "", // source-arrowhead.label: 1 targetArrowhead: { shape: "arrow" /* Arrowhead.ARROW */, style: { filled: false, }, }, style: { strokeDash: 5, }, }; if (t === 5 /* connType.ASSOCIATES */ || t === 2 /* connType.DEPENDS */) { tor.targetArrowhead.shape = "arrow" /* Arrowhead.ARROW */; } if (t === 0 /* connType.INHERITS */ || t === 1 /* connType.IMPLEMENTS */) { tor.targetArrowhead.shape = "triangle" /* Arrowhead.TRIANGLE */; } if (t === 3 /* connType.AGGREGATES */ || t === 4 /* connType.COMPOSES */) { tor.targetArrowhead.shape = "diamond" /* Arrowhead.DIAMOND */; } // dashed if (t === 1 /* connType.IMPLEMENTS */ || t === 2 /* connType.DEPENDS */) { tor.style.strokeDash = 5; } else { tor.style.strokeDash = 0; } // filled if (t === 4 /* connType.COMPOSES */) { tor.targetArrowhead.style.filled = true; } else { tor.targetArrowhead.style.filled = false; } return tor; } function arrowStyleToString(s) { var lines = ": ".concat(s.text ? "\"".concat(s.text, "\"") : "", "{\n target-arrowhead: {\n shape: ").concat(s.targetArrowhead.shape, "\n style.filled: ").concat(s.targetArrowhead.style.filled, "\n }\n style.stroke-dash: ").concat(s.style.strokeDash, "\n} \n"); return lines; } function drawArrow(relation) { return "".concat(typeToString(relation.src), " -> ").concat(typeToString(relation.dst), " ").concat(arrowStyleToString(getArrowStyle(relation.type))); } function compileRelations(relations) { var ans = []; for (var _i = 0, relations_1 = relations; _i < relations_1.length; _i++) { var r = relations_1[_i]; ans.push(drawArrow(r)); } return ans.join("\n"); } exports.compileRelations = compileRelations;