electron-settings
Advanced tools
Comparing version 3.0.7 to 3.0.8
/** | ||
* Electron Settings - User settings manager for Electron. | ||
* A simple persistent user settings framework for Electron. | ||
* | ||
* @version 3.0.0 | ||
* @module main | ||
* @author Nathan Buchar | ||
@@ -10,6 +10,4 @@ * @copyright 2016-2017 Nathan Buchar <hello@nathanbuchar.com> | ||
'use strict'; | ||
const Settings = require('./lib/settings'); | ||
module.exports = new Settings(); |
/** | ||
* A module that contains key path helpers. Adapted from atom/key-path-helpers. | ||
* | ||
* @module settings-helpers | ||
* @author Nathan Buchar | ||
* @copyright 2016-2017 Nathan Buchar <hello@nathanbuchar.com> | ||
* @license ISC | ||
*/ | ||
/** | ||
* Checks if the given object contains the given key path. | ||
@@ -3,0 +12,0 @@ * |
@@ -1,2 +0,10 @@ | ||
const clone = require('clone'); | ||
/** | ||
* A module that delegates settings changes. | ||
* | ||
* @module settings-observer | ||
* @author Nathan Buchar | ||
* @copyright 2016-2017 Nathan Buchar <hello@nathanbuchar.com> | ||
* @license ISC | ||
*/ | ||
const deepEqual = require('deep-equal'); | ||
@@ -6,3 +14,3 @@ | ||
constructor(settings, keyPath, handler) { | ||
constructor(settings, keyPath, handler, currentValue) { | ||
@@ -40,3 +48,3 @@ /** | ||
*/ | ||
this._currentValue = this._settings.get(this._keyPath); | ||
this._currentValue = currentValue; | ||
@@ -60,11 +68,2 @@ /** | ||
_init() { | ||
this._initEventListeners(); | ||
} | ||
/** | ||
* Initializes event listeners. | ||
* | ||
* @private | ||
*/ | ||
_initEventListeners() { | ||
this._settings.on('change', this._handleChange); | ||
@@ -83,3 +82,3 @@ } | ||
if (!deepEqual(newValue, oldValue)) { | ||
this._currentValue = clone(newValue); | ||
this._currentValue = newValue; | ||
@@ -86,0 +85,0 @@ // Call the watch handler and pass in the new and old values. |
@@ -0,5 +1,13 @@ | ||
/** | ||
* A module that handles read and writing to the disk. | ||
* | ||
* @module settings | ||
* @author Nathan Buchar | ||
* @copyright 2016-2017 Nathan Buchar <hello@nathanbuchar.com> | ||
* @license ISC | ||
*/ | ||
const assert = require('assert'); | ||
const clone = require('clone'); | ||
const electron = require('electron'); | ||
const events = require('events'); | ||
const { EventEmitter } = require('events'); | ||
const fs = require('fs-extra'); | ||
@@ -11,3 +19,3 @@ const path = require('path'); | ||
class Settings extends events.EventEmitter { | ||
class Settings extends EventEmitter { | ||
@@ -66,3 +74,3 @@ constructor() { | ||
} catch (err) { | ||
// File may not exist yet or possible user permissions error. | ||
// File may not exist yet or possible permissions error. | ||
} | ||
@@ -86,8 +94,7 @@ } | ||
}); | ||
// Watch the settings file for changes, if we are not already. | ||
this._watchSettings(); | ||
} catch (err) { | ||
// Something went wrong. | ||
} | ||
this._watchSettings(); | ||
} | ||
@@ -109,4 +116,5 @@ | ||
this._writeSettings(); | ||
return {}; | ||
} | ||
return {}; | ||
} | ||
@@ -123,7 +131,7 @@ | ||
switch (eventType) { | ||
case 'change': { | ||
case Settings.FSWatcherEvents.CHANGE: { | ||
this.emit(Settings.Events.CHANGE); | ||
break; | ||
} | ||
case 'rename': { | ||
case Settings.FSWatcherEvents.RENAME: { | ||
this._fsWatcher.close(); | ||
@@ -152,2 +160,23 @@ this._fsWatcher = null; | ||
/** | ||
* Sets the value at the given key path, or the entire settings object if | ||
* an empty key path is given. | ||
* | ||
* @param {string} keyPath | ||
* @param {any} value | ||
* @param {Object} opts | ||
* @private | ||
*/ | ||
_setValueAtKeyPath(keyPath, value, opts) { | ||
let obj = value; | ||
if (keyPath !== '') { | ||
obj = this._readSettings(); | ||
Helpers.setValueAtKeyPath(obj, keyPath, value); | ||
} | ||
this._writeSettings(obj, opts); | ||
} | ||
/** | ||
* Returns the value at the given key path, or sets the value at that key | ||
@@ -162,3 +191,3 @@ * path to the default value, if provided, if the key does not exist. If an | ||
*/ | ||
_getValueAtKeyPathWithDefaultValue(keyPath, defaultValue) { | ||
_getValueAtKeyPath(keyPath, defaultValue) { | ||
const obj = this._readSettings(); | ||
@@ -171,7 +200,8 @@ | ||
// The key does not exist but a default value does. Set the value at the | ||
// key path to the default value and return a copy of it. | ||
// key path to the default value and then get the new value. | ||
if (!exists && typeof defaultValue !== 'undefined') { | ||
Helpers.setValueAtKeyPath(obj, keyPath, defaultValue); | ||
this._setValueAtKeyPath(keyPath, defaultValue); | ||
return clone(defaultValue); | ||
// Get the new value now that the default has been set. | ||
return this._getValueAtKeyPath(keyPath); | ||
} | ||
@@ -186,22 +216,2 @@ | ||
/** | ||
* Sets the value at the given key path, or the entire settings object if | ||
* an empty key path is given. | ||
* | ||
* @param {string} keyPath | ||
* @param {any} value | ||
* @param {Object} opts | ||
* @private | ||
*/ | ||
_setValueAtKeyPath(keyPath, value, opts) { | ||
if (keyPath === '') { | ||
this._writeSettings(value, opts); | ||
} else { | ||
const obj = this._readSettings(); | ||
Helpers.setValueAtKeyPath(obj, keyPath, value); | ||
this._writeSettings(obj, opts); | ||
} | ||
} | ||
/** | ||
* Deletes the key and value at the given key path, or clears the entire | ||
@@ -216,3 +226,3 @@ * settings object if an empty key path is given. | ||
if (keyPath === '') { | ||
this._writeSettings({}); | ||
this._writeSettings({}, opts); | ||
} else { | ||
@@ -240,3 +250,5 @@ const obj = this._readSettings(); | ||
_watchValueAtKeyPath(keyPath, handler) { | ||
return new Observer(this, keyPath, handler); | ||
const currentValue = this._getValueAtKeyPath(keyPath); | ||
return new Observer(this, keyPath, handler, currentValue); | ||
} | ||
@@ -259,27 +271,2 @@ | ||
/** | ||
* Returns the value at the given key path, or sets the value at that key | ||
* path to the default value, if provided, if the key does not exist. | ||
* | ||
* @param {string} keyPath | ||
* @param {any} [defaultValue] | ||
* @returns {any} | ||
* @public | ||
*/ | ||
get(keyPath, defaultValue) { | ||
assert.strictEqual(typeof keyPath, 'string', 'Key path parameter must be a string. Did you mean to use `getAll()` instead?'); | ||
return this._getValueAtKeyPathWithDefaultValue(keyPath, defaultValue); | ||
} | ||
/** | ||
* Returns all settings. | ||
* | ||
* @returns {Object} | ||
* @public | ||
*/ | ||
getAll() { | ||
return this._getValueAtKeyPathWithDefaultValue(''); | ||
} | ||
/** | ||
* Sets the value at the given key path. | ||
@@ -316,2 +303,27 @@ * | ||
/** | ||
* Returns the value at the given key path, or sets the value at that key | ||
* path to the default value, if provided, if the key does not exist. | ||
* | ||
* @param {string} keyPath | ||
* @param {any} [defaultValue] | ||
* @returns {any} | ||
* @public | ||
*/ | ||
get(keyPath, defaultValue) { | ||
assert.strictEqual(typeof keyPath, 'string', 'Key path parameter must be a string. Did you mean to use `getAll()` instead?'); | ||
return this._getValueAtKeyPath(keyPath, defaultValue); | ||
} | ||
/** | ||
* Returns all settings. | ||
* | ||
* @returns {Object} | ||
* @public | ||
*/ | ||
getAll() { | ||
return this._getValueAtKeyPath(''); | ||
} | ||
/** | ||
* Deletes the key and value at the given key path. | ||
@@ -378,2 +390,13 @@ * | ||
*/ | ||
Settings.FSWatcherEvents = { | ||
CHANGE: 'change', | ||
RENAME: 'rename' | ||
}; | ||
/** | ||
* ElectronSettings event names. | ||
* | ||
* @enum {string} | ||
* @readonly | ||
*/ | ||
Settings.Events = { | ||
@@ -380,0 +403,0 @@ CHANGE: 'change' |
{ | ||
"name": "electron-settings", | ||
"version": "3.0.7", | ||
"description": "A simple persistent user settings manager for Electron.", | ||
"version": "3.0.8", | ||
"description": "A simple persistent user settings framework for Electron.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "npm run test:main && npm run test:renderer", | ||
"test:main": "electron-mocha test", | ||
"test:renderer": "electron-mocha test --renderer" | ||
"test:main": "electron-mocha tests", | ||
"test:renderer": "electron-mocha tests --renderer" | ||
}, | ||
@@ -33,6 +33,6 @@ "repository": { | ||
"deleteAll", | ||
"watch" | ||
"watch", | ||
"file" | ||
], | ||
"dependencies": { | ||
"clone": "^2.1.1", | ||
"deep-equal": "^1.0.1", | ||
@@ -42,3 +42,3 @@ "fs-extra": "^2.1.2" | ||
"devDependencies": { | ||
"electron": "^1.0.0", | ||
"electron": "^1.6.2", | ||
"electron-mocha": "^3.4.0", | ||
@@ -45,0 +45,0 @@ "eslint": "^3.18.0", |
electron-settings | ||
================= | ||
A simple persistent user settings manager for [Electron][external_electron]. | ||
The settings manager of choice for [Electron's own demo app](https://github.com/electron/electron-api-demos) and originally adapted from Atom's own configuration manager, electron-settings allows you to save your users' settings to the disk so that they can be loaded in the next time your app starts without skipping a beat. | ||
Also, you can [subscribe to settings and get notified when their value changes][api_method_watch]. So that's pretty neat. | ||
[![npm version](https://badge.fury.io/js/electron-settings.svg)](http://badge.fury.io/js/electron-settings) | ||
[![Npm Downloads](https://img.shields.io/npm/dm/electron-settings.svg)](https://npmjs.org/package/electron-settings) | ||
[![dependencies](https://david-dm.org/nathanbuchar/electron-settings.svg)](https://david-dm.org/nathanbuchar/electron-settings) | ||
@@ -15,6 +10,9 @@ [![Build Status](https://travis-ci.org/nathanbuchar/electron-settings.svg?branch=master)](https://travis-ci.org/nathanbuchar/electron-settings) | ||
A simple persistent user settings framework for [Electron](https://electron.atom.io). | ||
Originally adapted from Atom's own configuration manager and the settings manager of choice for [Electron's own demo app](https://github.com/electron/electron-api-demos), electron-settings allows you to persist user settings and other data simply and easily. | ||
*** | ||
Also, you can [subscribe to properties](./wiki/API-documentation#watch) and get notified when their values change. So that's pretty nifty. | ||
<br/> | ||
@@ -46,26 +44,23 @@ | ||
// => false | ||
settings.file(); | ||
// => /Users/Nathan/Application\ Support/MyApp/Settings | ||
``` | ||
FAQs | ||
---- | ||
A list of frequently asked questions can be found [here][docs_faq]. | ||
Resources | ||
--------- | ||
* [Wiki][wiki_home] | ||
* [API Documentation][wiki_api] | ||
* [FAQs][wiki_faq] | ||
* [Changelog][wiki_changelog] | ||
* [License (ISC)][license] | ||
API | ||
--- | ||
The API docs can be found [here][docs_api]. | ||
<br/> | ||
<br/> | ||
<hr/> | ||
License | ||
------- | ||
[ISC][license] | ||
<small>**Having trouble?** [Get help on Gitter](https://gitter.im/nathanbuchar/electron-settings).</small> | ||
*** | ||
<small>**Having trouble?** [Get help on Gitter][external_gitter].</small> | ||
@@ -75,29 +70,7 @@ | ||
[license]: ./LICENSE.md | ||
[Nathan Buchar]: mailto:hello@nathanbuchar.com | ||
[section_install]: #install | ||
[section_demo]: #demo | ||
[section_faqs]: #faq | ||
[section_api]: #api | ||
[section_authors]: #authors | ||
[section_license]: #license | ||
[docs_api]: ./docs/api.md | ||
[docs_faq]: ./docs/faq.md | ||
[api_method_has]: ./docs/api.md#has | ||
[api_method_get]: ./docs/api.md#get | ||
[api_method_get-all]: ./docs/api.md#getall | ||
[api_method_set]: ./docs/api.md#set | ||
[api_method_set-all]: ./docs/api.md#setall | ||
[api_method_delete]: ./docs/api.md#delete | ||
[api_method_delete-all]: ./docs/api.md#deleteall | ||
[api_method_watch]: ./docs/api.md#watch | ||
[api_method_file]: ./docs/api.md#file | ||
[external_electron]: https://electron.atom.io | ||
[external_gitter]: https://gitter.im/nathanbuchar/electron-settings | ||
[wiki_home]: ./wiki | ||
[wiki_api]: ./wiki/API-documentation | ||
[wiki_faq]: ./wiki/FAQs | ||
[wiki_changelog]: ./wiki/Changelog |
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
27698
2
10
772
74
2
- Removedclone@^2.1.1
- Removedclone@2.1.2(transitive)