Update Firebase SDKs in an instant with this Node.js script

By Ron Royston

Nov 2018

Table of Contents

Introduction

Run this Node script on your development machine to fetch and concatenate the Firebase code in to a single file.

Once you have all the Firebase client-side code in a single file on your development machine, it's easy to then concatenate your own minified Firebase app code to it. Finally, you have a single, minified script file to serve to your users.

Setup and Installation

This script requires the node-fetch package. To install this package, run the following command in the same terminal as you run firebase-tools from. This will install the development side package on your machine only.

                
npm install node-fetch --save-dev
                
            

Node Script

The script below is run from same terminal as you run firebase-tools from.

                
const fs = require('fs');
const fetch = require('node-fetch');
var version = process.argv[2];
var filePath = 'source/js/firebase.js';
var totalBytes = 0;
var promisesArray = [];
//const buf = Buffer.from();
var sdks = [
// Firebase App is always required and must be first
'https://www.gstatic.com/firebasejs/' + version + '/firebase-app.js',
// Add additional services that you want to use
'https://www.gstatic.com/firebasejs/' + version + '/firebase-auth.js',
'https://www.gstatic.com/firebasejs/' + version + '/firebase-database.js',
'https://www.gstatic.com/firebasejs/' + version + '/firebase-firestore.js',
'https://www.gstatic.com/firebasejs/' + version + '/firebase-messaging.js',
'https://www.gstatic.com/firebasejs/' + version + '/firebase-functions.js',
'https://www.gstatic.com/firebasejs/' + version + '/firebase-storage.js'
];

// delete existing local copy of SDK
fs.unlink(filePath, (err) => {
    if (err) {
        update();
    } else {
        update();
    }
});

for (var sdk of sdks){
    promisesArray.push(fetch(sdk));
}

function update(){
    return Promise.all(promisesArray).then(async function (fetchResponses) {
        for (var res of fetchResponses){
            const dest = fs.createWriteStream(filePath,{flags:'a'});
            totalBytes += Number(res.headers.get('content-length'));
            await new Promise((resolve, reject) => {
                res.body.pipe(dest);
                res.body.on("error", (err) => {
                    reject(err);
                });
                dest.on("finish", function() {
                    resolve();
                });
            });         
        }
        if(Math.round(totalBytes/1000) < 12){
            console.log('Warning. Saved ' + Math.round(totalBytes/1000) + ' kB. Double check that version exists');
        } else {
            console.log('Success. Saved ' + Math.round(totalBytes/1000) + ' kB.');
        }
    });
}

                
            

Usage

Run the Node script and pass the version as an argument. For example, node updateFirebase.js 5.5.9.

Troubleshooting

If you run into problems be sure you have installed the node-fetch module.

Dialog Title

Cell Phone Number

Your order ID is . The grand total is , or bitcoin.

Delete location?





A password reset link will be emailed to you.




New users register here for an email authenticated account.

Forget your password? Click here to have a new one emailed to you.

Find a bug? Experience an error? How can we do better? We appreciate your feedback.

email
facebook
github
google
twitter

Delete order? This action cannot be undone.

Delete order? This action cannot be undone.