Skip to content

Commit daa10ab

Browse files
authored
Use process.env.FIREBASE_PROJECT for Firebase config values if available (#51)
1 parent 93fc53a commit daa10ab

File tree

3 files changed

+53
-24
lines changed

3 files changed

+53
-24
lines changed

spec/config.spec.ts

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ describe('config()', () => {
3030
afterEach(() => {
3131
mockRequire.stopAll();
3232
unsetSingleton();
33+
delete process.env.FIREBASE_PROJECT;
34+
delete process.env.CLOUD_RUNTIME_CONFIG;
3335
});
3436

3537
it('loads config values from .runtimeconfig.json', () => {
@@ -39,25 +41,49 @@ describe('config()', () => {
3941
expect(loaded).to.have.property('foo','bar');
4042
});
4143

42-
it('loads config values from config.json', () => {
43-
mockRequire('../../../config.json', { foo: 'bar', firebase: {} });
44-
let loaded = config();
45-
expect(loaded).to.have.property('firebase');
46-
expect(loaded).to.have.property('foo','bar');
47-
});
48-
4944
it('injects a Firebase credential', () => {
5045
mockRequire('../../../.runtimeconfig.json', { firebase: {} });
5146
expect(config()).to.deep.property('firebase.credential');
5247
});
5348

54-
it('throws an error if config.json not present', () => {
49+
it('throws an error if .runtimeconfig.json not present', () => {
5550
mockRequire('../../../.runtimeconfig.json', 'does-not-exist');
5651
expect(config).to.throw('not available');
5752
});
5853

5954
it('throws an error if Firebase configs not present', () => {
6055
mockRequire('../../../.runtimeconfig.json', {});
61-
expect(config).to.throw('Firebase config variables are missing.');
56+
expect(config).to.throw('Firebase config variables are not available.');
57+
});
58+
59+
it('loads Firebase configs from FIREBASE_PROJECT env variable', () => {
60+
process.env.FIREBASE_PROJECT = JSON.stringify({
61+
databaseURL: 'foo@firebaseio.com',
62+
});
63+
let firebaseConfig = config().firebase;
64+
expect(firebaseConfig).to.have.property('databaseURL', 'foo@firebaseio.com');
65+
});
66+
67+
it('behaves well when both FIREBASE_PROJECT and .runtimeconfig.json present', () => {
68+
process.env.FIREBASE_PROJECT = JSON.stringify({
69+
databaseURL: 'foo@firebaseio.com',
70+
});
71+
mockRequire('../../../.runtimeconfig.json', {
72+
firebase: {
73+
databaseURL: 'foo@firebaseio.com',
74+
},
75+
foo: 'bar',
76+
});
77+
let loaded = config();
78+
expect(loaded.firebase).to.have.property('databaseURL', 'foo@firebaseio.com');
79+
expect(loaded).to.have.property('foo', 'bar');
80+
});
81+
82+
it('accepts alternative locations for config file', () => {
83+
process.env.CLOUD_RUNTIME_CONFIG = 'another.json';
84+
mockRequire('another.json', { foo: 'bar', firebase: {} });
85+
let loaded = config();
86+
expect(loaded).to.have.property('firebase');
87+
expect(loaded).to.have.property('foo','bar');
6288
});
6389
});

src/config.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,25 @@ export namespace config {
4242
}
4343

4444
function init (credential: firebase.credential.Credential) {
45-
let loaded: any;
45+
let loadedFromFile = {};
46+
let firebaseEnv = {};
47+
if (process.env.FIREBASE_PROJECT) {
48+
firebaseEnv = { firebase: JSON.parse(process.env.FIREBASE_PROJECT) };
49+
}
50+
4651
try {
47-
loaded = require('../../../.runtimeconfig.json');
52+
let path = process.env.CLOUD_RUNTIME_CONFIG || '../../../.runtimeconfig.json';
53+
loadedFromFile = require(path);
4854
} catch (e) {
49-
try {
50-
loaded = require('../../../config.json');
51-
} catch (e) {
52-
throw new Error(
53-
'functions.config() is not available. ' +
54-
'Please use the latest version of the Firebase CLI to deploy this function.'
55-
);
56-
}
55+
// Do nothing
5756
}
58-
if (!_.has(loaded, 'firebase')) {
59-
throw new Error('Firebase config variables are missing.');
57+
let merged= _.merge({}, loadedFromFile, firebaseEnv);
58+
59+
if (!_.has(merged, 'firebase')) {
60+
throw new Error('Firebase config variables are not available. ' +
61+
'Please use the latest version of the Firebase CLI to deploy this function.');
6062
}
6163

62-
_.set(loaded, 'firebase.credential', credential);
63-
config.singleton = loaded;
64+
_.set(merged, 'firebase.credential', credential);
65+
config.singleton = merged;
6466
}

src/providers/database.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ export function ref(path: string): RefBuilder {
6060
const normalized = normalizePath(path);
6161
const databaseURL = config().firebase.databaseURL;
6262
if (!databaseURL) {
63-
throw new Error('Missing expected config value firebase.databaseURL');
63+
throw new Error('Missing expected config value firebase.databaseURL, ' +
64+
'config is actually' + JSON.stringify(config()));
6465
}
6566
const match = databaseURL.match(databaseURLRegex);
6667
if (!match) {

0 commit comments

Comments
 (0)