diff --git a/index.js b/index.js index cd8f19f..8a030f0 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,7 @@ const writeFileAtomic = require('write-file-atomic'); const dotProp = require('dot-prop'); const uniqueString = require('unique-string'); -const configDir = xdgBasedir.config || path.join(os.tmpdir(), uniqueString()); +const configDirectory = xdgBasedir.config || path.join(os.tmpdir(), uniqueString()); const permissionError = 'You don\'t have access to this file.'; const makeDirOptions = {mode: 0o0700}; const writeFileOptions = {mode: 0o0600}; @@ -19,7 +19,7 @@ class Configstore { path.join(id, 'config.json') : path.join('configstore', `${id}.json`); - this.path = options.configPath || path.join(configDir, pathPrefix); + this.path = options.configPath || path.join(configDirectory, pathPrefix); if (defaults) { this.all = Object.assign({}, defaults, this.all); diff --git a/package.json b/package.json index d14913f..8dc4154 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,6 @@ "config", "store", "storage", - "conf", "configuration", "settings", "preferences", @@ -35,13 +34,13 @@ "dependencies": { "dot-prop": "^4.1.0", "graceful-fs": "^4.1.2", - "make-dir": "^1.0.0", + "make-dir": "^2.1.0", "unique-string": "^1.0.0", "write-file-atomic": "^2.0.0", "xdg-basedir": "^3.0.0" }, "devDependencies": { - "ava": "*", - "xo": "*" + "ava": "^1.3.1", + "xo": "^0.24.0" } } diff --git a/readme.md b/readme.md index 13298a3..e615328 100644 --- a/readme.md +++ b/readme.md @@ -2,7 +2,7 @@ > Easily load and persist config without having to think about where and how -Config is stored in a JSON file located in `$XDG_CONFIG_HOME` or `~/.config`.
+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.*
@@ -20,26 +20,25 @@ $ npm install configstore ```js const Configstore = require('configstore'); -const pkg = require('./package.json'); +const packageJson = require('./package.json'); -// create a Configstore instance with an unique ID e.g. -// Package name and optionally some default values -const conf = new Configstore(pkg.name, {foo: 'bar'}); +// Create a Configstore instance +const config = new Configstore(packageJson.name, {foo: 'bar'}); -console.log(conf.get('foo')); +console.log(config.get('foo')); //=> 'bar' -conf.set('awesome', true); -console.log(conf.get('awesome')); +config.set('awesome', true); +console.log(config.get('awesome')); //=> true // Use dot-notation to access nested properties -conf.set('bar.baz', true); -console.log(conf.get('bar')); +config.set('bar.baz', true); +console.log(config.get('bar')); //=> {baz: true} -conf.delete('awesome'); -console.log(conf.get('awesome')); +config.delete('awesome'); +console.log(config.get('awesome')); //=> undefined ``` @@ -135,7 +134,7 @@ Get the path to the config file. Can be used to show the user where the config f Get all the config as an object or replace the current config with an object: ```js -conf.all = { +config.all = { hello: 'world' }; ``` @@ -144,9 +143,3 @@ conf.all = { ## Security To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. - - -## License - -[BSD license](http://opensource.org/licenses/bsd-license.php)
-Copyright Google diff --git a/test.js b/test.js index 5f9fe65..9e443c2 100644 --- a/test.js +++ b/test.js @@ -14,20 +14,20 @@ const cleanUpFile = () => { test.beforeEach(t => { cleanUpFile(); - t.context.conf = new Configstore('configstore-test'); + t.context.config = new Configstore('configstore-test'); }); test('.set() and .get()', t => { - const {conf} = t.context; - conf.set('foo', 'bar'); - conf.set('baz.boo', true); - t.is(conf.get('foo'), 'bar'); - t.is(conf.get('baz.boo'), true); + const {config} = t.context; + config.set('foo', 'bar'); + config.set('baz.boo', true); + t.is(config.get('foo'), 'bar'); + t.is(config.get('baz.boo'), true); }); test('.set() with object and .get()', t => { - const {conf} = t.context; - conf.set({ + const {config} = t.context; + config.set({ foo1: 'bar1', foo2: 'bar2', baz: { @@ -37,101 +37,105 @@ test('.set() with object and .get()', t => { } } }); - t.is(conf.get('foo1'), 'bar1'); - t.is(conf.get('foo2'), 'bar2'); - t.deepEqual(conf.get('baz'), { + t.is(config.get('foo1'), 'bar1'); + t.is(config.get('foo2'), 'bar2'); + t.deepEqual(config.get('baz'), { boo: 'foo', foo: { bar: 'baz' } }); - t.is(conf.get('baz.boo'), 'foo'); - t.deepEqual(conf.get('baz.foo'), {bar: 'baz'}); - t.is(conf.get('baz.foo.bar'), 'baz'); + t.is(config.get('baz.boo'), 'foo'); + t.deepEqual(config.get('baz.foo'), {bar: 'baz'}); + t.is(config.get('baz.foo.bar'), 'baz'); }); test('.has()', t => { - const {conf} = t.context; - conf.set('foo', '🦄'); - conf.set('baz.boo', '🦄'); - t.true(conf.has('foo')); - t.true(conf.has('baz.boo')); - t.false(conf.has('missing')); + const {config} = t.context; + config.set('foo', '🦄'); + config.set('baz.boo', '🦄'); + t.true(config.has('foo')); + t.true(config.has('baz.boo')); + t.false(config.has('missing')); }); test('.delete()', t => { - const {conf} = t.context; - conf.set('foo', 'bar'); - conf.set('baz.boo', true); - conf.set('baz.foo.bar', 'baz'); - conf.delete('foo'); - t.not(conf.get('foo'), 'bar'); - conf.delete('baz.boo'); - t.not(conf.get('baz.boo'), true); - conf.delete('baz.foo'); - t.not(conf.get('baz.foo'), {bar: 'baz'}); - conf.set('foo.bar.baz', {awesome: 'icecream'}); - conf.set('foo.bar.zoo', {awesome: 'redpanda'}); - conf.delete('foo.bar.baz'); - t.is(conf.get('foo.bar.zoo.awesome'), 'redpanda'); + const {config} = t.context; + config.set('foo', 'bar'); + config.set('baz.boo', true); + config.set('baz.foo.bar', 'baz'); + config.delete('foo'); + t.not(config.get('foo'), 'bar'); + config.delete('baz.boo'); + t.not(config.get('baz.boo'), true); + config.delete('baz.foo'); + t.not(config.get('baz.foo'), {bar: 'baz'}); + config.set('foo.bar.baz', {awesome: 'icecream'}); + config.set('foo.bar.zoo', {awesome: 'redpanda'}); + config.delete('foo.bar.baz'); + t.is(config.get('foo.bar.zoo.awesome'), 'redpanda'); }); test('.clear()', t => { - const {conf} = t.context; - conf.set('foo', 'bar'); - conf.set('foo1', 'bar1'); - conf.set('baz.boo', true); - conf.clear(); - t.is(conf.size, 0); + const {config} = t.context; + config.set('foo', 'bar'); + config.set('foo1', 'bar1'); + config.set('baz.boo', true); + config.clear(); + t.is(config.size, 0); }); test('.all', t => { - const {conf} = t.context; - conf.set('foo', 'bar'); - conf.set('baz.boo', true); - t.is(conf.all.foo, 'bar'); - t.deepEqual(conf.all.baz, {boo: true}); + const {config} = t.context; + config.set('foo', 'bar'); + config.set('baz.boo', true); + t.is(config.all.foo, 'bar'); + t.deepEqual(config.all.baz, {boo: true}); }); test('.size', t => { - const {conf} = t.context; - conf.set('foo', 'bar'); - t.is(conf.size, 1); + const {config} = t.context; + config.set('foo', 'bar'); + t.is(config.size, 1); }); test('.path', t => { - const {conf} = t.context; - conf.set('foo', 'bar'); - t.true(fs.existsSync(conf.path)); + const {config} = t.context; + config.set('foo', 'bar'); + t.true(fs.existsSync(config.path)); }); test('use default value', t => { - const conf = new Configstore('configstore-test', {foo: 'bar'}); - t.is(conf.get('foo'), 'bar'); + const config = new Configstore('configstore-test', {foo: 'bar'}); + t.is(config.get('foo'), 'bar'); }); test('support `globalConfigPath` option', t => { - const conf = new Configstore('configstore-test', {}, {globalConfigPath: true}); - const regex = /configstore-test(\/|\\)config.json$/; - t.true(regex.test(conf.path)); + const config = new Configstore('configstore-test', {}, {globalConfigPath: true}); + t.regex(config.path, /configstore-test(\/|\\)config.json$/); }); test('support `configPath` option', t => { const customPath = path.join(os.tmpdir(), 'configstore-custom-path', 'foo.json'); - const conf = new Configstore('ignored-namespace', {}, {globalConfigPath: true, configPath: customPath}); - const regex = /configstore-custom-path(\/|\\)foo.json$/; - t.true(regex.test(conf.path)); + const config = new Configstore('ignored-namespace', {}, { + globalConfigPath: true, + configPath: customPath + }); + t.regex(config.path, /configstore-custom-path(\/|\\)foo.json$/); }); test('ensure `.all` is always an object', t => { cleanUpFile(); - t.notThrows(() => t.context.conf.get('foo')); + + t.notThrows(() => { + t.context.config.get('foo'); + }); }); test('the store is NOT created until write', t => { cleanUpFile(); - const conf = new Configstore('configstore-test'); - t.false(fs.existsSync(conf.path)); - conf.set('foo', 'bar'); - t.true(fs.existsSync(conf.path)); + const config = new Configstore('configstore-test'); + t.false(fs.existsSync(config.path)); + config.set('foo', 'bar'); + t.true(fs.existsSync(config.path)); });