How to show your current Firebase project name on the command line prompt to prevent dangerous errors

Thang Minh Vu
BlockedUnblockFollowFollowing


Firebase

When working on a project with multiple stages (development, staging, production), developers use the command firebase use to switch between projects. It’s very easy to run a command on the production environment instead of the development. This is very dangerous.

Command to switch between firebase project

Note: You can always find the latest script at my GitHub repository.

Normally, developers only work on the development project. They only switch to production in case of checking or doing a hotfix. There have been a few times when I forgot to switch back to the development project. I accidentally changed the database without thinking that it could impact the actual users.

Digging into the firebase CLI, I found that it uses configstore to manage local configuration. All config is saved in a JSON file and reads easily. I created a small script which is intended to show the firebase project name on shell prompt.

How to set it up

Bash

Add the following script to the end of ~/.bash_profile:

Add script to ~/.bash_profile

Then run source ~/.bash_profile or open a new terminal window:

Firebase project name is displayed as dev-project, stage-project and prod-project

iTerm2 with oh-my-zsh

Oh-my-zsh is a popular open source framework for Zshell. I like it because it has many beautiful terminal themes and many useful plugins.

Here, I will make an example with the agnoster theme:

Edit ~/.oh-my-zsh/themes/agnoster.zsh-theme

Script for oh-my-zsh

and then add prompt_firebase to build_prompt functions:

Change build_promt function

For the final step, run source ~/.zshrc or open a new terminal window:

saas-cs-deploy-taguro-5c55e is displayed as last text in terminal prompt

I hope this can help you prevent the an unexpected (and bad) situation.

Note: You can always find the latest script at my GitHub repository.