diff --git a/index.js b/index.js index 58d8a03..44b9c1c 100644 --- a/index.js +++ b/index.js @@ -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 @@ -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; @@ -173,7 +176,7 @@ module.exports = class Conf { return plainObject(); } - if (error.name === 'SyntaxError') { + if (this._options.clearInvalidConfig && error.name === 'SyntaxError') { return plainObject(); } diff --git a/readme.md b/readme.md index 0b434a1..0c84b31 100644 --- a/readme.md +++ b/readme.md @@ -100,13 +100,20 @@ When specified, the store will be encrypted using the [`aes-256-cbc`](https://en #### fileExtension -type: `string`
+Type: `string`
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`
+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`
@@ -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`
+Type: `Function`
Default: `JSON.parse` Function to deserialize the config object from a UTF-8 string when reading the config file. diff --git a/test.js b/test.js index 890cf5b..09d80b0 100644 --- a/test.js +++ b/test.js @@ -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'}); +});