Skip to content

Commit 3d82509

Browse files
authored
Load Runtime Config from .runtimeconfig.json (#109)
1 parent d93a074 commit 3d82509

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
.npm
44
npm-debug.log
55
lib
6-
6+
yarn.lock
77
coverage
88
node_modules
99
typings

spec/apps.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import { expect } from 'chai';
2424
import { fakeConfig } from './support/helpers';
25-
import {apps as appsNamespace, apps} from '../src/apps';
25+
import { apps as appsNamespace } from '../src/apps';
2626
import * as firebase from 'firebase-admin';
2727
import * as _ from 'lodash';
2828
import * as sinon from 'sinon';

spec/config.spec.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ describe('config()', () => {
3232
unsetSingleton();
3333
});
3434

35+
it('loads config values from .runtimeconfig.json', () => {
36+
mockRequire('../../../.runtimeconfig.json', { foo: 'bar', firebase: {} });
37+
let loaded = config();
38+
expect(loaded).to.have.property('firebase');
39+
expect(loaded).to.have.property('foo','bar');
40+
});
41+
3542
it('loads config values from config.json', () => {
3643
mockRequire('../../../config.json', { foo: 'bar', firebase: {} });
3744
let loaded = config();
@@ -40,17 +47,17 @@ describe('config()', () => {
4047
});
4148

4249
it('injects a Firebase credential', () => {
43-
mockRequire('../../../config.json', { firebase: {} });
50+
mockRequire('../../../.runtimeconfig.json', { firebase: {} });
4451
expect(config()).to.deep.property('firebase.credential');
4552
});
4653

4754
it('throws an error if config.json not present', () => {
48-
mockRequire('../../../config.json', 'does-not-exist');
55+
mockRequire('../../../.runtimeconfig.json', 'does-not-exist');
4956
expect(config).to.throw('not available');
5057
});
5158

5259
it('throws an error if Firebase configs not present', () => {
53-
mockRequire('../../../config.json', {});
60+
mockRequire('../../../.runtimeconfig.json', {});
5461
expect(config).to.throw('Firebase config variables are missing.');
5562
});
5663
});

src/config.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,16 @@ export namespace config {
4444
function init (credential: firebase.credential.Credential) {
4545
let loaded: any;
4646
try {
47-
loaded = require('../../../config.json');
47+
loaded = require('../../../.runtimeconfig.json');
4848
} catch (e) {
49-
throw new Error ('functions.config() is not available. '
50-
+ 'Please use the Firebase CLI to deploy, or manually '
51-
+ 'add a config.json to your functions directory.');
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+
}
5257
}
5358
if (!_.has(loaded, 'firebase')) {
5459
throw new Error('Firebase config variables are missing.');

0 commit comments

Comments
 (0)