fix: environmentalize

This commit is contained in:
cha0s 2024-02-01 14:55:13 -06:00
parent 3dba690c6d
commit dcf48dab95

View File

@ -15,40 +15,6 @@ const {
const debug = D('@flecks/build/build/build');
const debugSilly = debug.extend('silly');
function environmentalize(path) {
return path
// - `@flecks/core` -> `flecks_core`
.replace(/[^a-zA-Z0-9]/g, '_')
.replace(/_*(.*)_*/, '$1');
}
function environmentConfiguration(config) {
const keys = Object.keys(process.env);
Object.keys(config)
.sort((l, r) => (l < r ? 1 : -1))
.forEach((fleck) => {
const prefix = `FLECKS_ENV__${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;
}
module.exports = class Build extends Flecks {
aliased = {};
@ -73,7 +39,7 @@ module.exports = class Build extends Flecks {
static async buildRuntime(originalConfig, platforms, flecks = {}) {
const cleanConfig = JSON.parse(JSON.stringify(originalConfig));
// Dealias the config keys.
const dealiasedConfig = environmentConfiguration(
const dealiasedConfig = this.environmentConfiguration(
Object.fromEntries(
Object.entries(cleanConfig)
.map(([maybeAliasedPath, config]) => {
@ -116,6 +82,40 @@ 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,