import _ from 'lodash' ; import ProgressBar from 'progress' ; import fs from 'fs' ; import path from 'path' ; import lib from './lib' ; import { CLIOptions } from './types/cli' ; import { LibraryOptions } from './types/library' ; const USEFUL_COLORS = [ 'red' , 'green' , 'yellow' , 'blue' , 'magenta' , 'cyan' ] ; const colors = require ( 'colors/safe' ) ; const options = require ( 'optimist' ) . argv ; const files = options . _ ; let bar ; if ( options . version || options . v ) { const content = fs . readFileSync ( path . join ( __dirname , '/../package.json' ) ) . toString ( ) ; console . log ( JSON . parse ( content ) . version ) ; process . exit ( ) ; } if ( ! files . length ) { handler ( new Error ( 'No argument or file specified' ) ) ; process . exit ( ) ; } options . sortBy = options . sortBy || options . s || 1 ; if ( typeof options . sortBy !== 'number' ) { handler ( new Error ( '--sortBy must be a number' ) ) ; process . exit ( ) ; } const parsedOptions = { files , cols : [ 'count' , 'requested_resource' ] , prefixes : [ ] , sortBy : options . sortBy - 1 , limit : options . limit || 10 , ascending : options . a , } ; if ( ! _ . isEmpty ( options . start ) ) { options . start = String ( options . start ) ; if ( new Date ( options . start ) . toString ( ) === 'Invalid Date' ) { handler ( new Error ( 'Start date is invalid' ) ) ; process . exit ( ) ; } else { parsedOptions . start = new Date ( options . start ) ; } } if ( ! _ . isEmpty ( options . end ) ) { options . end = String ( options . end ) ; if ( new Date ( options . end ) . toString ( ) === 'Invalid Date' ) { handler ( new Error ( 'End date is invalid' ) ) ; process . exit ( ) ; } else { parsedOptions . end = new Date ( options . end ) ; } } _ . each ( options , function ( arg , key ) { let match = key . match ( /^p(refix){0,1}([0-9]+)$/ ) ; if ( match && ! isNaN ( Number ( match [ 2 ] ) ) ) { const index = Number ( match [ 2 ] ) - 1 ; if ( parsedOptions . prefixes ) { parsedOptions . prefixes [ index ] = arg ; } return ; } match = key . match ( /^c(ol){0,1}([0-9]+)$/ ) ; if ( match && ! isNaN ( Number ( match [ 2 ] ) ) ) { const index = Number ( match [ 2 ] ) - 1 ; if ( parsedOptions . cols ) { parsedOptions . cols [ index ] = arg ; } } } ) ; lib ( { ... parsedOptions , onProgress ( ) { bar . tick ( ) ; } , onStart ( filenames ) { bar = new ProgressBar ( ' processing [:bar] :percent :etas' , { complete : '=' , incomplete : ' ' , width : 30 , total : filenames . length , } ) ; } , } ) . then ( function ( logs ) { _ . each ( logs , ( log , i ) => { const coloredText = _ . map < string [ ] , string > ( log , ( l , index ) => { const colorName = USEFUL_COLORS [ index % USEFUL_COLORS . length ] ; return colors [ colorName ] ( l ) ; } ) . join ( colors . white ( ' - ' ) ) ; console . log ( colors . white ( String ( i + 1 ) ) + ' - ' + coloredText ) ; } ) ; } ) . catch ( handler ) ; function handler ( err ) { console . log ( `${ colors . red ( 'An error occured' ) }: ` , colors . cyan ( err . toString ( ) ) ) ; }