diff --git a/.gitattributes b/.gitattributes index 391f0a4..6313b56 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1 @@ -* text=auto -*.js text eol=lf +* text=auto eol=lf diff --git a/.travis.yml b/.travis.yml index 7d69d74..2ae9d62 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,5 @@ language: node_js node_js: + - '10' - '8' - '6' - - '4' diff --git a/index.js b/index.js index 52dc976..cd8f19f 100644 --- a/index.js +++ b/index.js @@ -14,57 +14,55 @@ const makeDirOptions = {mode: 0o0700}; const writeFileOptions = {mode: 0o0600}; class Configstore { - constructor(id, defaults, opts) { - opts = opts || {}; - - const pathPrefix = opts.globalConfigPath ? + constructor(id, defaults, options = {}) { + const pathPrefix = options.globalConfigPath ? path.join(id, 'config.json') : path.join('configstore', `${id}.json`); - this.path = opts.configPath || path.join(configDir, pathPrefix); + this.path = options.configPath || path.join(configDir, pathPrefix); if (defaults) { - this.all = Object.assign({}, defaults, this.all); + this.all = Object.assign({}, defaults, this.all); } } get all() { try { return JSON.parse(fs.readFileSync(this.path, 'utf8')); - } catch (err) { - // Create dir if it doesn't exist - if (err.code === 'ENOENT') { + } catch (error) { + // Create directory if it doesn't exist + if (error.code === 'ENOENT') { return {}; } // Improve the message of permission errors - if (err.code === 'EACCES') { - err.message = `${err.message}\n${permissionError}\n`; + if (error.code === 'EACCES') { + error.message = `${error.message}\n${permissionError}\n`; } // Empty the file if it encounters invalid JSON - if (err.name === 'SyntaxError') { + if (error.name === 'SyntaxError') { writeFileAtomic.sync(this.path, '', writeFileOptions); return {}; } - throw err; + throw error; } } - set all(val) { + set all(value) { try { // Make sure the folder exists as it could have been deleted in the meantime makeDir.sync(path.dirname(this.path), makeDirOptions); - writeFileAtomic.sync(this.path, JSON.stringify(val, null, '\t'), writeFileOptions); - } catch (err) { + writeFileAtomic.sync(this.path, JSON.stringify(value, null, '\t'), writeFileOptions); + } catch (error) { // Improve the message of permission errors - if (err.code === 'EACCES') { - err.message = `${err.message}\n${permissionError}\n`; + if (error.code === 'EACCES') { + error.message = `${error.message}\n${permissionError}\n`; } - throw err; + throw error; } } @@ -76,7 +74,7 @@ class Configstore { return dotProp.get(this.all, key); } - set(key, val) { + set(key, value) { const config = this.all; if (arguments.length === 1) { @@ -84,7 +82,7 @@ class Configstore { dotProp.set(config, k, key[k]); } } else { - dotProp.set(config, key, val); + dotProp.set(config, key, value); } this.all = config; diff --git a/license b/license index be304e2..1043c8b 100644 --- a/license +++ b/license @@ -1,9 +1,25 @@ -Copyright Sindre Sorhus (sindresorhus.com) +BSD 2-Clause License -Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: +Copyright (c) Google +All rights reserved. -1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: -2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/package.json b/package.json index 6a862f5..4bc8cf9 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "url": "sindresorhus.com" }, "engines": { - "node": ">=4" + "node": ">=6" }, "scripts": { "test": "xo && ava" diff --git a/readme.md b/readme.md index 3dbb494..797d6a0 100644 --- a/readme.md +++ b/readme.md @@ -8,6 +8,13 @@ Example: `~/.config/configstore/some-id.json` *If you need this for Electron, check out [`electron-store`](https://github.com/sindresorhus/electron-store) instead.* +## Install + +``` +$ npm install configstore +``` + + ## Usage ```js diff --git a/test.js b/test.js index c459d9a..5f9fe65 100644 --- a/test.js +++ b/test.js @@ -6,6 +6,12 @@ import Configstore from '.'; const configstorePath = new Configstore('configstore-test').path; +const cleanUpFile = () => { + if (fs.existsSync(configstorePath)) { + fs.unlinkSync(configstorePath); + } +}; + test.beforeEach(t => { cleanUpFile(); t.context.conf = new Configstore('configstore-test'); @@ -129,9 +135,3 @@ test('the store is NOT created until write', t => { conf.set('foo', 'bar'); t.true(fs.existsSync(conf.path)); }); - -function cleanUpFile() { - if (fs.existsSync(configstorePath)) { - fs.unlinkSync(configstorePath); - } -}