Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yeoman/configstore
Easily load and persist config without having to think about where and how
https://github.com/yeoman/configstore
Last synced: 10 days ago
JSON representation
Easily load and persist config without having to think about where and how
- Host: GitHub
- URL: https://github.com/yeoman/configstore
- Owner: yeoman
- License: bsd-2-clause
- Created: 2012-11-13T15:34:24.000Z (almost 12 years ago)
- Default Branch: main
- Last Pushed: 2023-04-20T09:37:30.000Z (over 1 year ago)
- Last Synced: 2024-05-17T09:03:02.639Z (6 months ago)
- Language: JavaScript
- Homepage:
- Size: 91.8 KB
- Stars: 861
- Watchers: 17
- Forks: 56
- Open Issues: 5
-
Metadata Files:
- Readme: readme.md
- Funding: .github/funding.yml
- License: license
- Security: .github/security.md
Awesome Lists containing this project
- awesome-web-cn - configstore - 轻松加载和持久化配置,可以将一些有关用户的数据存储到用户的磁盘中,下次再次运行程序的时候可以读取到最新的数据,一般会在桌面端或者 node 中使用 (Uncategorized / Uncategorized)
- jimsghstars - yeoman/configstore - Easily load and persist config without having to think about where and how (JavaScript)
README
# configstore
> Easily load and persist config without having to think about where and how
The config is stored in a JSON file located in `$XDG_CONFIG_HOME` or `~/.config`.\
Example: `~/.config/configstore/some-id.json`*If you need this for Electron, check out [`electron-store`](https://github.com/sindresorhus/electron-store) instead.*\
*And check out [`conf`](https://github.com/sindresorhus/conf) for a more modern version of `configstore`.*## Install
```sh
npm install configstore
```## Usage
```js
import fs from 'node:fs';
import Configstore from 'configstore';const packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf8'));
// Create a Configstore instance.
const config = new Configstore(packageJson.name, {foo: 'bar'});console.log(config.get('foo'));
//=> 'bar'config.set('awesome', true);
console.log(config.get('awesome'));
//=> true// Use dot-notation to access nested properties.
config.set('bar.baz', true);
console.log(config.get('bar'));
//=> {baz: true}config.delete('awesome');
console.log(config.get('awesome'));
//=> undefined
```## API
### Configstore(packageName, defaults?, options?)
Returns a new instance.
#### packageName
Type: `string`
Name of your package.
#### defaults
Type: `object`
Default config.
#### options
Type: `object`
##### globalConfigPath
Type: `boolean`\
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.
### .set(key, value)
Set an item.
### .set(object)
Set multiple items at once.
### .get(key)
Get an item.
### .has(key)
Check if an item exists.
### .delete(key)
Delete an item.
### .clear()
Delete all items.
### .size
Get the item count.
### .path
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.
### .all
Get all the config as an object or replace the current config with an object:
```js
config.all = {
hello: 'world'
};
```