Comparing version 1.1.1 to 2.0.0
@@ -22,9 +22,9 @@ 'use strict' | ||
* @example | ||
* > walkBack(process.cwd(), 'package.json') | ||
* > walkBack('/Users/lloyd/Documents/75lb/walk-back', 'package.json') | ||
* '/Users/lloyd/Documents/75lb/walk-back/package.json' | ||
* | ||
* > walkBack(process.cwd(), '75lb') | ||
* > walkBack('/Users/lloyd/Documents/75lb/walk-back', '75lb') | ||
* '/Users/lloyd/Documents/75lb' | ||
* | ||
* > walkBack(process.cwd(), '.bash_profile') | ||
* > walkBack('/Users/lloyd/Documents/75lb/walk-back', '.bash_profile') | ||
* '/Users/lloyd/.bash_profile' | ||
@@ -35,18 +35,23 @@ * | ||
* | ||
* > walkBack(process.cwd(), 'non-existent.file') | ||
* > walkBack('/Users/lloyd/Documents/75lb/walk-back', 'non-existent.file') | ||
* null | ||
*/ | ||
function walkBack (startAt, lookingFor) { | ||
var dirs = path.resolve(startAt).split(path.sep) | ||
for (var i = 0; i < dirs.length; i++) { | ||
var basedir = i < dirs.length - 1 | ||
? dirs.slice(0, dirs.length - i).join(path.sep) | ||
: path.sep | ||
startAt = path.resolve(startAt) | ||
if (fs.existsSync(startAt) && fs.statSync(startAt).isDirectory()) { | ||
var dirs = path.resolve(startAt).split(path.sep) | ||
for (var i = 0; i < dirs.length; i++) { | ||
var basedir = i < dirs.length - 1 | ||
? dirs.slice(0, dirs.length - i).join(path.sep) | ||
: path.sep | ||
if (fs.existsSync(path.join(basedir, lookingFor))) { | ||
return path.join(basedir, lookingFor) | ||
if (fs.existsSync(path.join(basedir, lookingFor))) { | ||
return path.join(basedir, lookingFor) | ||
} | ||
} | ||
/* if we reached here, nothing was found */ | ||
return null | ||
} else { | ||
throw new Error('startAt path must be an existing directory: ' + startAt) | ||
} | ||
/* if we reached here, nothing was found */ | ||
return null | ||
} |
{ | ||
"name": "walk-back", | ||
"author": "Lloyd Brookes <75pound@gmail.com>", | ||
"version": "1.1.1", | ||
"version": "2.0.0", | ||
"description": "Walk up the directory tree until the specified path is found.", | ||
@@ -17,2 +17,3 @@ "repository": "https://github.com/75lb/walk-back.git", | ||
"file", | ||
"search", | ||
"system" | ||
@@ -28,5 +29,5 @@ ], | ||
"devDependencies": { | ||
"jsdoc-to-markdown": "^1.3.3", | ||
"tape": "^4.4.0" | ||
"jsdoc-to-markdown": "^1.3.6", | ||
"tape": "^4.5.1" | ||
} | ||
} |
@@ -8,2 +8,3 @@ [![view on npm](http://img.shields.io/npm/v/walk-back.svg)](https://www.npmjs.org/package/walk-back) | ||
<a name="module_walk-back"></a> | ||
## walk-back | ||
@@ -17,2 +18,3 @@ Walk up the directory tree until the specified path is found. | ||
<a name="exp_module_walk-back--walkBack"></a> | ||
### walkBack(startAt, lookingFor) ⇒ <code>string</code> ⏏ | ||
@@ -30,9 +32,9 @@ Returns an absolute file path (if found) else `null`. | ||
```js | ||
> walkBack(process.cwd(), 'package.json') | ||
> walkBack('/Users/lloyd/Documents/75lb/walk-back', 'package.json') | ||
'/Users/lloyd/Documents/75lb/walk-back/package.json' | ||
> walkBack(process.cwd(), '75lb') | ||
> walkBack('/Users/lloyd/Documents/75lb/walk-back', '75lb') | ||
'/Users/lloyd/Documents/75lb' | ||
> walkBack(process.cwd(), '.bash_profile') | ||
> walkBack('/Users/lloyd/Documents/75lb/walk-back', '.bash_profile') | ||
'/Users/lloyd/.bash_profile' | ||
@@ -43,3 +45,3 @@ | ||
> walkBack(process.cwd(), 'non-existent.file') | ||
> walkBack('/Users/lloyd/Documents/75lb/walk-back', 'non-existent.file') | ||
null | ||
@@ -46,0 +48,0 @@ ``` |
@@ -38,1 +38,8 @@ var test = require('tape') | ||
}) | ||
test('startPath doesn\'t exist', function (t) { | ||
t.throws(function () { | ||
walkBack('slfnavnkln', 'file.txt') | ||
}) | ||
t.end() | ||
}) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6960
85
49