mddd-cli 4.0.5 → 4.1.0

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/bin/cli.js CHANGED
@@ -4,6 +4,7 @@ import { Command } from 'commander';
4
4
  import pc from 'picocolors';
5
5
  import { FileSystemService } from '../src/services/FileSystemService.js';
6
6
  import { InitService } from '../src/services/InitService.js';
7
+ import { validateMermaidSyntax } from '../src/commands/validator.js';
7
8
  import * as initCmd from '../src/commands/init.js';
8
9
 
9
10
  // ─── Services ────────────────────────────────────────────────────────────────
@@ -16,7 +17,7 @@ const program = new Command();
16
17
  program
17
18
  .name('md')
18
19
  .description('Manager for co-located specifications for Mermaid Diagram Driven Development (MDDD)')
19
- .version('4.0.5');
20
+ .version('4.1.0');
20
21
 
21
22
  // ==========================================
22
23
  // COMMAND: md init
@@ -33,5 +34,22 @@ program
33
34
  }
34
35
  });
35
36
 
37
+ program
38
+ .command('validate <target>')
39
+ .description('Validate Mermaid diagrams in .md files (returns JSON)')
40
+ .action(async (target) => {
41
+ // Executa a lógica de validação na memória
42
+ const result = await validateMermaidSyntax(target);
43
+
44
+ // Cospe APENAS o JSON no stdout para a IA ler de forma limpa
45
+ console.log(JSON.stringify(result));
46
+
47
+ // Controla o encerramento do processo com o exit code correto
48
+ if (!result.valid) {
49
+ process.exit(1);
50
+ }
51
+ process.exit(0);
52
+ });
53
+
36
54
  // ─── Parse ───────────────────────────────────────────────────────────────────
37
55
  program.parse(process.argv);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mddd-cli",
3
- "version": "4.0.5",
3
+ "version": "4.1.0",
4
4
  "description": "Official CLI for modular, co-located, and versioned Mermaid Diagram Driven Development (MDDD).",
5
5
  "type": "module",
6
6
  "exports": "./bin/cli.js",
@@ -39,8 +39,8 @@
39
39
  },
40
40
  "homepage": "https://github.com/JulioCRFilho/mermaid-diagram-driven-development#readme",
41
41
  "dependencies": {
42
- "@mermaid-js/mermaid-cli": "^11.15.0",
42
+ "@mermaid-js/parser": "^1.1.1",
43
43
  "commander": "^12.0.0",
44
44
  "picocolors": "^1.0.0"
45
45
  }
46
- }
46
+ }
@@ -0,0 +1,76 @@
1
+ import { parse } from '@mermaid-js/parser';
2
+ import fs from 'node:fs';
3
+
4
+ function extractMermaidBlocks(text) {
5
+ const regex = /```mermaid\s*([\s\S]*?)\s*```/g;
6
+ const blocks = [];
7
+ let match;
8
+
9
+ while ((match = regex.exec(text)) !== null) {
10
+ if (match[1].trim()) {
11
+ blocks.push(match[1].trim());
12
+ }
13
+ }
14
+
15
+ return blocks;
16
+ }
17
+
18
+ /**
19
+ * Limpa comentários do topo do diagrama que quebram o parser do Mermaid
20
+ * @param {string} code
21
+ * @returns {string}
22
+ */
23
+ function cleanDiagramCode(code) {
24
+ // Remove linhas que começam com %% no topo do arquivo antes da declaração do tipo
25
+ const lines = code.split('\n');
26
+ const cleanedLines = [];
27
+ let foundTypeDeclaration = false;
28
+
29
+ for (const line of lines) {
30
+ const trimmed = line.trim();
31
+
32
+ // Ignora linhas vazias ou comentários iniciais até achar a declaração do diagrama
33
+ if (!foundTypeDeclaration && (trimmed === '' || trimmed.startsWith('%%'))) {
34
+ continue;
35
+ }
36
+
37
+ foundTypeDeclaration = true;
38
+ cleanedLines.push(line);
39
+ }
40
+
41
+ return cleanedLines.join('\n').trim();
42
+ }
43
+
44
+ export async function validateMermaidSyntax(input) {
45
+ let content = input;
46
+
47
+ if (fs.existsSync(input)) {
48
+ content = fs.readFileSync(input, 'utf-8');
49
+ }
50
+
51
+ let diagramsToValidate = [content];
52
+ if (content.includes('```mermaid')) {
53
+ diagramsToValidate = extractMermaidBlocks(content);
54
+
55
+ if (diagramsToValidate.length === 0) {
56
+ return {
57
+ valid: false,
58
+ error: "Nenhum bloco de código '```mermaid' estruturado foi encontrado."
59
+ };
60
+ }
61
+ }
62
+
63
+ try {
64
+ for (const diagramCode of diagramsToValidate) {
65
+ // Aplica a limpeza antes de mandar para o parser em memória
66
+ const readyCode = cleanDiagramCode(diagramCode);
67
+ await parse(readyCode);
68
+ }
69
+ return { valid: true };
70
+ } catch (error) {
71
+ return {
72
+ valid: false,
73
+ error: error.message || 'Erro de sintaxe desconhecido no Mermaid.'
74
+ };
75
+ }
76
+ }
@@ -21,7 +21,7 @@ stateDiagram-v2
21
21
  CheckCode --> EvaluatedCodeIsChaotic: Draw BOTH current chaotic logic AND ideal target refactored graph (v1.0.0 - draft)
22
22
  }
23
23
 
24
- RenderTopology --> CheckDiagram: Use mmdc -i <path/to/diagram.md> --checkSyntax to validate diagram syntax
24
+ RenderTopology --> CheckDiagram: Use "npx md validate <path/to/spec.md>" to validate diagram syntax
25
25
 
26
26
  state CheckDiagram {
27
27
  [*] --> DiagramValid: Proceed to next step
@@ -22,7 +22,7 @@ stateDiagram-v2
22
22
  [*] --> IncrementMajor: Increment Major: Bump X in X.Y.Z
23
23
  }
24
24
 
25
- IncrementVersion --> CheckDiagram: Use mmdc -i <path/to/diagram.md> --checkSyntax to validate diagram syntax
25
+ IncrementVersion --> CheckDiagram: Use "npx md validate <path/to/spec.md>" to validate diagram syntax
26
26
 
27
27
  state CheckDiagram {
28
28
  [*] --> TryRender
@@ -19,7 +19,7 @@ stateDiagram-v2
19
19
 
20
20
  EvaluateContext --> GenerateBlueprint: Create .spec.md from "src/templates/spec-template.md".
21
21
  GenerateBlueprint --> FormatSpecOutput: Format blueprint into target .spec.md structure
22
- FormatSpecOutput --> CheckDiagram: Use mmdc -i <path/to/diagram.md> --checkSyntax to validate diagram syntax
22
+ FormatSpecOutput --> CheckDiagram: Use "npx md validate <path/to/spec.md>" to validate diagram syntax
23
23
 
24
24
  state CheckDiagram {
25
25
  [*] --> DiagramValid: Proceed to next step