Save conf to dedicated file (#283)
* Added support for saving configuration to a specific file Added support for saving configuration to a specific file * add test to cover the save to file feature * add posibility to specify a format to save to file * add a test with nconf-yaml to ensure specifying a format works
This commit is contained in:
parent
52e0a3566e
commit
b41c505c6e
3 changed files with 69 additions and 5 deletions
|
@ -74,12 +74,24 @@ util.inherits(File, Memory);
|
||||||
// using the format specified by `this.format`.
|
// using the format specified by `this.format`.
|
||||||
//
|
//
|
||||||
File.prototype.save = function (value, callback) {
|
File.prototype.save = function (value, callback) {
|
||||||
|
this.saveToFile(this.file, value, callback);
|
||||||
|
};
|
||||||
|
|
||||||
|
//
|
||||||
|
// ### function saveToFile (path, value, callback)
|
||||||
|
// #### @path {string} The path to the file where we save the configuration to
|
||||||
|
// #### @format {Object} Optional formatter, default behing the one of the store
|
||||||
|
// #### @callback {function} Continuation to respond to when complete.
|
||||||
|
// Saves the current configuration object to disk at `this.file`
|
||||||
|
// using the format specified by `this.format`.
|
||||||
|
//
|
||||||
|
File.prototype.saveToFile = function (path, format, callback) {
|
||||||
if (!callback) {
|
if (!callback) {
|
||||||
callback = value;
|
callback = format;
|
||||||
value = null;
|
format = this.format;
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.writeFile(this.file, this.stringify(), callback);
|
fs.writeFile(path, this.stringify(format), callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
//
|
//
|
||||||
|
@ -166,14 +178,17 @@ File.prototype.loadSync = function () {
|
||||||
// Returns an encrypted version of the contents IIF
|
// Returns an encrypted version of the contents IIF
|
||||||
// `this.secure` is enabled
|
// `this.secure` is enabled
|
||||||
//
|
//
|
||||||
File.prototype.stringify = function () {
|
File.prototype.stringify = function (format) {
|
||||||
var data = this.store;
|
var data = this.store;
|
||||||
|
if (!format) {
|
||||||
|
format = this.format
|
||||||
|
}
|
||||||
|
|
||||||
if (this.secure) {
|
if (this.secure) {
|
||||||
data = this.keys.encrypt(data);
|
data = this.keys.encrypt(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.format.stringify(data, null, this.spacing);
|
return format.stringify(data, null, this.spacing);
|
||||||
};
|
};
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
|
@ -22,6 +22,7 @@
|
||||||
"coveralls": "^2.11.4",
|
"coveralls": "^2.11.4",
|
||||||
"eslint": "^4.9.0",
|
"eslint": "^4.9.0",
|
||||||
"istanbul": "^0.4.1",
|
"istanbul": "^0.4.1",
|
||||||
|
"nconf-yaml": "^1.0.2",
|
||||||
"vows": "0.8.x"
|
"vows": "0.8.x"
|
||||||
},
|
},
|
||||||
"main": "./lib/nconf",
|
"main": "./lib/nconf",
|
||||||
|
|
|
@ -10,6 +10,7 @@ var fs = require('fs'),
|
||||||
vows = require('vows'),
|
vows = require('vows'),
|
||||||
assert = require('assert'),
|
assert = require('assert'),
|
||||||
nconf = require('../../lib/nconf'),
|
nconf = require('../../lib/nconf'),
|
||||||
|
yamlFormat = require('nconf-yaml'),
|
||||||
data = require('../fixtures/data').data,
|
data = require('../fixtures/data').data,
|
||||||
store;
|
store;
|
||||||
|
|
||||||
|
@ -128,6 +129,53 @@ vows.describe('nconf/stores/file').addBatch({
|
||||||
assert.isNull(err);
|
assert.isNull(err);
|
||||||
assert.deepEqual(read, data);
|
assert.deepEqual(read, data);
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"the saveToFile() method": {
|
||||||
|
topic: function (tmpStore) {
|
||||||
|
var that = this,
|
||||||
|
pathFile = '/tmp/nconf-save-toFile.json';
|
||||||
|
|
||||||
|
Object.keys(data).forEach(function (key) {
|
||||||
|
tmpStore.set(key, data[key]);
|
||||||
|
});
|
||||||
|
|
||||||
|
tmpStore.saveToFile(pathFile, function () {
|
||||||
|
fs.readFile(pathFile, function (err, d) {
|
||||||
|
fs.unlinkSync(pathFile);
|
||||||
|
|
||||||
|
return err
|
||||||
|
? that.callback(err)
|
||||||
|
: that.callback(err, JSON.parse(d.toString()));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
"should save the data correctly": function (err, read) {
|
||||||
|
assert.isNull(err);
|
||||||
|
assert.deepEqual(read, data);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"the saveToFile() method with custom format": {
|
||||||
|
topic: function (tmpStore) {
|
||||||
|
var that = this,
|
||||||
|
pathFile = '/tmp/nconf-save-toFile.yaml';
|
||||||
|
|
||||||
|
Object.keys(data).forEach(function (key) {
|
||||||
|
tmpStore.set(key, data[key]);
|
||||||
|
});
|
||||||
|
|
||||||
|
tmpStore.saveToFile(pathFile, yamlFormat, function () {
|
||||||
|
fs.readFile(pathFile, function (err, d) {
|
||||||
|
fs.unlinkSync(pathFile);
|
||||||
|
return err
|
||||||
|
? that.callback(err)
|
||||||
|
: that.callback(err, yamlFormat.parse(d.toString()));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
"should save the data correctly": function (err, read) {
|
||||||
|
assert.isNull(err);
|
||||||
|
assert.deepEqual(read, data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).addBatch({
|
}).addBatch({
|
||||||
|
|
Loading…
Reference in a new issue