From 0dc1a8fa77ae1d252dfaf0381b466c7093a83f68 Mon Sep 17 00:00:00 2001 From: Bret Comnes Date: Sat, 28 Jul 2018 08:50:20 -0700 Subject: [PATCH] Add `configPath` option (#58) --- index.js | 2 +- readme.md | 9 +++++++++ test.js | 11 ++++++++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index fb94420..ebdccab 100644 --- a/index.js +++ b/index.js @@ -21,7 +21,7 @@ class Configstore { path.join(id, 'config.json') : path.join('configstore', `${id}.json`); - this.path = path.join(configDir, pathPrefix); + this.path = opts.configPath || path.join(configDir, pathPrefix); this.all = Object.assign({}, defaults, this.all); } diff --git a/readme.md b/readme.md index 6af3771..3dbb494 100644 --- a/readme.md +++ b/readme.md @@ -63,6 +63,15 @@ Default: `false` Store the config at `$CONFIG/package-name/config.json` instead of the default `$CONFIG/configstore/package-name.json`. This is not recommended as you might end up conflicting with other tools, rendering the "without having to think" idea moot. +##### configPath + +Type: `string`
+Default: Automatic + +**Please don't use this option unless absolutely necessary and you know what you're doing.** + +Set the path of the config file. Overrides the `packageName` and `globalConfigPath` options. + ### Instance You can use [dot-notation](https://github.com/sindresorhus/dot-prop) in a `key` to access nested properties. diff --git a/test.js b/test.js index 189950b..07df43b 100644 --- a/test.js +++ b/test.js @@ -1,4 +1,6 @@ import fs from 'fs'; +import path from 'path'; +import os from 'os'; import {serial as test} from 'ava'; import Configstore from '.'; @@ -102,12 +104,19 @@ test('use default value', t => { t.is(conf.get('foo'), 'bar'); }); -test('support global namespace path option', t => { +test('support `globalConfigPath` option', t => { const conf = new Configstore('configstore-test', {}, {globalConfigPath: true}); const regex = /configstore-test(\/|\\)config.json$/; t.true(regex.test(conf.path)); }); +test('support `configPath` option', t => { + const customPath = path.join(os.tmpdir(), 'configstore-custom-path', 'foo.json'); + const conf = new Configstore('ignored-namespace', {}, {globalConfigPath: true, configPath: customPath}); + const regex = /configstore-custom-path(\/|\\)foo.json$/; + t.true(regex.test(conf.path)); +}); + test('ensure `.all` is always an object', t => { fs.unlinkSync(configstorePath); t.notThrows(() => t.context.conf.get('foo'));