@@ -18,15 +18,30 @@ const statistics = {};
18
18
const statusTypes = {
19
19
success : 'green' ,
20
20
error : 'red' ,
21
- exclude : 'blue ' ,
21
+ exclude : 'blueBright ' ,
22
22
empty : 'yellow'
23
23
} ;
24
24
25
25
const extensions = [ '.ts' , '.js' , '.tsx' , '.jsx' , '.vue' ] ;
26
26
27
+ /**A
28
+ * Add status and count to result
29
+ * @param {string } file
30
+ * @param {string } status
31
+ * @param {boolean } isFolder
32
+ */
33
+ const addToStatistics = ( file , status , isFolder = false ) => {
34
+ const extension = ! isFolder ? getExtension ( file ) : 'folder' ;
35
+
36
+ if ( ! statistics [ extension ] ) {
37
+ statistics [ extension ] = Object . keys ( statusTypes ) . reduce ( ( before , curr ) => ( { ...before , [ curr ] : 0 } ) , { } ) ;
38
+ }
39
+ statistics [ extension ] [ status ] ++ ;
40
+ } ;
41
+
27
42
/**
28
43
* Default command that generate md files
29
- * @param {object } argv Arguments passed by yargs
44
+ * @param {object } argv passed arguments
30
45
*/
31
46
async function generate ( argv ) {
32
47
const exclude = ( argv . exclude && argv . exclude . split ( ',' ) ) || [ argv . exclude || '' ] ;
@@ -41,15 +56,6 @@ async function generate(argv) {
41
56
// remove docs folder, except README.md
42
57
const deletedPaths = await del ( [ `${ docsFolder } /**/*` , `!${ docsFolder } /README.md` , ...rmPattern ] ) ;
43
58
44
- const addToStatistics = ( file , status , isFolder = false ) => {
45
- const extension = ! isFolder ? getExtension ( file ) : 'folder' ;
46
-
47
- if ( ! statistics [ extension ] ) {
48
- statistics [ extension ] = Object . keys ( statusTypes ) . reduce ( ( before , curr ) => ( { ...before , [ curr ] : 0 } ) , { } ) ;
49
- }
50
- statistics [ extension ] [ status ] ++ ;
51
- } ;
52
-
53
59
/**
54
60
* Read all files in directory
55
61
* @param {string } folder
@@ -72,8 +78,8 @@ async function generate(argv) {
72
78
73
79
// iterate through all files in folder
74
80
await asyncForEach ( files , async file => {
75
- if ( exclude && mm . contains ( `${ folder } /${ file } ` , exclude ) ) {
76
- console . log ( chalk . black . bgBlue . bold ( ' EXCLUDE ' ) , `${ folder } /${ file } ` ) ;
81
+ if ( exclude && mm . contains ( `${ chalk . dim ( folder ) } /${ file } ` , exclude ) ) {
82
+ console . log ( chalk . reset . inverse . bold . blueBright ( ' EXCLUDE ' ) , `${ chalk . dim ( folder ) } /${ chalk . bold ( file ) } ` ) ;
77
83
78
84
addToStatistics ( file , 'exclude' ) ;
79
85
return ;
@@ -100,9 +106,12 @@ async function generate(argv) {
100
106
}
101
107
102
108
addToStatistics ( file , 'success' , true ) ;
103
- } catch ( err ) {
104
- console . log ( err ) ;
105
- console . log ( chalk . yellow ( 'cannot create folder, because it already exists' ) , `${ folderPath } /${ file } ` ) ;
109
+ } catch ( error ) {
110
+ console . log ( error . message ) ;
111
+ console . log (
112
+ chalk . yellow ( 'cannot create folder, because it already exists' ) ,
113
+ `${ chalk . dim ( folderPath ) } /${ file } `
114
+ ) ;
106
115
107
116
addToStatistics ( file , 'error' , true ) ;
108
117
}
@@ -114,11 +123,7 @@ async function generate(argv) {
114
123
} ) ;
115
124
116
125
// read files from subfolder
117
- await readFiles (
118
- `${ folder } /${ file } ` ,
119
- depth + 1 ,
120
- tree . filter ( treeItem => file === treeItem . name ) [ 0 ] . children
121
- ) ;
126
+ await readFiles ( `${ folder } /${ file } ` , depth + 1 , tree . filter ( treeItem => file === treeItem . name ) [ 0 ] . children ) ;
122
127
}
123
128
// Else branch accessed when file is not a folder
124
129
else {
@@ -147,17 +152,15 @@ async function generate(argv) {
147
152
const isConfigExclude = error . message . includes ( 'no input files' ) ;
148
153
149
154
console . log (
150
- chalk . black . bgRed . bold ( isConfigExclude ? ' EXCLUDE BY CONFIG ' : ' ERROR ' ) ,
151
- `${ folder } /${ file } -> ${ folderPath } /${ fileName } .md`
155
+ chalk . reset . inverse . bold . red ( isConfigExclude ? ' EXCLUDE BY CONFIG ' : ' ERROR ' ) ,
156
+ `${ chalk . dim ( folder ) } /${ chalk . bold ( file ) } \u2192 ${ chalk . dim ( folderPath ) } /${ chalk . bold (
157
+ fileName + '.md'
158
+ ) } `
152
159
) ;
153
160
154
161
if ( ! isConfigExclude ) {
155
162
console . log ( error . message ) ;
156
163
157
- if ( process . env . CI || argv . ci ) {
158
- throw new Error ( error ) ;
159
- }
160
-
161
164
addToStatistics ( file , 'error' ) ;
162
165
}
163
166
}
@@ -166,7 +169,12 @@ async function generate(argv) {
166
169
if ( mdFileData ) {
167
170
const { frontmatter, attributes } = parseVuepressComment ( fileData ) ;
168
171
169
- console . log ( chalk . black . bgGreen . bold ( ' SUCCESS ' ) , `${ folder } /${ file } -> ${ folderPath } /${ fileName } .md` ) ;
172
+ console . log (
173
+ chalk . reset . inverse . bold . green ( ' SUCCESS ' ) ,
174
+ `${ chalk . dim ( folder ) } /${ chalk . bold ( file ) } \u2192 ${ chalk . dim ( folderPath ) } /${ chalk . bold (
175
+ fileName + '.md'
176
+ ) } `
177
+ ) ;
170
178
171
179
let fileContent = '---\n' ;
172
180
@@ -201,7 +209,12 @@ async function generate(argv) {
201
209
202
210
addToStatistics ( file , 'success' ) ;
203
211
} else {
204
- console . log ( chalk . black . bgYellow . bold ( ' EMPTY ' ) , `${ folder } /${ file } -> ${ folderPath } /${ fileName } .md` ) ;
212
+ console . log (
213
+ chalk . reset . inverse . bold . yellow ( ' EMPTY ' ) ,
214
+ `${ chalk . dim ( folder ) } /${ chalk . bold ( file ) } \u2192 ${ chalk . dim ( folderPath ) } /${ chalk . bold (
215
+ fileName + '.md'
216
+ ) } `
217
+ ) ;
205
218
206
219
addToStatistics ( file , 'empty' ) ;
207
220
}
@@ -214,11 +227,7 @@ async function generate(argv) {
214
227
if ( error . code === 'ENOENT' ) {
215
228
console . log ( 'cannot find source folder' ) ;
216
229
} else {
217
- console . log ( error ) ;
218
- }
219
-
220
- if ( process . env . CI || argv . ci ) {
221
- throw new Error ( error ) ;
230
+ console . log ( error . message ) ;
222
231
}
223
232
}
224
233
} ;
@@ -263,13 +272,13 @@ async function generate(argv) {
263
272
264
273
const resultTime = ( Math . abs ( startTime - + new Date ( ) ) / 1000 ) . toFixed ( 2 ) ;
265
274
266
- //
267
- const maxExtLength = Math . max . apply (
268
- null ,
269
- Object . keys ( statistics ) . map ( w => w . length )
270
- ) ;
275
+ // get longest type string
276
+ const maxExtLength = Math . max . apply ( null , Object . keys ( statistics ) . map ( w => w . length ) ) ;
271
277
272
278
console . log ( `\n${ Array ( maxExtLength + maxExtLength / 2 ) . join ( '-' ) } ` ) ;
279
+
280
+ const errorCount = Object . keys ( statistics ) . reduce ( ( b , c ) => b + statistics [ c ] . error , 0 ) ;
281
+ // iterate trough stats
273
282
Object . entries ( statistics )
274
283
. sort ( )
275
284
. forEach ( ( [ extension , types ] ) => {
@@ -286,7 +295,10 @@ async function generate(argv) {
286
295
) } | ${ content } - ${ total } total`
287
296
) ;
288
297
} ) ;
298
+
289
299
console . log ( `${ Array ( maxExtLength + maxExtLength / 2 ) . join ( '-' ) } \nTime: ${ resultTime } s\n` ) ;
300
+
301
+ process . exit ( errorCount ? 1 : 0 ) ;
290
302
} ) ;
291
303
}
292
304
0 commit comments