export function firebaseConfig()

in src/config.ts [67:112]


export function firebaseConfig(): firebase.AppOptions | null {
  if (firebaseConfigCache) {
    return firebaseConfigCache;
  }

  let env = process.env.FIREBASE_CONFIG;
  if (env) {
    // Firebase Tools will always use a JSON blob in prod, but docs
    // explicitly state that the user can set the env to a file:
    // https://firebase.google.com/docs/admin/setup#initialize-without-parameters
    if (!env.startsWith('{')) {
      env = fs.readFileSync(path.join(process.env.PWD, env)).toString('utf8');
    }

    firebaseConfigCache = JSON.parse(env);
    return firebaseConfigCache;
  }

  // Could have Runtime Config with Firebase in it as an ENV value.
  try {
    const config = JSON.parse(process.env.CLOUD_RUNTIME_CONFIG);
    if (config.firebase) {
      firebaseConfigCache = config.firebase;
      return firebaseConfigCache;
    }
  } catch (e) {
    // Do nothing
  }

  // Could have Runtime Config with Firebase in it as an ENV location or default.
  try {
    const configPath =
      process.env.CLOUD_RUNTIME_CONFIG ||
      path.join(process.cwd(), '.runtimeconfig.json');
    const contents = fs.readFileSync(configPath);
    const config = JSON.parse(contents.toString('utf8'));
    if (config.firebase) {
      firebaseConfigCache = config.firebase;
      return firebaseConfigCache;
    }
  } catch (e) {
    // Do nothing
  }

  return null;
}