@@ -13,44 +13,8 @@ var request = require('request'),
1313 auth = require ( './auth' ) ,
1414 api = require ( './api' ) ,
1515 util = require ( 'util' ) ,
16- chalk = require ( 'chalk' ) ;
17-
18- var supportedTemplates = {
19- angular : {
20- url : 'https://codeload.github.com/firebase/angularFire-seed/legacy.tar.gz/master' ,
21- settings : {
22- 'public' : 'app' ,
23- 'rules' : 'config/security-rules.json' ,
24- } ,
25- config : 'app/js/config.js' ,
26- configRegex : / h t t p s : \/ \/ I N S T A N C E \. f i r e b a s e i o \. c o m / ,
27- onComplete : function ( results ) {
28- this . _onComplete ( 'chat template' , results ) ;
29- }
30- } ,
31- chat : {
32- url : 'https://codeload.github.com/firebase/chat-seed/legacy.tar.gz/master' ,
33- settings : {
34- 'public' : 'public' ,
35- 'rules' : 'config/rules.json' ,
36- } ,
37- config : 'public/index.html' ,
38- configRegex : / h t t p s : \/ \/ I N S T A N C E \. f i r e b a s e i o \. c o m / ,
39- onComplete : function ( results ) {
40- this . _onComplete ( 'chat template' , results ) ;
41- }
42- } ,
43- _onComplete : function ( type , results ) {
44- console . log ( chalk . green ( 'Successfully added template' ) ) ;
45- console . log ( 'To deploy: %s then %s' , chalk . bold ( util . format ( 'cd %s/' , results . directory ) ) , chalk . bold ( 'firebase deploy' ) ) ;
46- //console.log('To deploy this app, run %s', chalk.bold(util.format('cd %s && firebase deploy', results.directory)));
47- } ,
48- _getTemplates : function ( ) {
49- return Object . keys ( this ) . filter ( function ( key ) {
50- return ! key . match ( / ^ _ / ) ;
51- } ) ;
52- }
53- } ;
16+ chalk = require ( 'chalk' ) ,
17+ _when = require ( 'when' ) ;
5418
5519var defaultSettings = {
5620 'public' : '.'
@@ -183,7 +147,9 @@ module.exports = {
183147 } ) ;
184148 } ,
185149 bootstrap : function ( argv ) {
186- auth . listFirebases ( ) . then ( function ( res ) {
150+ _when . join ( this . getTemplates ( ) , auth . listFirebases ( ) ) . done ( function ( resultSet ) {
151+ var supportedTemplates = resultSet [ 0 ] ,
152+ res = resultSet [ 1 ] ;
187153
188154 if ( res . firebases . length === 0 ) {
189155 console . log ( chalk . yellow ( 'You have no apps in your Firebase account' ) ) ;
@@ -193,9 +159,9 @@ module.exports = {
193159 }
194160
195161 // Firebase names always a subset of ^[0-9a-z-]*$ so safe to regex
162+ var templateList = Object . keys ( supportedTemplates ) ;
196163 var firebasePattern = new RegExp ( '^(' + res . firebases . join ( '|' ) + ')$' ) ;
197- var templatePattern = new RegExp ( '^(' +
198- supportedTemplates . _getTemplates ( ) . join ( '|' ) + ')$' ) ;
164+ var templatePattern = new RegExp ( '^(' + templateList . join ( '|' ) + ')$' ) ;
199165 if ( ! argv . firebase || ( typeof ( argv . firebase ) !== 'string' ) || ! argv . firebase . match ( firebasePattern ) ) {
200166 res . showFirebases ( ) ;
201167 }
@@ -220,7 +186,7 @@ module.exports = {
220186 console . log ( chalk . yellow ( '----------------------------------------------------' ) ) ;
221187 console . log ( chalk . yellow ( 'Available Templates' ) ) ;
222188 console . log ( chalk . yellow ( '----------------------------------------------------' ) ) ;
223- console . log ( supportedTemplates . _getTemplates ( ) . join ( '\n' ) ) ;
189+ console . log ( templateList . join ( '\n' ) ) ;
224190 console . log ( chalk . yellow ( '----------------------------------------------------' ) ) ;
225191 console . log ( 'Choose a template to help you get started with your app' ) ;
226192 }
@@ -272,7 +238,7 @@ module.exports = {
272238 var data = fs . readFileSync ( config , 'utf8' ) ,
273239 realtimeHost = api . realtimeUrl . replace ( / \/ \/ / , '//' + firebase + '.' ) ;
274240 var replaced = data . replace (
275- supportedTemplates [ results . template ] . configRegex ,
241+ new RegExp ( supportedTemplates [ results . template ] . configRegex ) ,
276242 realtimeHost
277243 ) ;
278244 fs . writeFileSync ( config , replaced ) ;
@@ -299,9 +265,9 @@ module.exports = {
299265 console . log ( chalk . red ( 'Filesystem Error' ) + ' - Could not save settings file' ) ;
300266 process . exit ( 1 ) ;
301267 }
302- if ( typeof supportedTemplates [ results . template ] . onComplete === 'function' ) {
303- supportedTemplates [ results . template ] . onComplete . call ( supportedTemplates , results ) ;
304- }
268+
269+ console . log ( chalk . green ( 'Successfully added template' ) ) ;
270+ console . log ( 'To deploy: %s then %s' , chalk . bold ( util . format ( 'cd %s/' , results . directory ) ) , chalk . bold ( 'firebase deploy' ) ) ;
305271 } ) ;
306272 } ) ;
307273 } , function ( error ) {
@@ -310,6 +276,14 @@ module.exports = {
310276 console . log ( chalk . red ( 'Login Error' ) ) ;
311277 process . exit ( 1 ) ;
312278 break ;
279+ case 'GET-TEMPLATES' :
280+ console . log ( chalk . red ( 'Bootstrapping Error: ' ) + 'Could not retrieve available templates.' ) ;
281+ process . exit ( 1 ) ;
282+ break ;
283+ case 'PARSE-TEMPLATES' :
284+ console . log ( chalk . red ( 'Bootstrapping Error: ' ) + 'Could not parse available templates.' ) ;
285+ process . exit ( 1 ) ;
286+ break ;
313287 default :
314288 console . log ( chalk . red ( 'Bootstrapping Error' ) ) ;
315289 process . exit ( 1 ) ;
@@ -460,5 +434,22 @@ module.exports = {
460434 process . exit ( 1 ) ;
461435 }
462436 open ( api . hostingUrl . replace ( / \/ \/ / , util . format ( '//%s.' , settings . firebase ) ) ) ;
437+ } ,
438+ getTemplates : function ( ) {
439+ return _when . promise ( function ( resolve , reject , notify ) {
440+ request ( 'https://firebase-public.firebaseio.com/cli-templates.json' , function ( error , response , body ) {
441+ if ( error ) {
442+ error . type = 'GET-TEMPLATES' ;
443+ return reject ( error ) ;
444+ }
445+
446+ try {
447+ resolve ( JSON . parse ( body ) ) ;
448+ } catch ( e ) {
449+ error . type = 'PARSE-TEMPLATES' ;
450+ return reject ( error ) ;
451+ }
452+ } ) ;
453+ } ) ;
463454 }
464455} ;
0 commit comments