refactor: env

This commit is contained in:
cha0s 2024-01-04 16:11:48 -06:00
parent fd4c1ba21e
commit 935e0a0054
7 changed files with 17 additions and 46 deletions

View File

@ -118,7 +118,7 @@ export default (program, flecks) => {
{
env: {
...(target ? {FLECKS_CORE_BUILD_LIST: target} : {}),
...(hot ? {FLECKS_ENV_FLECKS_SERVER_hot: 'true'} : {}),
...(hot ? {FLECKS_ENV__flecks_server__hot: 'true'} : {}),
},
},
);

View File

@ -167,10 +167,9 @@ export default class ServerFlecks extends Flecks {
static environmentalize(path) {
return path
// - `@flecks/core` -> `FLECKS_CORE`
// - `@flecks/core` -> `flecks_core`
.replace(/[^a-zA-Z0-9]/g, '_')
.replace(/_*(.*)_*/, '$1')
.toUpperCase();
.replace(/_*(.*)_*/, '$1');
}
exts() {
@ -468,25 +467,17 @@ export default class ServerFlecks extends Flecks {
const keys = Object.keys(process.env);
const seen = [];
Object.keys(this.flecks)
// Reverse-sorting means e.g. `@flecks/core/server` comes before `@flecks/core`.
// We want to select the most specific match.
//
// `FLECKS_ENV_FLECKS_CORE_SERVER_variable` is ambiguous as it can equate to both:
// - `flecks.set('@flecks/core.SERVER.variable');`
// - `flecks.set('@flecks/core/server.variable');`
//
// The latter will take precedence.
.sort((l, r) => (l < r ? 1 : -1))
.forEach((fleck) => {
const prefix = `FLECKS_ENV_${this.constructor.environmentalize(fleck)}`;
const prefix = `FLECKS_ENV__${this.constructor.environmentalize(fleck)}`;
keys
.filter((key) => key.startsWith(`${prefix}_`) && -1 === seen.indexOf(key))
.filter((key) => key.startsWith(`${prefix}__`) && -1 === seen.indexOf(key))
.map((key) => {
seen.push(key);
debug('reading environment from %s...', key);
return [key, process.env[key]];
})
.map(([key, value]) => [key.slice(prefix.length + 1), value])
.map(([key, value]) => [key.slice(prefix.length + 2), value])
.map(([subkey, value]) => [subkey.split('_'), value])
.forEach(([path, jsonOrString]) => {
try {

View File

@ -1,7 +1,7 @@
{
"private": true,
"scripts": {
"build": "FLECKS_ENV_FLECKS_SERVER_start=0 npm run build:only",
"build": "FLECKS_ENV__flecks_server__start=0 npm run build:only",
"build:only": "flecks build",
"debug": "DEBUG=*,-babel*,-eslint* npm run dev",
"dev": "npm run -- build:only -h",

View File

@ -29,7 +29,7 @@ export const generateComposeConfig = async (flecks) => {
dockerfile: 'dist/Dockerfile',
},
environment: {
FLECKS_ENV_FLECKS_DOCKER_SERVER_enabled: 'false',
FLECKS_ENV__flecks_docker_server__enabled: 'false',
},
volumes: [
'../node_modules:/var/www/node_modules',
@ -46,7 +46,7 @@ export const generateComposeConfig = async (flecks) => {
services[key] = {image: config.image, environment: {}, ...config.extra};
});
return [
`FLECKS_ENV_${flecks.constructor.environmentalize(fleck)}`,
`FLECKS_ENV__${flecks.constructor.environmentalize(fleck)}`,
config,
];
}),
@ -61,7 +61,7 @@ export const generateComposeConfig = async (flecks) => {
Object.entries(environment || {})
.forEach(([key, value]) => {
const [realKey, realService] = 'app' === configService
? [`${prefix}_${key}`, appServiceName]
? [`${prefix}__${key}`, appServiceName]
: [key, configService];
services[realService].environment[realKey] = value;
});

View File

@ -13,7 +13,7 @@ export const hooks = {
* See: http://expressjs.com/en/resources/middleware/cookie-parser.html
*/
cookieSecret: (
'Set the FLECKS_ENV_FLECKS_USER_SESSION_SERVER_cookieSecret environment variable!'
'Set the FLECKS_ENV__flecks_user_session_server__cookieSecret environment variable!'
),
}),
'@flecks/web/server.request.route': (flecks) => {

View File

@ -402,9 +402,9 @@ services:
context: ..
dockerfile: dist/Dockerfile
environment:
FLECKS_ENV_FLECKS_DOCKER_SERVER_enabled: 'false'
FLECKS_ENV__flecks_docker_server__enabled: 'false'
// highlight-next-line
FLECKS_ENV_FLECKS_DB_SERVER_host: sequelize
FLECKS_ENV__flecks_db_server__host: sequelize
volumes:
- ../node_modules:/var/www/node_modules
// highlight-start

View File

@ -24,7 +24,7 @@ fleck name and the key. The template literal for such a transformation would loo
### Syntax
```javascript
`FLECKS_ENV_${Flecks.environmentalize(path)}_${key}`
`FLECKS_ENV__${Flecks.environmentalize(path)}__${key}`
```
:::note
@ -33,7 +33,7 @@ Notice the `environmentalize` transformation: `@flecks/core`'s `id` key is set u
following variable:
```bash
FLECKS_ENV_FLECKS_CORE_id=foobar
FLECKS_ENV__flecks_core__id=foobar
```
<details>
@ -43,32 +43,12 @@ FLECKS_ENV_FLECKS_CORE_id=foobar
return path
// - `@flecks/core` -> `FLECKS_CORE`
.replace(/[^a-zA-Z0-9]/g, '_')
.replace(/_*(.*)_*/, '$1')
.toUpperCase();
.replace(/_*(.*)_*/, '$1');
}
```
</details>
Also note that the key is still case-sensitive. This is because configuration keys are
Also note that the fleck path and key are still case-sensitive. This is because they are
user-defined.
:::
## Resolution Order
There is a possibility for ambiguity arising from the fact that flecks may be contained within a
subpath of another fleck. For instance:
```bash
FLECKS_ENV_FLECKS_CORE_SERVER_variable=something
```
Could be configuring a key at `@flecks/core/server.variable`, but it could also be configuring a
key at `@flecks/core.SERVER.variable`.
To resolve this ambiguity, candidate paths are reverse-sorted alphabetically, so that the longer
fleck path will be preferred.
In the above case, it is `@flecks/core/server.variable` which receives the configuration.