[api] Add .saveSync()
and .loadSync()
methods to File store
This commit is contained in:
parent
b9951b44c1
commit
d65922dc7a
3 changed files with 77 additions and 1 deletions
19
lib/nconf.js
19
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) {
|
||||
|
|
|
@ -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.
|
||||
|
@ -70,3 +100,30 @@ File.prototype.load = function (callback) {
|
|||
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;
|
||||
};
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue