55const pkg = require ( '../package' ) ;
66
77const args = process . argv . slice ( 2 ) ;
8- const arg_0 = args [ 0 ] ;
8+ let arg_0 = args [ 0 ] ;
99
1010if ( arg_0 && ! ! ~ [ '-v' , '--version' ] . indexOf ( arg_0 . toLowerCase ( ) ) ) {
1111 console . log ( pkg . version ) ;
1212 process . exit ( 0 ) ;
1313}
1414
15- const port = parseInt ( arg_0 , 10 ) ;
15+ const removeByValue = ( arr , val ) => {
16+ for ( var i = 0 ; i < arr . length ; i ++ ) {
17+ if ( arr [ i ] === val ) {
18+ arr . splice ( i , 1 ) ;
19+ break ;
20+ }
21+ }
22+ } ;
1623
1724const main = require ( '..' ) ;
1825
26+ const port = parseInt ( arg_0 , 10 ) ;
27+ const isVerbose = ! ! ~ args . indexOf ( '--verbose' ) ;
28+
29+ removeByValue ( args , '--verbose' ) ;
30+ arg_0 = args [ 0 ] ;
31+
1932if ( ! arg_0 ) {
2033 const random = Math . floor ( 9000 + Math . random ( ) * ( 65535 - 9000 ) ) ;
2134
2235 main ( random , ( err , port ) => {
23- if ( err ) {
24- console . log ( `get available port failed with ${ err } ` ) ;
36+ if ( isVerbose ) {
37+ if ( err ) {
38+ console . log ( `get available port failed with ${ err } ` ) ;
39+ }
40+ console . log ( `get available port ${ port } randomly` ) ;
41+ } else {
42+ console . log ( port || random ) ;
2543 }
26- console . log ( `get available port ${ port } randomly` ) ;
2744 } ) ;
2845} else if ( isNaN ( port ) ) {
2946 console . log ( ) ;
@@ -36,20 +53,16 @@ if (!arg_0) {
3653 console . log ( ' Options:' ) ;
3754 console . log ( ) ;
3855 console . log ( ' -v, --version output version and exit' ) ;
39- console . log ( ' -s, --silent output port without verbose log' ) ;
4056 console . log ( ' -h, --help output usage information' ) ;
57+ console . log ( ' --verbose output verbose log' ) ;
4158 console . log ( ) ;
4259 console . log ( ' Further help:' ) ;
4360 console . log ( ) ;
4461 console . log ( ` ${ pkg . homepage } ` ) ;
4562 console . log ( ) ;
4663} else {
47- const isSilent = ! ! ~ process . argv . indexOf ( '-s' ) || ! ! ~ process . argv . indexOf ( '--silent' ) ;
48-
4964 main ( port , ( err , _port ) => {
50- if ( isSilent ) {
51- console . log ( _port || port ) ;
52- } else {
65+ if ( isVerbose ) {
5366 if ( err ) {
5467 console . log ( `get available port failed with ${ err } ` ) ;
5568 }
@@ -59,6 +72,8 @@ if (!arg_0) {
5972 }
6073
6174 console . log ( `get available port ${ _port } ` ) ;
75+ } else {
76+ console . log ( _port || port ) ;
6277 }
6378 } ) ;
6479}
0 commit comments