Skip to content

Commit

Permalink
Require Node.js 6
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jul 28, 2018
1 parent b8d6372 commit d9b3257
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 36 deletions.
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: node_js
node_js:
- '10'
- '8'
- '6'
- '4'
40 changes: 19 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand All @@ -76,15 +74,15 @@ class Configstore {
return dotProp.get(this.all, key);
}

set(key, val) {
set(key, value) {
const config = this.all;

if (arguments.length === 1) {
for (const k of Object.keys(key)) {
dotProp.set(config, k, key[k]);
}
} else {
dotProp.set(config, key, val);
dotProp.set(config, key, value);
}

this.all = config;
Expand Down
26 changes: 21 additions & 5 deletions license
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
Copyright Sindre Sorhus <[email protected]> (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.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
"node": ">=6"
},
"scripts": {
"test": "xo && ava"
Expand Down
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
}
}

0 comments on commit d9b3257

Please sign in to comment.