diff --git a/lib/nconf/stores/file.js b/lib/nconf/stores/file.js index 10d98a1..8d1e1e1 100644 --- a/lib/nconf/stores/file.js +++ b/lib/nconf/stores/file.js @@ -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); }; // @@ -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); }; // diff --git a/package.json b/package.json index c1c98ee..18780f2 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/stores/file-store-test.js b/test/stores/file-store-test.js index 02ef9fc..89e873a 100644 --- a/test/stores/file-store-test.js +++ b/test/stores/file-store-test.js @@ -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; @@ -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({