├── .gitignore ├── .travis.yml ├── bin └── find-npm-root.js ├── package.json ├── index.js ├── LICENSE.md ├── test └── test.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | branches: 3 | only: 4 | - master 5 | dist: trusty 6 | node_js: 7 | - 8 8 | - 6 9 | - 4 10 | sudo: false 11 | -------------------------------------------------------------------------------- /bin/find-npm-root.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | var findroot = require('..') 3 | var minimist = require('minimist') 4 | var argv = minimist(process.argv.slice(2)) 5 | var path = require('path') 6 | 7 | process.stdout.write(findroot(argv._[0] || process.cwd())) 8 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "find-root", 3 | "author": "jsdnxx", 4 | "version": "1.2.0", 5 | "description": "find the closest package.json", 6 | "keywords": [ 7 | "fs", 8 | "get", 9 | "find", 10 | "closest", 11 | "package", 12 | "module", 13 | "base", 14 | "root" 15 | ], 16 | "bin": { 17 | "find-npm-root": "bin/find-npm-root.js" 18 | }, 19 | "main": "index.js", 20 | "scripts": { 21 | "pretest": "standard", 22 | "test": "mocha" 23 | }, 24 | "repository": "git@github.com:js-n/find-root.git", 25 | "license": "MIT", 26 | "readmeFilename": "README.md", 27 | "devDependencies": { 28 | "chai": "^4.0.2", 29 | "mocha": "^3.4.2", 30 | "moquire": "^1.5.5", 31 | "standard": "^10.0.2" 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var path = require('path') 2 | var fs = require('fs') 3 | 4 | function defaultCheck (dir) { 5 | return fs.existsSync(path.join(dir, 'package.json')) 6 | } 7 | 8 | function findRoot (start, check) { 9 | start = start || module.parent.filename 10 | check = check || defaultCheck 11 | 12 | if (typeof start === 'string') { 13 | if (start[start.length - 1] !== path.sep) { 14 | start += path.sep 15 | } 16 | start = start.split(path.sep) 17 | } 18 | if (!start.length) { 19 | throw new Error('package.json not found in path') 20 | } 21 | start.pop() 22 | var dir = start.join(path.sep) 23 | try { 24 | if (check(dir)) { 25 | return dir 26 | } 27 | } catch (e) {} 28 | return findRoot(start, check) 29 | } 30 | 31 | module.exports = findRoot 32 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright © 2017 jsdnxx 2 | 3 | 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: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | 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. -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | /* globals describe, it */ 2 | var chai = require('chai') 3 | chai.should() 4 | var expect = chai.expect 5 | var moquire = require('moquire') 6 | 7 | var MODULE = '../' 8 | 9 | describe('find-root', function () { 10 | it('recursively looks for package.json', function () { 11 | var checked = [] 12 | var fs = { 13 | existsSync: function (path) { 14 | checked.push(path) 15 | return path === '/foo/package.json' 16 | } 17 | } 18 | 19 | var findRoot = moquire(MODULE, {fs: fs}) 20 | 21 | findRoot('/foo/bar/baz') 22 | .should.equal('/foo') 23 | 24 | checked.should.deep.equal([ 25 | '/foo/bar/baz/package.json', 26 | '/foo/bar/package.json', 27 | '/foo/package.json' 28 | ]) 29 | }) 30 | 31 | it('can take a custom check argument', function () { 32 | var checked = [] 33 | 34 | var findRoot = require(MODULE) 35 | 36 | findRoot('/foo/bar/baz', function (dir) { 37 | checked.push(dir) 38 | return dir === '/foo/bar' 39 | }) 40 | .should.equal('/foo/bar') 41 | 42 | checked.should.deep.equal([ 43 | '/foo/bar/baz', 44 | '/foo/bar' 45 | ]) 46 | }) 47 | 48 | it('throws if not found', function () { 49 | var fs = { 50 | statSync: function (path) { 51 | throw new Error() 52 | } 53 | } 54 | 55 | var findRoot = moquire(MODULE, {fs: fs}) 56 | 57 | expect(function () { 58 | findRoot('/foo/bar/baz/') 59 | }).to.throw('not found') 60 | }) 61 | }) 62 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # find-root 2 | recursively find the closest package.json 3 | 4 | [![Build Status](https://travis-ci.org/js-n/find-root.svg?branch=master)](https://travis-ci.org/js-n/find-root) 5 | 6 | ## usage 7 | Say you want to check if the directory name of a project matches its 8 | module name in package.json: 9 | 10 | ```js 11 | const path = require('path') 12 | const findRoot = require('find-root') 13 | 14 | // from a starting directory, recursively search for the nearest 15 | // directory containing package.json 16 | const root = findRoot('/Users/jsdnxx/Code/find-root/tests') 17 | // => '/Users/jsdnxx/Code/find-root' 18 | 19 | const dirname = path.basename(root) 20 | console.log('is it the same?') 21 | console.log(dirname === require(path.join(root, 'package.json')).name) 22 | ``` 23 | 24 | You can also pass in a custom check function (by default, it checks for the 25 | existence of `package.json` in a directory). In this example, we traverse up 26 | to find the root of a git repo: 27 | ```js 28 | const fs = require('fs') 29 | 30 | const gitRoot = findRoot('/Users/jsdnxx/Code/find-root/tests', function (dir) { 31 | return fs.existsSync(path.resolve(dir, '.git')) 32 | }) 33 | ``` 34 | 35 | 36 | ## api 37 | 38 | ### `findRoot: (startingPath : string, check?: (dir: string) => boolean) => string` 39 | 40 | Returns the path for the nearest directory to `startingPath` containing 41 | a `package.json` file, eg `/foo/module`. 42 | 43 | If `check` is provided, returns the path for the closest parent directory 44 | where `check` returns true. 45 | 46 | Throws an error if no `package.json` is found at any level in the 47 | `startingPath`. 48 | 49 | 50 | ## installation 51 | ```sh 52 | > npm install find-root 53 | ``` 54 | 55 | ## running the tests 56 | 57 | From package root: 58 | ```sh 59 | > npm install 60 | > npm test 61 | ``` 62 | 63 | ## contributors 64 | 65 | - jsdnxx 66 | 67 | 68 | ## license 69 | MIT. (c) 2017 jsdnxx 70 | --------------------------------------------------------------------------------