Add support for glob syntax to config file env variables

This change allows loading config files using glob syntax, for
exakple `production-*.yml` to load all config files prefixed with
*production*. With this change the config file can also be configured
using two additional env variables `SHARKEY_CONFIG_YML`
and `SHARKEY_CONFIG_FILE`.
This commit is contained in:
trivernis 2023-12-27 16:08:37 +01:00
parent 8b31c12607
commit df7f4aa3ec
No known key found for this signature in database
GPG key ID: DFFFCC2C7A02DB45

View file

@ -183,14 +183,27 @@ const _dirname = dirname(_filename);
*/
const dir = `${_dirname}/../../../.config`;
function buildPath() {
const envVars = ['MISSKEY_CONFIG_YML', 'SHARKEY_CONFIG_YML', 'SHARKEY_CONFIG_FILE'];
const envCfgFile = envVars
.map(v => process.env[v])
.map(v => v ? resolve(dir, v) : undefined)
.find(v => !!v);
if (envCfgFile) {
return envCfgFile;
}
if (process.env.NODE_ENV === 'test') {
return resolve(dir, 'test.yml');
}
return resolve(dir, 'default.yml');
}
/**
* Path of configuration file
* Path of configuration file. Supports loading multiple files using glob syntax
*/
const cfgDir = process.env.MISSKEY_CONFIG_DIR
? resolve(dir, process.env.MISSKEY_CONFIG_DIR)
: process.env.NODE_ENV === 'test'
? resolve(dir, './test/')
: dir;
const path = buildPath();
export function loadConfig(): Config {
const meta = JSON.parse(fs.readFileSync(`${_dirname}/../../../built/meta.json`, 'utf-8'));
@ -199,7 +212,7 @@ export function loadConfig(): Config {
? JSON.parse(fs.readFileSync(`${_dirname}/../../../built/_vite_/manifest.json`, 'utf-8'))
: { 'src/_boot_.ts': { file: 'src/_boot_.ts' } };
const config = globSync(`${cfgDir}/*.{yaml,yml}`)
const config = globSync(path)
.map(path => fs.readFileSync(path, 'utf-8'))
.map(contents => yaml.load(contents) as Source)
.reduce(