pax_global_header00006660000000000000000000000064130202725530014511gustar00rootroot0000000000000052 comment=10202fb1621f0c277d5d5eeaf01c1c32b008fbef find-up-2.1.0/000077500000000000000000000000001302027255300130535ustar00rootroot00000000000000find-up-2.1.0/.editorconfig000066400000000000000000000002761302027255300155350ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [{package.json,*.yml}] indent_style = space indent_size = 2 find-up-2.1.0/.gitattributes000066400000000000000000000000351302027255300157440ustar00rootroot00000000000000* text=auto *.js text eol=lf find-up-2.1.0/.gitignore000066400000000000000000000000151302027255300150370ustar00rootroot00000000000000node_modules find-up-2.1.0/.travis.yml000066400000000000000000000001011302027255300151540ustar00rootroot00000000000000os: - linux - osx language: node_js node_js: - '6' - '4' find-up-2.1.0/appveyor.yml000066400000000000000000000006011302027255300154400ustar00rootroot00000000000000environment: matrix: - nodejs_version: '6' - nodejs_version: '4' install: - ps: Install-Product node $env:nodejs_version - set CI=true - npm -g install npm@latest - set PATH=%APPDATA%\npm;%PATH% - npm install matrix: fast_finish: true build: off version: '{build}' shallow_clone: true clone_depth: 1 test_script: - node --version - npm --version - npm test find-up-2.1.0/fixture/000077500000000000000000000000001302027255300145415ustar00rootroot00000000000000find-up-2.1.0/fixture/baz.js000066400000000000000000000000001302027255300156410ustar00rootroot00000000000000find-up-2.1.0/fixture/foo/000077500000000000000000000000001302027255300153245ustar00rootroot00000000000000find-up-2.1.0/fixture/foo/bar/000077500000000000000000000000001302027255300160705ustar00rootroot00000000000000find-up-2.1.0/fixture/foo/bar/.gitkeep000066400000000000000000000000001302027255300175070ustar00rootroot00000000000000find-up-2.1.0/fixture/qux.js000066400000000000000000000000001302027255300157020ustar00rootroot00000000000000find-up-2.1.0/index.js000066400000000000000000000017601302027255300145240ustar00rootroot00000000000000'use strict'; const path = require('path'); const locatePath = require('locate-path'); module.exports = (filename, opts) => { opts = opts || {}; const startDir = path.resolve(opts.cwd || ''); const root = path.parse(startDir).root; const filenames = [].concat(filename); return new Promise(resolve => { (function find(dir) { locatePath(filenames, {cwd: dir}).then(file => { if (file) { resolve(path.join(dir, file)); } else if (dir === root) { resolve(null); } else { find(path.dirname(dir)); } }); })(startDir); }); }; module.exports.sync = (filename, opts) => { opts = opts || {}; let dir = path.resolve(opts.cwd || ''); const root = path.parse(dir).root; const filenames = [].concat(filename); // eslint-disable-next-line no-constant-condition while (true) { const file = locatePath.sync(filenames, {cwd: dir}); if (file) { return path.join(dir, file); } else if (dir === root) { return null; } dir = path.dirname(dir); } }; find-up-2.1.0/license000066400000000000000000000021371302027255300144230ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) Sindre Sorhus (sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. find-up-2.1.0/package.json000066400000000000000000000015231302027255300153420ustar00rootroot00000000000000{ "name": "find-up", "version": "2.1.0", "description": "Find a file by walking up parent directories", "license": "MIT", "repository": "sindresorhus/find-up", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "sindresorhus.com" }, "engines": { "node": ">=4" }, "scripts": { "test": "xo && ava" }, "files": [ "index.js" ], "keywords": [ "find", "up", "find-up", "findup", "look-up", "look", "file", "search", "match", "package", "resolve", "parent", "parents", "folder", "directory", "dir", "walk", "walking", "path" ], "dependencies": { "locate-path": "^2.0.0" }, "devDependencies": { "ava": "*", "tempfile": "^1.1.1", "xo": "*" }, "xo": { "esnext": true } } find-up-2.1.0/readme.md000066400000000000000000000034411302027255300146340ustar00rootroot00000000000000# find-up [![Build Status: Linux and macOS](https://travis-ci.org/sindresorhus/find-up.svg?branch=master)](https://travis-ci.org/sindresorhus/find-up) [![Build Status: Windows](https://ci.appveyor.com/api/projects/status/l0cyjmvh5lq72vq2/branch/master?svg=true)](https://ci.appveyor.com/project/sindresorhus/find-up/branch/master) > Find a file by walking up parent directories ## Install ``` $ npm install --save find-up ``` ## Usage ``` / └── Users └── sindresorhus ├── unicorn.png └── foo └── bar ├── baz └── example.js ``` ```js // example.js const findUp = require('find-up'); findUp('unicorn.png').then(filepath => { console.log(filepath); //=> '/Users/sindresorhus/unicorn.png' }); findUp(['rainbow.png', 'unicorn.png']).then(filepath => { console.log(filepath); //=> '/Users/sindresorhus/unicorn.png' }); ``` ## API ### findUp(filename, [options]) Returns a `Promise` for the filepath or `null`. ### findUp([filenameA, filenameB], [options]) Returns a `Promise` for the first filepath found (by respecting the order) or `null`. ### findUp.sync(filename, [options]) Returns a filepath or `null`. ### findUp.sync([filenameA, filenameB], [options]) Returns the first filepath found (by respecting the order) or `null`. #### filename Type: `string` Filename of the file to find. #### options ##### cwd Type: `string`
Default: `process.cwd()` Directory to start from. ## Related - [find-up-cli](https://github.com/sindresorhus/find-up-cli) - CLI for this module - [pkg-up](https://github.com/sindresorhus/pkg-up) - Find the closest package.json file - [pkg-dir](https://github.com/sindresorhus/pkg-dir) - Find the root directory of an npm package ## License MIT © [Sindre Sorhus](https://sindresorhus.com) find-up-2.1.0/test.js000066400000000000000000000116461302027255300144000ustar00rootroot00000000000000import fs from 'fs'; import path from 'path'; import test from 'ava'; import tempfile from 'tempfile'; import fn from './'; const name = { pkgDir: 'find-up', pkg: 'package.json', fixtureDir: 'fixture', baz: 'baz.js', qux: 'qux.js' }; // These paths are relative to the project root const rel = { fixtureDir: name.fixtureDir }; rel.baz = path.join(rel.fixtureDir, name.baz); rel.qux = path.join(rel.fixtureDir, name.qux); rel.barDir = path.join(rel.fixtureDir, 'foo', 'bar'); const abs = { pkgDir: __dirname }; abs.pkg = path.join(abs.pkgDir, name.pkg); abs.fixtureDir = path.join(abs.pkgDir, name.fixtureDir); abs.baz = path.join(abs.fixtureDir, name.baz); abs.qux = path.join(abs.fixtureDir, name.qux); abs.barDir = path.join(abs.fixtureDir, 'foo', 'bar'); // Create a disjoint directory, used for the not-found tests test.beforeEach(t => { const tmpDir = tempfile(); fs.mkdirSync(tmpDir); t.context.disjoint = tmpDir; }); test.afterEach(t => { fs.rmdirSync(t.context.disjoint); }); test('async (child file)', async t => { const filePath = await fn(name.pkg); t.is(filePath, abs.pkg); }); test('sync (child file)', t => { const filePath = fn.sync(name.pkg); t.is(filePath, abs.pkg); }); test('async (child dir)', async t => { const filePath = await fn(name.fixtureDir); t.is(filePath, abs.fixtureDir); }); test('sync (child dir)', t => { const filePath = fn.sync(name.fixtureDir); t.is(filePath, abs.fixtureDir); }); test('async (child file, custom cwd)', async t => { const filePath = await fn(name.baz, { cwd: rel.fixtureDir }); t.is(filePath, abs.baz); }); test('sync (child file, custom cwd)', t => { const filePath = fn.sync(name.baz, { cwd: rel.fixtureDir }); t.is(filePath, abs.baz); }); test('async (child file, array, custom cwd)', async t => { const filePath = await fn([name.baz], { cwd: rel.fixtureDir }); t.is(filePath, abs.baz); }); test('sync (child file, array, custom cwd)', t => { const filePath = fn.sync([name.baz], { cwd: rel.fixtureDir }); t.is(filePath, abs.baz); }); test('async (first child file, array, custom cwd)', async t => { const filePath = await fn([name.qux, name.baz], { cwd: rel.fixtureDir }); t.is(filePath, abs.qux); }); test('sync (first child file, array, custom cwd)', t => { const filePath = fn.sync([name.qux, name.baz], { cwd: rel.fixtureDir }); t.is(filePath, abs.qux); }); test('async (second child file, array, custom cwd)', async t => { const filePath = await fn(['fake', name.baz], { cwd: rel.fixtureDir }); t.is(filePath, abs.baz); }); test('sync (second child file, array, custom cwd)', t => { const filePath = fn.sync(['fake', name.baz], { cwd: rel.fixtureDir }); t.is(filePath, abs.baz); }); test('async (cwd)', async t => { const filePath = await fn(name.pkgDir, { cwd: abs.pkgDir }); t.is(filePath, abs.pkgDir); }); test('sync (cwd)', t => { const filePath = fn.sync(name.pkgDir, { cwd: abs.pkgDir }); t.is(filePath, abs.pkgDir); }); test('async (cousin file, custom cwd)', async t => { const filePath = await fn(name.baz, { cwd: rel.barDir }); t.is(filePath, abs.baz); }); test('sync (cousin file, custom cwd)', t => { const filePath = fn.sync(name.baz, { cwd: rel.barDir }); t.is(filePath, abs.baz); }); test('async (nested descendant file)', async t => { const filePath = await fn(rel.baz); t.is(filePath, abs.baz); }); test('sync (nested descendant file)', t => { const filePath = fn.sync(rel.baz); t.is(filePath, abs.baz); }); test('async (nested descendant dir)', async t => { const filePath = await fn(rel.barDir); t.is(filePath, abs.barDir); }); test('sync (nested descendant dir)', t => { const filePath = fn.sync(rel.barDir); t.is(filePath, abs.barDir); }); test('async (nested cousin dir, custom cwd)', async t => { const filePath = await fn(rel.barDir, { cwd: rel.fixtureDir }); t.is(filePath, abs.barDir); }); test('sync (nested cousin dir, custom cwd)', t => { const filePath = fn.sync(rel.barDir, { cwd: rel.fixtureDir }); t.is(filePath, abs.barDir); }); test('async (ancestor dir, custom cwd)', async t => { const filePath = await fn(name.fixtureDir, { cwd: rel.barDir }); t.is(filePath, abs.fixtureDir); }); test('sync (ancestor dir, custom cwd)', t => { const filePath = fn.sync(name.fixtureDir, { cwd: rel.barDir }); t.is(filePath, abs.fixtureDir); }); test('async (not found)', async t => { const filePath = await fn('somenonexistentfile.js'); t.is(filePath, null); }); test('sync (not found)', t => { const filePath = fn.sync('somenonexistentfile.js'); t.is(filePath, null); }); // Both tests start in a disjoint directory. `package.json` should not be found // and `null` should be returned. test('async (not found, custom cwd)', async t => { const filePath = await fn(name.pkg, { cwd: t.context.disjoint }); t.is(filePath, null); }); test('sync (not found, custom cwd)', t => { const filePath = fn.sync(name.pkg, { cwd: t.context.disjoint }); t.is(filePath, null); });