Skip to content

Commit

Permalink
Make the clearInvalidConfig option false by default
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jan 22, 2021
1 parent a96e9d7 commit b291021
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
4 changes: 2 additions & 2 deletions readme.md
Expand Up @@ -187,9 +187,9 @@ You would usually not need this, but could be useful if you want to interact wit
#### clearInvalidConfig

Type: `boolean`\
Default: `true`
Default: `false`

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.
The config is cleared if reading the config file causes a `SyntaxError`. This is a good behavior for unimportant data, 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.

#### serialize

Expand Down
2 changes: 1 addition & 1 deletion source/index.ts
Expand Up @@ -56,7 +56,7 @@ class Conf<T extends Record<string, any> = Record<string, unknown>> implements I
configName: 'config',
fileExtension: 'json',
projectSuffix: 'nodejs',
clearInvalidConfig: true,
clearInvalidConfig: false,
accessPropertiesByDotNotation: true,
...partialOptions
};
Expand Down
4 changes: 2 additions & 2 deletions source/types.ts
Expand Up @@ -131,9 +131,9 @@ export interface Options<T> {
fileExtension?: string;

/**
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.
The config is cleared if reading the config file causes a `SyntaxError`. This is a good behavior for unimportant data, 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.
@default true
@default false
*/
clearInvalidConfig?: boolean;

Expand Down
20 changes: 17 additions & 3 deletions test/index.ts
Expand Up @@ -406,7 +406,11 @@ test('fallback to cwd if `module.filename` is `null`', t => {
});

test('encryption', t => {
const config = new Conf({cwd: tempy.directory(), encryptionKey: 'abc123'});
const config = new Conf({
cwd: tempy.directory(),
encryptionKey: 'abc123'
});

t.is(config.get('foo'), undefined);
t.is(config.get('foo', '🐴'), '🐴');
config.set('foo', fixture);
Expand All @@ -429,13 +433,23 @@ test('encryption - upgrade', t => {
test('encryption - corrupt file', t => {
const cwd = tempy.directory();

const before = new Conf({cwd, encryptionKey: 'abc123'});
const before = new Conf({
cwd,
encryptionKey: 'abc123',
clearInvalidConfig: true
});

before.set('foo', fixture);
t.is(before.get('foo'), fixture);

fs.appendFileSync(path.join(cwd, 'config.json'), 'corrupt file');

const after = new Conf({cwd, encryptionKey: 'abc123'});
const after = new Conf({
cwd,
encryptionKey: 'abc123',
clearInvalidConfig: true
});

t.is(after.get('foo'), undefined);
});

Expand Down

0 comments on commit b291021

Please sign in to comment.