Skip to content

Commit

Permalink
[api dist] Begin to integrate features from reconf
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Aug 23, 2011
1 parent 57f0742 commit add8922
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
3 changes: 1 addition & 2 deletions lib/nconf/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
74 changes: 74 additions & 0 deletions lib/nconf/stores/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
};
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down

0 comments on commit add8922

Please sign in to comment.