Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 37 additions & 46 deletions lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,8 @@ var request = require('request'),
auth = require('./auth'),
api = require('./api'),
util = require('util'),
chalk = require('chalk');

var supportedTemplates = {
angular: {
url: 'https://codeload.github.com/firebase/angularFire-seed/legacy.tar.gz/master',
settings: {
'public': 'app',
'rules': 'config/security-rules.json',
},
config: 'app/js/config.js',
configRegex: /https:\/\/INSTANCE\.firebaseio\.com/,
onComplete: function(results) {
this._onComplete('chat template', results);
}
},
chat: {
url: 'https://codeload.github.com/firebase/chat-seed/legacy.tar.gz/master',
settings: {
'public': 'public',
'rules': 'config/rules.json',
},
config: 'public/index.html',
configRegex: /https:\/\/INSTANCE\.firebaseio\.com/,
onComplete: function(results) {
this._onComplete('chat template', results);
}
},
_onComplete: function(type, results) {
console.log(chalk.green('Successfully added template'));
console.log('To deploy: %s then %s', chalk.bold(util.format('cd %s/', results.directory)), chalk.bold('firebase deploy'));
//console.log('To deploy this app, run %s', chalk.bold(util.format('cd %s && firebase deploy', results.directory)));
},
_getTemplates: function() {
return Object.keys(this).filter(function(key) {
return !key.match(/^_/);
});
}
};
chalk = require('chalk'),
_when = require('when');

var defaultSettings = {
'public': '.'
Expand Down Expand Up @@ -183,7 +147,9 @@ module.exports = {
});
},
bootstrap: function(argv) {
auth.listFirebases().then(function(res) {
_when.join(this.getTemplates(), auth.listFirebases()).done(function(resultSet) {
var supportedTemplates = resultSet[0],
res = resultSet[1];

if (res.firebases.length === 0) {
console.log(chalk.yellow('You have no apps in your Firebase account'));
Expand All @@ -193,9 +159,9 @@ module.exports = {
}

// Firebase names always a subset of ^[0-9a-z-]*$ so safe to regex
var templateList = Object.keys(supportedTemplates);
var firebasePattern = new RegExp('^(' + res.firebases.join('|') + ')$');
var templatePattern = new RegExp('^(' +
supportedTemplates._getTemplates().join('|') + ')$');
var templatePattern = new RegExp('^(' + templateList.join('|') + ')$');
if (!argv.firebase || (typeof(argv.firebase) !== 'string') || !argv.firebase.match(firebasePattern)) {
res.showFirebases();
}
Expand All @@ -220,7 +186,7 @@ module.exports = {
console.log(chalk.yellow('----------------------------------------------------'));
console.log(chalk.yellow('Available Templates'));
console.log(chalk.yellow('----------------------------------------------------'));
console.log(supportedTemplates._getTemplates().join('\n'));
console.log(templateList.join('\n'));
console.log(chalk.yellow('----------------------------------------------------'));
console.log('Choose a template to help you get started with your app');
}
Expand Down Expand Up @@ -272,7 +238,7 @@ module.exports = {
var data = fs.readFileSync(config, 'utf8'),
realtimeHost = api.realtimeUrl.replace(/\/\//, '//' + firebase + '.');
var replaced = data.replace(
supportedTemplates[results.template].configRegex,
new RegExp(supportedTemplates[results.template].configRegex),
realtimeHost
);
fs.writeFileSync(config, replaced);
Expand All @@ -299,9 +265,9 @@ module.exports = {
console.log(chalk.red('Filesystem Error') + ' - Could not save settings file');
process.exit(1);
}
if (typeof supportedTemplates[results.template].onComplete === 'function') {
supportedTemplates[results.template].onComplete.call(supportedTemplates, results);
}

console.log(chalk.green('Successfully added template'));
console.log('To deploy: %s then %s', chalk.bold(util.format('cd %s/', results.directory)), chalk.bold('firebase deploy'));
});
});
}, function(error) {
Expand All @@ -310,6 +276,14 @@ module.exports = {
console.log(chalk.red('Login Error'));
process.exit(1);
break;
case 'GET-TEMPLATES':
console.log(chalk.red('Bootstrapping Error: ') + 'Could not retrieve available templates.');
process.exit(1);
break;
case 'PARSE-TEMPLATES':
console.log(chalk.red('Bootstrapping Error: ') + 'Could not parse available templates.');
process.exit(1);
break;
default:
console.log(chalk.red('Bootstrapping Error'));
process.exit(1);
Expand Down Expand Up @@ -460,5 +434,22 @@ module.exports = {
process.exit(1);
}
open(api.hostingUrl.replace(/\/\//, util.format('//%s.', settings.firebase)));
},
getTemplates: function() {
return _when.promise(function(resolve, reject, notify) {
request('https://firebase-public.firebaseio.com/cli-templates.json', function(error, response, body) {
if (error) {
error.type = 'GET-TEMPLATES';
return reject(error);
}

try {
resolve(JSON.parse(body));
} catch (e) {
error.type = 'PARSE-TEMPLATES';
return reject(error);
}
});
});
}
};
44 changes: 31 additions & 13 deletions lib/help.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var firebase = require('./firebase');
var app = require('./app'),
firebase = require('./firebase'),
chalk = require('chalk');

module.exports = {
showHelp: function(command) {
Expand Down Expand Up @@ -67,7 +69,8 @@ module.exports = {
' prompted to do so - see `firebase login --help` for more details.\n');
break;
case 'bootstrap':
console.log('\n' +
var helpOverview =
'\n' +
' firebase bootstrap\n' +
' Creates a new Firebase powered app from a prebuilt template to quickly\n' +
' get a project up and running. This creates a new folder and prompts\n' +
Expand All @@ -78,20 +81,35 @@ module.exports = {
' -f, --firebase The Firebase to initalize the app with. The current user\n' +
' must have access to this Firebase. This is used for the\n' +
' subdomain of firebaseapp.com\n' +
' -t, --template The name of the template to bootstrap the app with. The\n' +
' currently available templates are:\n' +
'\n' +
' angular - https://github.com/firebase/angularFire-seed\n' +
' The angularFire seed template\n' +
'\n' +
' chat - https://github.com/firebase/chat-seed\n' +
' A very simple Firebase powered chat template\n' +
'\n' +
' -t, --template The name of the template to bootstrap the app with.\n';

var helpFooter = '\n' +
' Downloads and unpacks the template into a folder named after the Firebase\n' +
' with which it has been initialized. Creates a firebase.json file in the\n' +
' directory with some pre-configured settings appropriate for the template.\n' +
' If the user is not currently logged in, they are prompted to do so - see\n' +
' `firebase login --help` for more details.\n');
' `firebase login --help` for more details.\n';

app.getTemplates()
.done(function(templates) {
var helpTemplates = '\n The currently available templates are:\n';
for (var key in templates) {
var template = templates[key];
helpTemplates += '\n ' + key;
helpTemplates += (template.repoUrl) ? ' - ' + template.repoUrl + '\n' : '\n';
if (template.description) {
helpTemplates += ' ' + template.description;
}
helpTemplates += '\n';
}

console.log(helpOverview + helpTemplates + helpFooter);
}, function(error) {
var errTemplates = '\n ';
errTemplates += chalk.red('Error: ') + 'Could not retrieve available templates.\n';

console.log(helpOverview + errTemplates + helpFooter);
});
break;
case 'deploy':
console.log('\n' +
Expand Down Expand Up @@ -166,4 +184,4 @@ module.exports = {
'Version ' + firebase.version + '\n' +
'https://www.firebase.com\n');
}
}
};