Skip to content

Commit 20bc3f2

Browse files
author
Chris Raynor
committed
Merge pull request #13 from firebase/more-templates
Remove hard-coded templates, store in firebase-public, update help, handle offline use
2 parents f5af2cf + 51d2dba commit 20bc3f2

File tree

2 files changed

+68
-59
lines changed

2 files changed

+68
-59
lines changed

lib/app.js

Lines changed: 37 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -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: /https:\/\/INSTANCE\.firebaseio\.com/,
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: /https:\/\/INSTANCE\.firebaseio\.com/,
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

5519
var 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
};

lib/help.js

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
var firebase = require('./firebase');
1+
var app = require('./app'),
2+
firebase = require('./firebase'),
3+
chalk = require('chalk');
24

35
module.exports = {
46
showHelp: function(command) {
@@ -67,7 +69,8 @@ module.exports = {
6769
' prompted to do so - see `firebase login --help` for more details.\n');
6870
break;
6971
case 'bootstrap':
70-
console.log('\n' +
72+
var helpOverview =
73+
'\n' +
7174
' firebase bootstrap\n' +
7275
' Creates a new Firebase powered app from a prebuilt template to quickly\n' +
7376
' get a project up and running. This creates a new folder and prompts\n' +
@@ -78,20 +81,35 @@ module.exports = {
7881
' -f, --firebase The Firebase to initalize the app with. The current user\n' +
7982
' must have access to this Firebase. This is used for the\n' +
8083
' subdomain of firebaseapp.com\n' +
81-
' -t, --template The name of the template to bootstrap the app with. The\n' +
82-
' currently available templates are:\n' +
83-
'\n' +
84-
' angular - https://github.com/firebase/angularFire-seed\n' +
85-
' The angularFire seed template\n' +
86-
'\n' +
87-
' chat - https://github.com/firebase/chat-seed\n' +
88-
' A very simple Firebase powered chat template\n' +
89-
'\n' +
84+
' -t, --template The name of the template to bootstrap the app with.\n';
85+
86+
var helpFooter = '\n' +
9087
' Downloads and unpacks the template into a folder named after the Firebase\n' +
9188
' with which it has been initialized. Creates a firebase.json file in the\n' +
9289
' directory with some pre-configured settings appropriate for the template.\n' +
9390
' If the user is not currently logged in, they are prompted to do so - see\n' +
94-
' `firebase login --help` for more details.\n');
91+
' `firebase login --help` for more details.\n';
92+
93+
app.getTemplates()
94+
.done(function(templates) {
95+
var helpTemplates = '\n The currently available templates are:\n';
96+
for (var key in templates) {
97+
var template = templates[key];
98+
helpTemplates += '\n ' + key;
99+
helpTemplates += (template.repoUrl) ? ' - ' + template.repoUrl + '\n' : '\n';
100+
if (template.description) {
101+
helpTemplates += ' ' + template.description;
102+
}
103+
helpTemplates += '\n';
104+
}
105+
106+
console.log(helpOverview + helpTemplates + helpFooter);
107+
}, function(error) {
108+
var errTemplates = '\n ';
109+
errTemplates += chalk.red('Error: ') + 'Could not retrieve available templates.\n';
110+
111+
console.log(helpOverview + errTemplates + helpFooter);
112+
});
95113
break;
96114
case 'deploy':
97115
console.log('\n' +
@@ -166,4 +184,4 @@ module.exports = {
166184
'Version ' + firebase.version + '\n' +
167185
'https://www.firebase.com\n');
168186
}
169-
}
187+
};

0 commit comments

Comments
 (0)