Skip to content

Instantly share code, notes, and snippets.

@dmi3y
Created April 18, 2014 23:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmi3y/11067810 to your computer and use it in GitHub Desktop.
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
/*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