Skip to content

Instantly share code, notes, and snippets.

@cairin
Created January 31, 2019 11:17
Show Gist options
  • Save cairin/cf88cea63c8035dc12da97ec8909c7d1 to your computer and use it in GitHub Desktop.
Save cairin/cf88cea63c8035dc12da97ec8909c7d1 to your computer and use it in GitHub Desktop.
'use strict'
/**
* This file automagically writes the redirects into firebase.json for all the files in the api folder for the respective firebase functions.
* You won't need to change this file.
* Fursther instructions can be found here: https://codeburst.io/organizing-your-firebase-cloud-functions-67dc17b3b0da
*/
const glob = require('glob')
const camelCase = require('camelcase')
const fs = require('fs')
fs.readFile('firebase.json', 'utf8', (err, data) => {
if (err) {
console.error(err)
} else {
let firebaseObj = JSON.parse(data)
firebaseObj.hosting.rewrites = []
const files = glob.sync('./**/*.f.js', { cwd: __dirname, ignore: './node_modules/**' })
for (let f = 0, fl = files.length; f < fl; f++) {
const file = files[f]
const functionName = camelCase(file.slice(0, -5).split('/').join('_')) // Strip off '.f.js'
if (functionName.slice(0, 3) === 'api') {
firebaseObj.hosting.rewrites.push({ source: file.slice(1, -5), function: functionName })
}
}
fs.writeFile('firebase.json', JSON.stringify(firebaseObj), 'utf8', (err, data) => {
if (err) {
console.error(err)
} else {
console.log('success', data)
}
})
}})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment