find-file-up
Advanced tools
Comparing version 0.1.0 to 0.1.1
77
index.js
@@ -17,2 +17,3 @@ /*! | ||
var resolve = require('resolve-dir'); | ||
var existsSync = require('try-open'); | ||
var cwd = process.cwd(); | ||
@@ -24,3 +25,3 @@ | ||
module.exports = function(filename, cwd, cb) { | ||
module.exports = function(filename, cwd, limit, cb) { | ||
if (typeof cwd === 'function') { | ||
@@ -31,13 +32,27 @@ cb = cwd; | ||
if (typeof limit === 'function') { | ||
cb = limit; | ||
limit = Infinity; | ||
} | ||
var dir = cwd ? resolve(cwd) : '.'; | ||
var n = 0; | ||
(function find(dir, next) { | ||
var filepath = path.resolve(dir, filename); | ||
exists(filepath, function(exists) { | ||
if (exists) return next(null, filepath); | ||
var fp = path.resolve(dir, filename); | ||
if (dir !== path.sep) { | ||
return find(path.dirname(dir), next); | ||
exists(fp, function(exists) { | ||
n++; | ||
if (exists) { | ||
next(null, fp); | ||
return; | ||
} | ||
return next(); | ||
if (n >= limit || dir === path.sep || dir === '.') { | ||
next(); | ||
return; | ||
} | ||
find(path.dirname(dir), next); | ||
}); | ||
@@ -47,11 +62,24 @@ }(dir, cb)); | ||
module.exports.sync = function(filename, cwd) { | ||
var dir = path.join(cwd ? resolve(cwd) : '.', '_'); | ||
module.exports.sync = function(filename, cwd, limit) { | ||
var dir = cwd ? resolve(cwd) : '.'; | ||
var fp = path.join(dir, filename); | ||
var n = 0; | ||
while ((dir = path.dirname(dir)) !== path.sep) { | ||
if (existsSync(fp, 'r')) { | ||
return path.resolve(fp); | ||
} | ||
if (limit === 0) return null; | ||
while ((dir = path.dirname(dir))) { | ||
n++; | ||
var filepath = path.resolve(dir, filename); | ||
if (existsSync(filepath)) { | ||
if (existsSync(filepath, 'r')) { | ||
return filepath; | ||
} | ||
if (n >= limit || dir === '.' || dir === path.sep) { | ||
return; | ||
} | ||
} | ||
@@ -61,4 +89,4 @@ }; | ||
/** | ||
* Utils for checking if a file exists. Uses `fs.access` since | ||
* `fs.exists` and `fs.existsSync` are deprecated. | ||
* Returns true if a file exists. `fs.exists` | ||
* and `fs.existsSync` are deprecated. | ||
* | ||
@@ -69,14 +97,13 @@ * See: https://nodejs.org/api/fs.html#fs_fs_exists_path_callback | ||
function exists(filepath, cb) { | ||
(fs.access || fs.stat)(filepath, function(err) { | ||
if (err) return cb(false); | ||
return cb(true); | ||
fs.open(filepath, 'r', function(err) { | ||
if (err && err.code === 'ENOENT') { | ||
cb(false); | ||
return | ||
} | ||
if (err) { | ||
cb(err); | ||
return; | ||
} | ||
cb(true); | ||
}); | ||
} | ||
function existsSync(filepath) { | ||
try { | ||
(fs.accessSync || fs.statSync)(filepath); | ||
return true; | ||
} catch (err) {} | ||
return false | ||
} |
{ | ||
"name": "find-file-up", | ||
"description": "Find a file, starting with the given cwd and recursively searching up one directory until it's found (or we run out of directories).", | ||
"version": "0.1.0", | ||
"description": "Find a file, starting with the given cwd and recursively searching up one directory until it's found (or we run out of directories). Async and sync.", | ||
"version": "0.1.1", | ||
"homepage": "https://github.com/jonschlinkert/find-file-up", | ||
@@ -23,3 +23,4 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"dependencies": { | ||
"resolve-dir": "^0.1.0" | ||
"resolve-dir": "^0.1.0", | ||
"try-open": "^0.1.0" | ||
}, | ||
@@ -30,12 +31,20 @@ "devDependencies": { | ||
}, | ||
"keywords": [ | ||
"file", | ||
"find", | ||
"up" | ||
], | ||
"verb": { | ||
"related": { | ||
"list": [ | ||
"load-module-pkg", | ||
"load-pkg", | ||
"look-up", | ||
"load-pkg", | ||
"load-module-pkg", | ||
"module-root" | ||
] | ||
} | ||
}, | ||
"plugins": [ | ||
"gulp-format-md" | ||
] | ||
} | ||
} |
@@ -1,4 +0,4 @@ | ||
# find-file-up [![NPM version](https://badge.fury.io/js/find-file-up.svg)](http://badge.fury.io/js/find-file-up) [![Build Status](https://travis-ci.org/jonschlinkert/find-file-up.svg)](https://travis-ci.org/jonschlinkert/find-file-up) | ||
# find-file-up [![NPM version](https://img.shields.io/npm/v/find-file-up.svg)](https://www.npmjs.com/package/find-file-up) [![Build Status](https://img.shields.io/travis/jonschlinkert/find-file-up.svg)](https://travis-ci.org/jonschlinkert/find-file-up) | ||
> Find a file, starting with the given cwd and recursively searching up one directory until it's found (or we run out of directories). | ||
> Find a file, starting with the given cwd and recursively searching up one directory until it's found (or we run out of directories). Async and sync. | ||
@@ -62,8 +62,8 @@ ## Install | ||
+ [github/jonschlinkert](https://github.com/jonschlinkert) | ||
+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert) | ||
* [github/jonschlinkert](https://github.com/jonschlinkert) | ||
* [twitter/jonschlinkert](http://twitter.com/jonschlinkert) | ||
## License | ||
Copyright © 2015 Jon Schlinkert | ||
Copyright © 2015 [Jon Schlinkert](https://github.com/jonschlinkert) | ||
Released under the MIT license. | ||
@@ -73,2 +73,2 @@ | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on November 27, 2015._ | ||
_This file was generated by [verb](https://github.com/verbose/verb) on December 20, 2015._ |
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
6320
83
2
+ Addedtry-open@^0.1.0
+ Addedtry-open@0.1.3(transitive)