UNPKG

1.7 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.firebaseConfig = exports.resetCache = void 0;
4const fs_1 = require("fs");
5const path = require("path");
6const logger = require("../logger");
7let cache = null;
8/**
9 * @internal
10 * @alpha
11 */
12function resetCache(newCache = null) {
13 cache = newCache;
14}
15exports.resetCache = resetCache;
16/**
17 * Get the fields you need to initialize a Firebase app
18 * @alpha
19 */
20function firebaseConfig() {
21 if (cache) {
22 return cache;
23 }
24 let env = process.env.FIREBASE_CONFIG;
25 if (env) {
26 // Firebase Tools will always use a JSON blob in prod, but docs
27 // explicitly state that the user can set the env to a file:
28 // https://firebase.google.com/docs/admin/setup#initialize-without-parameters
29 if (!env.startsWith("{")) {
30 env = (0, fs_1.readFileSync)(path.join(process.env.PWD, env)).toString("utf8");
31 }
32 cache = JSON.parse(env);
33 return cache;
34 }
35 if (process.env.GCLOUD_PROJECT) {
36 logger.warn("Warning, estimating Firebase Config based on GCLOUD_PROJECT. Initializing firebase-admin may fail");
37 cache = {
38 databaseURL: process.env.DATABASE_URL || `https://${process.env.GCLOUD_PROJECT}.firebaseio.com`,
39 storageBucket: process.env.STORAGE_BUCKET_URL || `${process.env.GCLOUD_PROJECT}.appspot.com`,
40 projectId: process.env.GCLOUD_PROJECT,
41 };
42 return cache;
43 }
44 else {
45 logger.warn("Warning, FIREBASE_CONFIG and GCLOUD_PROJECT environment variables are missing. Initializing firebase-admin will fail");
46 }
47 return null;
48}
49exports.firebaseConfig = firebaseConfig;