@@ -24,32 +24,60 @@ var defaultSettings = {
2424
2525temp . track ( ) ;
2626
27- function getPrompt ( schema , onComplete , idx , results ) {
27+ function getPrompt ( argv , schema , onComplete , index , results ) {
2828 if ( ! Array . isArray ( schema ) ) {
2929 console . log ( chalk . red ( 'An error occurred' ) ) ;
3030 process . exit ( 1 ) ;
3131 }
3232 onComplete = typeof onComplete !== 'function' ? function ( ) { } : onComplete ;
33- idx = typeof idx !== 'number' ? 0 : idx ;
33+ index = typeof index !== 'number' ? 0 : index ;
3434 results = typeof results !== 'object' ? { } : results ;
35- var item = schema [ idx ] ;
35+ var item = schema [ index ] ;
36+ if ( argv . silent ) {
37+ if ( ! prompt . override [ item . name ] || ( item . pattern && ! item . pattern . test ( prompt . override [ item . name ] ) ) ) {
38+ console . log ( chalk . red ( 'Input Error' ) + ' - Not enough or invalid parameters specified while in silent mode' ) ;
39+ console . log ( 'Required ' + chalk . bold ( item . name ) + ' parameter missing or invalid' ) ;
40+ process . exit ( 1 ) ;
41+ }
42+ }
3643 if ( typeof item . beforeValue === 'function' ) {
3744 item . beforeValue ( results ) ;
3845 }
39- prompt . get ( schema [ idx ] , function ( error , result ) {
46+ prompt . get ( schema [ index ] , function ( error , result ) {
4047 if ( error ) {
4148 console . log ( chalk . red ( 'Input Error' ) ) ;
4249 process . exit ( 1 ) ;
4350 }
4451 results [ item . name ] = result [ item . name ] ;
45- if ( ++ idx < schema . length ) {
46- getPrompt ( schema , onComplete , idx , results ) ;
52+ if ( ++ index < schema . length ) {
53+ getPrompt ( argv , schema , onComplete , index , results ) ;
4754 } else {
4855 onComplete ( results ) ;
4956 }
5057 } ) ;
5158}
5259
60+ function getSettings ( argv ) {
61+ var settingsFile = path . resolve ( './firebase.json' ) ;
62+ var settingsJSON , settings ;
63+ if ( ! fs . existsSync ( settingsFile ) ) {
64+ console . log ( chalk . red ( 'Initialization Error' ) + ' - Directory not ' +
65+ 'initialized' ) ;
66+ process . exit ( 1 ) ;
67+ }
68+ try {
69+ settingsJSON = fs . readFileSync ( settingsFile ) ;
70+ settings = JSON . parse ( settingsJSON ) ;
71+ } catch ( err ) {
72+ console . log ( chalk . red ( 'Initialization Error' ) + ' - Could not read ' +
73+ 'firebase.json settings file' ) ;
74+ process . exit ( 1 ) ;
75+ }
76+ util . _extend ( settings , argv ) ;
77+
78+ return settings ;
79+ }
80+
5381module . exports = {
5482 init : function ( argv ) {
5583 auth . listFirebases ( argv ) . then ( function ( res ) {
@@ -110,7 +138,7 @@ module.exports = {
110138 }
111139 } ] ;
112140
113- getPrompt ( schema , function ( results ) {
141+ getPrompt ( argv , schema , function ( results ) {
114142 if ( path . relative ( '.' , results [ 'public' ] ) . match ( / ^ \. / ) ) {
115143 console . log ( chalk . red ( 'init cancelled - the public directory must be within the current working directory' ) ) ;
116144 process . exit ( 1 ) ;
@@ -200,7 +228,7 @@ module.exports = {
200228 type : 'string'
201229 } ] ;
202230
203- getPrompt ( schema , function ( results ) {
231+ getPrompt ( argv , schema , function ( results ) {
204232 var firebase = results . firebase ;
205233 var dir = firebase ;
206234 var projectDir = path . resolve ( dir ) ;
@@ -334,22 +362,7 @@ module.exports = {
334362 console . log ( chalk . red ( 'Login Error' ) ) ;
335363 process . exit ( 1 ) ;
336364 }
337- var settingsFile = path . resolve ( './firebase.json' ) ;
338- var settingsJSON , settings ;
339- if ( ! fs . existsSync ( settingsFile ) ) {
340- console . log ( chalk . red ( 'Initialization Error' ) + ' - Directory not ' +
341- 'initialized' ) ;
342- process . exit ( 1 ) ;
343- }
344- try {
345- settingsJSON = fs . readFileSync ( settingsFile ) ;
346- settings = JSON . parse ( settingsJSON ) ;
347- } catch ( err ) {
348- console . log ( chalk . red ( 'Initialization Error' ) + ' - Could not read ' +
349- 'firebase.json settings file' ) ;
350- process . exit ( 1 ) ;
351- }
352- util . _extend ( settings , argv ) ;
365+ var settings = getSettings ( argv ) ;
353366 if ( typeof ( settings . firebase ) !== 'string' ) {
354367 console . log ( chalk . red ( 'Initialization Error' ) + ' - Could not read ' +
355368 'firebase.json settings file' ) ;
@@ -440,7 +453,7 @@ module.exports = {
440453 message = argv . message ;
441454 }
442455
443- upload . send ( settings . firebase , settings [ 'public' ] , settings . ignore || defaultSettings . ignore , directoryRef . name ( ) , message , function ( err , directory ) {
456+ upload . send ( settings . firebase , settings [ 'public' ] , settings . ignore , directoryRef . name ( ) , message , function ( err , directory ) {
444457 if ( err ) {
445458 console . log ( chalk . red ( 'Deploy Error' ) + ' - Couldn\'t upload app' ) ;
446459 console . log ( err ) ;
@@ -451,22 +464,83 @@ module.exports = {
451464 } ) ;
452465 } ) ;
453466 } ,
467+ deleteSite : function ( argv ) {
468+ auth . requireLogin ( argv , function ( err ) {
469+ if ( err ) {
470+ console . log ( chalk . red ( 'Login Error' ) ) ;
471+ process . exit ( 1 ) ;
472+ }
473+ var settings = getSettings ( argv ) ;
474+ if ( typeof ( settings . firebase ) !== 'string' ) {
475+ console . log ( chalk . red ( 'Initialization Error' ) + ' - Could not read ' +
476+ 'firebase.json settings file' ) ;
477+ process . exit ( 1 ) ;
478+ }
479+ auth . checkCanAccess ( settings . firebase , function ( err , tokens ) {
480+ if ( err ) {
481+ console . log ( chalk . red ( 'Permission Error' ) + ' - You do not have ' +
482+ 'permission to use this Firebase' ) ;
483+ process . exit ( 1 ) ;
484+ }
485+ var firebaseRef = new Firebase ( api . realtimeUrl . replace ( / \/ \/ / , '//firebase.' ) ) ;
486+ firebaseRef . auth ( tokens . firebaseToken , function ( error , result ) {
487+ if ( error ) {
488+ console . log ( 'Firebase authentication failed!' ) ;
489+ process . exit ( 1 ) ;
490+ }
491+ } ) ;
492+ var directoryRef = firebaseRef
493+ . child ( 'hosting/versions' )
494+ . child ( settings . firebase )
495+ . push ( ) ;
496+ var bar = null ;
497+ var total = 0 ;
498+ directoryRef . on ( 'value' , function ( snapshot ) {
499+ var status = snapshot . child ( 'status' ) . val ( ) ;
500+ if ( status === 'deploying' ) {
501+ if ( ! bar && snapshot . hasChild ( 'fileCount' ) ) {
502+ total = snapshot . child ( 'fileCount' ) . val ( ) ;
503+ bar = new ProgressBar ( chalk . yellow ( 'progress: :percent' ) , {
504+ total : total
505+ } ) ;
506+ }
507+ if ( bar ) {
508+ var uploadedCount = snapshot . hasChild ( 'uploadedCount' ) ? snapshot . child ( 'uploadedCount' ) . val ( ) : 0 ;
509+ bar . update ( uploadedCount / total ) ;
510+ }
511+ } else if ( status === 'removed' ) {
512+ console . log ( chalk . green ( 'Sucessfully removed' ) ) ;
513+ process . exit ( 0 ) ;
514+ } else if ( status === 'failed' ) {
515+ if ( bar ) {
516+ bar . terminate ( ) ;
517+ }
518+ var message = chalk . red ( 'Deploy Failed' ) ;
519+ if ( snapshot . hasChild ( 'statusMessage' ) ) {
520+ message += ' - ' + snapshot . child ( 'statusMessage' ) . val ( ) ;
521+ }
522+ console . log ( message ) ;
523+ process . exit ( 1 ) ;
524+ }
525+ } ) ;
526+
527+ var message = null ;
528+ if ( argv . message && ( typeof ( argv . message ) === 'string' ) ) {
529+ message = argv . message ;
530+ }
531+
532+ upload . deleteSite ( settings . firebase , directoryRef . name ( ) , message , function ( err , directory ) {
533+ if ( err ) {
534+ console . log ( chalk . red ( 'Deploy Error' ) + ' - Couldn\'t upload app' ) ;
535+ console . log ( err ) ;
536+ process . exit ( 1 ) ;
537+ }
538+ } ) ;
539+ } ) ;
540+ } ) ;
541+ } ,
454542 open : function ( argv ) {
455- var settingsFile = path . resolve ( './firebase.json' ) ;
456- var settingsJSON , settings ;
457- if ( ! fs . existsSync ( settingsFile ) ) {
458- console . log ( chalk . red ( 'Initialization Error' ) + ' - Directory not ' +
459- 'initialized' ) ;
460- process . exit ( 1 ) ;
461- }
462- try {
463- settingsJSON = fs . readFileSync ( settingsFile ) ;
464- settings = JSON . parse ( settingsJSON ) ;
465- } catch ( err ) {
466- console . log ( chalk . red ( 'Initialization Error' ) + ' - Could not read ' +
467- 'firebase.json settings file' ) ;
468- process . exit ( 1 ) ;
469- }
543+ var settings = getSettings ( argv ) ;
470544 if ( typeof ( settings . firebase ) !== 'string' ) {
471545 console . log ( chalk . red ( 'Initialization Error' ) + ' - Could not read ' +
472546 'firebase.json settings file' ) ;
0 commit comments