Skip to content
Permalink
Browse files

Do not create a file on read if it doesn't exist (#57)

  • Loading branch information
Jason3S authored and sindresorhus committed Jul 28, 2018
1 parent 0dc1a8f commit b8d6372dc3fde21d419eef1dd42577fd65c6c362
Showing with 20 additions and 4 deletions.
  1. +4 −2 index.js
  2. +16 −2 test.js
@@ -22,7 +22,10 @@ class Configstore {
path.join('configstore', `${id}.json`);

this.path = opts.configPath || path.join(configDir, pathPrefix);
this.all = Object.assign({}, defaults, this.all);

if (defaults) {
this.all = Object.assign({}, defaults, this.all);
}
}

get all() {
@@ -31,7 +34,6 @@ class Configstore {
} catch (err) {
// Create dir if it doesn't exist
if (err.code === 'ENOENT') {
makeDir.sync(path.dirname(this.path), makeDirOptions);
return {};
}

18 test.js
@@ -7,7 +7,7 @@ import Configstore from '.';
const configstorePath = new Configstore('configstore-test').path;

test.beforeEach(t => {
fs.unlinkSync(configstorePath);
cleanUpFile();
t.context.conf = new Configstore('configstore-test');
});

@@ -118,6 +118,20 @@ test('support `configPath` option', t => {
});

test('ensure `.all` is always an object', t => {
fs.unlinkSync(configstorePath);
cleanUpFile();
t.notThrows(() => t.context.conf.get('foo'));
});

test('the store is NOT created until write', t => {
cleanUpFile();
const conf = new Configstore('configstore-test');
t.false(fs.existsSync(conf.path));
conf.set('foo', 'bar');
t.true(fs.existsSync(conf.path));
});

function cleanUpFile() {
if (fs.existsSync(configstorePath)) {
fs.unlinkSync(configstorePath);
}
}

0 comments on commit b8d6372

Please sign in to comment.
You can’t perform that action at this time.