Socket
Socket
Sign inDemoInstall

find-file-up

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

find-file-up - npm Package Compare versions

Comparing version 1.0.2 to 2.0.0

109

index.js

@@ -1,21 +0,11 @@

/*!
* find-file-up <https://github.com/jonschlinkert/find-file-up>
*
* Copyright (c) 2015, 2017, Jon Schlinkert.
* Released under the MIT License.
*/
'use strict';
var fs = require('fs');
var path = require('path');
var resolve = require('resolve-dir');
const fs = require('fs');
const path = require('path');
const util = require('util');
const resolve = require('resolve-dir');
/**
* Find a file, starting with the given directory
*/
module.exports = function(filename, cwd, limit, cb) {
function find(filename, cwd, limit = Infinity, callback) {
if (typeof cwd === 'function') {
cb = cwd;
callback = cwd;
cwd = null;

@@ -25,15 +15,19 @@ }

if (typeof limit === 'function') {
cb = limit;
callback = limit;
limit = Infinity;
}
var dir = cwd ? resolve(cwd) : '.';
var n = 0;
var drive = path.resolve(path.sep);
if (typeof callback !== 'function') {
return find.promise(filename, cwd, limit);
}
(function find(dir, next) {
var fp = path.resolve(dir, filename);
const dirname = path.resolve(cwd ? resolve(cwd) : '.');
let depth = 0;
let prev;
fileExists(fp, function(err, exists) {
if (err) {
function recurse(dirname, next) {
const filepath = path.join(dirname, filename);
fs.stat(filepath, function(err, stat) {
if (err && err.code !== 'ENOENT') {
next(err);

@@ -43,35 +37,33 @@ return;

n++;
if (exists) {
next(null, fp);
if (stat) {
next(null, filepath);
return;
}
if (n >= limit || dir === path.sep || dir === '.' || dir === drive) {
next();
if (prev !== dirname && depth < limit) {
prev = dirname;
depth++;
recurse(path.dirname(dirname), next);
return;
}
find(path.dirname(dir), next);
next();
});
}(dir, cb));
};
}
module.exports.sync = function(filename, cwd, limit) {
var dir = cwd ? resolve(cwd) : '.';
var fp = path.join(dir, filename);
var n = 0;
var drive = path.resolve(path.sep);
recurse(dirname, callback);
}
if (fs.existsSync(fp)) {
return path.resolve(fp);
}
find.promise = function(filename, cwd, limit) {
return util.promisify(find)(filename, cwd, limit);
};
if (limit === 0) return null;
find.sync = function(filename, cwd, limit = Infinity) {
let dirname = path.resolve(cwd ? resolve(cwd) : '.');
let depth = 0;
let prev;
while ((dir = path.dirname(dir))) {
n++;
do {
const filepath = path.join(dirname, filename);
var filepath = path.resolve(dir, filename);
if (fs.existsSync(filepath)) {

@@ -81,25 +73,8 @@ return filepath;

if (n >= limit || dir === '.' || dir === path.sep || dir === drive) {
return;
}
}
depth++;
prev = dirname;
dirname = path.dirname(dirname);
} while (prev !== dirname && depth <= limit);
};
/**
* Returns true if a file exists, since `fs.exists` is deprecated.
* See: https://nodejs.org/api/fs.html#fs_fs_exists_path_callback
*/
function fileExists(filepath, cb) {
fs.stat(filepath, function(err) {
if (err && err.code === 'ENOENT') {
cb(null, false);
return;
}
if (err) {
cb(err);
return;
}
cb(null, true);
});
}
module.exports = find;
{
"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). Async and sync.",
"version": "1.0.2",
"description": "Find a file fast, by starting at the given cwd and recursing up one directory until the file is found or we run out of directories.",
"version": "2.0.0",
"homepage": "https://github.com/jonschlinkert/find-file-up",

@@ -27,8 +27,9 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)",

"dependencies": {
"resolve-dir": "^1.0.0"
"resolve-dir": "^1.0.1"
},
"devDependencies": {
"delete": "^1.0.1",
"gulp-format-md": "^0.1.12",
"mocha": "^3.4.1"
"delete": "^1.1.0",
"gulp-format-md": "^1.0.0",
"mocha": "^3.5.3",
"write": "^1.0.3"
},

@@ -53,11 +54,5 @@ "keywords": [

"findup-sync",
"global-modules",
"global-prefix",
"load-module-pkg",
"load-pkg"
"global-modules"
]
},
"reflinks": [
"verb"
],
"lint": {

@@ -64,0 +59,0 @@ "reflinks": true

# find-file-up [![NPM version](https://img.shields.io/npm/v/find-file-up.svg?style=flat)](https://www.npmjs.com/package/find-file-up) [![NPM monthly downloads](https://img.shields.io/npm/dm/find-file-up.svg?style=flat)](https://npmjs.org/package/find-file-up) [![NPM total downloads](https://img.shields.io/npm/dt/find-file-up.svg?style=flat)](https://npmjs.org/package/find-file-up) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/find-file-up.svg?style=flat&label=Travis)](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). Async and sync.
> Find a file fast, by starting at the given cwd and recursing up one directory until the file is found or we run out of directories.
Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
## Install

@@ -16,17 +18,46 @@

```js
var findFile = require('find-file-up');
const find = require('find-file-up');
```
### async
## async
```js
find(filename, cwd, limit, callback);
```
**Example**
* `filename` **String** - (required) the name of the file to find.
* `cwd` **String** - (optional) the starting directory. This value can be prefixed with `~` to search from the user home directory.
* `limit` **Number** - (optional) limit the number of directories to recurse.
* `callback` **Functional** - (optional) A promise is returned when no callback is passed.
**Promise example**
```js
// use "~" to search user home
find('foo.txt', '~/a/b/c')
.then(file => console.log(file)) //=> '/Users/jonschlinkert/foo.txt'
.catch(console.error);
```
**With async-await**
```js
(async function() {
const file = await find('foo.txt', '~/a/b/c');
console.log(file);
//=> '/Users/jonschlinkert/foo.txt'
})();
```
**Callback example**
```js
// find `foo.txt` starting at the given directory
findFile('foo.txt', '.', function(err, fp) {
find('foo.txt', 'a/b/c', function(err, file) {
if (err) throw err;
console.log(file);
//=> /Users/jonschlinkert/dev/find-file-up/fixtures/foo.txt
});
// search user home
findFile('foo.txt', '~/', function(err, fp) {
//=> /Users/jonschlinkert/foo.txt
});
```

@@ -37,29 +68,38 @@

```js
var file = findFile.sync('foo.txt', 'a/b/c/');
find.sync(filename, cwd, limit);
```
## About
**Example**
### Related projects
* `filename` **String** - (required) the name of the file to find.
* `cwd` **String** - (optional) the starting directory.
* `limit` **Number** - (optional) limit the number of directories to recurse.
* [find-pkg](https://www.npmjs.com/package/find-pkg): Find the first directory with a package.json, recursing up, starting with the given directory. Similar… [more](https://github.com/jonschlinkert/find-pkg) | [homepage](https://github.com/jonschlinkert/find-pkg "Find the first directory with a package.json, recursing up, starting with the given directory. Similar to look-up but does not support globs and only searches for package.json. Async and sync.")
* [findup-sync](https://www.npmjs.com/package/findup-sync): Find the first file matching a given pattern in the current directory or the nearest… [more](https://github.com/cowboy/node-findup-sync) | [homepage](https://github.com/cowboy/node-findup-sync "Find the first file matching a given pattern in the current directory or the nearest ancestor directory.")
* [global-modules](https://www.npmjs.com/package/global-modules): The directory used by npm for globally installed npm modules. | [homepage](https://github.com/jonschlinkert/global-modules "The directory used by npm for globally installed npm modules.")
* [global-prefix](https://www.npmjs.com/package/global-prefix): Get the npm global path prefix. | [homepage](https://github.com/jonschlinkert/global-prefix "Get the npm global path prefix.")
* [load-module-pkg](https://www.npmjs.com/package/load-module-pkg): Load the package.json for any project currently installed in node_modules. | [homepage](https://github.com/jonschlinkert/load-module-pkg "Load the package.json for any project currently installed in node_modules.")
* [load-pkg](https://www.npmjs.com/package/load-pkg): Loads the package.json from the root of the user's current project. | [homepage](https://github.com/jonschlinkert/load-pkg "Loads the package.json from the root of the user's current project.")
```js
const file = find.sync('foo.txt', 'a/b/c/');
```
### Contributing
## About
<details>
<summary><strong>Contributing</strong></summary>
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
### Contributors
</details>
| **Commits** | **Contributor** |
| --- | --- |
| 19 | [jonschlinkert](https://github.com/jonschlinkert) |
| 1 | [pointnet](https://github.com/pointnet) |
<details>
<summary><strong>Running Tests</strong></summary>
### Building docs
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
```sh
$ npm install && npm test
```
</details>
<details>
<summary><strong>Building docs</strong></summary>
_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_

@@ -73,10 +113,19 @@

### Running tests
</details>
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
### Related projects
```sh
$ npm install && npm test
```
You might also be interested in these projects:
* [find-pkg](https://www.npmjs.com/package/find-pkg): Find the first directory with a package.json, recursing up, starting with the given directory. Similar… [more](https://github.com/jonschlinkert/find-pkg) | [homepage](https://github.com/jonschlinkert/find-pkg "Find the first directory with a package.json, recursing up, starting with the given directory. Similar to look-up but does not support globs and only searches for package.json. Async and sync.")
* [findup-sync](https://www.npmjs.com/package/findup-sync): Find the first file matching a given pattern in the current directory or the nearest… [more](https://github.com/js-cli/node-findup-sync#readme) | [homepage](https://github.com/js-cli/node-findup-sync#readme "Find the first file matching a given pattern in the current directory or the nearest ancestor directory.")
* [global-modules](https://www.npmjs.com/package/global-modules): The directory used by npm for globally installed npm modules. | [homepage](https://github.com/jonschlinkert/global-modules "The directory used by npm for globally installed npm modules.")
### Contributors
| **Commits** | **Contributor** |
| --- | --- |
| 26 | [jonschlinkert](https://github.com/jonschlinkert) |
| 1 | [pointnet](https://github.com/pointnet) |
### Author

@@ -86,8 +135,9 @@

* [github/jonschlinkert](https://github.com/jonschlinkert)
* [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
* [LinkedIn Profile](https://linkedin.com/in/jonschlinkert)
* [GitHub Profile](https://github.com/jonschlinkert)
* [Twitter Profile](https://twitter.com/jonschlinkert)
### License
Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).
Released under the [MIT License](LICENSE).

@@ -97,2 +147,2 @@

_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on May 20, 2017._
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on March 28, 2018._

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc