11'use strict' ;
2- var _ = require ( 'underscore' ) ;
32
4- var chalk = require ( './chalk' ) ;
5- var cache = require ( './cache' ) ;
6- var config = require ( './config' ) ;
7- var h = require ( './helper' ) ;
8- var file = require ( './file' ) ;
9- var icon = require ( './icon' ) ;
10- var log = require ( './log' ) ;
11- var Plugin = require ( './plugin' ) ;
3+ const _ = require ( 'underscore' ) ;
4+ const chalk = require ( './chalk' ) ;
5+ const cache = require ( './cache' ) ;
6+ const config = require ( './config' ) ;
7+ const h = require ( './helper' ) ;
8+ const file = require ( './file' ) ;
9+ const icon = require ( './icon' ) ;
10+ const log = require ( './log' ) ;
11+ const Plugin = require ( './plugin' ) ;
1212
1313// We are expecting a tier configuration like:
1414// global config < local config < cli params
@@ -28,24 +28,22 @@ function initLogLevel() {
2828 log . init ( ) ;
2929
3030 let level = 'INFO' ;
31- if ( process . argv . indexOf ( '-v' ) >= 0 ) level = 'DEBUG' ;
32- if ( process . argv . indexOf ( '-vv' ) >= 0 ) level = 'TRACE' ;
31+ if ( process . argv . includes ( '-v' ) ) level = 'DEBUG' ;
32+ if ( process . argv . includes ( '-vv' ) ) level = 'TRACE' ;
3333
3434 // print HTTP details in TRACE
3535 if ( level === 'TRACE' ) {
3636 const request = require ( 'request' ) ;
3737 request . debug = true ;
3838
39- console . error = _ . wrap ( console . error , function ( func ) {
40- let args = Array . from ( arguments ) ;
41- args . shift ( ) ;
42-
43- // FIXME: hack HTTP request log, hope no one else use it...
44- if ( args . length > 0 && args [ 0 ] . indexOf ( 'REQUEST ' ) === 0 ) {
39+ // eslint-disable-next-line no-console
40+ console . error = _ . wrap ( console . error , ( func , ...args ) => {
41+ // FIXME: hack HTTP request log, hope no one else uses it...
42+ if ( args . length > 0 && args [ 0 ] . startsWith ( 'REQUEST ' ) ) {
4543 args = args . map ( ( x ) => h . printSafeHTTP ( x ) ) ;
46- log . trace . apply ( log , args ) ;
44+ log . trace ( ... args ) ;
4745 } else {
48- log . info . apply ( log , args ) ;
46+ log . info ( ... args ) ;
4947 }
5048 } ) ;
5149 }
@@ -55,39 +53,60 @@ function initLogLevel() {
5553
5654function initDir ( ) {
5755 file . init ( ) ;
58- file . mkdir ( file . homeDir ( ) )
56+ file . mkdir ( file . homeDir ( ) ) ;
5957}
6058
6159function initPlugins ( cb ) {
6260 if ( Plugin . init ( ) ) {
6361 Plugin . save ( ) ;
6462 return cb ( ) ;
6563 } else {
66- Plugin . installMissings ( function ( e ) {
64+ Plugin . installMissings ( ( e ) => {
6765 if ( e ) return cb ( e ) ;
6866 Plugin . init ( ) ;
6967 return cb ( ) ;
7068 } ) ;
7169 }
7270}
7371
74- var cli = { } ;
72+ const cli = { } ;
73+
74+ const commands = [
75+ require ( './commands/cache' ) ,
76+ require ( './commands/config' ) ,
77+ require ( './commands/list' ) ,
78+ require ( './commands/plugin' ) ,
79+ require ( './commands/session' ) ,
80+ require ( './commands/show' ) ,
81+ require ( './commands/star' ) ,
82+ require ( './commands/stat' ) ,
83+ require ( './commands/submission' ) ,
84+ require ( './commands/submit' ) ,
85+ require ( './commands/test' ) ,
86+ require ( './commands/user' ) ,
87+ require ( './commands/version' ) ,
88+ ] ;
7589
7690function runCommand ( ) {
77- var yargs = require ( 'yargs' ) ;
91+ const yargs = require ( 'yargs' ) ;
92+
7893 h . width = yargs . terminalWidth ( ) ;
79- yargs . commandDir ( 'commands' )
94+ commands . forEach ( ( command ) => yargs . command ( command ) ) ;
95+
96+ // eslint-disable-next-line no-unused-expressions
97+ yargs
8098 . completion ( )
8199 . help ( 'h' )
82100 . alias ( 'h' , 'help' )
83101 . version ( false )
84- . epilog ( 'Seek more help at https://skygragon.github.io/leetcode-cli/commands' )
85- . wrap ( Math . min ( h . width , 120 ) )
86- . argv ;
102+ . epilog (
103+ 'Seek more help at https://skygragon.github.io/leetcode-cli/commands'
104+ )
105+ . wrap ( Math . min ( h . width , 120 ) ) . argv ;
87106}
88107
89- cli . run = function ( ) {
90- process . stdout . on ( 'error' , function ( e ) {
108+ cli . run = function ( ) {
109+ process . stdout . on ( 'error' , ( e ) => {
91110 if ( e . code === 'EPIPE' ) process . exit ( ) ;
92111 } ) ;
93112
@@ -96,8 +115,8 @@ cli.run = function() {
96115 initColor ( ) ;
97116 initIcon ( ) ;
98117 initLogLevel ( ) ;
99- initDir ( )
100- initPlugins ( function ( e ) {
118+ initDir ( ) ;
119+ initPlugins ( ( e ) => {
101120 if ( e ) return log . fatal ( e ) ;
102121 cache . init ( ) ;
103122 runCommand ( ) ;
0 commit comments