1

有人可以解释一下如何firebase-tools用作 javascript 模块吗?我已经实现了一个命令行工具,我想在其中执行该功能data:push。问题是我没有收到文档中描述的承诺。

这是我到目前为止所拥有的:

module.exports = new Command('createLicense [command]')
  .description('creates a license for an organization')
  .option('--type <full|light>', 'the license type (full|light)')
  .option('--slots <number of slots>', 'the amount of free slots')
  .option('--orgaId <id>', 'the organization id')
  .option('-y, --confirm', 'pass this option to bypass confirmation prompt')
  .action(function(commandName) {
    var options = options || {};
    return prompt(options, [
      {
        type: 'list',
        name: 'licType',
        message: 'What kind of license type should be created?',
        default: 0,
        choices: ['full', 'light']
      },
      {
        type: 'input',
        name: 'slots',
        message: 'How many slots should the license serve:',
        validate: function ( value ) {
          if (!value || parseInt(value) === 0 || isNaN(parseInt(value))) return "Please enter a valid number > 0";
          else return true;
        }
      },
      {
        type: 'input',
        name: 'orgaId',
        message: "What's the organization Id?",
        validate: function ( value ) {
          if (!value || typeof(value) !== 'string') return "Please enter a GUID with format: `xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx`";
          else return true;
        }
      }
    ]).then(function() {
      logger.info('License type: ' + options.licType);
      logger.info('License slots: ' + options.slots);
      logger.info('Organization: ' + options.orgaId);
      var result = firebase.data.push({
        firebase: 'zxlkchjiuihsdasdslkhas',
        path: '/orga/' + options.orgaId + '/licenses/',
        token: process.env.FIREBASE_TOKEN,
        options: {data: {'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' : {
                         date       : moment().utc(),
                         no         : 'xyz',
                         nuser      : options.slots,
                         type       : options.licType,
                         validUntil : 1478735999}}}

      });
      result.then(function (resolve) {
        return logger.info('License created');
        process.exit(0);

      });
      result.then(function (reject) {
        logger.info('Error occured: ' + reject);
        process.exit(1);
      })
    });
  });

我如何提供pathoptions以及???datafirebase.data.push

4

1 回答 1

1

为了能够firebase-tools在自己的应用程序中将命令用作模块,您必须构建自己的options object并提供必要的参数。因此,如果要将数据推送到数据库,请执行以下操作:

var firebase = require('firebase-tools');
var data = JSON.stringify({date : "some data" });
var infile = null; //Either give a file or provide data in options! 
var path = '/<sub-path>'; //Path needs leading slash!
options.firebase = options.firebase || '<your-firebase-id>';
options.token = process.env.FIREBASE_TOKEN;
options.data = data;
var result = firebase.data.set(path, infile, options).then(function () {
  console.info('Successfully pushed data');
}).catch(function (err) {
  console.log('Error occured: ' + err);
});

所有命令都返回一个Promise对象,因此您可以使用then().

于 2015-11-25T05:58:22.663 回答