7

I'm trying to clean up my build system and I'd like to use the firebase-tools (Firebase-hosted docs) module in doing so. Documentation for using the package in the CLI is fine enough, but the documentation for using it as a node module is sparse (and diving through the source code of the package hasn't been straightforward, either).

At the time of this writing, here is the relevant documentation (it only appears in the GH repository):


Using as a Module

The Firebase CLI can also be used programmatically as a standard Node module. This can only be done on your machine, and cannot be done within Cloud Functions. Each command is exposed as a function that takes an options object and returns a Promise. For example:

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
});

As you can see, only two very basic examples are included, and there is no description of the various options that can be provided to each command.

For example, the CLI uses the --only tag (such as --only database if one only wants to push new rules). If I want to only deploy rules, do I include only: "database" in the options?

The output in the example does boast the "Rules have been deployed!" success message, but nothing about the options provided to deploy seem to indicate that this would only be pushing rules (rather than a full deploy without the --only option)!

Bonus question: what if I have different paths for my rules file vs. files that I would want hosted? I see only one cwd option provided in the example.

Some clearer documentation here would go a long way :)

MandM
  • 3,293
  • 4
  • 34
  • 56

2 Answers2

0

General Guidelines:

  • Use dotted notation for function access (e.g. database:get => client.database.get()
  • Pass positional parameters as function arguments
  • Pass global and command options in options object as the last function argument
  • All functions return a promise

Example:

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

Becomes:

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))
);
Mike
  • 2,429
  • 1
  • 27
  • 30
0

In case of target deploy you can use the option keys only or except.

You can specify a comma separated list of targets (e.g. "hosting,functions"), array is not accepted. For functions, can specify filters with colons to scope function deploys to only those functions (e.g. "functions:func1,functions:func2").

Valid targets are:

  • hosting: New releases of your Firebase Hosting sites,
  • database: Rules for Firebase Realtime Database,
  • firestore: Rules and Indexes for Cloud Firestore,
  • functions: New, updated, or existing Cloud Functions for Firebase,
  • storage: Rules for Cloud Storage for Firebase.

This is what I use in my gulpfile:

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

Token has to be generated as described here.

bvamos
  • 773
  • 7
  • 10