diff --git a/lib/nconf/provider.js b/lib/nconf/provider.js index 7ca4678..56246e3 100644 --- a/lib/nconf/provider.js +++ b/lib/nconf/provider.js @@ -74,7 +74,7 @@ Provider.prototype.set = function (key, value, callback) { // Provider.prototype.merge = function (key, value, callback) { return this.store.merge(key, value, callback); -} +}; // // ### function clear (key, callback) @@ -101,7 +101,6 @@ Provider.prototype.load = function (callback) { return this.store.loadSync(); } - if (!this.store.load) { var error = new Error('nconf store ' + this.store.type + ' has no load() method'); if (callback) { diff --git a/lib/nconf/stores/file.js b/lib/nconf/stores/file.js index fae5d96..d8b5615 100644 --- a/lib/nconf/stores/file.js +++ b/lib/nconf/stores/file.js @@ -25,6 +25,7 @@ var File = exports.File = function (options) { this.type = 'file'; this.file = options.file; + this.search = options.search || false; this.format = options.format || { stringify: function (obj) { return JSON.stringify(obj, null, 2) @@ -140,4 +141,77 @@ File.prototype.loadSync = function () { } return data; +}; + +// +// ### function resolve (base) +// #### @base {string} Base directory (or file) to begin searching for the target file. +// +File.prototype.resolve = function (base) { + var looking = this.search, + fullpath, + stats; + + 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 { + stats = fs.statSync(fs.realpathSync(fullpath)); + 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; + } + } + + while (looking) { + try { + stats = fs.statSync(fs.realpathSync(fullpath = path.join(base, this.file))); + looking = stats.isDirectory(); + } + catch (ex) { + var olddir = dir; + dir = path.dirname(dir); + + if (olddir === dir) { + try { + var stat = fs.statSync(fs.realpathSync(configPath = path.join(process.env.HOME, filename))); + if(stat.isDirectory()) { + configPath = undefined; + } + } + catch (ex) { + // + // Ignore errors + // + configPath = undefined; + } + + looking = false; + } + } + } + + return fullpath; }; \ No newline at end of file diff --git a/package.json b/package.json index 58b0da2..3a1061b 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,9 @@ "async": "0.1.x", "pkginfo": "0.2.x" }, + "devDependencies": { + "vows": "0.5.x >=0.5.11" + }, "main": "./lib/nconf", "scripts": { "test": "vows test/*-test.js --spec" }, "engines": { "node": ">= 0.4.0" }