Skip to content

Commit 0dc1a8f

Browse files
bcomnessindresorhus
authored andcommitted
Add configPath option (#58)
1 parent f09f067 commit 0dc1a8f

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Configstore {
2121
path.join(id, 'config.json') :
2222
path.join('configstore', `${id}.json`);
2323

24-
this.path = path.join(configDir, pathPrefix);
24+
this.path = opts.configPath || path.join(configDir, pathPrefix);
2525
this.all = Object.assign({}, defaults, this.all);
2626
}
2727

readme.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ Default: `false`
6363

6464
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.
6565

66+
##### configPath
67+
68+
Type: `string`<br>
69+
Default: Automatic
70+
71+
**Please don't use this option unless absolutely necessary and you know what you're doing.**
72+
73+
Set the path of the config file. Overrides the `packageName` and `globalConfigPath` options.
74+
6675
### Instance
6776

6877
You can use [dot-notation](https://github.com/sindresorhus/dot-prop) in a `key` to access nested properties.

test.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import fs from 'fs';
2+
import path from 'path';
3+
import os from 'os';
24
import {serial as test} from 'ava';
35
import Configstore from '.';
46

@@ -102,12 +104,19 @@ test('use default value', t => {
102104
t.is(conf.get('foo'), 'bar');
103105
});
104106

105-
test('support global namespace path option', t => {
107+
test('support `globalConfigPath` option', t => {
106108
const conf = new Configstore('configstore-test', {}, {globalConfigPath: true});
107109
const regex = /configstore-test(\/|\\)config.json$/;
108110
t.true(regex.test(conf.path));
109111
});
110112

113+
test('support `configPath` option', t => {
114+
const customPath = path.join(os.tmpdir(), 'configstore-custom-path', 'foo.json');
115+
const conf = new Configstore('ignored-namespace', {}, {globalConfigPath: true, configPath: customPath});
116+
const regex = /configstore-custom-path(\/|\\)foo.json$/;
117+
t.true(regex.test(conf.path));
118+
});
119+
111120
test('ensure `.all` is always an object', t => {
112121
fs.unlinkSync(configstorePath);
113122
t.notThrows(() => t.context.conf.get('foo'));

0 commit comments

Comments
 (0)