2011-03-31 06:32:47 +00:00
|
|
|
/*
|
|
|
|
* file.js: Simple file storage engine for nconf files
|
|
|
|
*
|
|
|
|
* (C) 2011, Charlie Robbins
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
var fs = require('fs'),
|
|
|
|
util = require('util'),
|
|
|
|
Memory = require('./memory').Memory;
|
|
|
|
|
2011-04-02 07:03:16 +00:00
|
|
|
//
|
|
|
|
// ### function File (options)
|
|
|
|
// #### @options {Object} Options for this instance
|
|
|
|
// Constructor function for the File nconf store, a simple abstraction
|
|
|
|
// around the Memory store that can persist configuration to disk.
|
|
|
|
//
|
2011-03-31 06:32:47 +00:00
|
|
|
var File = exports.File = function (options) {
|
|
|
|
if (!options.file) {
|
|
|
|
throw new Error ('Missing required option `files`');
|
|
|
|
}
|
|
|
|
|
2011-04-02 08:31:20 +00:00
|
|
|
Memory.call(this, options);
|
2011-03-31 06:32:47 +00:00
|
|
|
|
2011-04-02 23:17:04 +00:00
|
|
|
this.type = 'file';
|
2011-03-31 06:32:47 +00:00
|
|
|
this.file = options.file;
|
2011-04-19 21:32:13 +00:00
|
|
|
this.format = options.format || {
|
|
|
|
stringify: function (obj) {
|
|
|
|
return JSON.stringify(obj, null, 2)
|
|
|
|
},
|
|
|
|
parse: JSON.parse
|
|
|
|
};
|
2011-03-31 06:32:47 +00:00
|
|
|
};
|
|
|
|
|
2011-04-02 07:03:16 +00:00
|
|
|
// Inherit from the Memory store
|
2011-03-31 06:32:47 +00:00
|
|
|
util.inherits(File, Memory);
|
|
|
|
|
2011-04-02 07:03:16 +00:00
|
|
|
//
|
|
|
|
// ### function save (value, callback)
|
|
|
|
// #### @value {Object} _Ignored_ Left here for consistency
|
|
|
|
// #### @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.save = function (value, callback) {
|
|
|
|
if (!callback) {
|
|
|
|
callback = value;
|
|
|
|
value = null;
|
|
|
|
}
|
|
|
|
|
2011-04-02 08:31:20 +00:00
|
|
|
fs.writeFile(this.file, this.format.stringify(this.store), function (err) {
|
2011-03-31 06:32:47 +00:00
|
|
|
return err ? callback(err) : callback();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2011-04-20 05:57:56 +00:00
|
|
|
//
|
|
|
|
// ### 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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-04-02 07:03:16 +00:00
|
|
|
//
|
|
|
|
// ### function load (callback)
|
|
|
|
// #### @callback {function} Continuation to respond to when complete.
|
|
|
|
// Responds with an Object representing all keys associated in this instance.
|
|
|
|
//
|
2011-03-31 06:32:47 +00:00
|
|
|
File.prototype.load = function (callback) {
|
|
|
|
var self = this;
|
2011-04-02 08:31:20 +00:00
|
|
|
fs.readFile(this.file, function (err, data) {
|
2011-03-31 06:32:47 +00:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2011-04-02 08:31:20 +00:00
|
|
|
data = self.format.parse(data.toString());
|
|
|
|
self.store = data;
|
2011-03-31 06:32:47 +00:00
|
|
|
callback(null, self.store);
|
|
|
|
});
|
2011-04-20 05:57:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// ### 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;
|
2011-03-31 06:32:47 +00:00
|
|
|
};
|