From 32d04a058af97febf1c5aa22643e6a2e42402338 Mon Sep 17 00:00:00 2001 From: Jason Dent Date: Fri, 21 Jul 2017 22:16:09 +0200 Subject: [PATCH 1/3] Do not create a file on read if it doesn't exist. - Only update if there is something to do. - Add types --- index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 4883613..310bab8 100644 --- a/index.js +++ b/index.js @@ -22,7 +22,9 @@ class Configstore { path.join('configstore', `${id}.json`); this.path = path.join(configDir, pathPrefix); - this.all = Object.assign({}, defaults, this.all); + if (defaults) { + this.all = Object.assign({}, defaults, this.all); + } } get all() { try { @@ -30,7 +32,6 @@ class Configstore { } catch (err) { // Create dir if it doesn't exist if (err.code === 'ENOENT') { - makeDir.sync(path.dirname(this.path), makeDirOptions); return {}; } From 3ddb25652b73e0fc0781d0a3792705309626c612 Mon Sep 17 00:00:00 2001 From: Jason Dent Date: Thu, 25 Jan 2018 23:04:40 +0100 Subject: [PATCH 2/3] Add unit test to make sure the file is not created. --- test.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/test.js b/test.js index 189950b..523d4f3 100644 --- a/test.js +++ b/test.js @@ -5,7 +5,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'); }); @@ -109,6 +109,20 @@ test('support global namespace path 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); + } +} From 98fd8230f23ea65e3c181a071a97c64099cd076b Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sat, 28 Jul 2018 22:52:11 +0700 Subject: [PATCH 3/3] Update index.js --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 9c9dcb3..52dc976 100644 --- a/index.js +++ b/index.js @@ -24,7 +24,7 @@ class Configstore { this.path = opts.configPath || path.join(configDir, pathPrefix); if (defaults) { - this.all = Object.assign({}, defaults, this.all); + this.all = Object.assign({}, defaults, this.all); } }