From bbcb2712f123b025fdea260d33645ebc36171191 Mon Sep 17 00:00:00 2001 From: indexzero Date: Wed, 23 Nov 2011 16:40:18 -0500 Subject: [PATCH] [api fix] Dont eagerly create config files in `.load()` and `.loadSync()` --- lib/nconf/stores/file.js | 55 ++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 33 deletions(-) diff --git a/lib/nconf/stores/file.js b/lib/nconf/stores/file.js index 4e88912..4c144db 100644 --- a/lib/nconf/stores/file.js +++ b/lib/nconf/stores/file.js @@ -81,49 +81,38 @@ File.prototype.load = function (callback) { path.exists(self.file, function (exists) { if (!exists) { - // - // If the path we are attempting to load doesn't exist, create it - // - self.save({}, function (err) { - self.store = {}; - return callback(err, self.store); - }); - } - else { - // - // Else, the path exists, read it from disk - // - fs.readFile(self.file, function (err, data) { - if (err) { - return callback(err); - } - - try { - self.store = self.format.parse(data.toString()); - } - catch (ex) { - return callback(new Error("Error parsing your JSON configuration file.")); - } - - callback(null, self.store); - }); + return callback(null, {}); } + + // + // Else, the path exists, read it from disk + // + fs.readFile(self.file, function (err, data) { + if (err) { + return callback(err); + } + + try { + self.store = self.format.parse(data.toString()); + } + catch (ex) { + return callback(new Error("Error parsing your JSON configuration file.")); + } + + callback(null, self.store); + }); }); }; // -// ### function load (callback) -// #### @callback {function} **Optional** Continuation to respond to when complete. -// Attempts to load the data stored in `this.file` synchronously and responds appropriately. +// ### function loadSync (callback) +// Attempts to load the data stored in `this.file` synchronously +// and responds appropriately. // File.prototype.loadSync = function () { var data, self = this; if (!path.existsSync(self.file)) { - // - // If the path we are attempting to load doesn't exist, create it - // - self.saveSync({}); self.store = {}; data = {}; }