11const path = require ( 'path' ) ;
22const fs = require ( 'fs-extra' ) ;
33
4- const { listDirectories, listFiles } = require ( './utils' ) ;
4+ const { listDirectories, listFiles, signale } = require ( './utils' ) ;
55const { rootDir } = require ( './constants' ) ;
6- const validate = {
7- js : require ( './js' ) ,
8- } ;
6+ const validators = require ( './validators' ) ;
97
108const categories = listDirectories ( rootDir ) . filter ( dir => dir !== 'node_modules' ) ;
11- let hasError = false ;
129for ( const category of categories ) {
1310 const categoryDir = path . resolve ( rootDir , category ) ;
1411 const algorithms = listDirectories ( categoryDir ) ;
@@ -17,16 +14,28 @@ for (const category of categories) {
1714 const files = listFiles ( algorithmDir ) ;
1815 for ( const file of files ) {
1916 const ext = file . split ( '.' ) . pop ( ) ;
20- const validator = validate [ ext ] ;
17+ const validator = validators [ ext ] ;
2118 if ( validator ) {
19+ const errors = [ ] ;
20+ const error = message => errors . push ( message ) ;
21+ const warns = [ ] ;
22+ const warn = message => warns . push ( message ) ;
2223 const filePath = path . resolve ( algorithmDir , file ) ;
2324 const content = fs . readFileSync ( filePath , 'utf-8' ) ;
24- if ( ! validator ( category , algorithm , file , content ) ) {
25- hasError = true ;
26- }
25+ validator ( error , warn , category , algorithm , file , content )
26+ . then ( ( ) => {
27+ if ( errors . length || warns . length ) {
28+ signale . log ( `${ category } /${ algorithm } /${ file } ` ) ;
29+ errors . forEach ( error => signale . error ( error ) ) ;
30+ warns . forEach ( error => signale . warn ( error ) ) ;
31+ signale . log ( ) ;
32+ }
33+ if ( errors . length ) {
34+ process . exitCode = 1 ;
35+ }
36+ } )
37+ . catch ( console . error ) ;
2738 }
2839 }
2940 }
3041}
31-
32- process . exit ( hasError ? 1 : 0 ) ;
0 commit comments