diff --git a/packages/build/build/build.js b/packages/build/build/build.js index 61b3b08..a9770be 100644 --- a/packages/build/build/build.js +++ b/packages/build/build/build.js @@ -86,40 +86,6 @@ module.exports = class Build extends Flecks { await this.invokeSequentialAsync('@flecks/build.config.alter', config, env, argv); } - static environmentalize(path) { - return path - // - `@flecks/core` -> `flecks_core` - .replace(/[^a-zA-Z0-9]/g, '_') - .replace(/_*(.*)_*/, '$1'); - } - - static environmentConfiguration(config) { - const keys = Object.keys(process.env); - Object.keys(config) - .sort((l, r) => (l < r ? 1 : -1)) - .forEach((fleck) => { - const prefix = `FLECKS_ENV__${this.environmentalize(fleck)}`; - keys - .filter((key) => key.startsWith(`${prefix}__`)) - .map((key) => { - debug('reading environment from %s...', key); - return [key.slice(prefix.length + 2), process.env[key]]; - }) - .map(([subkey, value]) => [subkey.split('_'), value]) - .forEach(([path, jsonOrString]) => { - try { - Flecks.set(config, [fleck, ...path], JSON.parse(jsonOrString)); - debug('read (%s) as JSON', jsonOrString); - } - catch (error) { - Flecks.set(config, [fleck, ...path], jsonOrString); - debug('read (%s) as string', jsonOrString); - } - }); - }); - return config; - } - static async from( { config: configParameter, diff --git a/packages/core/build/flecks.js b/packages/core/build/flecks.js index 9163e23..78a1509 100644 --- a/packages/core/build/flecks.js +++ b/packages/core/build/flecks.js @@ -219,6 +219,52 @@ class Flecks { this.flecks = {}; } + /** + * Sanitize a fleck configuration path to an environment variable-friendly name. + * + * @param {string} path The path to sanitize. + * @returns {string} + */ + static environmentalize(path) { + return path + // - `@flecks/core` -> `flecks_core` + .replace(/[^a-zA-Z0-9]/g, '_') + .replace(/_*(.*)_*/, '$1'); + } + + /** + * Override configuration through environment variables. + * + * @param {Object} config The configuration to override. + * @returns {Object} + */ + static environmentConfiguration(config) { + const keys = Object.keys(process.env); + Object.keys(config) + .sort((l, r) => (l < r ? 1 : -1)) + .forEach((fleck) => { + const prefix = `FLECKS_ENV__${this.environmentalize(fleck)}`; + keys + .filter((key) => key.startsWith(`${prefix}__`)) + .map((key) => { + debug('reading environment from %s...', key); + return [key.slice(prefix.length + 2), process.env[key]]; + }) + .map(([subkey, value]) => [subkey.split('_'), value]) + .forEach(([path, jsonOrString]) => { + try { + Flecks.set(config, [fleck, ...path], JSON.parse(jsonOrString)); + debug('read (%s) as JSON', jsonOrString); + } + catch (error) { + Flecks.set(config, [fleck, ...path], jsonOrString); + debug('read (%s) as string', jsonOrString); + } + }); + }); + return config; + } + /** * Lists all flecks implementing a hook, including platform-specific and elided variants. * diff --git a/packages/server/build/runtime.js b/packages/server/build/runtime.js index 4208ff1..bd123f8 100644 --- a/packages/server/build/runtime.js +++ b/packages/server/build/runtime.js @@ -16,11 +16,13 @@ async function runtimeModule(compilation, flecks) { /* eslint-disable indent */ bootstrappedConfig: JSON.stringify(flecks.invoke('@flecks/core.config')), config: (` - dealiasedConfig(${ - 'production' === compiler.options.mode - ? JSON.stringify(flecks.originalConfig) - : `require('${ymlPath}').default` - }) + Flecks.environmentConfiguration( + Flecks.dealiasedConfig(${ + 'production' === compiler.options.mode + ? JSON.stringify(flecks.originalConfig) + : `require('${ymlPath}').default` + }) + ) `), /* eslint-enable indent */ loadFlecks: [ @@ -54,7 +56,7 @@ async function runtimeModule(compilation, flecks) { .map(([key, value]) => `"${key}": ${value}`).join(', ') }}`; const source = [ - `const {dealiasedConfig} = {${flecks.constructor.dealiasedConfig.toString()}};`, + "const {Flecks} = require('@flecks/core');", "process.env.FLECKS_CORE_BUILD_TARGET = 'server';", `module.exports = (async () => (${runtimeString}))();`, ]; diff --git a/packages/server/src/entry.js b/packages/server/src/entry.js index af48be6..b38af0b 100644 --- a/packages/server/src/entry.js +++ b/packages/server/src/entry.js @@ -7,7 +7,7 @@ import {D, Flecks} from '@flecks/core'; (async () => { const runtime = await import('@flecks/server/runtime'); - const {loadFlecks, version} = runtime; + const {config, loadFlecks, version} = runtime; // eslint-disable-next-line no-console console.log(`flecks server v${version}`); try { @@ -20,7 +20,11 @@ import {D, Flecks} from '@flecks/core'; } const debug = D('@flecks/server/entry'); debug('starting server...'); - global.flecks = await Flecks.from({...runtime, flecks: await loadFlecks()}); + global.flecks = await Flecks.from({ + ...runtime, + config: Flecks.environmentConfiguration(config), + flecks: await loadFlecks(), + }); await global.flecks.invokeSequentialAsync('@flecks/server.up'); debug('up!'); })();