Issue running cloud functions code locally using cloud functions shell - firebase

I am trying to test my functions locally using cloud functions shell. I was successful in making the shell thing work for my code. I see that this doesn't require my code to be deployed to the cloud. But whenever I run the function through shell it's working fine but it is using the deployed code, not the local code(I am checking this by using console statements as shown in sample code). I am not able to invoke local code unless I deploy.
Also, in my cloud functions, I am using the onCreate method for a real-time database and writing back to the same real-time database. When I test locally using the shell, I input data files for the function and write back to the real-time database. So I am actually trying to write code and run it locally to write to a real-time database on the cloud. Is this achievable using shell without deploying functions?
My sample function looks like this:
export const myCloudFunction = functions.database.instance(getDatabaseIdentifier()).ref(PATH).onCreate(async (snapshot, context) => {
console.log('local code invoked')
// or
console.log('deployed code invoked')
});

I figured it out since I am using typescript I need to transpile my code to javascript before I run the cloud functions shell.
The reason I thought it's invoking the deployed code is obvious as it is not actually invoking the deployed code but invoking the locally transpiled code which gets generated while I deploy to cloud. Now all I needed to do is transpile my code using the below command in my functions folder before I run the cloud functions shell.
// run this command in your functions folder
'npm run-script build'
This build generates transpiled javascript code in the 'lib' folder. Now we can run the below command to invoke the shell.
firebase functions:shell
Now we can emulate the local non deployed cloud functions and test them locally.
Check this medium post for detailed explanation:
https://medium.com/#moki298/test-your-firebase-cloud-functions-locally-using-cloud-functions-shell-32c821f8a5ce

Related

Firebase emulator for Cloud Functions not updating the code

I'm deploying the following cloud function using firebase deploy --only functions :
export const testFunction = functions.https.onCall((data, context) => {
return {"data": "hello"};
});
and when call it from the client app using the code
var testFunction = firebase.functions().httpsCallable("testFunction");
testFunction().then((result) => {
// Read result of the Cloud Function.
this.data = result.data;
});
it works as expected.
Now, I want to continue developing the function testing it on the local emulator so, following the documentation, I added this line to the web app code (before the function)
firebase.functions().useEmulator("localhost", 5001); // for local simulator
and I run the local emulator with:
firebase emulators:start --only functions
If I run the client app now, I correctly see the call going through the local emulator instead of the remote cloud function.
Problem: If modify the code the local function doesn't get updated. I need to run firebase deploy again in order to see the change in the response. How can I just deploy locally?
This stackoverflow solves the issue.
Solution in short:
After every change in your code, run npm run build to recomplie the code.
To auto-compile for every change in your code.
Change "build": "tsc" to "build": "tsc -w" in your package.json
file, and run the emulator in an another terminal window.
You can use the functions shell as described in this documentation here in order to deploy, run and test functions locally. I find the documentation quite confusing, as it only mentions how to use .sh scripts to test locally.
firebase functions:shell
Note that if you use the shell, you don't need to run the emulators separately; the shell will run them for you. The shell will automatically update based on changes to the index.js functions file, but if you're using TypeScript you may have to rebuild after you make changes. This is typically done by going into the functions directory and running npm run build. You can also automate this as well, according to this answer, but I haven't tried this approach yet.

Is there a way to add/modify functions to a running firebase emulator?

I am using a firebase emulator to develop and test cloud functions. Every time I modify an existing function or when I want to add a new function, I essentially shut down the emulator, deploy the functions, and then restart the emulator. In this process, I lose all the data in the local firestore database (as a part of the emulator). Is there a way to deploy functions to incorporate modifications to existing functions as well as to include new functions without shutting down the emulator?
It seems to depend what you are deploying. There is a not in Firebase documentation:
Note: Code changes you make during an active session are automatically
reloaded by the emulator. If your code needs to be transpiled
(TypeScript, React) make sure to do so before running the emulator.
So if you generally you can run the emulator and when you change the code without stopping it, with exception of the languages mentioned in the note.

Download or sync Firebase Functions to local environment

I am working on a Firebase project that currently uses several Cloud Functions.
The thing is that I want to download or sync the Cloud Functions to the local environment on my laptop.
I mean some command using firebase-tools or another like:
git clone [project name]
git fetch [something]
Usually, we create some cloud functions using the Firebase Console, and I would like to have these functions locally to edit these when needed and deploy them again.
I know that firebase-tools have these two commands, but it is only for configurations:
functions:config:clone [options]
functions:config:get [options]
There's no provided solution for automatically copying or synchronizing functions that have already been deployed. You can certain get the source code for deployed functions, but the Firebase CLI will not automate that process for you.
Ideally, you will want to manage all of your functions from the same source control and CLI in order to deal with them all consistent. Editing functions from the console is primarily a convenience, not a proper deployment mechanism.

How to Run Admin functions on Firebase

I'm trying to create a function to delete all users from the Database and write it on index.ts file and deploy it. Now, how can I run it?? I don't want my clients to run it from their mobile app, I need some admin tool to run the management functions. When I see cloud functions on Firebase console there's no option to run the functions, just to view their logs.
Cloud Functions isn't for running one-time scripts. You should do that on your local machine. Cloud Functions is for running code that responds to HTTP requests, or events that occur within other products in your project.
You may want to read this article: https://firebase.googleblog.com/2017/03/how-to-schedule-cron-jobs-with-cloud.html - it suggests using App Engine cron job for that purpose. Relevant project on github: https://github.com/FirebaseExtended/functions-cron
Also there's an example on similar topic in Firebase docs: https://github.com/firebase/functions-samples/tree/master/delete-unused-accounts-cron

Is it possible to run a script from Firebase Functions Shell?

I've been using the Firebase Functions Shell (firebase experimental:functions:shell) and am finding it excellent for testing the firebase database and http listeners.
However it is a little annoying that I have to copy-paste test code in the shell.
Is there any way I can just write some js code in a file and then run the complete file with the Firebase shell? Without copying and pasting?
Yes, send your script the the shell's stdin.
$ firebase experimental:functions:shell < tests.js
Taken from this blog.
You may still have to terminate the shell with ctl-C. Please file a bug report if you would like to see that change.

Resources