Skip to content

Commit 4178b8d

Browse files
authored
Fix json overrides (#77)
1 parent 6f23fff commit 4178b8d

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

README.md

+16-2
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ Overrides work in the following manner:
9090
key by key basis.
9191
- For every other config format, an override file replaces the entire
9292
config.
93+
- 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.
9394

9495
## Examples
9596

@@ -243,15 +244,28 @@ These are as you would expect, and returns an object as given in the file.
243244

244245
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.
245246

247+
### <a name="json-overrides">JSON Overrides</a>
246248
You can use JSON, HJSON or YAML files to override any other file by prefixing the outer variable name with a `!` e.g.
247249

248250
```js
249251
{
250-
"!smtpgreeting": [ 'this is line one', 'this is line two' ]
252+
"!smtpgreeting": ['this is line one', 'this is line two'],
253+
"!smtp.ini": {
254+
main: {
255+
nodes: 0,
256+
},
257+
headers: {
258+
max_lines: 1000,
259+
max_received: 100,
260+
},
261+
},
262+
"!custom-plugin.yaml": {
263+
secret: 'example',
264+
},
251265
}
252266
```
253267

254-
If the config/smtpgreeting file did not exist, then this value would replace it.
268+
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.
255269

256270
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.
257271

config.js

+3
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,6 @@ function merge_struct(defaults, overrides) {
157157
}
158158
return defaults
159159
}
160+
161+
// JSON overrides needs smtp.(json|yaml) loaded early
162+
module.exports.get('smtp.json');

lib/reader.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,9 @@ class Reader {
245245
const fn = key.substr(1)
246246
// Overwrite the config cache for this filename
247247
console.log(`Overriding file ${fn} with config from ${name}`)
248-
this._config_cache[path.join(cp, fn)] = result[key]
248+
const cache_key = path.join(cp, fn)
249+
this._overrides[cache_key] = true
250+
this._config_cache[cache_key] = result[key]
249251
}
250252
}
251253
}

0 commit comments

Comments
 (0)