Skip to content

Commit

Permalink
Do not create a file on read if it doesn't exist (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S authored and sindresorhus committed Jul 28, 2018
1 parent 0dc1a8f commit b8d6372
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand 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 {};
}

Expand Down
18 changes: 16 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});

Expand Down Expand Up @@ -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.