使用firebase-tools作为节点模块并仅部署规则

时间:2017-10-23 19:10:54

标签: firebase firebase-tools

我正在尝试清理我的构建系统,并且我希望使用firebase-toolsFirebase-hosted docs)模块来执行此操作。在CLI中使用软件包的文档已经足够了,但是将其用作节点模块的文档很少(并且通过软件包的源代码也不是很简单)。

在撰写本文时,这里是相关文档(它只出现在GH库中):

用作模块

Firebase CLI也可以编程方式用作标准节点模块。这只能在您的计算机上完成,并且无法在云功能中完成。每个命令都作为一个函数公开,它接受一个options对象并返回一个Promise。例如:

var client = require('firebase-tools');
client.list().then(function(data) {
  console.log(data);
}).catch(function(err) {
  // handle error
});

client.deploy({
  project: 'myfirebase',
  token: process.env.FIREBASE_TOKEN,
  cwd: '/path/to/project/folder'
}).then(function() {
  console.log('Rules have been deployed!')
}).catch(function(err) {
  // handle error
});

如您所见,仅包含两个非常基本的示例,并且没有可以为每个命令提供的各种选项的描述。

例如,CLI使用--only标记(例如--only database,如果只想推送新规则)。如果我只想部署规则,是否在选项中包含only: "database"

示例中的输出夸耀"规则已经部署!"成功消息,但没有提供给deploy的选项似乎表明这只会推动规则(而不是没有--only选项的完整部署)!

奖金问题:如果我的规则文件与我希望托管的文件有不同的路径,该怎么办?我看到示例中只提供了一个cwd选项。

这里有一些更清晰的文档会有很长的路要走:)

2 个答案:

答案 0 :(得分:0)

一般准则:

  • 使用点分符号表示函数访问权限(例如,database:get => client.database.get()
  • 将位置参数作为函数参数传递
  • 在选项对象中传递全局和命令选项作为最后一个函数参数
  • 所有功能均返回承诺

示例:

firebase --project my-firebase-project database:get /user/setting --output /tmp/out.json

成为:

var util = require('util'),
  client = require('firebase-tools');

client.database.get('/user/setting', {
  project: 'my-firebase-project', // global option
  output: '/tmp/out.json',        // global option
  shallow: true                   // database:get option
}).then(
  x => console.log(util.inspect(x))
).catch(
  e => console.log(util.inspect(e))
);

答案 1 :(得分:0)

如果目标为deploy,则可以使用选项键onlyexcept

您可以指定用逗号分隔的目标列表(例如“主机,功能”),不接受数组。对于函数,可以指定带有冒号的过滤器以将函数部署范围仅限制到那些函数(例如“ functions:func1,functions:func2”)。

Valid targets are

  • 托管:您的Firebase托管网站的新版本,
  • 数据库:Firebase实时数据库的规则,
  • firestore:Cloud Firestore的规则和索引,
  • 功能:Firebase的新功能,更新的功能或现有的Cloud功能,
  • 存储:Firebase的云存储规则。

这是我在gulpfile中使用的:

const fbTools = require('firebase-tools');
function deployProdTask() {
  return fbTools.deploy({
    project: 'abc',
    only: 'functions',
    token: '123',
    force: true
  });
}

令牌必须按照here的说明生成。