├── fixtures ├── a │ ├── b │ │ ├── a.md │ │ ├── ONE.js │ │ ├── c │ │ │ ├── ONE.txt │ │ │ ├── two.txt │ │ │ ├── d │ │ │ │ ├── one.txt │ │ │ │ ├── e │ │ │ │ │ ├── f │ │ │ │ │ │ ├── g │ │ │ │ │ │ │ ├── h │ │ │ │ │ │ │ │ ├── i │ │ │ │ │ │ │ │ │ ├── j │ │ │ │ │ │ │ │ │ │ └── package.json │ │ │ │ │ │ │ │ │ └── package.json │ │ │ │ │ │ │ │ └── package.json │ │ │ │ │ │ │ └── package.json │ │ │ │ │ │ └── package.json │ │ │ │ │ └── package.json │ │ │ │ └── package.json │ │ │ └── package.json │ │ ├── one.txt │ │ ├── .config │ │ │ └── waldo │ │ └── package.json │ ├── one.txt │ └── package.json └── package.json ├── benchmark ├── code │ ├── lookup.js │ ├── findup.js │ └── findup-sync.js ├── fixtures │ ├── glob │ │ ├── nested.js │ │ ├── shallow.js │ │ ├── deep-close.js │ │ └── deep-far.js │ └── non-glob │ │ ├── nested.js │ │ ├── shallow.js │ │ ├── deep-close.js │ │ └── deep-far.js ├── index.js ├── check.js └── last.md ├── README.md ├── .gitattributes ├── .gitignore ├── .travis.yml ├── .editorconfig ├── utils.js ├── gulpfile.js ├── LICENSE ├── package.json ├── .verb.md ├── index.js ├── .eslintrc └── test.js /fixtures/a/b/a.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /fixtures/a/one.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /fixtures/a/b/ONE.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /fixtures/a/b/c/ONE.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /fixtures/a/b/c/two.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /fixtures/a/b/one.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /fixtures/a/b/.config/waldo: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /fixtures/a/b/c/d/one.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /fixtures/a/b/c/d/e/f/g/h/i/j/package.json: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /fixtures/a/b/c/d/e/f/g/h/i/package.json: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /benchmark/code/lookup.js: -------------------------------------------------------------------------------- 1 | module.exports = require('../..'); 2 | -------------------------------------------------------------------------------- /benchmark/code/findup.js: -------------------------------------------------------------------------------- 1 | module.exports = require('find-up').sync; 2 | -------------------------------------------------------------------------------- /benchmark/code/findup-sync.js: -------------------------------------------------------------------------------- 1 | module.exports = require('findup-sync'); 2 | -------------------------------------------------------------------------------- /benchmark/fixtures/glob/nested.js: -------------------------------------------------------------------------------- 1 | module.exports = ['**/a/one.txt', {cwd: 'fixtures/a/b'}]; 2 | -------------------------------------------------------------------------------- /benchmark/fixtures/non-glob/nested.js: -------------------------------------------------------------------------------- 1 | module.exports = ['one.txt', {cwd: 'fixtures/a/b/c/d'}]; 2 | -------------------------------------------------------------------------------- /benchmark/fixtures/non-glob/shallow.js: -------------------------------------------------------------------------------- 1 | module.exports = ['a/one.txt', {cwd: 'fixtures/a/b'}]; 2 | -------------------------------------------------------------------------------- /fixtures/a/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "a", 3 | "description": "package.json from a." 4 | } 5 | -------------------------------------------------------------------------------- /benchmark/fixtures/glob/shallow.js: -------------------------------------------------------------------------------- 1 | module.exports = ['**/a/one.txt', {cwd: 'fixtures/a/b/c/d'}]; 2 | -------------------------------------------------------------------------------- /fixtures/a/b/c/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "c", 3 | "description": "package.json from c." 4 | } 5 | -------------------------------------------------------------------------------- /fixtures/a/b/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "b", 3 | "description": "package.json from b." 4 | } 5 | -------------------------------------------------------------------------------- /fixtures/a/b/c/d/e/f/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "f", 3 | "description": "package.json from f." 4 | } 5 | -------------------------------------------------------------------------------- /fixtures/a/b/c/d/e/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "e", 3 | "description": "package.json from e." 4 | } 5 | -------------------------------------------------------------------------------- /fixtures/a/b/c/d/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "d", 3 | "description": "package.json from d." 4 | } 5 | -------------------------------------------------------------------------------- /fixtures/a/b/c/d/e/f/g/h/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "h", 3 | "description": "package.json from h." 4 | } 5 | -------------------------------------------------------------------------------- /fixtures/a/b/c/d/e/f/g/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "g", 3 | "description": "package.json from g." 4 | } 5 | -------------------------------------------------------------------------------- /fixtures/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fixtures", 3 | "description": "package.json from ./fixtures." 4 | } 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DEPRECATED 2 | 3 | Please use [findup-sync](https://github.com/js-cli/node-findup-sync) instead. 4 | -------------------------------------------------------------------------------- /benchmark/fixtures/glob/deep-close.js: -------------------------------------------------------------------------------- 1 | module.exports = ['**/g/package.json', {cwd: 'fixtures/a/b/c/d/e/f/g/h'}]; 2 | -------------------------------------------------------------------------------- /benchmark/fixtures/glob/deep-far.js: -------------------------------------------------------------------------------- 1 | module.exports = ['**/a/package.json', {cwd: 'fixtures/a/b/c/d/e/f/g/h'}]; 2 | -------------------------------------------------------------------------------- /benchmark/fixtures/non-glob/deep-close.js: -------------------------------------------------------------------------------- 1 | module.exports = ['g/package.json', {cwd: 'fixtures/a/b/c/d/e/f/g/h'}]; 2 | -------------------------------------------------------------------------------- /benchmark/fixtures/non-glob/deep-far.js: -------------------------------------------------------------------------------- 1 | module.exports = ['a/package.json', {cwd: 'fixtures/a/b/c/d/e/f/g/h'}]; 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Enforce Unix newlines 2 | * text eol=lf 3 | 4 | # binaries 5 | *.ai binary 6 | *.psd binary 7 | *.jpg binary 8 | *.gif binary 9 | *.png binary 10 | *.jpeg binary -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.sublime-* 2 | _gh_pages 3 | actual 4 | bower_components 5 | node_modules 6 | npm-debug.log 7 | temp 8 | test/actual 9 | tmp 10 | TODO.md 11 | vendor 12 | *.DS_Store 13 | coverage 14 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - "stable" 5 | - "0.12" 6 | - "0.10" 7 | matrix: 8 | fast_finish: true 9 | allow_failures: 10 | - node_js: "0.10" 11 | -------------------------------------------------------------------------------- /benchmark/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var Suite = require('benchmarked'); 4 | 5 | var suite = new Suite({ 6 | result: false, 7 | fixtures: 'fixtures/non-glob/*.js', 8 | add: 'code/*.js', 9 | cwd: __dirname 10 | }); 11 | 12 | suite.run(); 13 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | end_of_line = lf 7 | charset = utf-8 8 | indent_size = 2 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | insert_final_newline = false 15 | 16 | [test/**] 17 | trim_trailing_whitespace = false 18 | insert_final_newline = false 19 | 20 | [templates/**] 21 | trim_trailing_whitespace = false 22 | insert_final_newline = false 23 | -------------------------------------------------------------------------------- /utils.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var utils = require('lazy-cache')(require); 4 | var fn = require; 5 | require = utils; 6 | 7 | /** 8 | * Lazily required module dependencies (globbing/fs stuff is a great 9 | * use case for lazy-evaluation) 10 | */ 11 | 12 | require('is-glob'); 13 | require('normalize-path', 'normalize'); 14 | require('resolve-dir', 'resolve'); 15 | require('micromatch', 'mm'); 16 | require = fn; 17 | 18 | /** 19 | * Expose `utils` 20 | */ 21 | 22 | module.exports = utils; 23 | -------------------------------------------------------------------------------- /benchmark/check.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var fs = require('fs'); 4 | var glob = require('glob'); 5 | var bold = require('ansi-bold'); 6 | var path = require('path'); 7 | 8 | var code = glob.sync(__dirname + '/code/*.js'); 9 | var fixtures = glob.sync(__dirname + '/fixtures/**/*.js'); 10 | 11 | /** 12 | * Sanity check 13 | * 14 | * Run to ensure that all fns return the same result. 15 | */ 16 | 17 | code.forEach(function (fp) { 18 | var fn = require(path.resolve(__dirname, 'code', fp)); 19 | var name = path.basename(fp, path.extname(fp)); 20 | 21 | fixtures.forEach(function (fixture) { 22 | console.log(bold(name) + ':', fn.apply(null, require(fixture))); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /benchmark/last.md: -------------------------------------------------------------------------------- 1 | #1: deep-close 2 | findup-sync x 5,797 ops/sec ±1.66% (90 runs sampled) 3 | findup x 22,972 ops/sec ±0.82% (92 runs sampled) 4 | lookup x 64,389 ops/sec ±0.75% (92 runs sampled) 5 | 6 | #2: deep-far 7 | findup-sync x 2,165 ops/sec ±1.01% (92 runs sampled) 8 | findup x 8,250 ops/sec ±0.78% (96 runs sampled) 9 | lookup x 12,730 ops/sec ±0.93% (92 runs sampled) 10 | 11 | #3: nested 12 | findup-sync x 21,603 ops/sec ±0.76% (93 runs sampled) 13 | findup x 103,427 ops/sec ±0.93% (95 runs sampled) 14 | lookup x 381,425 ops/sec ±0.91% (91 runs sampled) 15 | 16 | #4: shallow 17 | findup-sync x 6,565 ops/sec ±0.66% (92 runs sampled) 18 | findup x 23,674 ops/sec ±0.89% (95 runs sampled) 19 | lookup x 57,029 ops/sec ±0.83% (93 runs sampled) 20 | -------------------------------------------------------------------------------- /gulpfile.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var gulp = require('gulp'); 4 | var mocha = require('gulp-mocha'); 5 | var istanbul = require('gulp-istanbul'); 6 | var eslint = require('gulp-eslint'); 7 | 8 | var lint = ['index.js', 'lib.js']; 9 | 10 | gulp.task('coverage', function() { 11 | return gulp.src(lint) 12 | .pipe(istanbul()) 13 | .pipe(istanbul.hookRequire()); 14 | }); 15 | 16 | gulp.task('test', ['coverage'], function() { 17 | return gulp.src('test.js') 18 | .pipe(mocha({reporter: 'spec'})) 19 | .pipe(istanbul.writeReports()); 20 | }); 21 | 22 | gulp.task('lint', function() { 23 | return gulp.src(lint.concat(['test.js', 'gulpfile.js'])) 24 | .pipe(eslint()) 25 | .pipe(eslint.format()); 26 | }); 27 | 28 | gulp.task('default', ['test', 'lint']); 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014-2015, Jon Schlinkert. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "look-up", 3 | "description": "Faster drop-in replacement for find-up and findup-sync.", 4 | "version": "0.8.3", 5 | "homepage": "https://github.com/jonschlinkert/look-up", 6 | "author": "Jon Schlinkert (https://github.com/jonschlinkert)", 7 | "repository": "jonschlinkert/look-up", 8 | "bugs": { 9 | "url": "https://github.com/jonschlinkert/look-up/issues" 10 | }, 11 | "license": "MIT", 12 | "files": [ 13 | "index.js", 14 | "utils.js" 15 | ], 16 | "main": "index.js", 17 | "engines": { 18 | "node": ">=0.10.0" 19 | }, 20 | "scripts": { 21 | "test": "mocha", 22 | "benchmark": "npm run benchmark" 23 | }, 24 | "dependencies": { 25 | "is-glob": "^2.0.1", 26 | "lazy-cache": "^1.0.3", 27 | "micromatch": "^2.3.7", 28 | "normalize-path": "^2.0.1", 29 | "resolve-dir": "^0.1.0" 30 | }, 31 | "devDependencies": { 32 | "ansi-bold": "^0.1.1", 33 | "benchmarked": "^0.1.4", 34 | "find-up": "^1.0.0", 35 | "findup-sync": "^0.3.0", 36 | "glob": "^5.0.15", 37 | "gulp": "^3.9.0", 38 | "gulp-eslint": "^1.1.0", 39 | "gulp-istanbul": "^0.10.1", 40 | "gulp-mocha": "^2.1.3", 41 | "is-absolute": "^0.2.2", 42 | "minimist": "^1.2.0", 43 | "mocha": "*", 44 | "resolve": "^1.1.6", 45 | "should": "*", 46 | "user-home": "^2.0.0" 47 | }, 48 | "keywords": [ 49 | "file", 50 | "find", 51 | "find-up", 52 | "findup", 53 | "glob", 54 | "look", 55 | "match", 56 | "package", 57 | "package.json", 58 | "pattern", 59 | "resolve", 60 | "search", 61 | "sync", 62 | "up" 63 | ], 64 | "verb": { 65 | "related": { 66 | "list": [ 67 | "look-up-cli", 68 | "is-glob", 69 | "micromatch" 70 | ] 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /.verb.md: -------------------------------------------------------------------------------- 1 | # {%= name %} {%= badge("fury") %} {%= badge("travis") %} 2 | 3 | > {%= description %} 4 | 5 | ## Install 6 | {%= include("install-npm", {save: true}) %} 7 | 8 | See the [benchmarks](#run-benchmarks) or [unit tests](./test.js). 9 | 10 | ## Usage 11 | 12 | ```js 13 | var lookup = require('{%= name %}'); 14 | lookup('package.json'); 15 | //=> '/Users/jonschlinkert/dev/look-up/package.json' 16 | ``` 17 | 18 | look-up will recurse _up from the cwd_ until it finds the given file. 19 | 20 | ```js 21 | lookup('package.json', { cwd: 'foo/bar' }); 22 | //=> '/Users/jonschlinkert/dev/look-up/package.json' 23 | ``` 24 | 25 | Glob patterns are also supported (string or array): 26 | 27 | ```js 28 | lookup(['*.json', '*.foo'], { cwd: 'foo/bar' }); 29 | //=> '/Users/jonschlinkert/dev/look-up/package.json' 30 | ``` 31 | 32 | 33 | ## Running benchmarks 34 | 35 | Benchmarks were run on [mac and windows](https://github.com/jonschlinkert/look-up/issues/1). look-up is 5x-20x faster than [findup-sync][] and 3x faster than [find-up][] 36 | 37 | **Note** that [find-up][] does not support glob patterns, so these benchmarks only include arguments that are supported by all three libs. 38 | 39 | As of {%= date() %}: 40 | 41 | ```bash 42 | {%= docs("last", {cwd: "benchmark"}) %} 43 | ``` 44 | 45 | To run the [benchmarks](./benchmark), install dev dependencies: 46 | 47 | ```bash 48 | npm i -d && npm run benchmark 49 | ``` 50 | 51 | ## Related 52 | {%= related(verb.related.list) %} 53 | 54 | ## Running tests 55 | {%= include("tests") %} 56 | 57 | ## Coverage 58 | 59 | As of {%= date() %}: 60 | 61 | ``` 62 | Statements : 100% (57/57) 63 | Branches : 100% (26/26) 64 | Functions : 100% (5/5) 65 | Lines : 100% (55/55) 66 | ``` 67 | 68 | ## Contributing 69 | {%= include("contributing") %} 70 | 71 | ## Author 72 | {%= include("author") %} 73 | 74 | ## License 75 | {%= copyright() %} 76 | {%= license() %} 77 | 78 | *** 79 | 80 | {%= include("footer") %} 81 | 82 | {%= reflinks(['micromatch', 'findup-sync', 'find-up']) %} 83 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /** 4 | * Module dependencies 5 | */ 6 | 7 | var fs = require('fs'); 8 | var path = require('path'); 9 | var utils = require('./utils'); 10 | 11 | /** 12 | * @param {String|Array} `pattern` Glob pattern or file path(s) to match against. 13 | * @param {Object} `options` Options to pass to [micromatch]. Note that if you want to start in a different directory than the current working directory, specify a `cwd` property here. 14 | * @return {String} Returns the first matching file. 15 | * @api public 16 | */ 17 | 18 | module.exports = function (patterns, options) { 19 | if (typeof patterns === 'string') { 20 | return lookup(patterns, options); 21 | } 22 | 23 | if (!Array.isArray(patterns)) { 24 | throw new TypeError('expected a string or array.'); 25 | } 26 | 27 | var len = patterns.length, i = -1; 28 | while (++i < len) { 29 | var res = lookup(patterns[i], options); 30 | if (res) return res; 31 | } 32 | 33 | return null; 34 | }; 35 | 36 | function lookup(pattern, options) { 37 | var cwd = resolveCwd(options || {}); 38 | if (utils.isGlob(pattern)) { 39 | return matchFile(cwd, pattern, options); 40 | } else { 41 | return findFile(cwd, pattern); 42 | } 43 | } 44 | 45 | function matchFile(cwd, pattern, opts) { 46 | var isMatch = utils.mm.matcher(pattern, opts); 47 | var files = fs.readdirSync(cwd); 48 | var len = files.length, i = -1; 49 | 50 | while (++i < len) { 51 | var name = files[i]; 52 | var fp = path.join(cwd, name); 53 | if (isMatch(name) || isMatch(fp)) { 54 | return fp; 55 | } 56 | } 57 | 58 | var dir = path.dirname(cwd); 59 | if (dir === cwd) return null; 60 | 61 | return matchFile(dir, pattern, opts); 62 | } 63 | 64 | function findFile(cwd, filename) { 65 | var fp = cwd ? (cwd + '/' + filename) : filename; 66 | if (fs.existsSync(fp)) { 67 | return fp; 68 | } 69 | 70 | var segs = cwd.split(path.sep); 71 | var len = segs.length; 72 | 73 | while (len--) { 74 | cwd = segs.slice(0, len).join('/'); 75 | fp = cwd + '/' + filename; 76 | if (fs.existsSync(fp)) { 77 | return fp; 78 | } 79 | } 80 | return null; 81 | } 82 | 83 | function resolveCwd(opts) { 84 | var cwd = opts.cwd || ''; 85 | if (/^\W/.test(cwd)) { 86 | cwd = utils.resolve(cwd); 87 | } 88 | return cwd; 89 | } 90 | 91 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "ecmaFeatures": { 3 | "modules": true, 4 | "experimentalObjectRestSpread": true 5 | }, 6 | 7 | "env": { 8 | "browser": false, 9 | "es6": true, 10 | "node": true, 11 | "mocha": true 12 | }, 13 | 14 | "globals": { 15 | "document": false, 16 | "navigator": false, 17 | "window": false 18 | }, 19 | 20 | "rules": { 21 | "accessor-pairs": 2, 22 | "arrow-spacing": [2, { "before": true, "after": true }], 23 | "block-spacing": [2, "always"], 24 | "brace-style": [2, "1tbs", { "allowSingleLine": true }], 25 | "comma-dangle": [2, "never"], 26 | "comma-spacing": [2, { "before": false, "after": true }], 27 | "comma-style": [2, "last"], 28 | "constructor-super": 2, 29 | "curly": [2, "multi-line"], 30 | "dot-location": [2, "property"], 31 | "eol-last": 2, 32 | "eqeqeq": [2, "allow-null"], 33 | "generator-star-spacing": [2, { "before": true, "after": true }], 34 | "handle-callback-err": [2, "^(err|error)$" ], 35 | "indent": [2, 2, { "SwitchCase": 1 }], 36 | "key-spacing": [2, { "beforeColon": false, "afterColon": true }], 37 | "new-cap": [2, { "newIsCap": true, "capIsNew": false }], 38 | "new-parens": 2, 39 | "no-array-constructor": 2, 40 | "no-caller": 2, 41 | "no-class-assign": 2, 42 | "no-cond-assign": 2, 43 | "no-const-assign": 2, 44 | "no-control-regex": 2, 45 | "no-debugger": 2, 46 | "no-delete-var": 2, 47 | "no-dupe-args": 2, 48 | "no-dupe-class-members": 2, 49 | "no-dupe-keys": 2, 50 | "no-duplicate-case": 2, 51 | "no-empty-character-class": 2, 52 | "no-empty-label": 2, 53 | "no-eval": 2, 54 | "no-ex-assign": 2, 55 | "no-extend-native": 2, 56 | "no-extra-bind": 2, 57 | "no-extra-boolean-cast": 2, 58 | "no-extra-parens": [2, "functions"], 59 | "no-fallthrough": 2, 60 | "no-floating-decimal": 2, 61 | "no-func-assign": 2, 62 | "no-implied-eval": 2, 63 | "no-inner-declarations": [2, "functions"], 64 | "no-invalid-regexp": 2, 65 | "no-irregular-whitespace": 2, 66 | "no-iterator": 2, 67 | "no-label-var": 2, 68 | "no-labels": 2, 69 | "no-lone-blocks": 2, 70 | "no-mixed-spaces-and-tabs": 2, 71 | "no-multi-spaces": 2, 72 | "no-multi-str": 2, 73 | "no-multiple-empty-lines": [2, { "max": 1 }], 74 | "no-native-reassign": 2, 75 | "no-negated-in-lhs": 2, 76 | "no-new": 2, 77 | "no-new-func": 2, 78 | "no-new-object": 2, 79 | "no-new-require": 2, 80 | "no-new-wrappers": 2, 81 | "no-obj-calls": 2, 82 | "no-octal": 2, 83 | "no-octal-escape": 2, 84 | "no-proto": 0, 85 | "no-redeclare": 2, 86 | "no-regex-spaces": 2, 87 | "no-return-assign": 2, 88 | "no-self-compare": 2, 89 | "no-sequences": 2, 90 | "no-shadow-restricted-names": 2, 91 | "no-spaced-func": 2, 92 | "no-sparse-arrays": 2, 93 | "no-this-before-super": 2, 94 | "no-throw-literal": 2, 95 | "no-trailing-spaces": 0, 96 | "no-undef": 2, 97 | "no-undef-init": 2, 98 | "no-unexpected-multiline": 2, 99 | "no-unneeded-ternary": [2, { "defaultAssignment": false }], 100 | "no-unreachable": 2, 101 | "no-unused-vars": [2, { "vars": "all", "args": "none" }], 102 | "no-useless-call": 0, 103 | "no-with": 2, 104 | "one-var": [0, { "initialized": "never" }], 105 | "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }], 106 | "padded-blocks": [0, "never"], 107 | "quotes": [2, "single", "avoid-escape"], 108 | "radix": 2, 109 | "semi": [2, "always"], 110 | "semi-spacing": [2, { "before": false, "after": true }], 111 | "space-after-keywords": [2, "always"], 112 | "space-before-blocks": [2, "always"], 113 | "space-before-function-paren": [2, "never"], 114 | "space-before-keywords": [2, "always"], 115 | "space-in-parens": [2, "never"], 116 | "space-infix-ops": 2, 117 | "space-return-throw-case": 2, 118 | "space-unary-ops": [2, { "words": true, "nonwords": false }], 119 | "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }], 120 | "use-isnan": 2, 121 | "valid-typeof": 2, 122 | "wrap-iife": [2, "any"], 123 | "yoda": [2, "never"] 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | require('mocha'); 4 | require('should'); 5 | var fs = require('fs'); 6 | var path = require('path'); 7 | var assert = require('assert'); 8 | var resolve = require('resolve'); 9 | var expand = require('resolve-dir'); 10 | var isAbsolute = require('is-absolute'); 11 | var norm = require('normalize-path'); 12 | var home = require('user-home'); 13 | var argv = require('minimist')(process.argv.slice(2)); 14 | var lookup = require('./'); 15 | var cwd, actual, opts; 16 | 17 | if (argv.bench) { 18 | var b = path.join(__dirname, 'benchmark/code', argv.bench); 19 | console.log(b); 20 | lookup = require(b); 21 | } 22 | 23 | assert.isPath = function (fp, basename) { 24 | assert(fp); 25 | assert.equal(typeof fp, 'string'); 26 | }; 27 | 28 | assert.isAbsolute = function (fp) { 29 | assert(fp); 30 | assert(isAbsolute(fp)); 31 | }; 32 | 33 | assert.exists = function (fp) { 34 | assert(fp); 35 | try { 36 | fs.statSync(fp); 37 | } catch(err) { 38 | assert(fp, err); 39 | } 40 | }; 41 | 42 | assert.basename = function (fp, basename) { 43 | assert(fp); 44 | assert.equal(path.basename(fp), basename); 45 | }; 46 | 47 | assert.dirname = function (fp, dirname) { 48 | assert(fp); 49 | assert.equal(path.dirname(path.resolve(fp)), path.resolve(dirname)); 50 | }; 51 | 52 | function npm(name) { 53 | return path.dirname(resolve.sync(name)); 54 | } 55 | 56 | describe('lookup', function () { 57 | 58 | before(function () { 59 | fs.writeFileSync(home + '/_aaa.txt', ''); 60 | fs.writeFileSync(home + '/_bbb.txt', ''); 61 | }); 62 | after(function () { 63 | fs.unlinkSync(home + '/_aaa.txt'); 64 | fs.unlinkSync(home + '/_bbb.txt'); 65 | }); 66 | 67 | it('should throw when the first arg is not a string or array:', function () { 68 | (function() { 69 | lookup(); 70 | }).should.throw('expected a string or array.') 71 | }); 72 | 73 | it('should work when no cwd is given', function () { 74 | var actual = lookup('package.json'); 75 | assert(actual); 76 | assert.dirname(actual, __dirname); 77 | assert.basename(actual, 'package.json'); 78 | }); 79 | 80 | it('should support normal (non-glob) file paths:', function () { 81 | cwd = path.dirname(resolve.sync('normalize-path')); 82 | var actual = lookup('package.json', {cwd: cwd}); 83 | assert.dirname(actual, cwd); 84 | assert.basename(actual, 'package.json'); 85 | 86 | actual = lookup('c/package.json', {cwd: 'fixtures/a/b/c/d/e/f/g'}); 87 | assert.basename(actual, 'package.json'); 88 | assert.dirname(actual, 'fixtures/a/b/c'); 89 | 90 | cwd = path.dirname(resolve.sync('is-glob')); 91 | actual = lookup('package.json', {cwd: cwd}); 92 | assert.dirname(actual, cwd); 93 | assert.basename(actual, 'package.json'); 94 | }); 95 | 96 | it('should support finding file in immediate parent dir', function () { 97 | cwd = path.join(__dirname, 'fixtures/a/b/c'); 98 | var actual = lookup('a.md', { cwd: cwd }); 99 | assert.dirname(actual, path.dirname(cwd)); 100 | assert.basename(actual, 'a.md'); 101 | }); 102 | 103 | it('should support glob patterns', function () { 104 | var opts = {cwd: 'fixtures/a/b/c/d/e/f/g'}; 105 | 106 | actual = lookup('**/c/package.json', opts); 107 | assert.dirname(actual, 'fixtures/a/b/c'); 108 | assert.basename(actual, 'package.json'); 109 | 110 | actual = lookup('c/package.json', opts); 111 | assert.dirname(actual, 'fixtures/a/b/c'); 112 | assert.basename(actual, 'package.json'); 113 | 114 | actual = lookup('**/ONE.txt', opts); 115 | assert.dirname(actual, 'fixtures/a/b/c'); 116 | assert.basename(actual, 'ONE.txt'); 117 | 118 | actual = lookup('**/two.txt', opts); 119 | assert.dirname(actual, 'fixtures/a/b/c'); 120 | assert.basename(actual, 'two.txt'); 121 | 122 | cwd = npm('is-glob'); 123 | actual = lookup('p*.json', {cwd: cwd}); 124 | assert.dirname(actual, cwd); 125 | assert.basename(actual, 'package.json'); 126 | }); 127 | 128 | it('should support arrays of glob patterns', function () { 129 | var opts = {cwd: 'fixtures/a/b/c/d/e/f/g'}; 130 | 131 | actual = lookup(['lslsl', '**/c/package.json'], opts); 132 | assert.dirname(actual, 'fixtures/a/b/c'); 133 | assert.basename(actual, 'package.json'); 134 | 135 | actual = lookup(['lslsl', 'c/package.json'], opts); 136 | assert.dirname(actual, 'fixtures/a/b/c'); 137 | assert.basename(actual, 'package.json'); 138 | 139 | actual = lookup(['lslsl', '**/ONE.txt'], opts); 140 | assert.dirname(actual, 'fixtures/a/b/c'); 141 | assert.basename(actual, 'ONE.txt'); 142 | 143 | actual = lookup(['lslsl', '**/two.txt'], opts); 144 | assert.dirname(actual, 'fixtures/a/b/c'); 145 | assert.basename(actual, 'two.txt'); 146 | 147 | actual = lookup(['lslsl', '**/blah.txt'], opts); 148 | assert(actual === null); 149 | 150 | cwd = npm('is-glob'); 151 | actual = lookup(['lslsl', 'p*.json'], {cwd: cwd}); 152 | assert.dirname(actual, cwd); 153 | assert.basename(actual, 'package.json'); 154 | }); 155 | 156 | it('should support micromatch `matchBase` option:', function () { 157 | var opts = { matchBase: true, cwd: 'fixtures/a/b/c/d/e/f/g' }; 158 | 159 | actual = lookup('package.json', opts); 160 | assert.basename(actual, 'package.json'); 161 | assert.dirname(actual, 'fixtures/a/b/c/d/e/f/g'); 162 | 163 | actual = lookup('one.txt', opts); 164 | assert.basename(actual, 'one.txt'); 165 | assert.dirname(actual, 'fixtures/a/b/c/d'); 166 | 167 | actual = lookup('two.txt', opts); 168 | assert.basename(actual, 'two.txt'); 169 | assert.dirname(actual, 'fixtures/a/b/c'); 170 | }); 171 | 172 | it('should support micromatch `nocase` option:', function () { 173 | actual = lookup('ONE.*', { cwd: 'fixtures/a/b/c/d' }); 174 | assert.basename(actual, 'ONE.txt'); 175 | assert.dirname(actual, 'fixtures/a/b/c'); 176 | 177 | actual = lookup('ONE.*', { cwd: 'fixtures/a/b/c/d', nocase: true }); 178 | assert.basename(actual, 'one.txt'); 179 | assert.dirname(actual, 'fixtures/a/b/c/d'); 180 | }); 181 | 182 | it('should find files from absolute paths:', function () { 183 | var actual = lookup('package.json', { cwd: __dirname }) 184 | 185 | assert.basename(actual, 'package.json'); 186 | assert.dirname(actual, __dirname); 187 | 188 | actual = lookup('one.txt', { cwd: __dirname + '/fixtures/a' }); 189 | assert.basename(actual, 'one.txt'); 190 | assert.dirname(actual, 'fixtures/a'); 191 | 192 | actual = lookup('two.txt', { cwd: __dirname + '/fixtures/a/b/c' }); 193 | assert.basename(actual, 'two.txt'); 194 | assert.dirname(actual, 'fixtures/a/b/c'); 195 | }); 196 | 197 | it('should find files in user home:', function () { 198 | var actual = lookup('*', { cwd: home }); 199 | assert.isPath(actual); 200 | assert.exists(actual); 201 | assert.dirname(actual, home); 202 | }); 203 | 204 | it('should find files in user home using tilde expansion:', function () { 205 | var actual = lookup('*', { cwd: '~' }); 206 | assert.isPath(actual); 207 | assert.exists(actual); 208 | assert.dirname(actual, home); 209 | }); 210 | 211 | it('should find files in global npm modules:', function () { 212 | if (!process.env.TRAVIS) { 213 | var actual = lookup('moc*', { cwd: '@/' }); 214 | assert.isPath(actual); 215 | assert.exists(actual); 216 | assert.dirname(actual, expand('@')); 217 | } 218 | }); 219 | 220 | it('should return `null` when no files are found:', function () { 221 | assert.equal(lookup('foo.json', {cwd: 'fixtures/a/b/c/d/e/f/g'}), null); 222 | assert.equal(lookup('foo.json', {cwd: 'fixtures/a/b/c/d/e/f/g', matchBase: true}), null); 223 | }); 224 | }); 225 | --------------------------------------------------------------------------------