3
3
const fs = require ( 'fs' ) ;
4
4
const path = require ( 'path' ) ;
5
5
6
- // Read the components.json file
7
- const componentsPath = path . join ( __dirname , '../specs/components.json' ) ;
8
- const components = JSON . parse ( fs . readFileSync ( componentsPath , 'utf8' ) ) ;
9
-
10
- // Create a map of components for easy lookup
11
- const componentMap = new Map ( ) ;
12
- components . components . forEach ( component => {
13
- componentMap . set ( component . tag , component ) ;
14
- } ) ;
15
-
6
+ // Constants
7
+ const MERMAID_CONFIG = {
8
+ layout : 'elk' ,
9
+ look : 'classic' ,
10
+ theme : 'base'
11
+ } ;
16
12
13
+ const OUTPUT_DIR = '../specs' ;
14
+ const DIAGRAM_FILE = 'gcds-components-diagram.md' ;
15
+ const PREVIEW_FILE = 'gcds-components-diagram-preview.md' ;
17
16
18
17
// Function to convert TypeScript type to a more readable format
19
18
function formatType ( type ) {
@@ -28,69 +27,67 @@ function formatType(type) {
28
27
}
29
28
30
29
// Function to generate class definitions for all components
31
- function generateClassDefinitions ( ) {
32
- let output = '' ;
30
+ function generateClassDefinitions ( components ) {
31
+ const lines = [ ] ;
33
32
34
33
// Generate classes for all components in the order they appear
35
- components . components . forEach ( component => {
34
+ components . forEach ( component => {
36
35
const className = component . tag . replace ( / - / g, '_' ) ;
37
36
38
- output += ` class ${ className } {\n` ;
37
+ lines . push ( ` class ${ className } {` ) ;
39
38
40
39
if ( component . props && component . props . length > 0 ) {
41
40
component . props . forEach ( prop => {
42
41
const propType = formatType ( prop . type ) ;
43
- output += `\t ${ propType } ${ prop . name } \n` ;
42
+ lines . push ( `\t ${ propType } ${ prop . name } ` ) ;
44
43
} ) ;
45
44
}
46
45
47
- output += ` }\n` ;
46
+ lines . push ( ' }' ) ;
48
47
} ) ;
49
48
50
- return output ;
49
+ return lines . join ( '\n' ) ;
51
50
}
52
51
53
52
// Function to generate relationships based on actual dependencies
54
- function generateRelationships ( ) {
55
- let output = '' ;
53
+ function generateRelationships ( components ) {
54
+ const lines = [ ] ;
56
55
57
56
// Generate relationships based on actual component dependencies
58
- components . components . forEach ( component => {
57
+ components . forEach ( component => {
59
58
if ( component . dependencies && component . dependencies . length > 0 ) {
60
59
const fromClass = component . tag . replace ( / - / g, '_' ) ;
61
60
62
61
component . dependencies . forEach ( dep => {
63
62
const toClass = dep . replace ( / - / g, '_' ) ;
64
- output += ` ${ fromClass } --> ${ toClass } : uses\n` ;
63
+ lines . push ( ` ${ fromClass } --> ${ toClass } : uses` ) ;
65
64
} ) ;
66
65
}
67
66
} ) ;
68
67
69
- return output ;
68
+ return lines . join ( '\n' ) ;
70
69
}
71
70
72
71
// Generate the complete MermaidJS diagram
73
- function generateMermaidDiagram ( ) {
72
+ function generateMermaidDiagram ( components ) {
74
73
const header = `---
75
74
config:
76
- layout: elk
77
- look: classic
78
- theme: base
75
+ layout: ${ MERMAID_CONFIG . layout }
76
+ look: ${ MERMAID_CONFIG . look }
77
+ theme: ${ MERMAID_CONFIG . theme }
79
78
---
80
79
classDiagram
81
80
direction BT
82
81
` ;
83
82
84
- const classDefinitions = generateClassDefinitions ( ) ;
85
- const relationships = generateRelationships ( ) ;
83
+ const classDefinitions = generateClassDefinitions ( components ) ;
84
+ const relationships = generateRelationships ( components ) ;
86
85
87
- return header + classDefinitions + '\n' + relationships ;
86
+ return ` ${ header } ${ classDefinitions } \n ${ relationships } ` ;
88
87
}
89
88
90
89
// Generate GitHub-friendly markdown with Mermaid diagram
91
- function generateGitHubMarkdown ( ) {
92
- const diagram = generateMermaidDiagram ( ) ;
93
-
90
+ function generateGitHubMarkdown ( diagram ) {
94
91
return `# GCDS Components Diagram
95
92
96
93
This diagram shows the component structure and relationships for the GCDS (Government of Canada Design System) components.
@@ -116,16 +113,27 @@ The diagram includes all GCDS components with their properties and dependencies
116
113
` ;
117
114
}
118
115
119
- // Generate and output the diagram
120
- const diagram = generateMermaidDiagram ( ) ;
121
- const githubMarkdown = generateGitHubMarkdown ( ) ;
122
-
123
- // Save the original MermaidJS diagram
124
- const outputPath = path . join ( __dirname , '../specs/gcds-components-diagram.md' ) ;
125
- fs . writeFileSync ( outputPath , diagram ) ;
126
- console . log ( `\Diagram saved to: ${ outputPath } ` ) ;
127
-
128
- // Save the GitHub-friendly preview markdown
129
- const previewPath = path . join ( __dirname , '../specs/gcds-components-diagram-preview.md' ) ;
130
- fs . writeFileSync ( previewPath , githubMarkdown ) ;
131
- console . log ( `\nGitHub preview saved to: ${ previewPath } ` ) ;
116
+ // Main execution
117
+ try {
118
+ // Read the components.json file
119
+ const componentsPath = path . join ( __dirname , OUTPUT_DIR , 'components.json' ) ;
120
+ const components = JSON . parse ( fs . readFileSync ( componentsPath , 'utf8' ) ) ;
121
+
122
+ // Generate and output the diagram
123
+ const diagram = generateMermaidDiagram ( components . components ) ;
124
+ const githubMarkdown = generateGitHubMarkdown ( diagram ) ;
125
+
126
+ // Save the original MermaidJS diagram
127
+ const outputPath = path . join ( __dirname , OUTPUT_DIR , DIAGRAM_FILE ) ;
128
+ fs . writeFileSync ( outputPath , diagram ) ;
129
+ console . log ( `\nDiagram saved to: ${ outputPath } ` ) ;
130
+
131
+ // Save the GitHub-friendly preview markdown
132
+ const previewPath = path . join ( __dirname , OUTPUT_DIR , PREVIEW_FILE ) ;
133
+ fs . writeFileSync ( previewPath , githubMarkdown ) ;
134
+ console . log ( `\nGitHub preview saved to: ${ previewPath } ` ) ;
135
+
136
+ } catch ( error ) {
137
+ console . error ( '❌ Error generating diagram:' , error . message ) ;
138
+ process . exit ( 1 ) ;
139
+ }
0 commit comments