fix: environment configuration

This commit is contained in:
cha0s 2024-02-15 03:20:10 -06:00
parent 78a2ffb1c2
commit 0942827ed4
5 changed files with 16 additions and 9 deletions

View File

@ -75,6 +75,7 @@ module.exports = class Build extends Flecks {
});
const runtime = {
config: this.environmentConfiguration(
paths,
Object.fromEntries(paths.map((path) => [path, dealiasedConfig[path] || {}])),
),
flecks: Object.fromEntries(paths.map((path) => [

View File

@ -33,10 +33,13 @@ it('configures from environment', async () => {
env.FLECKS_ENV__one__foo = '{"boo": 2}';
env.FLECKS_ENV__two__bar = 'yo';
expect(
Build.environmentConfiguration({
one: {foo: {boo: 1}},
two: {bar: 'hi'},
}),
Build.environmentConfiguration(
['one', 'two'],
{
one: {foo: {boo: 1}},
two: {bar: 'hi'},
},
),
)
.to.deep.equal({
one: {foo: {boo: 2}},

View File

@ -277,12 +277,13 @@ class Flecks {
/**
* Override configuration through environment variables.
*
* @param {string[]} flecks The fleck paths.
* @param {Object} config The configuration to override.
* @returns {Object}
*/
static environmentConfiguration(config) {
static environmentConfiguration(flecks, config) {
const keys = Object.keys(process.env);
Object.keys(config)
flecks
.sort((l, r) => (l < r ? 1 : -1))
.forEach((fleck) => {
const prefix = `FLECKS_ENV__${this.environmentalize(fleck)}`;

View File

@ -17,11 +17,12 @@ async function runtimeModule(compilation, flecks) {
bootstrappedConfig: JSON.stringify(flecks.invoke('@flecks/core.config')),
config: (`
Flecks.environmentConfiguration(
${JSON.stringify(paths)},
Flecks.dealiasedConfig(${
'production' === compiler.options.mode
? JSON.stringify(flecks.originalConfig)
: `require('${ymlPath}').default`
})
}),
)
`),
/* eslint-enable indent */

View File

@ -20,10 +20,11 @@ import {D, Flecks} from '@flecks/core';
}
const debug = D('@flecks/server/entry');
debug('starting server...');
const flecks = await loadFlecks();
global.flecks = await Flecks.from({
...runtime,
config: Flecks.environmentConfiguration(config),
flecks: await loadFlecks(),
config: Flecks.environmentConfiguration(Object.keys(flecks), config),
flecks,
});
await global.flecks.invokeSequentialAsync('@flecks/server.up');
debug('up!');