can I make a Cloud Function that only for local Firebase Emulators?

Viewed 182

I need to make a function that will be used only for Firebase emulator. I need to perform some actions to my Firestore emulators, to populate Firestore emulators using dummy data.

I write this code in my index.ts

exports.onlyForFirebaseSimulator = functions.https.onRequest(async (req, res) => {
  
  
});

I want to make this function available when I run firebase emulators:start , but it will not be deployed to production when I run firebase deploy.

I have to comment it out manually before deployment. I don't want to manually set it, because it is error prone.

2 Answers

If you are deploying with the Firebase CLI deploy command you could select only the functions to deploy to production by specifying the --only flag as explained on the relevant sections of the docs: e.g. if you have three functions named addMessage, makeUppercase, and onlyForFirebaseSimulator running the following command:

firebase deploy --only functions:addMessage,functions:makeUppercase

will not deploy your onlyForFirebaseSimulator to production.

I think I find the answer.

I am using group functions to organize multiple functions in my index.ts , I suggest you to read it first, otherwise you will be confused to read my index.ts . I am using Typescript/ES6, but the concept will be the same

for 'normal' deployment, in index.ts will be like this

// Firestore triggers
export * as users from "./firestore/triggers/users/users_triggers";
export * as users_attendedEvents from "./firestore/triggers/users/attendedEvents/attended_events_triggers";
export * as users_followers from "./firestore/triggers/users/followers/followers_triggers";

// Firebase Authentication triggers
export * as auth from "./firebase_authentication/triggers/firebase_auth_triggers";

and now I want to add functions that will not be deployed on production (when I run firebase deploy ) but will be available when emulators is running (when I run firebase emulators:start)

to detect whether the emulators is running or not, we can check its environment variable, process.env.FUNCTIONS_EMULATOR will be true. check this answer for more info

so my full index.ts will be like this

// Firestore triggers
export * as users from "./firestore/triggers/users/users_triggers";
export * as users_attendedEvents from "./firestore/triggers/users/attendedEvents/attended_events_triggers";
export * as users_followers from "./firestore/triggers/users/followers/followers_triggers";
    
// Firebase Authentication triggers
export * as auth from "./firebase_authentication/triggers/firebase_auth_triggers";

// The code below will be exported only if emulators are running, to avoid deployment to production server
const isRunningOnEmulator = (process.env.FUNCTIONS_EMULATOR && process.env.FIRESTORE_EMULATOR_HOST);
import * as emulator_functions from "./utilities/emulators/http_triggers/for_testing_via_firestore_emulators";
export const forEmulator = isRunningOnEmulator ? emulator_functions : undefined;
Related