@webpieces/nx-webpieces-rules 0.3.329 โ 0.3.331
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webpieces/nx-webpieces-rules",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.331",
|
|
4
4
|
"description": "Nx-specific webpieces validation rules and graph tooling. Bundles all @webpieces rule packages with Nx graph validators and an inference plugin.",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "./src/index.js",
|
|
@@ -21,11 +21,11 @@
|
|
|
21
21
|
"README.md"
|
|
22
22
|
],
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@webpieces/ai-hook-rules": "0.3.
|
|
25
|
-
"@webpieces/code-rules": "0.3.
|
|
26
|
-
"@webpieces/eslint-rules": "0.3.
|
|
27
|
-
"@webpieces/pr-gate": "0.3.
|
|
28
|
-
"@webpieces/rules-config": "0.3.
|
|
24
|
+
"@webpieces/ai-hook-rules": "0.3.331",
|
|
25
|
+
"@webpieces/code-rules": "0.3.331",
|
|
26
|
+
"@webpieces/eslint-rules": "0.3.331",
|
|
27
|
+
"@webpieces/pr-gate": "0.3.331",
|
|
28
|
+
"@webpieces/rules-config": "0.3.331",
|
|
29
29
|
"madge": "8.0.0"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
@@ -164,6 +164,7 @@ function generateHTML(dot, title = 'Monorepo Dependency Architecture') {
|
|
|
164
164
|
<body>
|
|
165
165
|
<h1>${title}</h1>
|
|
166
166
|
<p class="hint">๐ก Click any box with a generated DI design to open its <strong>design.html</strong> (what the AI sees inside that project).</p>
|
|
167
|
+
<p class="hint">๐ฆ <strong>Hover any box</strong> to bolden all of its connections (incoming <em>and</em> outgoing) and highlight the boxes on the other end โ the rest of the graph dims so you can trace one box at a glance.</p>
|
|
167
168
|
${legend}
|
|
168
169
|
<div id="graph"></div>
|
|
169
170
|
<script>${script}</script>
|
|
@@ -172,24 +173,37 @@ function generateHTML(dot, title = 'Monorepo Dependency Architecture') {
|
|
|
172
173
|
}
|
|
173
174
|
function generateHTMLStyles() {
|
|
174
175
|
return `
|
|
175
|
-
body {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
}
|
|
185
|
-
.hint {
|
|
186
|
-
text-align: center;
|
|
187
|
-
color: #555;
|
|
188
|
-
margin: 0 0 16px;
|
|
189
|
-
}
|
|
190
|
-
/* viz.js renders node URLs as <a> โ show they are clickable. */
|
|
176
|
+
body { margin: 0; padding: 20px; font-family: Arial, sans-serif; background: #f5f5f5; }
|
|
177
|
+
h1 { text-align: center; color: #333; }
|
|
178
|
+
.hint { text-align: center; color: #555; margin: 0 0 16px; }
|
|
179
|
+
/* viz.js renders node URLs as <a> โ only boxes with a design.html get
|
|
180
|
+
* one, so a:hover is a clickable-only signal. Make it pop clearly: a
|
|
181
|
+
* thicker border PLUS a blue glow lift the box off the page, so it is
|
|
182
|
+
* obvious which boxes you can click into vs. which you cannot. (A plain
|
|
183
|
+
* stroke bump is invisible on server/client boxes, whose resting border
|
|
184
|
+
* is already thick.) */
|
|
191
185
|
#graph a { cursor: pointer; }
|
|
192
|
-
#graph a
|
|
186
|
+
#graph a polygon,
|
|
187
|
+
#graph a ellipse { transition: stroke-width 0.12s ease, filter 0.12s ease; }
|
|
188
|
+
#graph a:hover polygon,
|
|
189
|
+
#graph a:hover ellipse {
|
|
190
|
+
stroke: #1976d2;
|
|
191
|
+
stroke-width: 5;
|
|
192
|
+
filter: drop-shadow(0 0 6px rgba(25, 118, 210, 0.85));
|
|
193
|
+
}
|
|
194
|
+
/* Hover-highlight (wired up in JS after viz.js renders โ see
|
|
195
|
+
* wireHoverHighlight). Hovering a node adds .wp-dim to the <svg> and
|
|
196
|
+
* .wp-focus/.wp-neighbor/.wp-hl to the connected box, its neighbors, and
|
|
197
|
+
* its edges. We ONLY dim: the connected subgraph keeps its exact normal
|
|
198
|
+
* look (full opacity), the rest recedes. The un-dim rules repeat the
|
|
199
|
+
* "svg.wp-dim" ancestor so they out-specify the dim rule (which has an
|
|
200
|
+
* extra type selector) โ else the highlighted subgraph stays dimmed. */
|
|
201
|
+
#graph .node, #graph .edge { transition: opacity 0.12s ease; }
|
|
202
|
+
#graph svg.wp-dim .node,
|
|
203
|
+
#graph svg.wp-dim .edge { opacity: 0.15; }
|
|
204
|
+
#graph svg.wp-dim .node.wp-focus,
|
|
205
|
+
#graph svg.wp-dim .node.wp-neighbor,
|
|
206
|
+
#graph svg.wp-dim .edge.wp-hl { opacity: 1; }
|
|
193
207
|
#graph {
|
|
194
208
|
text-align: center;
|
|
195
209
|
background: white;
|
|
@@ -265,19 +279,75 @@ function generateHTMLLegend() {
|
|
|
265
279
|
</div>
|
|
266
280
|
</div>`;
|
|
267
281
|
}
|
|
282
|
+
/**
|
|
283
|
+
* The page script (injected as a string). After viz.js renders the Graphviz
|
|
284
|
+
* SVG, `wireHoverHighlight` indexes its nodes and edges so hovering a box
|
|
285
|
+
* boldens every connection (incoming AND outgoing) and highlights the boxes on
|
|
286
|
+
* the other end, dimming the rest. viz.js emits a predictable structure:
|
|
287
|
+
* <g class="node"><title>NAME</title> ... </g>
|
|
288
|
+
* <g class="edge"><title>FROM->TO</title> <path/> <polygon/> ... </g>
|
|
289
|
+
* The edge <title> text (decoded by the DOM as "FROM->TO") gives both
|
|
290
|
+
* endpoints, so adjacency is built without touching the DOT.
|
|
291
|
+
*/
|
|
268
292
|
function generateHTMLScript(dot) {
|
|
269
293
|
return `
|
|
270
294
|
const dot = ${JSON.stringify(dot)};
|
|
271
295
|
const viz = new Viz();
|
|
272
|
-
|
|
273
296
|
viz.renderSVGElement(dot)
|
|
274
297
|
.then(element => {
|
|
275
298
|
document.getElementById('graph').appendChild(element);
|
|
299
|
+
wireHoverHighlight(element);
|
|
276
300
|
})
|
|
277
301
|
.catch(err => {
|
|
278
302
|
console.error(err);
|
|
279
303
|
document.getElementById('graph').innerHTML = '<pre>' + err + '</pre>';
|
|
280
304
|
});
|
|
305
|
+
function wireHoverHighlight(svg) {
|
|
306
|
+
const nodeByName = new Map();
|
|
307
|
+
svg.querySelectorAll('g.node').forEach(g => {
|
|
308
|
+
const t = g.querySelector('title');
|
|
309
|
+
if (t) nodeByName.set(t.textContent.trim(), g);
|
|
310
|
+
});
|
|
311
|
+
const edgesOf = new Map();
|
|
312
|
+
const neighborsOf = new Map();
|
|
313
|
+
const ensure = (map, key) => {
|
|
314
|
+
let v = map.get(key);
|
|
315
|
+
if (!v) { v = new Set(); map.set(key, v); }
|
|
316
|
+
return v;
|
|
317
|
+
};
|
|
318
|
+
svg.querySelectorAll('g.edge').forEach(edge => {
|
|
319
|
+
const t = edge.querySelector('title');
|
|
320
|
+
if (!t) return;
|
|
321
|
+
const idx = t.textContent.indexOf('->');
|
|
322
|
+
if (idx < 0) return;
|
|
323
|
+
const from = t.textContent.slice(0, idx).trim();
|
|
324
|
+
const to = t.textContent.slice(idx + 2).trim();
|
|
325
|
+
ensure(edgesOf, from).add(edge);
|
|
326
|
+
ensure(edgesOf, to).add(edge);
|
|
327
|
+
ensure(neighborsOf, from).add(to);
|
|
328
|
+
ensure(neighborsOf, to).add(from);
|
|
329
|
+
});
|
|
330
|
+
const clear = () => {
|
|
331
|
+
svg.classList.remove('wp-dim');
|
|
332
|
+
svg.querySelectorAll('.wp-focus, .wp-neighbor, .wp-hl').forEach(el => {
|
|
333
|
+
el.classList.remove('wp-focus', 'wp-neighbor', 'wp-hl');
|
|
334
|
+
});
|
|
335
|
+
};
|
|
336
|
+
const highlight = (name, focusEl) => {
|
|
337
|
+
clear();
|
|
338
|
+
svg.classList.add('wp-dim');
|
|
339
|
+
focusEl.classList.add('wp-focus');
|
|
340
|
+
(edgesOf.get(name) || []).forEach(e => e.classList.add('wp-hl'));
|
|
341
|
+
(neighborsOf.get(name) || []).forEach(n => {
|
|
342
|
+
const g = nodeByName.get(n);
|
|
343
|
+
if (g) g.classList.add('wp-neighbor');
|
|
344
|
+
});
|
|
345
|
+
};
|
|
346
|
+
nodeByName.forEach((g, name) => {
|
|
347
|
+
g.addEventListener('mouseenter', () => highlight(name, g));
|
|
348
|
+
g.addEventListener('mouseleave', clear);
|
|
349
|
+
});
|
|
350
|
+
}
|
|
281
351
|
`;
|
|
282
352
|
}
|
|
283
353
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"graph-visualizer.js","sourceRoot":"","sources":["../../../../../../packages/tooling/nx-webpieces-rules/src/lib/graph-visualizer.ts"],"names":[],"mappings":";AAAA;;;;;;;;GAQG;;AAqFH,kCA2DC;AAKD,oCAsBC;AAkID,gDAiBC;AAKD,8CAqBC;;AAtVD,+CAAyB;AACzB,mDAA6B;AAC7B,iDAAyC;AAIzC;;;;GAIG;AACH,MAAM,gBAAgB,GAA2B;IAC7C,OAAO,EAAE,SAAS,EAAE,6BAA6B;IACjD,KAAK,EAAE,SAAS,EAAE,2BAA2B;IAC7C,OAAO,EAAE,SAAS,EAAE,wCAAwC;IAC5D,OAAO,EAAE,SAAS,EAAE,iCAAiC;IACrD,IAAI,EAAE,SAAS,EAAE,kCAAkC;CACtD,CAAC;AAEF,MAAM,uBAAuB,GAAG,SAAS,CAAC,CAAC,+BAA+B;AAE1E;;;GAGG;AACH,SAAS,cAAc,CAAC,UAAoB;IACxC,KAAK,MAAM,GAAG,IAAI,UAAU,EAAE,CAAC;QAC3B,MAAM,KAAK,GAAG,gBAAgB,CAAC,GAAG,CAAC,CAAC;QACpC,IAAI,KAAK,KAAK,SAAS;YAAE,OAAO,KAAK,CAAC;IAC1C,CAAC;IACD,OAAO,uBAAuB,CAAC;AACnC,CAAC;AAED;;;;;;;;GAQG;AACH,SAAS,eAAe,CAAC,IAAY;IACjC,IAAI,IAAI,KAAK,QAAQ;QAAE,OAAO,6BAA6B,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CAAC,CAAC;QAC3C,OAAO,IAAI,CAAC;IAChB,CAAC;IAAC,OAAO,GAAY,EAAE,CAAC;QACpB,6BAA6B;QAC7B,KAAK,GAAG,CAAC;QACT,OAAO,KAAK,CAAC;IACjB,CAAC;AACL,CAAC","sourcesContent":["/**\n * Graph Visualizer\n *\n * Generates visual representations of the architecture graph:\n * - DOT format (for Graphviz)\n * - Interactive HTML (using viz.js)\n *\n * Output files go to tmp/webpieces/ for easy viewing without committing.\n */\n\nimport * as fs from 'fs';\nimport * as path from 'path';\nimport { execSync } from 'child_process';\nimport type { EnhancedGraph } from './graph-sorter';\nimport { toError } from '../toError';\n\n/**\n * Framework (libType) colors for visualization โ nodes are filled by the FIRST\n * env in their set that has a color, so it is obvious at a glance which side a\n * project targets. A project's full env set is shown in the label.\n */\nconst FRAMEWORK_COLORS: Record<string, string> = {\n angular: '#FCE4EC', // pink - Angular front-end\n react: '#E3F2FD', // blue - React front-end\n browser: '#EDE7F6', // purple - browser (front-end base env)\n express: '#E8F5E9', // green - Express / server side\n node: '#FFF9C4', // yellow - node (server base env)\n};\n\nconst DEFAULT_FRAMEWORK_COLOR = '#F5F5F5'; // grey - unknown/empty env set\n\n/**\n * Fill color for an env set โ the color of the first env in the set that has a\n * known color, else the default.\n */\nfunction frameworkColor(frameworks: string[]): string {\n for (const env of frameworks) {\n const color = FRAMEWORK_COLORS[env];\n if (color !== undefined) return color;\n }\n return DEFAULT_FRAMEWORK_COLOR;\n}\n\n/**\n * Role border styling โ fill stays keyed on framework; the border shows a\n * project's ROLE at a glance. Server and client are the top-level runnable\n * nodes, so they get bold, colored borders to stand out:\n * server โ thick GREEN border (a runnable server app)\n * client โ thick RED border (a client app, e.g. angular)\n * designed-lib โ bold border (a library with a generated @DocumentDesign design)\n * lib / other โ plain thin border\n */\nfunction roleBorderAttrs(role: string): string {\n if (role === 'server') return ', color=\"green\", penwidth=3';\n if (role === 'client') return ', color=\"red\", penwidth=3';\n if (role === 'designed-lib') return ', penwidth=2';\n return '';\n}\n\n/**\n * Remove scope from name for display\n * '@scope/name' โ 'name'\n * 'name' โ 'name'\n */\nfunction getShortName(name: string): string {\n return name.includes('/') ? name.split('/').pop()! : name;\n}\n\n/**\n * Directory (repo-relative) that the committed architecture HTML lives in.\n * Node click-through links are computed relative to this so they resolve when\n * the file is opened straight from the checkout.\n */\nconst ARCH_OUTPUT_DIR = 'architecture';\n\n/**\n * Click-through href for a node: the project's committed design.html, made\n * relative to architecture/dependencies.html. Returns null when the project has\n * no generated DI design (no design.json โ no clickable design page).\n *\n * designFile is repo-relative posix (e.g. 'packages/http/http-api/design.json');\n * we swap the extension and re-root it at architecture/ so the browser resolves\n * '../packages/http/http-api/design.html' from the checkout.\n */\nfunction designHtmlHref(designFile: string | undefined): string | null {\n if (!designFile) return null;\n const designHtml = designFile.replace(/design\\.json$/, 'design.html');\n return path.posix.relative(ARCH_OUTPUT_DIR, designHtml);\n}\n\n/**\n * Generate Graphviz DOT format from the graph\n */\nexport function generateDot(graph: EnhancedGraph, title: string = 'Monorepo Dependency Architecture'): string {\n let dot = 'digraph Architecture {\\n';\n dot += ' rankdir=TB;\\n';\n dot += ' node [shape=box, style=filled, fontname=\"Arial\"];\\n';\n dot += ' edge [fontname=\"Arial\"];\\n\\n';\n\n // Group projects by level\n const levels: Record<number, string[]> = {};\n for (const [project, info] of Object.entries(graph)) {\n if (!levels[info.level]) levels[info.level] = [];\n levels[info.level].push(project);\n }\n\n // Nodes: fill colored by framework env set (libType), border shaped by role;\n // the label shows the env set + role (e.g. [browser, node] ยท server).\n // A node with a generated DI design also gets a URL so the rendered SVG box\n // is clickable โ it opens that project's committed design.html in a new tab.\n for (const [project, info] of Object.entries(graph)) {\n const shortName = getShortName(project);\n const frameworks = info.framework ?? [];\n const role = info.role ?? 'lib';\n const color = frameworkColor(frameworks);\n const border = roleBorderAttrs(role);\n const href = designHtmlHref(info.designFile);\n const link = href ? `, URL=\"${href}\", target=\"_blank\"` : '';\n const envSet = `[${frameworks.join(', ')}]`;\n const labelMeta = `L${info.level} ยท ${envSet} ยท ${role}`;\n dot += ` \"${shortName}\" [fillcolor=\"${color}\"${border}${link}, label=\"${shortName}\\\\n(${labelMeta})\"];\\n`;\n }\n\n dot += '\\n';\n\n // Create same-rank subgraphs for each level\n for (const [level, projects] of Object.entries(levels)) {\n dot += ` { rank=same; `;\n projects.forEach((p) => {\n const shortName = getShortName(p);\n dot += `\"${shortName}\"; `;\n });\n dot += '}\\n';\n }\n\n dot += '\\n';\n\n // Create edges (dependencies)\n for (const [project, info] of Object.entries(graph)) {\n const shortName = getShortName(project);\n for (const dep of info.dependsOn || []) {\n const depShortName = getShortName(dep);\n dot += ` \"${shortName}\" -> \"${depShortName}\";\\n`;\n }\n }\n\n dot += '\\n labelloc=\"t\";\\n';\n dot += ` label=\"${title}\\\\n(from architecture/dependencies.json)\";\\n`;\n dot += ' fontsize=20;\\n';\n dot += '}\\n';\n\n return dot;\n}\n\n/**\n * Generate interactive HTML with embedded SVG using viz.js\n */\nexport function generateHTML(dot: string, title: string = 'Monorepo Dependency Architecture'): string {\n const styles = generateHTMLStyles();\n const legend = generateHTMLLegend();\n const script = generateHTMLScript(dot);\n\n return `<!DOCTYPE html>\n<html>\n<head>\n <meta charset=\"utf-8\">\n <title>${title}</title>\n <script src=\"https://cdn.jsdelivr.net/npm/viz.js@2.1.2/viz.js\"></script>\n <script src=\"https://cdn.jsdelivr.net/npm/viz.js@2.1.2/full.render.js\"></script>\n <style>${styles}</style>\n</head>\n<body>\n <h1>${title}</h1>\n <p class=\"hint\">๐ก Click any box with a generated DI design to open its <strong>design.html</strong> (what the AI sees inside that project).</p>\n ${legend}\n <div id=\"graph\"></div>\n <script>${script}</script>\n</body>\n</html>`;\n}\n\nfunction generateHTMLStyles(): string {\n return `\n body {\n margin: 0;\n padding: 20px;\n font-family: Arial, sans-serif;\n background: #f5f5f5;\n }\n h1 {\n text-align: center;\n color: #333;\n }\n .hint {\n text-align: center;\n color: #555;\n margin: 0 0 16px;\n }\n /* viz.js renders node URLs as <a> โ show they are clickable. */\n #graph a { cursor: pointer; }\n #graph a:hover polygon { stroke-width: 2.5; }\n #graph {\n text-align: center;\n background: white;\n padding: 20px;\n border-radius: 8px;\n box-shadow: 0 2px 4px rgba(0,0,0,0.1);\n }\n .legend {\n margin: 20px auto;\n max-width: 600px;\n padding: 15px;\n background: white;\n border-radius: 8px;\n box-shadow: 0 2px 4px rgba(0,0,0,0.1);\n }\n .legend h2 {\n margin-top: 0;\n }\n .legend-item {\n margin: 8px 0;\n }\n .legend-box {\n display: inline-block;\n width: 20px;\n height: 20px;\n border: 1px solid #ccc;\n margin-right: 10px;\n vertical-align: middle;\n }\n `;\n}\n\nfunction generateHTMLLegend(): string {\n return `<div class=\"legend\">\n <h2>Legend โ fill = framework (libType), border = role</h2>\n <div class=\"legend-item\">\n <span class=\"legend-box\" style=\"background: #FCE4EC;\"></span>\n <strong>angular:</strong> Angular front-end\n </div>\n <div class=\"legend-item\">\n <span class=\"legend-box\" style=\"background: #E3F2FD;\"></span>\n <strong>react:</strong> React front-end\n </div>\n <div class=\"legend-item\">\n <span class=\"legend-box\" style=\"background: #EDE7F6;\"></span>\n <strong>browser:</strong> browser front-end base env\n </div>\n <div class=\"legend-item\">\n <span class=\"legend-box\" style=\"background: #E8F5E9;\"></span>\n <strong>express:</strong> Express / server side\n </div>\n <div class=\"legend-item\">\n <span class=\"legend-box\" style=\"background: #FFF9C4;\"></span>\n <strong>node:</strong> node server base env\n </div>\n <div class=\"legend-item\" style=\"margin-top: 12px;\">\n <span class=\"legend-box\" style=\"border: 3px solid green;\"></span>\n <strong>server:</strong> runnable server app (thick green border)\n </div>\n <div class=\"legend-item\">\n <span class=\"legend-box\" style=\"border: 3px solid red;\"></span>\n <strong>client:</strong> client app, e.g. angular (thick red border)\n </div>\n <div class=\"legend-item\">\n <span class=\"legend-box\" style=\"border: 2px solid #333;\"></span>\n <strong>designed-lib:</strong> library with a generated @DocumentDesign design (bold border)\n </div>\n <div class=\"legend-item\">\n <span class=\"legend-box\" style=\"border: 1px solid #ccc;\"></span>\n <strong>lib:</strong> plain library, no generated design (thin border)\n </div>\n <div class=\"legend-item\" style=\"margin-top: 15px;\">\n <em>Each node label shows its dependency level (L#), its framework env set (e.g. [browser, node]), and its role. Rows are laid out by level (top = no dependencies), with the deepest libraries at the bottom. Transitive dependencies are allowed but not shown.</em>\n </div>\n </div>`;\n}\n\nfunction generateHTMLScript(dot: string): string {\n return `\n const dot = ${JSON.stringify(dot)};\n const viz = new Viz();\n\n viz.renderSVGElement(dot)\n .then(element => {\n document.getElementById('graph').appendChild(element);\n })\n .catch(err => {\n console.error(err);\n document.getElementById('graph').innerHTML = '<pre>' + err + '</pre>';\n });\n `;\n}\n\ninterface VisualizationPaths {\n htmlPath: string;\n}\n\n/**\n * Write the committed architecture visualization to architecture/dependencies.html,\n * next to dependencies.json.\n *\n * This is a checked-in artifact, regenerated deterministically by\n * architecture:generate so the boxes stay clickable into each project's\n * design.html. The DOT is embedded in the HTML (rendered client-side by\n * viz.js), so no separate .dot file is committed โ same as design.html. Output\n * is deterministic (sorted graph in โ same bytes out) so git only shows a diff\n * when the architecture actually changed.\n */\nexport function writeVisualization(\n graph: EnhancedGraph,\n workspaceRoot: string,\n title: string = 'Monorepo Dependency Architecture'\n): VisualizationPaths {\n const outputDir = path.join(workspaceRoot, ARCH_OUTPUT_DIR);\n\n // Ensure directory exists\n if (!fs.existsSync(outputDir)) {\n fs.mkdirSync(outputDir, { recursive: true });\n }\n\n const html = generateHTML(generateDot(graph, title), title);\n const htmlPath = path.join(outputDir, 'dependencies.html');\n fs.writeFileSync(htmlPath, html, 'utf-8');\n\n return { htmlPath };\n}\n\n/**\n * Open the HTML visualization in the default browser\n */\nexport function openVisualization(htmlPath: string): boolean {\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n const platform = process.platform;\n let openCommand: string;\n\n if (platform === 'darwin') {\n openCommand = `open \"${htmlPath}\"`;\n } else if (platform === 'win32') {\n openCommand = `start \"\" \"${htmlPath}\"`;\n } else {\n openCommand = `xdg-open \"${htmlPath}\"`;\n }\n\n execSync(openCommand, { stdio: 'ignore' });\n return true;\n } catch (err: unknown) {\n //const error = toError(err);\n void err;\n return false;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"graph-visualizer.js","sourceRoot":"","sources":["../../../../../../packages/tooling/nx-webpieces-rules/src/lib/graph-visualizer.ts"],"names":[],"mappings":";AAAA;;;;;;;;GAQG;;AAqFH,kCA2DC;AAKD,oCAuBC;AAuMD,gDAiBC;AAKD,8CAqBC;;AA5ZD,+CAAyB;AACzB,mDAA6B;AAC7B,iDAAyC;AAIzC;;;;GAIG;AACH,MAAM,gBAAgB,GAA2B;IAC7C,OAAO,EAAE,SAAS,EAAE,6BAA6B;IACjD,KAAK,EAAE,SAAS,EAAE,2BAA2B;IAC7C,OAAO,EAAE,SAAS,EAAE,wCAAwC;IAC5D,OAAO,EAAE,SAAS,EAAE,iCAAiC;IACrD,IAAI,EAAE,SAAS,EAAE,kCAAkC;CACtD,CAAC;AAEF,MAAM,uBAAuB,GAAG,SAAS,CAAC,CAAC,+BAA+B;AAE1E;;;GAGG;AACH,SAAS,cAAc,CAAC,UAAoB;IACxC,KAAK,MAAM,GAAG,IAAI,UAAU,EAAE,CAAC;QAC3B,MAAM,KAAK,GAAG,gBAAgB,CAAC,GAAG,CAAC,CAAC;QACpC,IAAI,KAAK,KAAK,SAAS;YAAE,OAAO,KAAK,CAAC;IAC1C,CAAC;IACD,OAAO,uBAAuB,CAAC;AACnC,CAAC;AAED;;;;;;;;GAQG;AACH,SAAS,eAAe,CAAC,IAAY;IACjC,IAAI,IAAI,KAAK,QAAQ;QAAE,OAAO,6BAA6B,CAAC;IAC5D,IAAI,IAAI,KAAK,QAAQ;QAAE,OAAO,2BAA2B,CAAC;IAC1D,IAAI,IAAI,KAAK,cAAc;QAAE,OAAO,cAAc,CAAC;IACnD,OAAO,EAAE,CAAC;AACd,CAAC;AAED;;;;GAIG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AAC;QACpB,6BAA6B;QAC7B,KAAK,GAAG,CAAC;QACT,OAAO,KAAK,CAAC;IACjB,CAAC;AACL,CAAC","sourcesContent":["/**\n * Graph Visualizer\n *\n * Generates visual representations of the architecture graph:\n * - DOT format (for Graphviz)\n * - Interactive HTML (using viz.js)\n *\n * Output files go to tmp/webpieces/ for easy viewing without committing.\n */\n\nimport * as fs from 'fs';\nimport * as path from 'path';\nimport { execSync } from 'child_process';\nimport type { EnhancedGraph } from './graph-sorter';\nimport { toError } from '../toError';\n\n/**\n * Framework (libType) colors for visualization โ nodes are filled by the FIRST\n * env in their set that has a color, so it is obvious at a glance which side a\n * project targets. A project's full env set is shown in the label.\n */\nconst FRAMEWORK_COLORS: Record<string, string> = {\n angular: '#FCE4EC', // pink - Angular front-end\n react: '#E3F2FD', // blue - React front-end\n browser: '#EDE7F6', // purple - browser (front-end base env)\n express: '#E8F5E9', // green - Express / server side\n node: '#FFF9C4', // yellow - node (server base env)\n};\n\nconst DEFAULT_FRAMEWORK_COLOR = '#F5F5F5'; // grey - unknown/empty env set\n\n/**\n * Fill color for an env set โ the color of the first env in the set that has a\n * known color, else the default.\n */\nfunction frameworkColor(frameworks: string[]): string {\n for (const env of frameworks) {\n const color = FRAMEWORK_COLORS[env];\n if (color !== undefined) return color;\n }\n return DEFAULT_FRAMEWORK_COLOR;\n}\n\n/**\n * Role border styling โ fill stays keyed on framework; the border shows a\n * project's ROLE at a glance. Server and client are the top-level runnable\n * nodes, so they get bold, colored borders to stand out:\n * server โ thick GREEN border (a runnable server app)\n * client โ thick RED border (a client app, e.g. angular)\n * designed-lib โ bold border (a library with a generated @DocumentDesign design)\n * lib / other โ plain thin border\n */\nfunction roleBorderAttrs(role: string): string {\n if (role === 'server') return ', color=\"green\", penwidth=3';\n if (role === 'client') return ', color=\"red\", penwidth=3';\n if (role === 'designed-lib') return ', penwidth=2';\n return '';\n}\n\n/**\n * Remove scope from name for display\n * '@scope/name' โ 'name'\n * 'name' โ 'name'\n */\nfunction getShortName(name: string): string {\n return name.includes('/') ? name.split('/').pop()! : name;\n}\n\n/**\n * Directory (repo-relative) that the committed architecture HTML lives in.\n * Node click-through links are computed relative to this so they resolve when\n * the file is opened straight from the checkout.\n */\nconst ARCH_OUTPUT_DIR = 'architecture';\n\n/**\n * Click-through href for a node: the project's committed design.html, made\n * relative to architecture/dependencies.html. Returns null when the project has\n * no generated DI design (no design.json โ no clickable design page).\n *\n * designFile is repo-relative posix (e.g. 'packages/http/http-api/design.json');\n * we swap the extension and re-root it at architecture/ so the browser resolves\n * '../packages/http/http-api/design.html' from the checkout.\n */\nfunction designHtmlHref(designFile: string | undefined): string | null {\n if (!designFile) return null;\n const designHtml = designFile.replace(/design\\.json$/, 'design.html');\n return path.posix.relative(ARCH_OUTPUT_DIR, designHtml);\n}\n\n/**\n * Generate Graphviz DOT format from the graph\n */\nexport function generateDot(graph: EnhancedGraph, title: string = 'Monorepo Dependency Architecture'): string {\n let dot = 'digraph Architecture {\\n';\n dot += ' rankdir=TB;\\n';\n dot += ' node [shape=box, style=filled, fontname=\"Arial\"];\\n';\n dot += ' edge [fontname=\"Arial\"];\\n\\n';\n\n // Group projects by level\n const levels: Record<number, string[]> = {};\n for (const [project, info] of Object.entries(graph)) {\n if (!levels[info.level]) levels[info.level] = [];\n levels[info.level].push(project);\n }\n\n // Nodes: fill colored by framework env set (libType), border shaped by role;\n // the label shows the env set + role (e.g. [browser, node] ยท server).\n // A node with a generated DI design also gets a URL so the rendered SVG box\n // is clickable โ it opens that project's committed design.html in a new tab.\n for (const [project, info] of Object.entries(graph)) {\n const shortName = getShortName(project);\n const frameworks = info.framework ?? [];\n const role = info.role ?? 'lib';\n const color = frameworkColor(frameworks);\n const border = roleBorderAttrs(role);\n const href = designHtmlHref(info.designFile);\n const link = href ? `, URL=\"${href}\", target=\"_blank\"` : '';\n const envSet = `[${frameworks.join(', ')}]`;\n const labelMeta = `L${info.level} ยท ${envSet} ยท ${role}`;\n dot += ` \"${shortName}\" [fillcolor=\"${color}\"${border}${link}, label=\"${shortName}\\\\n(${labelMeta})\"];\\n`;\n }\n\n dot += '\\n';\n\n // Create same-rank subgraphs for each level\n for (const [level, projects] of Object.entries(levels)) {\n dot += ` { rank=same; `;\n projects.forEach((p) => {\n const shortName = getShortName(p);\n dot += `\"${shortName}\"; `;\n });\n dot += '}\\n';\n }\n\n dot += '\\n';\n\n // Create edges (dependencies)\n for (const [project, info] of Object.entries(graph)) {\n const shortName = getShortName(project);\n for (const dep of info.dependsOn || []) {\n const depShortName = getShortName(dep);\n dot += ` \"${shortName}\" -> \"${depShortName}\";\\n`;\n }\n }\n\n dot += '\\n labelloc=\"t\";\\n';\n dot += ` label=\"${title}\\\\n(from architecture/dependencies.json)\";\\n`;\n dot += ' fontsize=20;\\n';\n dot += '}\\n';\n\n return dot;\n}\n\n/**\n * Generate interactive HTML with embedded SVG using viz.js\n */\nexport function generateHTML(dot: string, title: string = 'Monorepo Dependency Architecture'): string {\n const styles = generateHTMLStyles();\n const legend = generateHTMLLegend();\n const script = generateHTMLScript(dot);\n\n return `<!DOCTYPE html>\n<html>\n<head>\n <meta charset=\"utf-8\">\n <title>${title}</title>\n <script src=\"https://cdn.jsdelivr.net/npm/viz.js@2.1.2/viz.js\"></script>\n <script src=\"https://cdn.jsdelivr.net/npm/viz.js@2.1.2/full.render.js\"></script>\n <style>${styles}</style>\n</head>\n<body>\n <h1>${title}</h1>\n <p class=\"hint\">๐ก Click any box with a generated DI design to open its <strong>design.html</strong> (what the AI sees inside that project).</p>\n <p class=\"hint\">๐ฆ <strong>Hover any box</strong> to bolden all of its connections (incoming <em>and</em> outgoing) and highlight the boxes on the other end โ the rest of the graph dims so you can trace one box at a glance.</p>\n ${legend}\n <div id=\"graph\"></div>\n <script>${script}</script>\n</body>\n</html>`;\n}\n\nfunction generateHTMLStyles(): string {\n return `\n body { margin: 0; padding: 20px; font-family: Arial, sans-serif; background: #f5f5f5; }\n h1 { text-align: center; color: #333; }\n .hint { text-align: center; color: #555; margin: 0 0 16px; }\n /* viz.js renders node URLs as <a> โ only boxes with a design.html get\n * one, so a:hover is a clickable-only signal. Make it pop clearly: a\n * thicker border PLUS a blue glow lift the box off the page, so it is\n * obvious which boxes you can click into vs. which you cannot. (A plain\n * stroke bump is invisible on server/client boxes, whose resting border\n * is already thick.) */\n #graph a { cursor: pointer; }\n #graph a polygon,\n #graph a ellipse { transition: stroke-width 0.12s ease, filter 0.12s ease; }\n #graph a:hover polygon,\n #graph a:hover ellipse {\n stroke: #1976d2;\n stroke-width: 5;\n filter: drop-shadow(0 0 6px rgba(25, 118, 210, 0.85));\n }\n /* Hover-highlight (wired up in JS after viz.js renders โ see\n * wireHoverHighlight). Hovering a node adds .wp-dim to the <svg> and\n * .wp-focus/.wp-neighbor/.wp-hl to the connected box, its neighbors, and\n * its edges. We ONLY dim: the connected subgraph keeps its exact normal\n * look (full opacity), the rest recedes. The un-dim rules repeat the\n * \"svg.wp-dim\" ancestor so they out-specify the dim rule (which has an\n * extra type selector) โ else the highlighted subgraph stays dimmed. */\n #graph .node, #graph .edge { transition: opacity 0.12s ease; }\n #graph svg.wp-dim .node,\n #graph svg.wp-dim .edge { opacity: 0.15; }\n #graph svg.wp-dim .node.wp-focus,\n #graph svg.wp-dim .node.wp-neighbor,\n #graph svg.wp-dim .edge.wp-hl { opacity: 1; }\n #graph {\n text-align: center;\n background: white;\n padding: 20px;\n border-radius: 8px;\n box-shadow: 0 2px 4px rgba(0,0,0,0.1);\n }\n .legend {\n margin: 20px auto;\n max-width: 600px;\n padding: 15px;\n background: white;\n border-radius: 8px;\n box-shadow: 0 2px 4px rgba(0,0,0,0.1);\n }\n .legend h2 {\n margin-top: 0;\n }\n .legend-item {\n margin: 8px 0;\n }\n .legend-box {\n display: inline-block;\n width: 20px;\n height: 20px;\n border: 1px solid #ccc;\n margin-right: 10px;\n vertical-align: middle;\n }\n `;\n}\n\nfunction generateHTMLLegend(): string {\n return `<div class=\"legend\">\n <h2>Legend โ fill = framework (libType), border = role</h2>\n <div class=\"legend-item\">\n <span class=\"legend-box\" style=\"background: #FCE4EC;\"></span>\n <strong>angular:</strong> Angular front-end\n </div>\n <div class=\"legend-item\">\n <span class=\"legend-box\" style=\"background: #E3F2FD;\"></span>\n <strong>react:</strong> React front-end\n </div>\n <div class=\"legend-item\">\n <span class=\"legend-box\" style=\"background: #EDE7F6;\"></span>\n <strong>browser:</strong> browser front-end base env\n </div>\n <div class=\"legend-item\">\n <span class=\"legend-box\" style=\"background: #E8F5E9;\"></span>\n <strong>express:</strong> Express / server side\n </div>\n <div class=\"legend-item\">\n <span class=\"legend-box\" style=\"background: #FFF9C4;\"></span>\n <strong>node:</strong> node server base env\n </div>\n <div class=\"legend-item\" style=\"margin-top: 12px;\">\n <span class=\"legend-box\" style=\"border: 3px solid green;\"></span>\n <strong>server:</strong> runnable server app (thick green border)\n </div>\n <div class=\"legend-item\">\n <span class=\"legend-box\" style=\"border: 3px solid red;\"></span>\n <strong>client:</strong> client app, e.g. angular (thick red border)\n </div>\n <div class=\"legend-item\">\n <span class=\"legend-box\" style=\"border: 2px solid #333;\"></span>\n <strong>designed-lib:</strong> library with a generated @DocumentDesign design (bold border)\n </div>\n <div class=\"legend-item\">\n <span class=\"legend-box\" style=\"border: 1px solid #ccc;\"></span>\n <strong>lib:</strong> plain library, no generated design (thin border)\n </div>\n <div class=\"legend-item\" style=\"margin-top: 15px;\">\n <em>Each node label shows its dependency level (L#), its framework env set (e.g. [browser, node]), and its role. Rows are laid out by level (top = no dependencies), with the deepest libraries at the bottom. Transitive dependencies are allowed but not shown.</em>\n </div>\n </div>`;\n}\n\n/**\n * The page script (injected as a string). After viz.js renders the Graphviz\n * SVG, `wireHoverHighlight` indexes its nodes and edges so hovering a box\n * boldens every connection (incoming AND outgoing) and highlights the boxes on\n * the other end, dimming the rest. viz.js emits a predictable structure:\n * <g class=\"node\"><title>NAME</title> ... </g>\n * <g class=\"edge\"><title>FROM->TO</title> <path/> <polygon/> ... </g>\n * The edge <title> text (decoded by the DOM as \"FROM->TO\") gives both\n * endpoints, so adjacency is built without touching the DOT.\n */\nfunction generateHTMLScript(dot: string): string {\n return `\n const dot = ${JSON.stringify(dot)};\n const viz = new Viz();\n viz.renderSVGElement(dot)\n .then(element => {\n document.getElementById('graph').appendChild(element);\n wireHoverHighlight(element);\n })\n .catch(err => {\n console.error(err);\n document.getElementById('graph').innerHTML = '<pre>' + err + '</pre>';\n });\n function wireHoverHighlight(svg) {\n const nodeByName = new Map();\n svg.querySelectorAll('g.node').forEach(g => {\n const t = g.querySelector('title');\n if (t) nodeByName.set(t.textContent.trim(), g);\n });\n const edgesOf = new Map();\n const neighborsOf = new Map();\n const ensure = (map, key) => {\n let v = map.get(key);\n if (!v) { v = new Set(); map.set(key, v); }\n return v;\n };\n svg.querySelectorAll('g.edge').forEach(edge => {\n const t = edge.querySelector('title');\n if (!t) return;\n const idx = t.textContent.indexOf('->');\n if (idx < 0) return;\n const from = t.textContent.slice(0, idx).trim();\n const to = t.textContent.slice(idx + 2).trim();\n ensure(edgesOf, from).add(edge);\n ensure(edgesOf, to).add(edge);\n ensure(neighborsOf, from).add(to);\n ensure(neighborsOf, to).add(from);\n });\n const clear = () => {\n svg.classList.remove('wp-dim');\n svg.querySelectorAll('.wp-focus, .wp-neighbor, .wp-hl').forEach(el => {\n el.classList.remove('wp-focus', 'wp-neighbor', 'wp-hl');\n });\n };\n const highlight = (name, focusEl) => {\n clear();\n svg.classList.add('wp-dim');\n focusEl.classList.add('wp-focus');\n (edgesOf.get(name) || []).forEach(e => e.classList.add('wp-hl'));\n (neighborsOf.get(name) || []).forEach(n => {\n const g = nodeByName.get(n);\n if (g) g.classList.add('wp-neighbor');\n });\n };\n nodeByName.forEach((g, name) => {\n g.addEventListener('mouseenter', () => highlight(name, g));\n g.addEventListener('mouseleave', clear);\n });\n }\n `;\n}\n\ninterface VisualizationPaths {\n htmlPath: string;\n}\n\n/**\n * Write the committed architecture visualization to architecture/dependencies.html,\n * next to dependencies.json.\n *\n * This is a checked-in artifact, regenerated deterministically by\n * architecture:generate so the boxes stay clickable into each project's\n * design.html. The DOT is embedded in the HTML (rendered client-side by\n * viz.js), so no separate .dot file is committed โ same as design.html. Output\n * is deterministic (sorted graph in โ same bytes out) so git only shows a diff\n * when the architecture actually changed.\n */\nexport function writeVisualization(\n graph: EnhancedGraph,\n workspaceRoot: string,\n title: string = 'Monorepo Dependency Architecture'\n): VisualizationPaths {\n const outputDir = path.join(workspaceRoot, ARCH_OUTPUT_DIR);\n\n // Ensure directory exists\n if (!fs.existsSync(outputDir)) {\n fs.mkdirSync(outputDir, { recursive: true });\n }\n\n const html = generateHTML(generateDot(graph, title), title);\n const htmlPath = path.join(outputDir, 'dependencies.html');\n fs.writeFileSync(htmlPath, html, 'utf-8');\n\n return { htmlPath };\n}\n\n/**\n * Open the HTML visualization in the default browser\n */\nexport function openVisualization(htmlPath: string): boolean {\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n const platform = process.platform;\n let openCommand: string;\n\n if (platform === 'darwin') {\n openCommand = `open \"${htmlPath}\"`;\n } else if (platform === 'win32') {\n openCommand = `start \"\" \"${htmlPath}\"`;\n } else {\n openCommand = `xdg-open \"${htmlPath}\"`;\n }\n\n execSync(openCommand, { stdio: 'ignore' });\n return true;\n } catch (err: unknown) {\n //const error = toError(err);\n void err;\n return false;\n }\n}\n"]}
|