11const _ = require ( 'underscore' ) ;
22const colors = require ( 'colors' ) ;
3+ const prompt = require ( 'prompt' ) ;
34const spinner = require ( 'node-spinner' ) ( ) ;
45const cursor = require ( 'cli-cursor' ) ;
56const progress = require ( 'progress' ) ;
@@ -38,19 +39,53 @@ class cli {
3839 // return '🍺';
3940 }
4041
42+ /**
43+ * ask
44+ * @param message
45+ * @returns {Promise }
46+ */
47+ static ask ( message ) {
48+ return new Promise ( ( resolve , reject ) => {
49+ prompt . start ( ) ;
50+
51+ let question = {
52+ name : 'yesno' ,
53+ message : message ,
54+ validator : / y [ e s ] * | n [ o ] ? / ,
55+ warning : 'Must respond yes or no' ,
56+ default : 'yes'
57+ } ;
58+
59+ prompt . get ( question , function ( error , result ) {
60+ if ( error || result . yesno === 'no' || result . yesno === 'n' ) return reject ( error ) ;
61+
62+ resolve ( ) ;
63+ } ) ;
64+ } ) ;
65+ }
66+
67+ /**
68+ * print
69+ * @param string
70+ */
71+ static out ( string ) {
72+ if ( cli . quiet ) return ;
73+ process . stdout . write ( string ) ;
74+ }
75+
4176 /**
4277 * print done message
4378 */
4479 static done ( ) {
45- console . log ( `\n${ cli . party } Finished!` . green ) ;
80+ cli . out ( `\n${ cli . party } Finished!\n ` . green ) ;
4681 }
4782
4883 /**
4984 * print a warning
5085 * @param message
5186 */
5287 static warn ( message ) {
53- console . log ( ` Warning: ${ message } ` . bgWhite . black ) ;
88+ cli . out ( ` Warning: ${ message } ` . bgWhite . black ) ;
5489 }
5590
5691 /**
@@ -62,6 +97,8 @@ class cli {
6297 static bar ( message , total ) {
6398 cli . resolve ( false ) ;
6499
100+ if ( cli . quiet ) return cli . promise ( ) ;
101+
65102 this . active = {
66103 started : new Date ( ) ,
67104 message : `\r${ message } ... ` . bold . grey ,
@@ -72,8 +109,8 @@ class cli {
72109 renderThrottle : 100
73110 } ) ,
74111 interval : setInterval ( ( ) => {
75- if ( ! this . active . bar || this . active . bar . complete ) return clearInterval ( this . active . interval ) ;
76- this . tick ( 0 ) ;
112+ if ( ! cli . active . bar || cli . active . bar . complete ) return clearInterval ( cli . active . interval ) ;
113+ cli . tick ( 0 ) ;
77114 } , 1000 )
78115 } ;
79116
@@ -86,19 +123,19 @@ class cli {
86123 * @param amount
87124 */
88125 static tick ( amount = 0 ) {
89- if ( ! this . active . bar || ! this . active . started ) return ;
126+ if ( ! cli . active . bar || ! cli . active . started ) return ;
90127
91128 let left ;
92129
93- if ( this . active . bar . curr > 0 ) {
94- let elapsed = Math . ceil ( ( new Date ( ) - this . active . started ) / 1000 ) ;
95- left = ( ( elapsed / this . active . bar . curr ) * ( this . active . bar . total - this . active . bar . curr ) ) / 60 ;
130+ if ( cli . active . bar . curr > 0 ) {
131+ let elapsed = Math . ceil ( ( new Date ( ) - cli . active . started ) / 1000 ) ;
132+ left = ( ( elapsed / cli . active . bar . curr ) * ( cli . active . bar . total - cli . active . bar . curr ) ) / 60 ;
96133 left = left < 1 ? `<1` : Math . ceil ( left ) ;
97134 } else {
98135 left = 0 ;
99136 }
100137
101- this . active . bar . tick ( amount , {
138+ cli . active . bar . tick ( amount , {
102139 minutes : left
103140 } ) ;
104141 }
@@ -107,7 +144,7 @@ class cli {
107144 * advance an existing bar
108145 */
109146 static advance ( ) {
110- this . tick ( 1 ) ;
147+ cli . tick ( 1 ) ;
111148 }
112149
113150 /**
@@ -120,7 +157,7 @@ class cli {
120157
121158 this . active = { message : `\r${ message } ... ` . bold . grey } ;
122159 this . active . interval = setInterval ( ( ) => {
123- process . stdout . write ( this . active . message + spinner . next ( ) . bold . blue ) ;
160+ cli . out ( cli . active . message + spinner . next ( ) . bold . blue ) ;
124161 } , 100 ) ;
125162
126163 return cli . promise ( ) ;
@@ -132,7 +169,7 @@ class cli {
132169 */
133170 static mark ( ) {
134171 cli . resolve ( ) ;
135- process . stdout . write ( `${ this . active . message } ` + `✓\n` . green ) ;
172+ cli . out ( `${ cli . active . message } ` + `✓\n` . green ) ;
136173
137174 return cli . promise ( ) ;
138175 }
@@ -145,7 +182,7 @@ class cli {
145182 */
146183 static x ( message = false , error = false ) {
147184 cli . resolve ( ) ;
148- process . stdout . write ( `${ this . active . message } ` + `✗\n` . red ) ;
185+ cli . out ( `${ cli . active . message } ` + `✗\n` . red ) ;
149186
150187 if ( message ) cli . error ( message , error ) ;
151188 return cli . promise ( ) ;
@@ -156,7 +193,7 @@ class cli {
156193 */
157194 static resolve ( show = true ) {
158195 cursor . toggle ( show ) ;
159- if ( this . active && this . active . interval ) clearInterval ( this . active . interval ) ;
196+ if ( cli . active && cli . active . interval ) clearInterval ( cli . active . interval ) ;
160197 }
161198
162199 /**
@@ -168,7 +205,7 @@ class cli {
168205 static error ( message , error ) {
169206 cli . resolve ( ) ;
170207
171- console . log ( ` Error: ${ message } ` . bgRed . white ) ;
208+ cli . out ( ` Error: ${ message } ` . bgRed . white + '\n' ) ;
172209 if ( error ) console . log ( error ) ;
173210
174211 process . exit ( 1 ) ;
0 commit comments