UNPKG

2.3 kBMarkdownView Raw
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
5Config is stored in a JSON file located in `$XDG_CONFIG_HOME` or `~/.config`.<br>
6Example: `~/.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
14const Configstore = require('configstore');
15const pkg = require('./package.json');
16
17// create a Configstore instance with an unique ID e.g.
18// Package name and optionally some default values
19const conf = new Configstore(pkg.name, {foo: 'bar'});
20
21console.log(conf.get('foo'));
22//=> 'bar'
23
24conf.set('awesome', true);
25console.log(conf.get('awesome'));
26//=> true
27
28// Use dot-notation to access nested properties
29conf.set('bar.baz', true);
30console.log(conf.get('bar'));
31//=> {baz: true}
32
33conf.delete('awesome');
34console.log(conf.get('awesome'));
35//=> undefined
36```
37
38
39## API
40
41### Configstore(packageName, [defaults], [options])
42
43Returns a new instance.
44
45#### packageName
46
47Type: `string`
48
49Name of your package.
50
51#### defaults
52
53Type: `Object`
54
55Default config.
56
57#### options
58
59##### globalConfigPath
60
61Type: `boolean`<br>
62Default: `false`
63
64Store 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
68You can use [dot-notation](https://github.com/sindresorhus/dot-prop) in a `key` to access nested properties.
69
70### .set(key, value)
71
72Set an item.
73
74### .set(object)
75
76Set multiple items at once.
77
78### .get(key)
79
80Get an item.
81
82### .has(key)
83
84Check if an item exists.
85
86### .delete(key)
87
88Delete an item.
89
90### .clear()
91
92Delete all items.
93
94### .size
95
96Get the item count.
97
98### .path
99
100Get 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
104Get all the config as an object or replace the current config with an object:
105
106```js
107conf.all = {
108 hello: 'world'
109};
110```
111
112
113## License
114
115[BSD license](http://opensource.org/licenses/bsd-license.php)<br>
116Copyright Google