Adding a firebase prompt to oh-my-zsh

March 17, 2019

firebase
shell
zsh

Working at Freetrade allows me to have more exposure to Cloud Functions for Firebase and I found out that it would be nice, at least for me, to know before hand which is the active project you would deploy to, before hitting firebase deploy.
While you can use firebase use, it is slow and I wanted something that show the active project constantly without me worried to run any commands.

Think of the git prompt but for the firebase active project.

I use oh-my-zsh and digging around I found out this nice tutorial and the relative github project but I wanted something that works for sub folders of a firebase project too and something that works with the robbyrussell theme (the default oh-my-zsh theme) out of the box.

So I created a plugin for oh-my-zsh, you can find it in my github.

Here is a screenshot of how your prompt should look like in case you are in a firebase project:

firebase_prompt_info_robbbyrussell

In the README you will find how to install and use the plugin.

Why I needed this?

When you work with Firebase Functions, you might have multiple projects linked to the same codebase.
Apart from production / staging environments, you might need multiple test projects.
In that case during the heat of the development you might deploy to the wrong test project.
While is possible to revert, I wanted some visual clue to help me out.
Even if you want to try out the samples:

firebase_functions_samples

You might want several projects, to try them at the same time:

firebase_functions_projects

And if you change the samples you might have different projects live (example: the original one and the modified one).
Or maybe you want to actually point different samples to the same project, to avoid creating multiple projects:

firebase_multiple_samples_same_projects

How it works?

First of all I read how the firebase cli determine if it is called from a directory, or a sub directory, which is a firebase project.
From the source code, it seems that it detects the project by checking if the current folder or its parent folders has the “firebase.json” file.
I translated that function into a shell function.
Then I used awk on the “firebase-tools.json” file to extract the active project.