electron-json-config
Advanced tools
Comparing version 1.5.3 to 2.0.0-beta
{ | ||
"name": "electron-json-config", | ||
"version": "1.5.3", | ||
"version": "2.0.0-beta", | ||
"description": "Simply set and get configuration from a json file for your Electron app", | ||
"main": "src/index.js", | ||
"main": "lib/index.js", | ||
"keywords": [ | ||
@@ -21,3 +21,3 @@ "conf", | ||
"email": "dev@de-luca.io", | ||
"url": "de-luca.io" | ||
"url": "https://de-luca.io" | ||
}, | ||
@@ -29,14 +29,51 @@ "repository": { | ||
"scripts": { | ||
"test": "eslint . && electron-mocha --recursive test -R progress && electron-mocha --renderer --recursive test -R progress" | ||
"lint": "eslint src/*.ts", | ||
"test": "electron-mocha -r ts-node/register '**/*.spec.ts'", | ||
"test:coverage": "nyc npm run test", | ||
"build": "tsc" | ||
}, | ||
"license": "BSD-2-Clause", | ||
"devDependencies": { | ||
"electron": "latest", | ||
"electron-mocha": "latest", | ||
"eslint": "^3.18.0", | ||
"mochainon": "latest" | ||
"@types/chai": "^4.2.12", | ||
"@types/mocha": "^8.0.3", | ||
"@types/node": "^14.11.2", | ||
"@typescript-eslint/eslint-plugin": "^4.3.0", | ||
"@typescript-eslint/parser": "^4.3.0", | ||
"chai": "^4.2.0", | ||
"electron": "^10.0.0", | ||
"electron-mocha": "^9.2.0", | ||
"eslint": "^7.0.0", | ||
"nyc": "^15.1.0", | ||
"source-map-support": "^0.5.19", | ||
"ts-node": "^9.0.0", | ||
"typescript": "^3.7.0" | ||
}, | ||
"files": [ | ||
"src/" | ||
] | ||
"lib/" | ||
], | ||
"nyc": { | ||
"extension": [ | ||
".ts" | ||
], | ||
"include": [ | ||
"src/**/*.js", | ||
"src/**/*.ts" | ||
], | ||
"exclude": [ | ||
"src/**/*.spec.*", | ||
"src/index.ts" | ||
], | ||
"require": [ | ||
"ts-node/register" | ||
], | ||
"reporter": [ | ||
"lcovonly", | ||
"text" | ||
], | ||
"all": true, | ||
"sourceMap": true, | ||
"instrument": true, | ||
"check-coverage": false, | ||
"cache": true | ||
} | ||
} |
295
README.md
# electron-json-config | ||
![David](https://img.shields.io/david/de-luca/electron-json-config) | ||
![npm](https://img.shields.io/npm/v/electron-json-config) | ||
[![codecov](https://codecov.io/gh/de-luca/electron-json-config/branch/master/graph/badge.svg)](https://codecov.io/gh/de-luca/electron-json-config) | ||
![](https://github.com/de-luca/electron-json-config/workflows/CI/badge.svg) | ||
> Simply set and get configuration from a json file for your Electron app | ||
The config file (`config.json`) is located in the path returned by `app.getPath('userData')`. | ||
This package can be used from **browser and renderer** process. | ||
**This is the 2.x.x tree.** | ||
**For 1.x.x code and documentation please refer to [the 1.x.x tree](https://github.com/de-luca/electron-json-config/tree/1.x.x).** | ||
**See [UPGRADE.md](./UPGRADE.md) for an upgrade guide.** | ||
This package can be used from **[main and renderer process](https://www.electronjs.org/docs/tutorial/quick-start#main-and-renderer-processes)**. | ||
## Installation | ||
### NPM | ||
``` | ||
npm install --save electron-json-config | ||
``` | ||
### yarn | ||
``` | ||
yarn add electron-json-config | ||
``` | ||
## Usage | ||
#### CommonJS | ||
```js | ||
const config = require('electron-json-config'); | ||
const config = require('electron-json-config').factory(); | ||
config.set('foo', 'bar'); | ||
console.log(config.get('foo')); // shows 'bar' | ||
console.log(config.get('foo')); // bar | ||
``` | ||
#### ES Modules | ||
```ts | ||
import { factory } from 'electron-json-config'; | ||
## Documentation | ||
const config = factory(); | ||
*All `key` can be a classic key (eg: `foo`) or a multiple level key with levels separated by `.` (eg: `foo.bar`)* | ||
config.set('foo', 'bar'); | ||
console.log(config.get('foo')); // bar | ||
``` | ||
### `.file()` | ||
Returns the name of the file the config is stored in. | ||
**Returns:** [String] | ||
## Documentation | ||
### Key | ||
### `.set(key, value)` | ||
Sets a key with the specified value. Overwrites the value, if the key already exists.. | ||
```ts | ||
type Key = string | Array<string>; | ||
``` | ||
A `key` can be : | ||
- a classic string key | ||
eg: `'foo'` | ||
- a dotted string multi level key | ||
eg: `'foo.bar'` | ||
- an array of string representing a multi level key | ||
eg: `['foo', 'bar']` | ||
| Parameters | Type | Optional | Description | | ||
|:----------:|:--------:|:--------:|:-----------------------------------:| | ||
| key | [String] | | The key to set | | ||
| value | * | | The value to set under the key | | ||
**Returns:** void | ||
**Example:** | ||
```js | ||
config.set('foo', 'bar'); // Sets 'bar' under 'foo' key | ||
config.set('anArray', [1, 2]); // Sets [1, 2] under 'anArray' key | ||
config.set('the.answer', 42); // Sets 42 under 'answer' under 'the' | ||
### Storable | ||
```ts | ||
interface Storable { | ||
[key: string]: Storable | any; | ||
} | ||
``` | ||
### `.setBulk(items)` | ||
Like `.set()` but sets multiple keys in a single call. | ||
### `factory(file?: string, key?: string): Conf` | ||
| Parameters | Type | Optional | Description | | ||
|:----------:|:--------:|:--------:|:-------------------------------------------:| | ||
| items | [Object] | | An object whose attributes will become keys | | ||
**Description:** | ||
Create an instance of [Config] and returns it. | ||
If an instance with the same key exist, returns this instance instead. | ||
**Returns:** void | ||
**Example:** | ||
```js | ||
// Sets 'bar' under 'foo' key | ||
// Sets 42 under 'answer' under 'the' | ||
config.setBulk({ | ||
'foo': 'bar', | ||
'the.answer': 42, | ||
}); | ||
``` | ||
If file is specified, the configuration will be saved to that file instead of the default `app.getPath('userData') + '/config.json'`. | ||
If key is specified, the requested instance will be saved under une given key instead of the default `userData`. | ||
### `.has(key)` | ||
Checks if a key exists. | ||
**Examples:** | ||
```ts | ||
// file: app.getPath('userData') + '/config.json' | ||
// key: 'userData' | ||
factory(); | ||
| Parameters | Type | Optional | Description | | ||
|:----------:|:--------:|:--------:|:-----------------------------------:| | ||
| key | [String] | | The name of a key to test existence | | ||
// file: '/data/test.json' | ||
// key: '/data/test.json' | ||
factory('/data/test.json'); | ||
**Returns:** [Boolean] | ||
**Example:** | ||
```js | ||
config.set('foo', 'bar'); | ||
// file: '/data/test.json' | ||
// key: 'test' | ||
factory('/data/test.json', 'test'); | ||
config.has('foo'); // true | ||
config.has('bar'); // false | ||
// file: app.getPath('userData') + '/config.json' | ||
// key: 'test' | ||
factory(undefined, 'test'); | ||
``` | ||
**Parameters:** | ||
| Name | Type | Default | | ||
| ------- | -------- | ------------------------------------------ | | ||
| `file?` | [string] | `app.getPath('userData') + '/config.json'` | | ||
| `key?` | [string] | `key || file || 'userData'` | | ||
### `.get(key[, defaultValue])` | ||
Returns the value associated with the key, `undefined` otherwise. | ||
You can specify a default value returned in case the key does not exists. | ||
**Returns:** void | ||
| Parameters | Type | Optional | Description | | ||
|:------------:|:--------:|:--------:|:-------------------------------------------------:| | ||
| key | [String] | | The name of the key to get | | ||
| defaultValue | * | ✓ | The value to return in case value does not exists | | ||
**Returns:** \* | ||
**Example:** | ||
```js | ||
config.set('foo', 'bar'); // Sets 'bar' under 'foo' key | ||
### Config | ||
config.get('foo'); // Returns 'bar' | ||
config.get('bar', 42); // Returns 42 | ||
``` | ||
The config class is a set of wrappers and helpers providing access to configuration and file synchronization. | ||
#### `new Config(file: string, data: Storable): Config` | ||
**Parameters:** | ||
| Name | Type | | ||
| ------ | ---------- | | ||
| `file` | [string] | | ||
| `data` | [Storable] | | ||
### `.keys([key])` | ||
If `key` is omitted, returns an array containing all keys in the config file. | ||
If `key` is provided, returns an array containing all sub keys in the `key` object. | ||
**Returns:** [Config] | ||
| Parameters | Type | Optional | Description | | ||
|:----------:|:--------:|:--------:|:---------------------------------:| | ||
| key | [String] | ✓ | The name of a key to get sub keys | | ||
#### `get file(): string` | ||
**Description:** Returns the name of the file the config is stored in. | ||
**Returns:** [Array]<[String]> | ||
**Example:** | ||
```js | ||
config.setBulk({ | ||
'foo': 'bar', | ||
'the.answer': 42, | ||
}); | ||
**Returns:** [string] | ||
config.keys(); // Returns ['foo', 'the'] | ||
config.keys('the'); // Returns ['answer'] | ||
``` | ||
#### `all(): Storable` | ||
**Description:** Returns all the data currently saved. | ||
### `.all()` | ||
Returns an object with all the data currently saved. | ||
**Returns:** [Storable] | ||
**Returns:** [Object] | ||
**Example:** | ||
```js | ||
config.setBulk({ | ||
'foo': 'bar', | ||
'the.answer': 42, | ||
}); | ||
#### `delete(key: Key): void` | ||
**Description:** Removes the key and its value from the config file. | ||
config.all(); | ||
/* | ||
{ | ||
'foo': 'bar', | ||
'the': { | ||
'answer': 42 | ||
} | ||
} | ||
*/ | ||
``` | ||
**Parameters:** | ||
| Name | Type | | ||
| ----- | ----- | | ||
| `key` | [Key] | | ||
**Returns:** void | ||
### `.delete(key)` | ||
Removes the key and its value from the config file. | ||
#### `deleteBulk(keys: Array<Key>): void` | ||
**Description:** Removes all the keys specified and theirs value from the config file. | ||
| Parameters | Type | Optional | Description | | ||
|:----------:|:--------:|:--------:|:---------------------------:| | ||
| key | [String] | | The name of a key to delete | | ||
**Parameters:** | ||
| Name | Type | | ||
| ------ | --------------- | | ||
| `keys` | [Array]\<[Key]> | | ||
**Returns:** void | ||
**Example:** | ||
```js | ||
config.set('foo', 'bar'); // Sets 'bar' under 'foo' key | ||
config.delete('foo'); // Removes key 'bar' and its value | ||
``` | ||
**Returns:** void | ||
#### `get<T>(key: Key, defaultValue?: T): T | undefined` | ||
**Description:** Returns the value associated with the key, undefined otherwise. | ||
You can specify a default value returned in case the key does not exists. | ||
### `.deleteBulk(keys)` | ||
Removes all the keys specified and theirs value from the config file. | ||
**Parameters:** | ||
| Name | Type | | ||
| --------------- | ----- | | ||
| `key` | [Key] | | ||
| `defaultValue?` | T | | ||
| Parameters | Type | Optional | Description | | ||
|:----------:|:-----------------:|:--------:|:--------------------------:| | ||
| keys | [Array]<[String]> | | An array of keys to remove | | ||
**Returns:** T \| undefined | ||
**Returns:** void | ||
**Example:** | ||
```js | ||
config.setBulk({ | ||
'foo': 'bar', | ||
'the.answer': 42, | ||
}); | ||
#### `has(key: Key): boolean` | ||
**Description:** Checks if a key exists. | ||
// Remove keys 'foo' and 'answer' | ||
config.deleteBulk(['foo', 'answer']); | ||
``` | ||
**Parameters:** | ||
| Name | Type | | ||
| ----- | ----- | | ||
| `key` | [Key] | | ||
**Returns:** [boolean] | ||
### `.purge()` | ||
Removes all data from the config file. | ||
#### `keys(key?: Key): Array<string>` | ||
**Description:** If `key` is omitted, returns an array containing all keys in the config file. | ||
If `key` is provided, returns an array containing all sub keys in the key object. | ||
**Returns:** void | ||
**Example:** | ||
```js | ||
config.purge(); // All keys are removed | ||
``` | ||
**Parameters:** | ||
| Name | Type | | ||
| ------ | ----- | | ||
| `key?` | [Key] | | ||
**Returns:** [Array]\<[string]> | ||
[String]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String | ||
[Boolean]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean | ||
#### `purge(): void` | ||
**Description:** Removes all data from the config file. | ||
**Returns:** void | ||
#### `set<T>(key: Key, `value`: Storable | T): void` | ||
**Description:** Sets a key with the specified value. Overwrites the value, if the key already exists. | ||
**Parameters:** | ||
| Name | Type | | ||
| ------- | --------------- | | ||
| `key` | [Key] | | ||
| `value` | [Storable] \| T | | ||
**Returns:** void | ||
#### `setBulk<T>(items: { [key:string]: Storable | T }): void` | ||
**Description:** Like .set() but sets multiple keys in a single call. | ||
**Parameters:** | ||
| Name | Type | | ||
| ------- | ------------------------------------ | | ||
| `items` | { [key: [string]]: [Storable] \| T } | | ||
**Returns:** void | ||
[Key]: #key | ||
[Storable]: #storable | ||
[Config]: #config | ||
[string]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String | ||
[boolean]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean | ||
[Array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array | ||
[Object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
20317
18
211
223
1
13
2
1