Skip to content

Commit

Permalink
Save conf to dedicated file (#283)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
AdrieanKhisbe authored and mhamann committed Oct 30, 2017
1 parent 52e0a35 commit b41c505
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
25 changes: 20 additions & 5 deletions lib/nconf/stores/file.js
Expand Up @@ -74,12 +74,24 @@ util.inherits(File, Memory);
// using the format specified by `this.format`.
//
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) {
callback = value;
value = null;
callback = format;
format = this.format;
}

fs.writeFile(this.file, this.stringify(), callback);
fs.writeFile(path, this.stringify(format), callback);
};

//
Expand Down Expand Up @@ -166,14 +178,17 @@ File.prototype.loadSync = function () {
// Returns an encrypted version of the contents IIF
// `this.secure` is enabled
//
File.prototype.stringify = function () {
File.prototype.stringify = function (format) {
var data = this.store;
if (!format) {
format = this.format
}

if (this.secure) {
data = this.keys.encrypt(data);
}

return this.format.stringify(data, null, this.spacing);
return format.stringify(data, null, this.spacing);
};

//
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -22,6 +22,7 @@
"coveralls": "^2.11.4",
"eslint": "^4.9.0",
"istanbul": "^0.4.1",
"nconf-yaml": "^1.0.2",
"vows": "0.8.x"
},
"main": "./lib/nconf",
Expand Down
48 changes: 48 additions & 0 deletions test/stores/file-store-test.js
Expand Up @@ -10,6 +10,7 @@ var fs = require('fs'),
vows = require('vows'),
assert = require('assert'),
nconf = require('../../lib/nconf'),
yamlFormat = require('nconf-yaml'),
data = require('../fixtures/data').data,
store;

Expand Down Expand Up @@ -128,6 +129,53 @@ vows.describe('nconf/stores/file').addBatch({
assert.isNull(err);
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({
Expand Down

0 comments on commit b41c505

Please sign in to comment.