1 | # configstore [![Build Status](https://travis-ci.org/yeoman/configstore.svg?branch=legacy-v3)](https://travis-ci.org/yeoman/configstore)
|
2 |
|
3 | > Easily load and persist config without having to think about where and how
|
4 |
|
5 | Config is stored in a JSON file located in `$XDG_CONFIG_HOME` or `~/.config`.<br>
|
6 | Example: `~/.config/configstore/some-id.json`
|
7 |
|
8 | *If you need this for Electron, check out [`electron-store`](https://github.com/sindresorhus/electron-store) instead.*
|
9 |
|
10 |
|
11 | ## Usage
|
12 |
|
13 | ```js
|
14 | const Configstore = require('configstore');
|
15 | const pkg = require('./package.json');
|
16 |
|
17 | // create a Configstore instance with an unique ID e.g.
|
18 | // Package name and optionally some default values
|
19 | const conf = new Configstore(pkg.name, {foo: 'bar'});
|
20 |
|
21 | console.log(conf.get('foo'));
|
22 | //=> 'bar'
|
23 |
|
24 | conf.set('awesome', true);
|
25 | console.log(conf.get('awesome'));
|
26 | //=> true
|
27 |
|
28 | // Use dot-notation to access nested properties
|
29 | conf.set('bar.baz', true);
|
30 | console.log(conf.get('bar'));
|
31 | //=> {baz: true}
|
32 |
|
33 | conf.delete('awesome');
|
34 | console.log(conf.get('awesome'));
|
35 | //=> undefined
|
36 | ```
|
37 |
|
38 |
|
39 | ## API
|
40 |
|
41 | ### Configstore(packageName, [defaults], [options])
|
42 |
|
43 | Returns a new instance.
|
44 |
|
45 | #### packageName
|
46 |
|
47 | Type: `string`
|
48 |
|
49 | Name of your package.
|
50 |
|
51 | #### defaults
|
52 |
|
53 | Type: `Object`
|
54 |
|
55 | Default config.
|
56 |
|
57 | #### options
|
58 |
|
59 | ##### globalConfigPath
|
60 |
|
61 | Type: `boolean`<br>
|
62 | Default: `false`
|
63 |
|
64 | 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.
|
65 |
|
66 | ### Instance
|
67 |
|
68 | You can use [dot-notation](https://github.com/sindresorhus/dot-prop) in a `key` to access nested properties.
|
69 |
|
70 | ### .set(key, value)
|
71 |
|
72 | Set an item.
|
73 |
|
74 | ### .set(object)
|
75 |
|
76 | Set multiple items at once.
|
77 |
|
78 | ### .get(key)
|
79 |
|
80 | Get an item.
|
81 |
|
82 | ### .has(key)
|
83 |
|
84 | Check if an item exists.
|
85 |
|
86 | ### .delete(key)
|
87 |
|
88 | Delete an item.
|
89 |
|
90 | ### .clear()
|
91 |
|
92 | Delete all items.
|
93 |
|
94 | ### .size
|
95 |
|
96 | Get the item count.
|
97 |
|
98 | ### .path
|
99 |
|
100 | Get the path to the config file. Can be used to show the user where the config file is located or even better open it for them.
|
101 |
|
102 | ### .all
|
103 |
|
104 | Get all the config as an object or replace the current config with an object:
|
105 |
|
106 | ```js
|
107 | conf.all = {
|
108 | hello: 'world'
|
109 | };
|
110 | ```
|
111 |
|
112 |
|
113 | ## License
|
114 |
|
115 | [BSD license](http://opensource.org/licenses/bsd-license.php)<br>
|
116 | Copyright Google
|