@@ -15,20 +15,26 @@ const _ = require('underscore');
1515const cliProgress = require ( 'cli-progress' ) ;
1616const colors = require ( 'ansi-colors' ) ;
1717const EventEmitter = require ( 'events' ) ;
18+ const { jsonToExcel, exportJsonToExcel } = require ( '@papb/json-excel' ) ;
1819
1920class Emitter extends EventEmitter { } ;
2021class Emitter2 extends EventEmitter { } ;
2122const parser_events = new Emitter ( ) ;
2223const tasks_events = new Emitter2 ( ) ;
2324
25+ const linux = ( process . platform === 'linux' ) ;
26+ const win32 = ( process . platform === 'win32' ) ;
27+
2428var Directories = [ ] ;
2529var Projects = [ ] ;
26- var Files = [ ] ;
30+ var newFiles = [ ] ;
31+ var fileGroups = [ ] ;
2732var Schema = { } ;
2833var Blocks = { } ;
2934var Outdated = { } ;
3035var Empty_pages = [ ] ;
3136var No_translation = [ ] ;
37+ var xlsx = [ ] ;
3238var words_count_total = 0 ;
3339var verbose = false ;
3440var recount_words = false ;
@@ -37,9 +43,11 @@ var empty_pages = false;
3743var check_translations = false ;
3844var translation_commiter = false ;
3945var new_pages = false ;
46+ var xls_table = false ;
4047var lang , lang_name , regexp , begin , end , begin_ts , end_ts ;
4148
4249const basedir = '../Superalgos' ;
50+ const xlsFileName = 'Superalgos-Docs.xlsx' ;
4351
4452const date = new Date ( ) ;
4553var m = date . getMonth ( ) + 1 ;
@@ -69,6 +77,7 @@ process.argv.forEach((arg, i) => {
6977 console . log ( '[ --new-pages=[begin]]\t If [end] is not specified, then up to the present.The date format is "YYYY-MM-DD".' ) ;
7078 console . log ( '\t\t\t\t Without parameters it is displayed for the current month.' ) ;
7179 console . log ( '\t\t\t\t Examples: node main --new-pages=2021.01.01:2022.01.07, node main --new-pages=2022.01.01' ) ;
80+ console . log ( '[-x, --xls-table]\t\t Create an Excel spreadsheet of the project documents' ) ;
7281 }
7382 process . exit ( ) ;
7483 break ;
@@ -161,14 +170,19 @@ process.argv.forEach((arg, i) => {
161170 new_pages = true ;
162171 break ;
163172
173+ case '-x' :
174+ case '--xls-table' :
175+ xls_table = true ;
176+ break ;
177+
164178 default :
165179 console . log ( `Invalid argument[${ i - 1 } ]: ${ arg } ` ) ;
166180 break ;
167181 }
168182 }
169183} ) ;
170184
171- if ( ! recount_words && ! check_outdated && ! check_translations && ! empty_pages && ! translation_commiter && ! new_pages )
185+ if ( ! recount_words && ! check_outdated && ! check_translations && ! empty_pages && ! translation_commiter && ! new_pages && ! xls_table )
172186 { console . log ( "No assignments. Use '-h' for help" ) ; process . exit ( ) ; }
173187
174188const bar = new cliProgress . SingleBar ( {
@@ -188,7 +202,7 @@ FileHound.create()
188202 . then ( directories => {
189203 console . log ( ) ;
190204 directories . forEach ( ( directory ) => {
191- Projects . push (
205+ fileGroups . push (
192206 FileHound . create ( )
193207 . path ( directory )
194208 . discard ( 'App-Schema' )
@@ -197,14 +211,13 @@ FileHound.create()
197211 ) ;
198212 } ) ;
199213
200- Promise . all ( Projects ) . then ( ( groups ) => {
214+ Promise . all ( fileGroups ) . then ( ( groups ) => {
201215 groups = _ . reject ( groups , function ( elem ) { return ! elem . length ; } ) ;
202216 groups . forEach ( ( group , gi ) => {
203217 group . forEach ( ( file , fi ) => {
204- let arr = file . split ( '\\' ) , project = arr [ 3 ] , category , type , block , translated = true ;
205- let isBlock ;
206-
207- switch ( arr [ 5 ] ) {
218+ let arr = ( win32 ) ? file . split ( '\\' ) : file . split ( '/' ) , project = arr [ 3 ] , categoryDirName = arr [ 5 ] , category , block , translated = true , isBlock , file_name = _ . last ( arr ) ;
219+
220+ switch ( categoryDirName ) {
208221 case 'Docs-Nodes' :
209222 category = 'Node' ;
210223 break ;
@@ -233,12 +246,15 @@ FileHound.create()
233246 console . warn ( `No category is assigned for --> ${ arr [ 5 ] } ` ) ;
234247 break ;
235248 }
249+ if ( ! Projects [ project ] ) Projects [ project ] = { } ;
250+ if ( ! Projects [ project ] [ categoryDirName ] ) Projects [ project ] [ categoryDirName ] = { category : category + 's' } ;
236251
237252 try {
238253 let type , topic ;
239254 let obj = JSON . parse ( fs . readFileSync ( file ) ) ;
240255
241256 type = obj . type ;
257+ Projects [ project ] [ categoryDirName ] [ file_name ] = { type : type } ;
242258
243259 if ( _ . has ( obj , 'topic' ) ) {
244260 topic = obj . topic ;
@@ -352,7 +368,7 @@ parser_events.on('end', (groups) => {
352368 group . forEach ( ( file , fi ) => {
353369 try {
354370 if ( recount_words || translation_commiter ) console . log ( file ) ;
355- let words_count = 0 , type , topic , definition ;
371+ let words_count = 0 , type , topic , definition , arr = ( win32 ) ? file . split ( '\\' ) : file . split ( '/' ) , categoryDirName = arr [ 5 ] , file_name = _ . last ( arr ) , project = arr [ 3 ] ;
356372 let obj = JSON . parse ( fs . readFileSync ( file ) ) ;
357373
358374 if ( translation_commiter || new_pages ) {
@@ -374,7 +390,7 @@ parser_events.on('end', (groups) => {
374390 if ( new_pages ) {
375391 let t = _ . first ( text ) , ts = Date . parse ( t . split ( '(' ) . pop ( ) . split ( ')' ) . shift ( ) . split ( ' +' ) . shift ( ) . slice ( - 19 ) . split ( ' ' ) . shift ( ) ) ;
376392 bar . increment ( ) ; bar . update ( { file : fn } ) ;
377- if ( begin_ts <= ts && ts <= end_ts ) { Files . push ( file ) ; }
393+ if ( begin_ts <= ts && ts <= end_ts ) { newFiles . push ( file ) ; }
378394 if ( gi === ( groups . length - 1 ) && fi === ( group . length - 1 ) ) { setTimeout ( ( ) => { tasks_events . emit ( 'complete' ) ; } , 3000 ) ; }
379395 }
380396 }
@@ -462,6 +478,7 @@ parser_events.on('end', (groups) => {
462478 if ( verbose ) console . log ( obj ) ;
463479 console . log ( `words: ${ words_count } \n` ) ;
464480 }
481+ Projects [ project ] [ categoryDirName ] [ file_name ] . wordcount = words_count ;
465482 words_count_total += words_count ;
466483 }
467484 } catch ( err ) {
@@ -478,6 +495,7 @@ tasks_events.on('complete', () => {
478495
479496process . on ( 'beforeExit' , ( ) => {
480497 //console.log(Schema);
498+ //console.log(Projects);
481499 if ( recount_words ) console . log ( `Words Total: ${ words_count_total } ` ) ;
482500 if ( check_outdated && Object . keys ( Outdated ) . length ) {
483501 console . log ( 'Pages with outdated translations:' ) ;
@@ -508,10 +526,32 @@ process.on('beforeExit', () => {
508526 bar . stop ( ) ;
509527 console . log ( ) ;
510528 console . log ( `Pages added from ${ begin . d } -${ begin . m } -${ begin . y } to ${ end . d } -${ end . m } -${ end . y } ` ) ;
511- if ( Files . length ) Files . forEach ( page => console . log ( page ) ) ;
529+ if ( newFiles . length ) newFiles . forEach ( page => console . log ( page ) ) ;
512530 else console . log ( 'No new pages found' ) ;
513531 }
514532
533+ if ( xls_table ) {
534+ console . log ( ) ;
535+ for ( let project in Projects ) {
536+ xlsx . push ( { sheetName : project . split ( '-' ) . join ( ' ' ) , data : [ ] , formatAsTable : false } ) ;
537+ for ( let category in Projects [ project ] ) {
538+ let last = xlsx . length - 1 , categories = Projects [ project ] [ category ] , start_of_line ;
539+ for ( let item in categories ) {
540+ if ( item === 'category' ) xlsx [ last ] . data . push ( [ categories [ item ] , '' ] ) ;
541+ else xlsx [ last ] . data . push ( [ categories [ item ] . type , ( categories [ item ] . wordcount ) ? categories [ item ] . wordcount . toString ( ) : '' ] ) ;
542+ }
543+ xlsx [ last ] . data . push ( [ '' , '' ] ) ;
544+ }
545+ }
546+
547+ exportJsonToExcel ( xlsFileName , xlsx , { overwrite : true } )
548+ . then ( ( ) => {
549+ console . log ( `File ${ xlsFileName } exported to the main directory` ) ;
550+ process . exit ( ) ;
551+ } ) ;
552+
553+ }
554+
515555 console . log ( '\nDone!' ) ;
516556} ) ;
517557
0 commit comments