diff --git a/lib/nconf.js b/lib/nconf.js index 8fc5750..399d6f7 100644 --- a/lib/nconf.js +++ b/lib/nconf.js @@ -69,6 +69,16 @@ nconf.clear = function (key, callback) { // Responds with an Object representing all keys associated in this instance. // nconf.load = function (callback) { + // + // If we don't have a callback and the current + // store is capable of loading synchronously + // then do so. + // + if (!callback && nconf.store.loadSync) { + return nconf.store.loadSync(); + } + + if (!nconf.store.load) { var error = new Error('nconf store ' + nconf.store.type + ' has no load() method'); if (callback) { @@ -92,6 +102,15 @@ nconf.save = function (value, callback) { if (!callback) { callback = value; value = null; + + // + // If we still don't have a callback and the + // current store is capable of saving synchronously + // then do so. + // + if (!callback && nconf.store.saveSync) { + return nconf.store.saveSync(); + } } if (!nconf.store.save) { diff --git a/lib/nconf/stores/file.js b/lib/nconf/stores/file.js index 28f5d9f..33fa1c1 100644 --- a/lib/nconf/stores/file.js +++ b/lib/nconf/stores/file.js @@ -53,6 +53,36 @@ File.prototype.save = function (value, callback) { }); }; +// +// ### function saveSync (value, callback) +// #### @value {Object} _Ignored_ Left here for consistency +// #### @callback {function} **Optional** Continuation to respond to when complete. +// Saves the current configuration object to disk at `this.file` +// using the format specified by `this.format` synchronously. +// +File.prototype.saveSync = function (value, callback) { + if (!callback) { + callback = value; + value = null; + } + + var err; + try { + fs.writeFileSync(this.file, this.format.stringify(this.store)); + } + catch (ex) { + err = ex; + } + + if (callback) { + return callback(err); + } + + if (err) { + throw err; + } +}; + // // ### function load (callback) // #### @callback {function} Continuation to respond to when complete. @@ -69,4 +99,31 @@ File.prototype.load = function (callback) { self.store = data; 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. +// +File.prototype.loadSync = function (callback) { + var err, data; + + try { + data = fs.readFileSync(this.file, 'utf8'); + this.store = this.format.parse(data); + } + catch (ex) { + err = ex; + } + + if (callback) { + return callback(err, data); + } + + if (err) { + throw err; + } + + return data; }; \ No newline at end of file diff --git a/test/file-store-test.js b/test/file-store-test.js index 2d2eacb..afd668d 100644 --- a/test/file-store-test.js +++ b/test/file-store-test.js @@ -38,7 +38,7 @@ vows.describe('nconf/stores/file').addBatch({ topic: function () { var tmpPath = path.join(__dirname, 'fixtures', 'tmp.json'), tmpStore = new nconf.stores.File({ file: tmpPath }); - return tmpStore + return tmpStore; }, "the save() method": { topic: function (tmpStore) {