Created
April 18, 2014 23:01
-
-
Save dmi3y/11067810 to your computer and use it in GitHub Desktop.
node js find file up from current location into file system until user home dir or top level dir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*jshint node:true*/ | |
var | |
path = require("path"), | |
fs = require("fs"), | |
userhome = path.resolve(process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE), | |
out; | |
function lookup(filename) { | |
var lookupd = process.cwd(), | |
isFile, | |
fullpath; | |
function isGoodToGoUp() { | |
var | |
isUserhome = (lookupd === userhome), | |
_lookupd = path.resolve(lookupd + "/../"), | |
isTop = (lookupd === _lookupd), | |
gtg; | |
isFile = (fs.existsSync(fullpath) && fs.statSync(fullpath).isFile()); | |
gtg = (!isFile && !isUserhome && !isTop); | |
lookupd = _lookupd; | |
return gtg; | |
} | |
(function traverseUp() { | |
fullpath = path.resolve(lookupd, filename); | |
if (isGoodToGoUp()) { | |
traverseUp(); | |
} | |
}()); | |
return isFile ? fullpath : null; | |
} | |
out = lookup(".npmrc"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment