1
- import { readdirSync , writeFileSync } from "node:fs" ;
2
- import path from " node:path" ;
3
- import { readFileSync } from "fs" ;
1
+ import { readFileSync } from 'fs' ;
2
+ import { readdirSync , writeFileSync } from ' node:fs' ;
3
+ import path from 'node:path' ;
4
4
5
5
function readAllFiles ( dir : string , ext : string ) : string [ ] {
6
- const out : string [ ] = [ ]
7
- const files = readdirSync ( dir , { withFileTypes : true , recursive : true } )
6
+ const out = new Set < string > ( ) ;
7
+ const files = readdirSync ( dir , { withFileTypes : true , recursive : true } ) ;
8
8
files . forEach ( file => {
9
9
if ( file . isFile ( ) && path . extname ( file . name ) === ext ) {
10
- out . push ( path . join ( dir , file . name ) )
11
- return
10
+ out . add ( path . join ( file . path || dir , file . name ) ) ;
11
+ return ;
12
12
}
13
13
if ( file . isDirectory ( ) ) {
14
- out . push ( ... readAllFiles ( path . join ( dir , file . name ) , ext ) )
15
- return
14
+ readAllFiles ( path . join ( dir , file . name ) , ext ) . forEach ( v => out . add ( v ) )
15
+ return ;
16
16
}
17
- } )
18
- return out
17
+ } ) ;
18
+ return Array . from ( out ) . slice ( ) ;
19
19
}
20
20
21
21
interface TagProp {
@@ -34,7 +34,7 @@ interface Tag {
34
34
}
35
35
36
36
function extractTags ( data : string ) : Tag {
37
- const out : Tag = { name : '' , code : '' , desc : '' , group : '' , html : '' , other : '' , props : [ ] }
37
+ const out : Tag = { name : '' , code : '' , desc : '' , group : '' , html : '' , other : '' , props : [ ] } ;
38
38
39
39
const rexName = / @ n a m e ( .* ?) $ / gms;
40
40
const rexGroup = / @ g r o u p ( .* ?) $ / gms;
@@ -45,38 +45,38 @@ function extractTags(data: string): Tag {
45
45
const rexOther = / @ o t h e r ( .* ?) [ @ * ] / gms;
46
46
47
47
for ( const item of data . matchAll ( rexName ) ) {
48
- out . name = item [ 1 ] . trim ( )
48
+ out . name = item [ 1 ] . trim ( ) ;
49
49
}
50
50
for ( const item of data . matchAll ( rexGroup ) ) {
51
- out . group = item [ 1 ] . trim ( )
51
+ out . group = item [ 1 ] . trim ( ) ;
52
52
}
53
53
for ( const item of data . matchAll ( rexDesc ) ) {
54
- out . desc = item [ 1 ] . trim ( )
54
+ out . desc = item [ 1 ] . trim ( ) ;
55
55
}
56
56
for ( const item of data . matchAll ( rexHtml ) ) {
57
- out . html = item [ 1 ] . trimEnd ( )
57
+ out . html = item [ 1 ] . trimEnd ( ) ;
58
58
}
59
59
for ( const item of data . matchAll ( rexCode ) ) {
60
- out . code = item [ 1 ] . trimEnd ( )
60
+ out . code = item [ 1 ] . trimEnd ( ) ;
61
61
}
62
62
for ( const item of data . matchAll ( rexOther ) ) {
63
- out . other = item [ 1 ] . trimEnd ( )
63
+ out . other = item [ 1 ] . trimEnd ( ) ;
64
64
}
65
65
for ( const item of data . matchAll ( rexProp ) ) {
66
- out . props . push ( { key : item [ 1 ] . trim ( ) , value : item [ 2 ] . trim ( ) } )
66
+ out . props . push ( { key : item [ 1 ] . trim ( ) , value : item [ 2 ] . trim ( ) } ) ;
67
67
}
68
68
69
- return out
69
+ return out ;
70
70
}
71
71
72
72
function extractComment ( file : string ) : Tag [ ] {
73
- const out : Tag [ ] = [ ]
74
- const buf = readFileSync ( file ) . toString ( )
73
+ const out : Tag [ ] = [ ] ;
74
+ const buf = readFileSync ( file ) . toString ( ) ;
75
75
const rex = / \/ \* ( .* ?) \* \/ / sg;
76
- for ( let item of buf . matchAll ( rex ) ) {
77
- out . push ( extractTags ( item [ 0 ] ) )
76
+ for ( const item of buf . matchAll ( rex ) ) {
77
+ out . push ( extractTags ( item [ 0 ] ) ) ;
78
78
}
79
- return out
79
+ return out ;
80
80
}
81
81
82
82
interface DocTemplate {
@@ -85,43 +85,43 @@ interface DocTemplate {
85
85
filename : string
86
86
}
87
87
88
- function escapeHTML ( data : string ) : string {
88
+ function escapeHTML ( data : string ) : string {
89
89
return data
90
- . replaceAll ( '<' , '<' )
91
- . replaceAll ( '>' , '>' )
92
- . replaceAll ( '{' , '{' )
93
- . replaceAll ( '}' , '}' )
90
+ . replaceAll ( '<' , '<' )
91
+ . replaceAll ( '>' , '>' )
92
+ . replaceAll ( '{' , '{' )
93
+ . replaceAll ( '}' , '}' ) ;
94
94
}
95
95
96
96
function docTemplate ( name : string , mod : string , data : Tag [ ] ) : DocTemplate {
97
- const selector = `${ mod . toLowerCase ( ) } -${ name . toLowerCase ( ) . replaceAll ( ' ' , '-' ) } `
98
- const modName = mod . split ( ' ' ) . map ( value => value [ 0 ] . toUpperCase ( ) + value . substring ( 1 ) ) . join ( '' )
99
- const className = modName + name . split ( ' ' ) . map ( value => value [ 0 ] . toUpperCase ( ) + value . substring ( 1 ) ) . join ( '' )
97
+ const selector = `${ mod . toLowerCase ( ) } -${ name . toLowerCase ( ) . replaceAll ( ' ' , '-' ) } ` ;
98
+ const modName = mod . split ( ' ' ) . map ( value => value [ 0 ] . toUpperCase ( ) + value . substring ( 1 ) ) . join ( '' ) ;
99
+ const className = modName + name . split ( ' ' ) . map ( value => value [ 0 ] . toUpperCase ( ) + value . substring ( 1 ) ) . join ( '' ) ;
100
100
101
- const other : string [ ] = [ ]
102
- let view : string = `\n` ;
101
+ const other : string [ ] = [ ] ;
102
+ let view = '\n' ;
103
103
104
104
data . forEach ( tag => {
105
- other . push ( tag . other )
106
- view += `<h4 class="bq bq-warning demo-name">${ tag . name } </h4>\n`
107
- view += `<p class="demo-desc">${ tag . desc } </p>\n`
105
+ other . push ( tag . other ) ;
106
+ view += `<h4 class="bq bq-warning demo-name">${ tag . name } </h4>\n` ;
107
+ view += `<p class="demo-desc">${ tag . desc } </p>\n` ;
108
108
if ( tag . html . length > 0 ) {
109
- view += `<div class="demo-view w-full">${ tag . html } </div>\n`
110
- view += `<pre class="demo-html w-full">${ escapeHTML ( tag . html ) } </pre>\n`
109
+ view += `<div class="demo-view w-full">${ tag . html } </div>\n` ;
110
+ view += `<pre class="demo-html w-full">${ escapeHTML ( tag . html ) } </pre>\n` ;
111
111
}
112
112
if ( tag . code . length > 0 ) {
113
- view += `<pre class="demo-code w-full">${ tag . code } </pre>\n`
113
+ view += `<pre class="demo-code w-full">${ tag . code } </pre>\n` ;
114
114
}
115
- if ( tag . props . length > 0 ) {
116
- view += ` <table class="demo-attr">\n`
115
+ if ( tag . props . length > 0 ) {
116
+ view += ' <table class="demo-attr">\n' ;
117
117
tag . props . forEach ( value => {
118
118
view += `<tr><td class="t-wrap-line" style="width: 30%;"><b>${ value . key } </b></td>
119
- <td class="t-wrap-line">${ value . value } </td></tr>`
120
- } )
121
- view += ` </table>\n`
119
+ <td class="t-wrap-line">${ value . value } </td></tr>` ;
120
+ } ) ;
121
+ view += ' </table>\n' ;
122
122
}
123
- view += ` <div class="demo-end"> </div>\n`
124
- } )
123
+ view += ' <div class="demo-end"> </div>\n' ;
124
+ } ) ;
125
125
126
126
return {
127
127
data : `import { Component } from '@angular/core';
@@ -137,7 +137,7 @@ export class ${className} {
137
137
}` ,
138
138
filename : selector ,
139
139
class : className ,
140
- }
140
+ } ;
141
141
}
142
142
143
143
interface ModuleTemplate {
@@ -151,15 +151,16 @@ function moduleTemplate(mod: string, data: ModuleTemplate[]): string {
151
151
const links : string [ ] = [ ] ;
152
152
const declarations : string [ ] = [ ] ;
153
153
154
- const modName = mod . split ( ' ' ) . map ( value => value [ 0 ] . toUpperCase ( ) + value . substring ( 1 ) ) . join ( '' )
154
+ const modName = mod . split ( ' ' ) . map ( value => value [ 0 ] . toUpperCase ( ) + value . substring ( 1 ) ) . join ( '' ) ;
155
155
156
156
data . forEach ( value => {
157
- imports . push ( `import { ${ value . class } } from './${ value . filename } ';` )
158
- links . push ( `{ link: '${ value . title } ', component: ${ value . class } },` )
159
- declarations . push ( `${ value . class } ,` )
160
- } )
157
+ imports . push ( `import { ${ value . class } } from './${ value . filename } ';` ) ;
158
+ links . push ( `{ link: '${ value . title } ', component: ${ value . class } },` ) ;
159
+ declarations . push ( `${ value . class } ,` ) ;
160
+ } ) ;
161
161
162
162
return `import { NgModule } from '@angular/core';
163
+ import { CoreModule } from '../../../../../core/src/lib/core.module';
163
164
import { KitModule } from '../../../../../kit/src/lib/kit.module';
164
165
import { ApiLink } from '../../root/api.models';
165
166
${ imports . join ( '\n' ) }
@@ -176,47 +177,52 @@ export function links(): ApiLink[] {
176
177
],
177
178
imports: [
178
179
KitModule,
180
+ CoreModule,
179
181
],
180
182
})
181
183
export class ${ modName } Module { }
182
- `
184
+ ` ;
183
185
}
184
186
185
- function parse ( dir : string , ext : string , mod : string , out : string ) : void {
187
+ function parse ( ext : string , mod : string , out : string , dirs : string [ ] ) : void {
186
188
const tags : Tag [ ] = [ ] ;
187
- const files = readAllFiles ( dir , ext )
188
- files . forEach ( file => {
189
- tags . push ( ...extractComment ( file ) )
189
+ dirs . forEach ( dir => {
190
+ const files = readAllFiles ( dir , ext ) ;
191
+ files . forEach ( file => {
192
+ tags . push ( ...extractComment ( file ) ) ;
193
+ } ) ;
190
194
} )
191
195
192
- const _groups = new Map < string , string > ( ) ;
193
- const groups : string [ ] = [ ]
194
- tags . forEach ( value => _groups . set ( value . group , value . group ) )
195
- for ( let group of _groups . keys ( ) ) {
196
- groups . push ( group )
196
+ const tmpGroups = new Map < string , string > ( ) ;
197
+ const groups : string [ ] = [ ] ;
198
+ tags . forEach ( value => tmpGroups . set ( value . group , value . group ) ) ;
199
+ for ( const group of tmpGroups . keys ( ) ) {
200
+ groups . push ( group ) ;
197
201
}
198
- groups . sort ( )
202
+ groups . sort ( ) ;
199
203
200
- const module : ModuleTemplate [ ] = [ ]
204
+ const module : ModuleTemplate [ ] = [ ] ;
201
205
202
206
groups . forEach ( group => {
203
- const list = tags . filter ( value => value . group === group )
204
- const doc = docTemplate ( group , mod , list )
205
- module . push ( { filename : doc . filename , class : doc . class , title : group } )
207
+ const list = tags . filter ( value => value . group === group ) ;
208
+ const doc = docTemplate ( group , mod , list ) ;
209
+ module . push ( { filename : doc . filename , class : doc . class , title : group } ) ;
206
210
207
211
writeFileSync (
208
212
path . join ( out , doc . filename + '.ts' ) ,
209
213
doc . data ,
210
- { encoding : " utf-8" } ,
211
- )
212
- } )
214
+ { encoding : ' utf-8' } ,
215
+ ) ;
216
+ } ) ;
213
217
214
218
writeFileSync (
215
- path . join ( out , ` module.ts` ) ,
219
+ path . join ( out , ' module.ts' ) ,
216
220
moduleTemplate ( mod , module ) ,
217
- { encoding : " utf-8" } ,
218
- )
221
+ { encoding : ' utf-8' } ,
222
+ ) ;
219
223
}
220
224
221
- parse ( 'projects/styles/src/' , '.scss' , 'style' , 'projects/website/src/pages/styles/models' )
222
- parse ( 'projects/kit/src/lib/' , '.ts' , 'comp' , 'projects/website/src/pages/kit/models' )
225
+ parse ( '.scss' , 'style' , 'projects/website/src/pages/styles/models' ,
226
+ [ 'projects/styles/src/' ] ) ;
227
+ parse ( '.ts' , 'comp' , 'projects/website/src/pages/kit/models' ,
228
+ [ 'projects/kit/src/lib/' , 'projects/core/src/lib/' ] ) ;
0 commit comments