2011-03-31 06:32:47 +00:00
|
|
|
/*
|
|
|
|
* file.js: Simple file storage engine for nconf files
|
|
|
|
*
|
2014-11-26 06:31:48 +00:00
|
|
|
* (C) 2011, Charlie Robbins and the Contributors.
|
2011-03-31 06:32:47 +00:00
|
|
|
*
|
|
|
|
*/
|
2011-06-24 01:06:26 +00:00
|
|
|
|
2011-08-28 12:50:26 +00:00
|
|
|
var fs = require('fs'),
|
|
|
|
path = require('path'),
|
|
|
|
util = require('util'),
|
|
|
|
formats = require('../formats'),
|
2012-03-29 11:13:16 +00:00
|
|
|
Memory = require('./memory').Memory,
|
2012-05-15 09:02:46 +00:00
|
|
|
exists = fs.exists || path.exists,
|
2012-03-29 11:13:16 +00:00
|
|
|
existsSync = fs.existsSync || path.existsSync;
|
2012-04-14 19:28:55 +00:00
|
|
|
|
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) {
|
2011-09-19 01:37:01 +00:00
|
|
|
if (!options || !options.file) {
|
2012-05-24 05:27:19 +00:00
|
|
|
throw new Error ('Missing required option `file`');
|
2011-09-19 01:37:01 +00:00
|
|
|
}
|
2011-03-31 06:32:47 +00:00
|
|
|
|
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-08-23 10:38:51 +00:00
|
|
|
this.dir = options.dir || process.cwd();
|
2011-08-28 12:50:26 +00:00
|
|
|
this.format = options.format || formats.json;
|
2012-04-05 06:58:27 +00:00
|
|
|
this.json_spacing = options.json_spacing || 2;
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-10-22 06:36:45 +00:00
|
|
|
if (options.search) {
|
|
|
|
this.search(this.dir);
|
|
|
|
}
|
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
|
|
|
//
|
2012-04-14 19:28:55 +00:00
|
|
|
// ### function save (value, callback)
|
2011-04-02 07:03:16 +00:00
|
|
|
// #### @value {Object} _Ignored_ Left here for consistency
|
|
|
|
// #### @callback {function} Continuation to respond to when complete.
|
2012-04-14 19:28:55 +00:00
|
|
|
// Saves the current configuration object to disk at `this.file`
|
2011-04-02 07:03:16 +00:00
|
|
|
// using the format specified by `this.format`.
|
|
|
|
//
|
|
|
|
File.prototype.save = function (value, callback) {
|
|
|
|
if (!callback) {
|
|
|
|
callback = value;
|
|
|
|
value = null;
|
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2012-04-05 06:58:27 +00:00
|
|
|
fs.writeFile(this.file, this.format.stringify(this.store, null, this.json_spacing), function (err) {
|
2011-03-31 06:32:47 +00:00
|
|
|
return err ? callback(err) : callback();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2011-04-20 05:57:56 +00:00
|
|
|
//
|
2012-04-14 19:28:55 +00:00
|
|
|
// ### function saveSync (value, callback)
|
2011-04-20 05:57:56 +00:00
|
|
|
// #### @value {Object} _Ignored_ Left here for consistency
|
|
|
|
// #### @callback {function} **Optional** Continuation to respond to when complete.
|
2012-04-14 19:28:55 +00:00
|
|
|
// Saves the current configuration object to disk at `this.file`
|
2011-04-20 05:57:56 +00:00
|
|
|
// using the format specified by `this.format` synchronously.
|
|
|
|
//
|
2011-06-24 01:06:26 +00:00
|
|
|
File.prototype.saveSync = function (value) {
|
2011-04-20 05:57:56 +00:00
|
|
|
try {
|
2012-04-05 06:58:27 +00:00
|
|
|
fs.writeFileSync(this.file, this.format.stringify(this.store, null, this.json_spacing));
|
2011-04-20 05:57:56 +00:00
|
|
|
}
|
|
|
|
catch (ex) {
|
2011-06-24 01:06:26 +00:00
|
|
|
throw(ex);
|
2011-04-20 05:57:56 +00:00
|
|
|
}
|
2011-12-25 14:30:48 +00:00
|
|
|
return this.store;
|
2011-04-20 05:57:56 +00:00
|
|
|
};
|
|
|
|
|
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-06-24 01:06:26 +00:00
|
|
|
|
2012-05-15 09:02:46 +00:00
|
|
|
exists(self.file, function (exists) {
|
2011-06-24 01:06:26 +00:00
|
|
|
if (!exists) {
|
2011-11-23 21:40:18 +00:00
|
|
|
return callback(null, {});
|
2011-06-24 01:06:26 +00:00
|
|
|
}
|
2011-11-23 21:40:18 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Else, the path exists, read it from disk
|
|
|
|
//
|
|
|
|
fs.readFile(self.file, function (err, data) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-11-23 21:40:18 +00:00
|
|
|
try {
|
2013-10-26 19:40:12 +00:00
|
|
|
//deals with string that include BOM
|
|
|
|
var stringData = data.toString();
|
2013-10-03 08:17:36 +00:00
|
|
|
|
2013-10-26 19:40:12 +00:00
|
|
|
if (stringData.charAt(0) === '\uFEFF') stringData = stringData.substr(1);
|
|
|
|
self.store = self.format.parse(stringData);
|
2013-10-03 08:17:36 +00:00
|
|
|
|
2011-11-23 21:40:18 +00:00
|
|
|
}
|
|
|
|
catch (ex) {
|
2014-03-09 12:40:10 +00:00
|
|
|
return callback(new Error("Error parsing your configuration file: [" + self.file + ']: ' + ex.message));
|
2011-11-23 21:40:18 +00:00
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-11-23 21:40:18 +00:00
|
|
|
callback(null, self.store);
|
|
|
|
});
|
2011-03-31 06:32:47 +00:00
|
|
|
});
|
2011-04-20 05:57:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
2011-11-23 21:40:18 +00:00
|
|
|
// ### function loadSync (callback)
|
|
|
|
// Attempts to load the data stored in `this.file` synchronously
|
|
|
|
// and responds appropriately.
|
2011-04-20 05:57:56 +00:00
|
|
|
//
|
2011-06-24 01:06:26 +00:00
|
|
|
File.prototype.loadSync = function () {
|
|
|
|
var data, self = this;
|
|
|
|
|
2012-03-29 11:13:16 +00:00
|
|
|
if (!existsSync(self.file)) {
|
2011-06-24 01:06:26 +00:00
|
|
|
self.store = {};
|
|
|
|
data = {};
|
2011-04-20 05:57:56 +00:00
|
|
|
}
|
2011-06-24 01:06:26 +00:00
|
|
|
else {
|
2011-06-24 07:31:26 +00:00
|
|
|
//
|
2011-06-24 01:06:26 +00:00
|
|
|
// Else, the path exists, read it from disk
|
2011-06-24 07:31:26 +00:00
|
|
|
//
|
2011-06-24 01:06:26 +00:00
|
|
|
try {
|
2013-10-26 19:40:12 +00:00
|
|
|
//deals with file that include BOM
|
|
|
|
var fileData = fs.readFileSync(this.file, 'utf8');
|
|
|
|
if (fileData.charAt(0) === '\uFEFF') fileData = fileData.substr(1);
|
2013-10-03 08:17:36 +00:00
|
|
|
|
2013-10-26 19:40:12 +00:00
|
|
|
data = this.format.parse(fileData);
|
|
|
|
this.store = data;
|
2011-06-24 01:06:26 +00:00
|
|
|
}
|
|
|
|
catch (ex) {
|
2014-03-09 12:40:10 +00:00
|
|
|
throw new Error("Error parsing your configuration file: [" + self.file + ']: ' + ex.message);
|
2011-06-24 01:06:26 +00:00
|
|
|
}
|
2011-04-20 05:57:56 +00:00
|
|
|
}
|
2011-06-24 01:06:26 +00:00
|
|
|
|
2011-04-20 05:57:56 +00:00
|
|
|
return data;
|
2011-08-23 00:17:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
2011-08-23 10:38:51 +00:00
|
|
|
// ### function search (base)
|
2011-08-23 00:17:28 +00:00
|
|
|
// #### @base {string} Base directory (or file) to begin searching for the target file.
|
2012-04-14 19:28:55 +00:00
|
|
|
// Attempts to find `this.file` by iteratively searching up the
|
|
|
|
// directory structure
|
2011-08-23 00:17:28 +00:00
|
|
|
//
|
2011-08-23 10:38:51 +00:00
|
|
|
File.prototype.search = function (base) {
|
|
|
|
var looking = true,
|
2011-08-23 00:17:28 +00:00
|
|
|
fullpath,
|
2011-08-23 10:38:51 +00:00
|
|
|
previous,
|
2011-08-23 00:17:28 +00:00
|
|
|
stats;
|
|
|
|
|
2011-08-23 10:38:51 +00:00
|
|
|
base = base || process.cwd();
|
|
|
|
|
2011-08-23 00:17:28 +00:00
|
|
|
if (this.file[0] === '/') {
|
|
|
|
//
|
|
|
|
// If filename for this instance is a fully qualified path
|
|
|
|
// (i.e. it starts with a `'/'`) then check if it exists
|
|
|
|
//
|
|
|
|
try {
|
2011-08-23 10:38:51 +00:00
|
|
|
stats = fs.statSync(fs.realpathSync(this.file));
|
2011-08-23 00:17:28 +00:00
|
|
|
if (stats.isFile()) {
|
|
|
|
fullpath = this.file;
|
|
|
|
looking = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (ex) {
|
|
|
|
//
|
|
|
|
// Ignore errors
|
|
|
|
//
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (looking && base) {
|
|
|
|
//
|
|
|
|
// Attempt to stat the realpath located at `base`
|
|
|
|
// if the directory does not exist then return false.
|
|
|
|
//
|
|
|
|
try {
|
|
|
|
var stat = fs.statSync(fs.realpathSync(base));
|
|
|
|
looking = stat.isDirectory();
|
|
|
|
}
|
|
|
|
catch (ex) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-08-23 00:17:28 +00:00
|
|
|
while (looking) {
|
2011-08-23 10:38:51 +00:00
|
|
|
//
|
|
|
|
// Iteratively look up the directory structure from `base`
|
|
|
|
//
|
2011-08-23 00:17:28 +00:00
|
|
|
try {
|
|
|
|
stats = fs.statSync(fs.realpathSync(fullpath = path.join(base, this.file)));
|
|
|
|
looking = stats.isDirectory();
|
|
|
|
}
|
|
|
|
catch (ex) {
|
2011-08-23 10:38:51 +00:00
|
|
|
previous = base;
|
|
|
|
base = path.dirname(base);
|
|
|
|
|
|
|
|
if (previous === base) {
|
|
|
|
//
|
|
|
|
// If we've reached the top of the directory structure then simply use
|
|
|
|
// the default file path.
|
|
|
|
//
|
2011-08-23 00:17:28 +00:00
|
|
|
try {
|
2011-08-23 10:38:51 +00:00
|
|
|
stats = fs.statSync(fs.realpathSync(fullpath = path.join(this.dir, this.file)));
|
|
|
|
if (stats.isDirectory()) {
|
|
|
|
fullpath = undefined;
|
2011-08-23 00:17:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (ex) {
|
|
|
|
//
|
|
|
|
// Ignore errors
|
|
|
|
//
|
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-08-23 00:17:28 +00:00
|
|
|
looking = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-23 10:38:51 +00:00
|
|
|
//
|
|
|
|
// Set the file for this instance to the fullpath
|
|
|
|
// that we have found during the search. In the event that
|
|
|
|
// the search was unsuccessful use the original value for `this.file`.
|
|
|
|
//
|
|
|
|
this.file = fullpath || this.file;
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-08-23 00:17:28 +00:00
|
|
|
return fullpath;
|
2011-12-25 14:30:48 +00:00
|
|
|
};
|