configstore
Advanced tools
Comparing version 3.1.2 to 4.0.0
44
index.js
@@ -17,11 +17,12 @@ 'use strict'; | ||
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 = path.join(configDir, pathPrefix); | ||
this.all = Object.assign({}, defaults, this.all); | ||
this.path = options.configPath || path.join(configDir, pathPrefix); | ||
if (defaults) { | ||
this.all = Object.assign({}, defaults, this.all); | ||
} | ||
} | ||
@@ -32,6 +33,5 @@ | ||
return JSON.parse(fs.readFileSync(this.path, 'utf8')); | ||
} catch (err) { | ||
// Create dir if it doesn't exist | ||
if (err.code === 'ENOENT') { | ||
makeDir.sync(path.dirname(this.path), makeDirOptions); | ||
} catch (error) { | ||
// Create directory if it doesn't exist | ||
if (error.code === 'ENOENT') { | ||
return {}; | ||
@@ -41,8 +41,8 @@ } | ||
// 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); | ||
@@ -52,7 +52,7 @@ return {}; | ||
throw err; | ||
throw error; | ||
} | ||
} | ||
set all(val) { | ||
set all(value) { | ||
try { | ||
@@ -62,10 +62,10 @@ // Make sure the folder exists as it could have been deleted in the meantime | ||
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; | ||
} | ||
@@ -82,3 +82,3 @@ } | ||
set(key, val) { | ||
set(key, value) { | ||
const config = this.all; | ||
@@ -91,3 +91,3 @@ | ||
} else { | ||
dotProp.set(config, key, val); | ||
dotProp.set(config, key, value); | ||
} | ||
@@ -94,0 +94,0 @@ |
{ | ||
"name": "configstore", | ||
"version": "3.1.2", | ||
"version": "4.0.0", | ||
"description": "Easily load and save config without having to think about where and how", | ||
@@ -13,3 +13,3 @@ "license": "BSD-2-Clause", | ||
"engines": { | ||
"node": ">=4" | ||
"node": ">=6" | ||
}, | ||
@@ -16,0 +16,0 @@ "scripts": { |
@@ -11,2 +11,9 @@ # configstore [![Build Status](https://travis-ci.org/yeoman/configstore.svg?branch=master)](https://travis-ci.org/yeoman/configstore) | ||
## Install | ||
``` | ||
$ npm install configstore | ||
``` | ||
## Usage | ||
@@ -67,2 +74,11 @@ | ||
##### configPath | ||
Type: `string`<br> | ||
Default: Automatic | ||
**Please don't use this option unless absolutely necessary and you know what you're doing.** | ||
Set the path of the config file. Overrides the `packageName` and `globalConfigPath` options. | ||
### Instance | ||
@@ -69,0 +85,0 @@ |
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
7164
133