nconf icon indicating copy to clipboard operation
nconf copied to clipboard

When using multiple files how to save to specific one?

Open WeaponX86 opened this issue 3 years ago • 3 comments

In the example you can open multiple files. When you run save(), how do I specify where it goes?

nconf.file('path/to/your/config.json');
// add multiple files, hierarchically. notice the unique key for each file
nconf.file('user', 'path/to/your/user.json');
nconf.file('global', 'path/to/your/global.json');

nconf.set('foo','bar');

nconf.save(); // Where is 'foo' saved?

WeaponX86 avatar Apr 28 '22 19:04 WeaponX86

So I've been looking into this as well. At the moment, with your example, foo would be saved to the 'global' file.

I've been looking at the tests, (specifically for the file store) and I think I've figured out how to control where things are saved. I'm testing this out right now, and will probably submit a PR for documentation when this is done, but the gist of this is that instead of using nconf.file() for both of your files, you can do this:

nconf.file('path/to/your/config.json')
const userStore = new nconf.File('path/to/your/user.json')
userStore.load() // I think this loads/merges onto the memory store
const globalStore = new nconf.File('path/to/your/global.json')
globalStore.load() // i think this loads/merges onto the memory store

nconf.set('foo', 'bar')

// now we save to the path/to/your/user.json
userStore.save((err) => console.error('problem saving to path/to/your/user.json'))

LongLiveCHIEF avatar Sep 08 '22 15:09 LongLiveCHIEF

I haven't tested the above, but that looks correct to me. PRs are always welcome :-)

mhamann avatar Sep 09 '22 03:09 mhamann

Unfortunately, it did not work. At this point, i'm just using my own save function for writing to disk.

LongLiveCHIEF avatar Sep 09 '22 12:09 LongLiveCHIEF