config with envs
Massimo Melina committed
Sep 17, 2023 at 22:43 UTC
8a4da74fe8d18ee32eabd127d72f31e3d3e75415
3 files changed
+8
-5
README.md
+2
-1
@@ -143,9 +143,10 @@ If you have access to HFS' console, you can enter commands. Start with `help` to
143
Configuration can be done in several ways
144
- accessing the Admin-panel with your browser
145
- it will automatically open when you start HFS. Bookmark it. if your port is 8000 the address will be http://localhost:8000/~/admin
146
-- after HFS has started you can enter console command in the form `config NAME VALUE`
146
- passing via command line at start in the form `--NAME VALUE`
147
+- using envs in the form `HFS_NAME` (eg: `HFS_PORT`)
148
- directly editing the `config.yaml` file. As soon as you save it is reloaded and changes are applied
149
+- after HFS has started you can enter console command in the form `config NAME VALUE`
150
151
`NAME` stands for the property name that you want to change. See the [complete list](config.md).
152
src/config.ts
+4
-2
@@ -5,7 +5,7 @@ import { argv, ORIGINAL_CWD, VERSION } from './const'
5
import { watchLoad } from './watchLoad'
6
import yaml from 'yaml'
7
import _ from 'lodash'
8
-import { DAY, debounceAsync, newObj, onOff, wait, with_ } from './misc'
8
+import { DAY, debounceAsync, newObj, onOff, tryJson, wait, with_ } from './misc'
9
import { statSync } from 'fs'
10
import { join, resolve } from 'path'
11
import events from './events'
@@ -121,7 +121,9 @@ export function getWholeConfig({ omit, only }: { omit?:string[], only?:string[]
121
export function setConfig(newCfg: Record<string,unknown>, save?: boolean) {
122
const version = _.isString(newCfg.version) ? new Version(newCfg.version) : undefined
123
// first time we consider also CLI args
124
- const argCfg = !started && _.pickBy(newObj(configProps, (x, k) => argv[k]), x => x !== undefined)
124
+ const argCfg = !started && _.pickBy(newObj(configProps,
125
+ (x, k) => argv[k] ?? tryJson(process.env['HFS_' + k.toUpperCase()], _.identity)),
126
+ x => x !== undefined)
127
if (! _.isEmpty(argCfg)) {
128
saveConfigAsap() // don't set `save` argument, as it would interfere below at check `save===false`
129
Object.assign(newCfg, argCfg)
src/cross.ts
+2
-2
@@ -95,9 +95,9 @@ export function basename(path: string) {
95
return path.slice(path.lastIndexOf('/') + 1 || path.lastIndexOf('\\') + 1)
96
}
97
98
-export function tryJson(s?: string) {
98
+export function tryJson(s?: string, except?: (s?: string) => unknown) {
99
try { return s && JSON.parse(s) }
100
- catch {}
100
+ catch { return except?.(s) }
101
}
102
103
export function swap<T>(obj: T, k1: keyof T, k2: keyof T) {