confortable
Advanced tools
Comparing version 0.1.2 to 0.1.3
var fs = require('fs') | ||
, path = require('path') | ||
, exists = fs.existsSync || path.existsSync; | ||
, existsSync = fs.existsSync || path.existsSync; | ||
module.exports = function (name, start) { | ||
var findFromStart = function (name, start) { | ||
start = start || process.cwd(); | ||
var relative = path.relative(process.env.HOME, start) | ||
, differentDrive = (relative === start) | ||
, noRelation = (relative === start) // i.e. different drive or undefined HOME | ||
, isAbove = (relative.slice(0, 2) === '..') | ||
, cfg; | ||
if (differentDrive || isAbove) { | ||
if (noRelation || isAbove) { | ||
// start is outside home, check start only | ||
cfg = path.join(start, name); | ||
return exists(cfg) ? cfg : null; | ||
return existsSync(cfg) ? cfg : null; | ||
} | ||
@@ -20,7 +20,17 @@ else { | ||
var dir = start; | ||
while (path.relative(process.env.HOME, dir).slice(0, 2) !== '..') { | ||
while (true) { | ||
relative = path.relative(process.env.HOME, dir); | ||
if (relative.slice(0, 2) === '..') { | ||
break; // dir is above HOME | ||
} | ||
cfg = path.join(dir, name); | ||
if (exists(cfg)) { | ||
if (existsSync(cfg)) { | ||
return cfg; | ||
} | ||
// windows users can put a drive root to home in which case adding '..' does nothing | ||
if (path.join(dir, '..') === path.normalize(dir)) { | ||
break; // next iteration's dir is root, and nothing's there, prevent infinite loop | ||
} | ||
dir = path.join(dir, '..'); | ||
@@ -31,1 +41,11 @@ } | ||
}; | ||
module.exports = function (name, start, fallbackDir) { | ||
var result = findFromStart(name, start); | ||
if (!fallbackDir) { | ||
return result; | ||
} | ||
var fbPath = path.join(fallbackDir, name); | ||
return (existsSync(fbPath)) ? fbPath : null; | ||
} |
@@ -5,3 +5,3 @@ { | ||
"description": "Finds the right recursively placed config file", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"repository": { | ||
@@ -8,0 +8,0 @@ "type": "git", |
@@ -12,3 +12,3 @@ # Confortable [![Build Status](https://secure.travis-ci.org/clux/confortable.png)](http://travis-ci.org/clux/confortable) | ||
var conf = require('confortable'); | ||
confPath = conf('.logule'); // if non-null, this can be read by fs or required if js compatible | ||
confPath = conf('.confName'); // if non-null, this can be read by fs or required if js compatible | ||
```` | ||
@@ -22,2 +22,9 @@ | ||
A final optional setting is a fallback directory, in case the recursive search fails, but you still want to see if a config exists somewhere else (like say the path of the parent module). In this use case, you have to specify the start as well. | ||
````javascript | ||
var fallback = require('path').dirname(module.parent.filename); | ||
var confPath = require('confortable')('.logule', process.cwd(), fallback); | ||
``` | ||
## Installation | ||
@@ -24,0 +31,0 @@ |
6092
8
67
49