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 = { const runtime = {
config: this.environmentConfiguration( config: this.environmentConfiguration(
paths,
Object.fromEntries(paths.map((path) => [path, dealiasedConfig[path] || {}])), Object.fromEntries(paths.map((path) => [path, dealiasedConfig[path] || {}])),
), ),
flecks: Object.fromEntries(paths.map((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__one__foo = '{"boo": 2}';
env.FLECKS_ENV__two__bar = 'yo'; env.FLECKS_ENV__two__bar = 'yo';
expect( expect(
Build.environmentConfiguration({ Build.environmentConfiguration(
['one', 'two'],
{
one: {foo: {boo: 1}}, one: {foo: {boo: 1}},
two: {bar: 'hi'}, two: {bar: 'hi'},
}), },
),
) )
.to.deep.equal({ .to.deep.equal({
one: {foo: {boo: 2}}, one: {foo: {boo: 2}},

View File

@ -277,12 +277,13 @@ class Flecks {
/** /**
* Override configuration through environment variables. * Override configuration through environment variables.
* *
* @param {string[]} flecks The fleck paths.
* @param {Object} config The configuration to override. * @param {Object} config The configuration to override.
* @returns {Object} * @returns {Object}
*/ */
static environmentConfiguration(config) { static environmentConfiguration(flecks, config) {
const keys = Object.keys(process.env); const keys = Object.keys(process.env);
Object.keys(config) flecks
.sort((l, r) => (l < r ? 1 : -1)) .sort((l, r) => (l < r ? 1 : -1))
.forEach((fleck) => { .forEach((fleck) => {
const prefix = `FLECKS_ENV__${this.environmentalize(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')), bootstrappedConfig: JSON.stringify(flecks.invoke('@flecks/core.config')),
config: (` config: (`
Flecks.environmentConfiguration( Flecks.environmentConfiguration(
${JSON.stringify(paths)},
Flecks.dealiasedConfig(${ Flecks.dealiasedConfig(${
'production' === compiler.options.mode 'production' === compiler.options.mode
? JSON.stringify(flecks.originalConfig) ? JSON.stringify(flecks.originalConfig)
: `require('${ymlPath}').default` : `require('${ymlPath}').default`
}) }),
) )
`), `),
/* eslint-enable indent */ /* eslint-enable indent */

View File

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