Skip to content

Commit a66c1cd

Browse files
Merge pull request #17 from stackbithq/env-variable-fallback
Use value of environment variable as default value
2 parents 6f1bef8 + 5e59302 commit a66c1cd

File tree

2 files changed

+61
-9
lines changed

2 files changed

+61
-9
lines changed

lib/sourcebit.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,19 @@ class Sourcebit {
131131
const overrides = {};
132132

133133
Object.keys(optionsSchema).forEach(key => {
134-
if (optionsSchema[key].default !== undefined) {
135-
defaults[key] = optionsSchema[key].default;
134+
const option = optionsSchema[key];
135+
136+
// If the option defines an `env` property and there's an environment variable defined with that name, we'll use
137+
// that as the default value. Otherwise, the default value will be the one defined by the `default` property, if
138+
// one is set.
139+
if (option.env && process.env[option.env] !== undefined) {
140+
defaults[key] = process.env[option.env];
141+
} else if (option.default !== undefined) {
142+
defaults[key] = option.default;
136143
}
137144

138-
if (
139-
typeof optionsSchema[key].runtimeParameter === 'string' &&
140-
this.runtimeParameters[optionsSchema[key].runtimeParameter] !== undefined
141-
) {
142-
overrides[key] = this.runtimeParameters[optionsSchema[key].runtimeParameter];
145+
if (typeof option.runtimeParameter === 'string' && this.runtimeParameters[option.runtimeParameter] !== undefined) {
146+
overrides[key] = this.runtimeParameters[option.runtimeParameter];
143147
}
144148
});
145149

lib/sourcebit.test.js

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ const mockOra = {
3131

3232
jest.mock('ora', () => jest.fn(() => mockOra));
3333

34+
process.env.FORBIDDEN_ELEMENT = 'Cadmium';
35+
3436
const Sourcebit = require('./sourcebit');
3537

3638
beforeEach(() => {
@@ -89,9 +91,8 @@ describe('`bootstrapAll()`', () => {
8991
await sourcebit.bootstrapAll();
9092
});
9193

92-
test('passes an `options` block plugins', async () => {
94+
test('passes an `options` block to plugins', async () => {
9395
const mockPluginBase = {
94-
name: 'sourcebit-mock-plugin1',
9596
options: {
9697
forbiddenElement: {
9798
default: 'Arsenic'
@@ -104,20 +105,23 @@ describe('`bootstrapAll()`', () => {
104105
};
105106
const mockPlugin1 = {
106107
...mockPluginBase,
108+
name: 'sourcebit-mock-plugin1',
107109
bootstrap: jest.fn(({ options }) => {
108110
expect(options.forbiddenElement).toEqual(mockPluginBase.options.forbiddenElement.default);
109111
expect(options.requiredElement).toEqual(mockPluginBase.options.requiredElement.default);
110112
})
111113
};
112114
const mockPlugin2 = {
113115
...mockPluginBase,
116+
name: 'sourcebit-mock-plugin2',
114117
bootstrap: jest.fn(({ options }) => {
115118
expect(options.forbiddenElement).toEqual('Cadmium');
116119
expect(options.requiredElement).toEqual('Calcium');
117120
})
118121
};
119122
const mockPlugin3 = {
120123
...mockPluginBase,
124+
name: 'sourcebit-mock-plugin3',
121125
bootstrap: jest.fn(({ options }) => {
122126
expect(options.forbiddenElement).toEqual(mockPluginBase.options.forbiddenElement.default);
123127
expect(options.requiredElement).toEqual('Magnesium');
@@ -154,6 +158,50 @@ describe('`bootstrapAll()`', () => {
154158
await sourcebit3.bootstrapAll();
155159
});
156160

161+
test('uses the value of an environment variable named after the `env` option as a default value', async () => {
162+
const mockPluginBase = {
163+
options: {
164+
forbiddenElement: {
165+
env: 'FORBIDDEN_ELEMENT',
166+
default: 'Arsenic'
167+
}
168+
}
169+
};
170+
const mockPlugin1 = {
171+
...mockPluginBase,
172+
name: 'sourcebit-mock-plugin1',
173+
bootstrap: jest.fn(({ options }) => {
174+
expect(options.forbiddenElement).toEqual(process.env.FORBIDDEN_ELEMENT);
175+
})
176+
};
177+
const mockPlugin2 = {
178+
...mockPluginBase,
179+
name: 'sourcebit-mock-plugin2',
180+
bootstrap: jest.fn(({ options }) => {
181+
expect(options.forbiddenElement).toEqual('Einsteinium');
182+
})
183+
};
184+
const config1 = {
185+
plugins: [{ module: mockPlugin1 }]
186+
};
187+
const config2 = {
188+
plugins: [
189+
{
190+
module: mockPlugin2,
191+
options: { forbiddenElement: 'Einsteinium' }
192+
}
193+
]
194+
};
195+
const sourcebit1 = new Sourcebit();
196+
const sourcebit2 = new Sourcebit();
197+
198+
sourcebit1.loadPlugins(config1.plugins);
199+
sourcebit2.loadPlugins(config2.plugins);
200+
201+
await sourcebit1.bootstrapAll();
202+
await sourcebit2.bootstrapAll();
203+
});
204+
157205
test('passes a `getPluginContext` method to plugins', async () => {
158206
mockCache = {
159207
'sourcebit-mock-plugin1': {

0 commit comments

Comments
 (0)