@@ -28,17 +28,21 @@ readline.on('SIGINT', () => {
2828 readline . close ( ) ;
2929} ) ;
3030
31- const question = input => new Promise ( ( resolve ) => {
31+ const question = ( input ) => new Promise ( ( resolve ) => {
3232 readline . question ( input , ( answer ) => {
3333 resolve ( answer ) ;
3434 } ) ;
3535} ) ;
3636
37- const saveAuth = ( obj , filename , cb ) => {
37+ const saveAuth = ( obj , filename ) => new Promise ( ( resolve , reject ) => {
3838 const lock = ( obj ) => {
3939 cipher . lock ( cipher . k , obj , filename , ( err ) => {
40- process . stdout . write ( err || 'done!\n' ) ;
41- cb ( err ) ;
40+ if ( ! err ) {
41+ process . stdout . write ( err || 'done!\n' ) ;
42+ resolve ( ) ;
43+ } else {
44+ reject ( err ) ;
45+ }
4246 } ) ;
4347 } ;
4448 if ( fs . existsSync ( filename ) ) {
@@ -47,54 +51,109 @@ const saveAuth = (obj, filename, cb) => {
4751 res = Object . assign ( res , obj ) ;
4852 lock ( res ) ;
4953 } else {
50- cb ( err ) ;
54+ reject ( err ) ;
5155 }
5256 } ) ;
5357 } else {
5458 lock ( obj ) ;
5559 }
56- } ;
60+ } ) ;
5761
58- const updateRabbit = ( cb ) => {
62+ const updateRabbit = ( ) => new Promise ( ( resolve , reject ) => {
5963 question ( 'Update RabbitMQ account?[yes/no]' )
6064 . then ( ( answer ) => {
6165 answer = answer . toLowerCase ( ) ;
6266 if ( answer !== 'y' && answer !== 'yes' ) {
63- cb ( ) ;
67+ resolve ( ) ;
6468 return ;
6569 }
6670 question ( `(${ authBase } ) Enter username of rabbitmq: ` )
6771 . then ( ( username ) => {
6872 question ( `(${ authBase } ) Enter password of rabbitmq: ` )
6973 . then ( ( password ) => {
7074 mutableStdout . muted = false ;
71- saveAuth ( { rabbit : { username, password } } , authStore , cb ) ;
75+ saveAuth ( { rabbit : { username, password } } , authStore )
76+ . then ( resolve )
77+ . catch ( reject ) ;
7278 } ) ;
7379 mutableStdout . muted = true ;
7480 } ) ;
7581 } ) ;
76- } ;
82+ } ) ;
7783
78- const updateMongo = ( cb ) => {
84+ const updateMongo = ( ) => new Promise ( ( resolve , reject ) => {
7985 question ( 'Update MongoDB account?[yes/no]' )
8086 . then ( ( answer ) => {
8187 answer = answer . toLowerCase ( ) ;
8288 if ( answer !== 'y' && answer !== 'yes' ) {
83- cb ( ) ;
89+ resolve ( ) ;
8490 return ;
8591 }
8692 question ( `(${ authBase } ) Enter username of mongodb: ` )
8793 . then ( ( username ) => {
8894 question ( `(${ authBase } ) Enter password of mongodb: ` )
8995 . then ( ( password ) => {
9096 mutableStdout . muted = false ;
91- saveAuth ( { mongo : { username, password } } , authStore , cb ) ;
97+ saveAuth ( { mongo : { username, password } } , authStore )
98+ . then ( resolve )
99+ . catch ( reject ) ;
92100 } ) ;
93101 mutableStdout . muted = true ;
94102 } ) ;
95103 } ) ;
96- } ;
104+ } ) ;
97105
98- updateRabbit ( ( ) => {
99- updateMongo ( ( ) => readline . close ( ) )
106+ const updateInternal = ( ) => new Promise ( ( resolve , reject ) => {
107+ question ( 'Update internal passphrase?[yes/no]' )
108+ . then ( ( answer ) => {
109+ answer = answer . toLowerCase ( ) ;
110+ if ( answer !== 'y' && answer !== 'yes' ) {
111+ resolve ( ) ;
112+ return ;
113+ }
114+ question ( `(${ authBase } ) Enter internal passphrase: ` )
115+ . then ( ( passphrase ) => {
116+ mutableStdout . muted = false ;
117+ saveAuth ( { internalPass : passphrase } , authStore )
118+ . then ( resolve )
119+ . catch ( reject ) ;
120+ } ) ;
121+ mutableStdout . muted = true ;
122+ } ) ;
100123} ) ;
124+
125+ const options = { } ;
126+ const parseArgs = ( ) => {
127+ if ( process . argv . includes ( '--rabbitmq' ) ) {
128+ options . rabbit = true ;
129+ }
130+ if ( process . argv . includes ( '--mongodb' ) ) {
131+ options . mongo = true ;
132+ }
133+ if ( process . argv . includes ( '--internal' ) ) {
134+ options . internal = true ;
135+ }
136+ if ( Object . keys ( options ) . length === 0 ) {
137+ options . rabbit = true ;
138+ options . mongo = true ;
139+ }
140+ return Promise . resolve ( ) ;
141+ }
142+
143+ parseArgs ( )
144+ . then ( ( ) => {
145+ if ( options . rabbit ) {
146+ return updateRabbit ( ) ;
147+ }
148+ } )
149+ . then ( ( ) => {
150+ if ( options . mongo ) {
151+ return updateMongo ( ) ;
152+ }
153+ } )
154+ . then ( ( ) => {
155+ if ( options . internal ) {
156+ return updateInternal ( ) ;
157+ }
158+ } )
159+ . finally ( ( ) => readline . close ( ) ) ;
0 commit comments