Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expand the documentation on how to use firebase-tools as a module #322

Closed
SJAnderson opened this issue Apr 28, 2017 · 7 comments · Fixed by #2723
Closed

expand the documentation on how to use firebase-tools as a module #322

SJAnderson opened this issue Apr 28, 2017 · 7 comments · Fixed by #2723
Labels

Comments

@SJAnderson
Copy link

A lot of the commands you can do via CLI are very hard to figure out how to do programmatically when loading it as a module. Updated documentation on syntax, available commands, etc, would be helpful.

Questions I have (or had but soluved):

  • how to print a list of all keys using tools.functions.config.get
  • how to deploy functions
  • how to get a list of all functions deployed

    note on this one: had to really dig into cloudfunctions.js, api.js and auth.js. turns out this functionality is available inside the package, just not surfaced in firebase-tools. this is really helpful.

@gerhardcit
Copy link

gerhardcit commented Sep 16, 2017

There is a sample

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

But I had to install firebase-tools (without -g) for that to work.

Tried something like this


client.database.get('/app-settings/test').then((data) => {
    console.log(data);
}).catch(function (err) {
    console.log(err);
});

due to the lack of documentation but got this error

TypeError: Cannot create property 'project' on string '/app-settings/test'
    at Command._prepare (/Users/someone/Documents/test/node_modules/firebase-tools/lib/command.js:104:19)

A proper set of samples of using this will be great please.

@ahaverty
Copy link

I'm struggling to get anything working here too.
@SJAnderson Can you post some of your examples here? Particularly the config get (and set if you have it) 🙏

@jesperp
Copy link

jesperp commented May 12, 2019

I struggled with this until I dug into the source. So, until we have docs for this I think this works:

  • Arguments are passed in to the function as usual
  • Options (--foo bar) need to be passed in as part of an options object
  • The options object must be passed in as the last argument
  • You always merge global options with the command options
  • The global options are the same as command line but with cwd additionally
  • Commands that have a colon are run as client.<command>.<subcommand>

Examples:

firebase target hosting --project foo
==>
client.target("hosting", { project: foo})

firebase deploy --only hosting  --token $FIREBASE_TOKEN
==>
client.deploy({ only: 'hosting', token: process.env.FIREBASE_TOKEN })

firebase target:apply mytype mytarget
==>
client.target.apply('mytype', 'mytarget')     // No options added!

Also, I don't think you need to add a token option in node code if you have have a process.env.FIREBASE_TOKEN, it loads automatically. HTH

Read more in this file and commands directory:
https://github.com/firebase/firebase-tools/blob/master/src/commands/index.js

@samtstern
Copy link
Contributor

@jesperp thanks for your clear and concise explanation! Even as someone who "knows" the answer to this, I will refer to your answer when I forget until we get the docs up.

@danielpox
Copy link

I'm struggling to get anything working here too.
@SJAnderson Can you post some of your examples here? Particularly the config get (and set if you have it) 🙏

I’m having errors with config:set. Can’t seem to figure out their expected syntax for environment variables.

I would expect firebase functions:config:set my_var="foo bar" to be written as something like firebase.functions.config.set('my_var="foo bar"'). But it fails to parse it, saying I didn’t provide a key=value pair, which I obviously did.

After debugging I found it loops though the argument string, letter by letter instead of as an array. So it takes the “m” of “my_var” and complains. I’ve tried passing an array instead like so: firebase.functions.config.set(['my_var="foo bar"']), but then it complains that I must provide at least one key=value pair, since it expects strings as arguments.

@samtstern
Copy link
Contributor

samtstern commented Jan 5, 2021

@danielpox hmmm I have some scripts which use the syntax you mentioned with no problems, for example:
https://github.com/firebase/oss-bot/blob/master/functions/src/scripts/deploy-config.ts#L86

So I do:

const firebase = require('firebase-tools');

firebase.functions.config.set(["foo.bar=abc", "foo.baz=xyz"], { project: 'my-project-id})

And it all works. If you're still having this issue could you please put together a simple reproduction and open a new issue on this repo?

@danielpox
Copy link

@samtstern I see. I tried running the CLI instead, and it turns out

  1. I had the wrong key syntax, since it’s necessary to include a dot like so: my_namespace.my_var="foo bar", and
  2. I didn’t include the project id as an option either.

I found an option to run the CLI directly instead of as a module, as part of an npm script, but good to understand how the module syntax works. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants