Skip to content

Commit

Permalink
Add configPath option (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcomnes authored and sindresorhus committed Jul 28, 2018
1 parent f09f067 commit 0dc1a8f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
9 changes: 9 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`<br>
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.
Expand Down
11 changes: 10 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -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 '.';

Expand Down Expand Up @@ -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'));
Expand Down

0 comments on commit 0dc1a8f

Please sign in to comment.