diff --git a/README.md b/README.md index 014514e..75921f5 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,7 @@ Overrides work in the following manner: key by key basis. - For every other config format, an override file replaces the entire config. +- If `smtp.json` or `smtp.yaml` exist, their contents will be loaded before all other config files. You can make use of [JSON Overrides](#json-overrides) here for a single file config. ## Examples @@ -243,15 +244,28 @@ These are as you would expect, and returns an object as given in the file. If a requested .json or .hjson file does not exist then the same file will be checked for with a .yaml extension and that will be loaded instead. This is done because YAML files are far easier for a human to write. +### JSON Overrides You can use JSON, HJSON or YAML files to override any other file by prefixing the outer variable name with a `!` e.g. ```js { - "!smtpgreeting": [ 'this is line one', 'this is line two' ] + "!smtpgreeting": ['this is line one', 'this is line two'], + "!smtp.ini": { + main: { + nodes: 0, + }, + headers: { + max_lines: 1000, + max_received: 100, + }, + }, + "!custom-plugin.yaml": { + secret: 'example', + }, } ``` -If the config/smtpgreeting file did not exist, then this value would replace it. +If the config/smtpgreeting wasn't loaded before, then this value would replace it. Since `smtp.json` is always loaded first, it can be used to override existing config files. NOTE: You must ensure that the data type (e.g. Object, Array or String) for the replaced value is correct. This cannot be done automatically. diff --git a/config.js b/config.js index 795298b..ab0eb02 100644 --- a/config.js +++ b/config.js @@ -157,3 +157,6 @@ function merge_struct(defaults, overrides) { } return defaults } + +// JSON overrides needs smtp.(json|yaml) loaded early +module.exports.get('smtp.json'); \ No newline at end of file diff --git a/lib/reader.js b/lib/reader.js index f70a982..a6bb385 100644 --- a/lib/reader.js +++ b/lib/reader.js @@ -245,7 +245,9 @@ class Reader { const fn = key.substr(1) // Overwrite the config cache for this filename console.log(`Overriding file ${fn} with config from ${name}`) - this._config_cache[path.join(cp, fn)] = result[key] + const cache_key = path.join(cp, fn) + this._overrides[cache_key] = true + this._config_cache[cache_key] = result[key] } } }