Skip to content

Commit

Permalink
Add clearInvalidConfig option
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Feb 11, 2019
1 parent e18a4a3 commit d725709
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ module.exports = class Conf {
configName: 'config',
fileExtension: 'json',
projectSuffix: 'nodejs',
clearInvalidConfig: true,
serialize: value => JSON.stringify(value, null, '\t'),
deserialize: JSON.parse,
...options
Expand All @@ -59,6 +60,8 @@ module.exports = class Conf {
options.cwd = envPaths(options.projectName, {suffix: options.projectSuffix}).config;
}

this._options = options;

this.events = new EventEmitter();
this.encryptionKey = options.encryptionKey;
this.serialize = options.serialize;
Expand Down Expand Up @@ -173,7 +176,7 @@ module.exports = class Conf {
return plainObject();
}

if (error.name === 'SyntaxError') {
if (this._options.clearInvalidConfig && error.name === 'SyntaxError') {
return plainObject();
}

Expand Down
11 changes: 9 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,20 @@ When specified, the store will be encrypted using the [`aes-256-cbc`](https://en

#### fileExtension

type: `string`<br>
Type: `string`<br>
Default: `json`

Extension of the config file.

You would usually not need this, but could be useful if you want to interact with a file with a custom file extension that can be associated with your app. These might be simple save/export/preference files that are intended to be shareable or saved outside of the app.

#### clearInvalidConfig

Type: `boolean`<br>
Default: `true`

The config is cleared if reading the config file causes a `SyntaxError`. This is a good default, as the config file is not intended to be hand-edited, so it usually means the config is corrupt and there's nothing the user can do about it anyway. However, if you let the user edit the config file directly, mistakes might happen and it could be more useful to throw an error when the config is invalid instead of clearing. Disabling this option will make it throw a `SyntaxError` on invalid config instead of clearing.

#### serialize

Type: `Function`<br>
Expand All @@ -118,7 +125,7 @@ You would usually not need this, but it could be useful if you want to use a for

#### deserialize

type: `Function`<br>
Type: `Function`<br>
Default: `JSON.parse`

Function to deserialize the config object from a UTF-8 string when reading the config file.
Expand Down
15 changes: 15 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,3 +419,18 @@ test('doesn\'t write to disk upon instanciation if and only if the store didn\'t
exists = fs.existsSync(conf.path);
t.is(exists, true);
});

test('`clearInvalidConfig` option - invalid data', t => {
const conf = new Conf({cwd: tempy.directory(), clearInvalidConfig: false});
fs.writeFileSync(conf.path, '🦄');

t.throws(() => {
conf.store; // eslint-disable-line no-unused-expressions
}, {instanceOf: SyntaxError});
});

test('`clearInvalidConfig` option - valid data', t => {
const conf = new Conf({cwd: tempy.directory(), clearInvalidConfig: false});
conf.set('foo', 'bar');
t.deepEqual(conf.store, {foo: 'bar'});
});

0 comments on commit d725709

Please sign in to comment.