mirror of
https://github.com/zhigang1992/firebase-tools.git
synced 2026-01-12 17:22:36 +08:00
basic Firebase listing and creation
This commit is contained in:
14
bin/firebase
14
bin/firebase
@@ -2,8 +2,14 @@
|
||||
|
||||
var optimist = require('optimist'),
|
||||
argv = optimist.usage('Usage: firebase').argv,
|
||||
prompt = require('prompt'),
|
||||
Auth = require('../lib/auth'),
|
||||
App = require('../lib/app');
|
||||
App = require('../lib/app'),
|
||||
Firebase = require('../lib/firebase');
|
||||
|
||||
prompt.override = argv;
|
||||
prompt.message = '';
|
||||
prompt.delimiter = '';
|
||||
|
||||
if (argv._.length === 0) {
|
||||
|
||||
@@ -25,6 +31,12 @@ if (argv._.length === 0) {
|
||||
case 'app':
|
||||
new App();
|
||||
break;
|
||||
case 'list':
|
||||
Firebase.list();
|
||||
break;
|
||||
case 'create':
|
||||
Firebase.create();
|
||||
break;
|
||||
default:
|
||||
// No route found
|
||||
optimist.showHelp();
|
||||
|
||||
28
lib/api.js
28
lib/api.js
@@ -5,7 +5,20 @@ var http = require('http'),
|
||||
|
||||
var Api = {
|
||||
_server: {},
|
||||
request: function(method, resource, data, callback) {
|
||||
request: function(method, resource, data, authenticate, callback) {
|
||||
|
||||
// Runtime fetch of Auth singleton to prevent circular module dependencies
|
||||
var Auth = require('./auth');
|
||||
|
||||
if ((typeof(data) === 'undefined') || !data) {
|
||||
var data = {};
|
||||
}
|
||||
|
||||
var getData = {};
|
||||
|
||||
if ((typeof(authenticate) !== 'undefined') && (authenticate)) {
|
||||
getData.token = Auth.token;
|
||||
}
|
||||
|
||||
var validMethods = ['GET', 'PUT', 'POST', 'DELETE'];
|
||||
|
||||
@@ -13,11 +26,20 @@ var Api = {
|
||||
method = 'GET';
|
||||
}
|
||||
|
||||
if (method === 'GET') {
|
||||
for (var attr in data) {
|
||||
if (data.hasOwnProperty(attr)) {
|
||||
getData[attr] = data[attr];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var getDataString = querystring.stringify(getData)
|
||||
var dataString = querystring.stringify(data);
|
||||
|
||||
if ((method === 'GET') && (dataString.length > 0)) {
|
||||
if (getDataString.length > 0) {
|
||||
var separator = resource.match(/\?/) ? '&' : '?';
|
||||
resource += separator + dataString;
|
||||
resource += separator + getDataString;
|
||||
}
|
||||
|
||||
var options = {
|
||||
|
||||
20
lib/auth.js
20
lib/auth.js
@@ -21,9 +21,8 @@ var Auth = {
|
||||
Api.request(
|
||||
'GET',
|
||||
'/token/validate',
|
||||
{
|
||||
token: this.token
|
||||
},
|
||||
{},
|
||||
true,
|
||||
function(statusCode, response) {
|
||||
if (typeof(callback) === 'function') {
|
||||
if (response.success) {
|
||||
@@ -46,7 +45,7 @@ var Auth = {
|
||||
} else {
|
||||
console.log('Email or password incorrect, please try again');
|
||||
}
|
||||
this.loginRequest(function(err, email, token) {
|
||||
this._loginRequest(function(err, email, token) {
|
||||
if (err) {
|
||||
that._attemptLogin(tries - 1, callback);
|
||||
} else {
|
||||
@@ -55,11 +54,15 @@ var Auth = {
|
||||
});
|
||||
} else {
|
||||
if (typeof(callback) === 'function') {
|
||||
setTimeout(callback, 0, new Error("Couldn't log in after " + this.maxRetries + ' tries'));
|
||||
setTimeout(
|
||||
callback,
|
||||
0,
|
||||
new Error("Couldn't log in after " + this.maxRetries + ' tries')
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
loginRequest: function(callback) {
|
||||
_loginRequest: function(callback) {
|
||||
var that = this,
|
||||
schema = {
|
||||
properties: {
|
||||
@@ -81,10 +84,6 @@ var Auth = {
|
||||
schema.properties.email.default = this.email;
|
||||
}
|
||||
|
||||
prompt.override = argv;
|
||||
prompt.message = '';
|
||||
prompt.delimiter = '';
|
||||
|
||||
prompt.get(schema, function(err, result) {
|
||||
if (err) {
|
||||
if (typeof(callback) === 'function') {
|
||||
@@ -106,6 +105,7 @@ var Auth = {
|
||||
'GET',
|
||||
'/account/login',
|
||||
data,
|
||||
false,
|
||||
this._handleLogin.bind(this, callback)
|
||||
);
|
||||
},
|
||||
|
||||
88
lib/firebase.js
Normal file
88
lib/firebase.js
Normal file
@@ -0,0 +1,88 @@
|
||||
var Table = require('easy-table'),
|
||||
prompt = require('prompt'),
|
||||
Auth = require('./auth'),
|
||||
Api = require('./api');
|
||||
|
||||
var Firebase = {
|
||||
list: function() {
|
||||
var that = this;
|
||||
Auth.requireLogin(function(err) {
|
||||
if (err) {
|
||||
console.log('Could not list Firebases');
|
||||
return;
|
||||
}
|
||||
that._list();
|
||||
});
|
||||
},
|
||||
_list: function() {
|
||||
Api.request('GET', '/account', {}, true, function(statusCode, response) {
|
||||
if (typeof(response.firebases) !== 'undefined') {
|
||||
var t = new Table;
|
||||
for (var firebase in response.firebases) {
|
||||
if (response.firebases.hasOwnProperty(firebase)) {
|
||||
t.cell('Name', firebase);
|
||||
t.cell('Url', 'https://' + firebase + '.firebaseio.com');
|
||||
t.cell('Role', response.firebases[firebase].role);
|
||||
t.newRow();
|
||||
}
|
||||
}
|
||||
console.log("\n" + t.toString());
|
||||
}
|
||||
});
|
||||
},
|
||||
create: function() {
|
||||
var that = this;
|
||||
Auth.requireLogin(function(err) {
|
||||
if (err) {
|
||||
console.log('Could not list Firebases');
|
||||
return;
|
||||
}
|
||||
that._create();
|
||||
});
|
||||
},
|
||||
_create: function() {
|
||||
var that = this,
|
||||
schema = {
|
||||
properties: {
|
||||
name: {
|
||||
description: 'New Firebase name',
|
||||
pattern: /^[a-z0-9]([a-z0-9-]*[a-z0-9]|)$/,
|
||||
message: 'Your Firebase name may only contain [a-z], [0-9], and' +
|
||||
' hyphen (-). It may not start or end with a hyphen.',
|
||||
required: true
|
||||
}
|
||||
}
|
||||
};
|
||||
prompt.get(schema, function(err, result) {
|
||||
if (err) {
|
||||
return;
|
||||
}
|
||||
Api.request(
|
||||
'POST',
|
||||
'/firebase/' + result.name,
|
||||
{},
|
||||
true,
|
||||
function(statusCode, response) {
|
||||
if (response.success) {
|
||||
console.log('\nFirebase \'' + result.name + '\' created');
|
||||
that._list();
|
||||
} else {
|
||||
var errorMessage = '';
|
||||
if (typeof(response.error) !== 'undefined') {
|
||||
errorMessage = ': ' + response.error;
|
||||
}
|
||||
console.log('Could not create Firebase' + errorMessage);
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
function initFirebase() {
|
||||
|
||||
}
|
||||
|
||||
initFirebase();
|
||||
|
||||
module.exports = Firebase;
|
||||
@@ -15,7 +15,9 @@
|
||||
"homepage": "https://github.com/firebase/firebase-cli",
|
||||
"dependencies": {
|
||||
"optimist": "0.6.x",
|
||||
"prompt": "0.2.x"
|
||||
"prompt": "0.2.x",
|
||||
"bitballoon": "0.1.x",
|
||||
"easy-table": "0.2.x"
|
||||
},
|
||||
"bin": {
|
||||
"firebase": "./bin/firebase"
|
||||
|
||||
Reference in New Issue
Block a user