electron-json-config
Advanced tools
Comparing version 2.0.0 to 2.1.0
import Storable from './Storable'; | ||
/** | ||
* Type to support configuration of pretty JSON when writing JSON file | ||
*/ | ||
export type PrettyJSONConfig = { | ||
enabled: boolean; | ||
indentSize?: number; | ||
}; | ||
/** | ||
* Options that can be passed to Config for writing and storing data | ||
* | ||
* Currently supports pretty JSON format for storing indented, | ||
* multi-line in file | ||
*/ | ||
export type ConfigOptions = { | ||
prettyJson?: PrettyJSONConfig; | ||
}; | ||
/** | ||
* Default config, used for optional `options` args throughout | ||
*/ | ||
export declare const DEFAULT_CONFIG: { | ||
prettyJson: { | ||
enabled: boolean; | ||
}; | ||
}; | ||
/** | ||
* A Key can be: | ||
@@ -8,7 +32,8 @@ * - a simple string: 'foo' | ||
*/ | ||
export declare type Key = string | Array<string>; | ||
export type Key = string | Array<string>; | ||
export default class Config { | ||
private _file; | ||
private _data; | ||
constructor(file: string, data: Storable); | ||
private _options; | ||
constructor(file: string, data: Storable, options?: ConfigOptions); | ||
get file(): string; | ||
@@ -15,0 +40,0 @@ has(key: Key): boolean; |
@@ -9,2 +9,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.DEFAULT_CONFIG = void 0; | ||
const util = require("./utils"); | ||
@@ -16,3 +17,3 @@ function sync(_target, _propertyKey, descriptor) { | ||
const res = originalMethod.apply(this, args); | ||
util.sync(this._file, this._data); | ||
util.sync(this._file, this._data, this._options.prettyJson); | ||
return res; | ||
@@ -22,6 +23,15 @@ }; | ||
} | ||
/** | ||
* Default config, used for optional `options` args throughout | ||
*/ | ||
exports.DEFAULT_CONFIG = { | ||
prettyJson: { | ||
enabled: false, | ||
}, | ||
}; | ||
class Config { | ||
constructor(file, data) { | ||
constructor(file, data, options = exports.DEFAULT_CONFIG) { | ||
this._file = file; | ||
this._data = data; | ||
this._options = options; | ||
} | ||
@@ -64,2 +74,3 @@ get file() { | ||
} | ||
exports.default = Config; | ||
__decorate([ | ||
@@ -80,3 +91,2 @@ sync | ||
], Config.prototype, "purge", null); | ||
exports.default = Config; | ||
//# sourceMappingURL=Config.js.map |
@@ -1,2 +0,2 @@ | ||
import Conf from './Config'; | ||
export declare function factory(file?: string, key?: string): Conf; | ||
import Conf, { ConfigOptions } from './Config'; | ||
export declare function factory(file?: string, key?: string, options?: ConfigOptions): Conf; |
@@ -11,7 +11,7 @@ "use strict"; | ||
const instances = new Map(); | ||
function factory(file, key) { | ||
function factory(file, key, options = Config_1.DEFAULT_CONFIG) { | ||
const actualKey = key || file || defaultKey; | ||
if (!instances.has(actualKey)) { | ||
const actualFile = file || defaultFile; | ||
instances.set(actualKey, new Config_1.default(actualFile, (0, utils_1.read)(actualFile))); | ||
instances.set(actualKey, new Config_1.default(actualFile, (0, utils_1.read)(actualFile), options)); | ||
} | ||
@@ -18,0 +18,0 @@ return instances.get(actualKey); |
@@ -1,4 +0,4 @@ | ||
import Storable from "./Storable"; | ||
import { Key } from './Config'; | ||
export declare function sync(file: string, data: Record<string, unknown>): void; | ||
import Storable from './Storable'; | ||
import { Key, PrettyJSONConfig } from './Config'; | ||
export declare function sync(file: string, data: Record<string, unknown>, options?: PrettyJSONConfig): void; | ||
export declare function read(file: string): Storable; | ||
@@ -5,0 +5,0 @@ export declare function pathiffy(key: Key): Array<string>; |
@@ -5,4 +5,10 @@ "use strict"; | ||
const fs_1 = require("fs"); | ||
function sync(file, data) { | ||
(0, fs_1.writeFileSync)(file, JSON.stringify(data)); | ||
const Config_1 = require("./Config"); | ||
function sync(file, data, options = Config_1.DEFAULT_CONFIG['prettyJson']) { | ||
if (options.enabled) { | ||
(0, fs_1.writeFileSync)(file, JSON.stringify(data, null, options.indentSize || 2)); | ||
} | ||
else { | ||
(0, fs_1.writeFileSync)(file, JSON.stringify(data)); | ||
} | ||
} | ||
@@ -9,0 +15,0 @@ exports.sync = sync; |
{ | ||
"name": "electron-json-config", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Simply set and get configuration from a json file for your Electron app", | ||
@@ -32,3 +32,4 @@ "main": "lib/index.js", | ||
"test:docker": "xvfb-run --server-args=\"-screen 0 1024x780x24\" npm run test", | ||
"build": "tsc" | ||
"build": "tsc", | ||
"prepare": "npm run build" | ||
}, | ||
@@ -38,14 +39,15 @@ "license": "BSD-2-Clause", | ||
"@types/chai": "^4.2.12", | ||
"@types/mocha": "^9.0.0", | ||
"@types/node": "^17.0.13", | ||
"@typescript-eslint/eslint-plugin": "^5.7.0", | ||
"@typescript-eslint/parser": "^5.7.0", | ||
"@types/mocha": "^10.0.1", | ||
"@types/node": "^20.0.0", | ||
"@typescript-eslint/eslint-plugin": "^6.7.0", | ||
"@typescript-eslint/parser": "^6.7.0", | ||
"chai": "^4.2.0", | ||
"electron": "^17.0.0", | ||
"electron-mocha": "^11.0.2", | ||
"electron": "^26.1.0", | ||
"electron-mocha": "^12.0.0", | ||
"eslint": "^8.4.1", | ||
"nyc": "^15.1.0", | ||
"prettier": "^3.0.3", | ||
"source-map-support": "^0.5.19", | ||
"ts-node": "^10.0.0", | ||
"typescript": "^4.1.2" | ||
"typescript": "^5.0.4" | ||
}, | ||
@@ -52,0 +54,0 @@ "files": [ |
@@ -65,3 +65,20 @@ # electron-json-config | ||
### ConfigOptions | ||
```ts | ||
type ConfigOptions = { | ||
prettyJson?: { | ||
enabled: boolean; | ||
indentSize?: number; | ||
}; | ||
}; | ||
``` | ||
Options for creating and storing Config contents. | ||
Currently supports formatting the JSON file contents in a pretty, indented | ||
format for easy readability or editability. | ||
`prettyJson.identSize` defaults to `2` if this option is `enabled`. | ||
### Storable | ||
@@ -76,3 +93,3 @@ | ||
### `factory(file?: string, key?: string): Conf` | ||
### `factory(file?: string, key?: string, options?: ConfigOptions): Conf` | ||
@@ -104,9 +121,15 @@ **Description:** | ||
factory(undefined, 'test'); | ||
// file: app.getPath('userData') + '/config.json' | ||
// key: 'test' | ||
// JSON stored in readable, indented format (with default 2 space tab) | ||
factory(undefined, 'test', { prettyJson: { enabled: true }}); | ||
``` | ||
**Parameters:** | ||
| Name | Type | Default | | ||
| ------- | -------- | ------------------------------------------ | | ||
| `file?` | [string] | `app.getPath('userData') + '/config.json'` | | ||
| `key?` | [string] | `key || file || 'userData'` | | ||
| Name | Type | Default | | ||
| ---------- | --------------- | ------------------------------------------ | | ||
| `file?` | [string] | `app.getPath('userData') + '/config.json'` | | ||
| `key?` | [string] | `key || file || 'userData'` | | ||
| `options?` | [ConfigOptions] | `{ prettyJson: { enabled: false }}` | | ||
@@ -120,8 +143,9 @@ **Returns:** void | ||
#### `new Config(file: string, data: Storable): Config` | ||
#### `new Config(file: string, data: Storable, options?: ConfigOptions): Config` | ||
**Parameters:** | ||
| Name | Type | | ||
| ------ | ---------- | | ||
| `file` | [string] | | ||
| `data` | [Storable] | | ||
| Name | Type | | ||
| ---------- | --------------- | | ||
| `file` | [string] | | ||
| `data` | [Storable] | | ||
| `options?` | [ConfigOptions] | | ||
@@ -221,2 +245,3 @@ **Returns:** [Config] | ||
[Key]: #key | ||
[ConfigOptions]: #configOptions | ||
[Storable]: #storable | ||
@@ -223,0 +248,0 @@ [Config]: #config |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23034
253
247
14