vfile-find-up
Advanced tools
Comparing version 2.0.0 to 2.0.2
188
index.js
@@ -1,203 +0,159 @@ | ||
/** | ||
* @author Titus Wormer | ||
* @copyright 2015 Titus Wormer | ||
* @license MIT | ||
* @module vfile:find-up | ||
* @fileoverview Find files by searching the file system upwards. | ||
*/ | ||
'use strict' | ||
'use strict'; | ||
var fs = require('fs') | ||
var path = require('path') | ||
var toVFile = require('to-vfile') | ||
/* Dependencies. */ | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var toVFile = require('to-vfile'); | ||
var INCLUDE = 1 | ||
var BREAK = 4 | ||
/* Constants. */ | ||
var INCLUDE = 1; | ||
var BREAK = 4; | ||
exports.INCLUDE = INCLUDE | ||
exports.BREAK = BREAK | ||
exports.one = findOne | ||
exports.all = findAll | ||
/* Expose. */ | ||
exports.INCLUDE = INCLUDE; | ||
exports.BREAK = BREAK; | ||
exports.one = findOne; | ||
exports.all = findAll; | ||
var readdir = fs.readdir | ||
var resolve = path.resolve | ||
var dirname = path.dirname | ||
var basename = path.basename | ||
/* Methods. */ | ||
var readdir = fs.readdir; | ||
var resolve = path.resolve; | ||
var dirname = path.dirname; | ||
var basename = path.basename; | ||
/** | ||
* Find a file or a directory upwards. | ||
* | ||
* @param {Function} test - Filter function. | ||
* @param {string?} [cwd] - Path to search from. | ||
* @param {Function} callback - Invoked with a result. | ||
*/ | ||
/* Find a file or a directory upwards. */ | ||
function findOne(test, cwd, callback) { | ||
return find(test, cwd, callback, true); | ||
return find(test, cwd, callback, true) | ||
} | ||
/** | ||
* Find files or directories upwards. | ||
* | ||
* @param {Function} test - Filter function. | ||
* @param {string?} [cwd] - Directory to search from. | ||
* @param {Function} callback - Invoked with results. | ||
*/ | ||
/* Find files or directories upwards. */ | ||
function findAll(test, cwd, callback) { | ||
return find(test, cwd, callback); | ||
return find(test, cwd, callback) | ||
} | ||
/** | ||
* Find applicable files. | ||
* | ||
* @param {*} test - Filter. | ||
* @param {string?} cwd - Path to search from. | ||
* @param {Function} callback - Invoked with results. | ||
* @param {boolean?} [one] - When `true`, returns the | ||
* first result (`string`), otherwise, returns an array | ||
* of strings. | ||
*/ | ||
/* Find applicable files. */ | ||
function find(test, cwd, callback, one) { | ||
var results = []; | ||
var current; | ||
var results = [] | ||
var current | ||
test = augment(test); | ||
test = augment(test) | ||
if (!callback) { | ||
callback = cwd; | ||
cwd = null; | ||
callback = cwd | ||
cwd = null | ||
} | ||
current = cwd ? resolve(cwd) : process.cwd(); | ||
current = cwd ? resolve(cwd) : process.cwd() | ||
once(); | ||
once() | ||
return; | ||
/** | ||
* Test a file and check what should be done with | ||
* the resulting file. | ||
* | ||
* @param {string} filePath - Path to file. | ||
* @return {boolean} - `true` when `callback` is | ||
* invoked and iteration should stop. | ||
*/ | ||
/* Test a file and check what should be done with | ||
* the resulting file. */ | ||
function handle(filePath) { | ||
var file = toVFile(filePath); | ||
var result = test(file); | ||
var file = toVFile(filePath) | ||
var result = test(file) | ||
if (mask(result, INCLUDE)) { | ||
if (one) { | ||
callback(null, file); | ||
return true; | ||
callback(null, file) | ||
return true | ||
} | ||
results.push(file); | ||
results.push(file) | ||
} | ||
if (mask(result, BREAK)) { | ||
callback(null, one ? null : results); | ||
return true; | ||
callback(null, one ? null : results) | ||
return true | ||
} | ||
} | ||
/** Check one directory. */ | ||
/* Check one directory. */ | ||
function once(child) { | ||
if (handle(current) === true) { | ||
return; | ||
return | ||
} | ||
readdir(current, function (err, entries) { | ||
var length = entries ? entries.length : 0; | ||
var index = -1; | ||
var entry; | ||
readdir(current, onread) | ||
function onread(err, entries) { | ||
var length = entries ? entries.length : 0 | ||
var index = -1 | ||
var entry | ||
if (err) { | ||
entries = []; | ||
entries = [] | ||
} | ||
while (++index < length) { | ||
entry = entries[index]; | ||
entry = entries[index] | ||
if (entry !== child && handle(resolve(current, entry)) === true) { | ||
return; | ||
return | ||
} | ||
} | ||
child = current; | ||
current = dirname(current); | ||
child = current | ||
current = dirname(current) | ||
if (current === child) { | ||
callback(null, one ? null : results); | ||
return; | ||
callback(null, one ? null : results) | ||
return | ||
} | ||
once(basename(child)); | ||
}); | ||
once(basename(child)) | ||
} | ||
} | ||
} | ||
/** Augment `test` */ | ||
/* Augment `test` */ | ||
function augment(test) { | ||
if (typeof test === 'function') { | ||
return test; | ||
return test | ||
} | ||
return typeof test === 'string' ? testString(test) : multiple(test); | ||
return typeof test === 'string' ? testString(test) : multiple(test) | ||
} | ||
/** Check multiple tests. */ | ||
/* Check multiple tests. */ | ||
function multiple(test) { | ||
var length = test.length; | ||
var index = -1; | ||
var tests = []; | ||
var length = test.length | ||
var index = -1 | ||
var tests = [] | ||
while (++index < length) { | ||
tests[index] = augment(test[index]); | ||
tests[index] = augment(test[index]) | ||
} | ||
return check; | ||
return check | ||
function check(file) { | ||
var result; | ||
var result | ||
index = -1; | ||
index = -1 | ||
while (++index < length) { | ||
result = tests[index](file); | ||
result = tests[index](file) | ||
if (result) { | ||
return result; | ||
return result | ||
} | ||
} | ||
return false; | ||
return false | ||
} | ||
} | ||
/** | ||
* Wrap a string given as a test. | ||
/* Wrap a string given as a test. | ||
* | ||
* A normal string checks for equality to both the filename | ||
* and extension. A string starting with a `.` checks for | ||
* that equality too, and also to just the extension. | ||
* | ||
* @param {string} test - Basename or extname. | ||
* @return {Function} - File-path test. | ||
*/ | ||
* that equality too, and also to just the extension. */ | ||
function testString(test) { | ||
return check; | ||
return check | ||
function check(file) { | ||
return test === file.basename || test === file.extname; | ||
return test === file.basename || test === file.extname | ||
} | ||
} | ||
/** Check a mask. */ | ||
/* Check a mask. */ | ||
function mask(value, bitmask) { | ||
return (value & bitmask) === bitmask; | ||
return (value & bitmask) === bitmask | ||
} |
{ | ||
"name": "vfile-find-up", | ||
"version": "2.0.0", | ||
"version": "2.0.2", | ||
"description": "Find files by searching the file system upwards", | ||
@@ -13,10 +13,4 @@ "license": "MIT", | ||
], | ||
"dependencies": { | ||
"to-vfile": "^2.0.0" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"repository": "https://github.com/wooorm/vfile-find-up", | ||
"bugs": "https://github.com/wooorm/vfile-find-up/issues", | ||
"repository": "vfile/vfile-find-up", | ||
"bugs": "https://github.com/vfile/vfile-find-up/issues", | ||
"author": "Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)", | ||
@@ -26,19 +20,21 @@ "contributors": [ | ||
], | ||
"files": [ | ||
"index.js" | ||
], | ||
"dependencies": { | ||
"to-vfile": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
"nyc": "^8.1.0", | ||
"remark-cli": "^1.0.0", | ||
"remark-comment-config": "^4.0.0", | ||
"remark-github": "^5.0.0", | ||
"remark-lint": "^4.0.0", | ||
"remark-validate-links": "^4.0.0", | ||
"nyc": "^11.0.0", | ||
"prettier": "^1.12.1", | ||
"remark-cli": "^5.0.0", | ||
"remark-preset-wooorm": "^4.0.0", | ||
"tape": "^4.0.0", | ||
"xo": "^0.16.0" | ||
"xo": "^0.20.0" | ||
}, | ||
"scripts": { | ||
"build-md": "remark . --quiet --frail", | ||
"build": "npm run build-md", | ||
"lint": "xo", | ||
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix", | ||
"test-api": "node test", | ||
"test-coverage": "nyc --reporter lcov tape test/index.js", | ||
"test": "npm run build && npm run lint && npm run test-coverage" | ||
"test": "npm run format && npm run test-coverage" | ||
}, | ||
@@ -51,20 +47,24 @@ "nyc": { | ||
}, | ||
"prettier": { | ||
"tabWidth": 2, | ||
"useTabs": false, | ||
"singleQuote": true, | ||
"bracketSpacing": false, | ||
"semi": false, | ||
"trailingComma": "none" | ||
}, | ||
"xo": { | ||
"space": true | ||
"prettier": true, | ||
"esnext": false, | ||
"rules": { | ||
"no-var": "off", | ||
"prefer-arrow-callback": "off", | ||
"object-shorthand": "off" | ||
} | ||
}, | ||
"remarkConfig": { | ||
"output": true, | ||
"plugins": { | ||
"comment-config": null, | ||
"github": null, | ||
"lint": { | ||
"list-item-spacing": false, | ||
"heading-increment": false | ||
}, | ||
"validate-links": null | ||
}, | ||
"settings": { | ||
"bullet": "*" | ||
} | ||
"plugins": [ | ||
"preset-wooorm" | ||
] | ||
} | ||
} |
105
readme.md
@@ -7,3 +7,3 @@ # vfile-find-up [![Build Status][travis-badge]][travis] [![Coverage Status][codecov-badge]][codecov] | ||
[npm][npm-install]: | ||
[npm][]: | ||
@@ -16,39 +16,16 @@ ```bash | ||
Dependencies: | ||
```js | ||
var findUp = require('vfile-find-up'); | ||
``` | ||
var findUp = require('vfile-find-up') | ||
Search for files named `package.json` from the current working directory | ||
upwards: | ||
```js | ||
findUp.all('package.json', console.log); | ||
findUp.all('package.json', console.log) | ||
``` | ||
Logs: | ||
Yields: | ||
```js | ||
null [ VFile { | ||
data: {}, | ||
messages: [], | ||
history: [ '/Users/tilde/projects/oss/vfile-find-up/package.json' ], | ||
cwd: '/Users/tilde/projects/oss/vfile-find-up' } ] | ||
``` | ||
Search for the first file: | ||
```js | ||
findUp.one('package.json', console.log); | ||
``` | ||
Logs: | ||
```js | ||
null VFile { | ||
data: {}, | ||
messages: [], | ||
history: [ '/Users/tilde/projects/oss/vfile-find-up/package.json' ], | ||
cwd: '/Users/tilde/projects/oss/vfile-find-up' } | ||
cwd: '/Users/tilde/projects/oss/vfile-find-up' } ] | ||
``` | ||
@@ -58,3 +35,3 @@ | ||
### `vfileFindUp.all(tests[, path], callback)` | ||
### `findUp.all(tests[, path], callback)` | ||
@@ -65,18 +42,27 @@ Search for `tests` upwards. Invokes callback with either an error | ||
###### Parameters | ||
##### Parameters | ||
* `tests` (`string|Function|Array.<tests>`) | ||
— A test is a [function invoked with a `vfile`][test]. | ||
If an array is passed in, any test must match a given file for it | ||
to be included. | ||
If a `string` is passed in, the `basename` or `extname` of files | ||
must match it for them to be included. | ||
* `path` (`string`, default: `process.cwd()`) | ||
— Place to searching from; | ||
* `callback` (`function cb(err[, files])`); | ||
— Function invoked with all matching files. | ||
###### `tests` | ||
### `vfileFindUp.one(tests[, path], callback)` | ||
Things to search for (`string|Function|Array.<tests>`). | ||
Like `vfileFindUp.all`, but invokes `callback` with the first found | ||
If a `string` is passed in, the `basename` or `extname` of files | ||
must match it for them to be included. | ||
If an array is passed in, any test must match a given file for it | ||
to be included. | ||
Otherwise, they must be [`function`][test]. | ||
###### `path` | ||
Place to searching from (`string`, default: `process.cwd()`). | ||
###### `callback` | ||
Function invoked with all matching files (`function cb(err[, files])`). | ||
### `findUp.one(tests[, path], callback)` | ||
Like `findUp.all`, but invokes `callback` with the first found | ||
file, or `null`. | ||
@@ -89,33 +75,44 @@ | ||
###### Returns | ||
##### Returns | ||
* `true` or `vfileFindUp.INCLUDE` — Include the file in the results; | ||
* `vfileFindUp.BREAK` — Stop searching for files; | ||
* `true` or `findUp.INCLUDE` — Include the file in the results; | ||
* `findUp.BREAK` — Stop searching for files; | ||
* anything else is ignored: the file is not included. | ||
The different flags can be combined by using the pipe operator: | ||
`vfileFindUp.INCLUDE | vfileFindUp.BREAK`. | ||
`findUp.INCLUDE | findUp.BREAK`. | ||
## Contribute | ||
See [`contributing.md` in `vfile/vfile`][contributing] for ways to get started. | ||
This organisation has a [Code of Conduct][coc]. By interacting with this | ||
repository, organisation, or community you agree to abide by its terms. | ||
## License | ||
[MIT][license] © [Titus Wormer][author] | ||
[MIT][] © [Titus Wormer][author] | ||
<!-- Definitions --> | ||
[travis-badge]: https://img.shields.io/travis/wooorm/vfile-find-up.svg | ||
[travis-badge]: https://img.shields.io/travis/vfile/vfile-find-up.svg | ||
[travis]: https://travis-ci.org/wooorm/vfile-find-up | ||
[travis]: https://travis-ci.org/vfile/vfile-find-up | ||
[codecov-badge]: https://img.shields.io/codecov/c/github/wooorm/vfile-find-up.svg | ||
[codecov-badge]: https://img.shields.io/codecov/c/github/vfile/vfile-find-up.svg | ||
[codecov]: https://codecov.io/github/wooorm/vfile-find-up | ||
[codecov]: https://codecov.io/github/vfile/vfile-find-up | ||
[npm-install]: https://docs.npmjs.com/cli/install | ||
[npm]: https://docs.npmjs.com/cli/install | ||
[license]: LICENSE | ||
[mit]: LICENSE | ||
[author]: http://wooorm.com | ||
[vfile]: https://github.com/wooorm/vfile | ||
[vfile]: https://github.com/vfile/vfile | ||
[test]: #function-testfile | ||
[contributing]: https://github.com/vfile/vfile/blob/master/contributing.md | ||
[coc]: https://github.com/vfile/vfile/blob/master/code-of-conduct.md |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
6
8163
122
114