Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

3 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # find-root
  2. recursively find the closest package.json
  3. [![Build Status](https://travis-ci.org/js-n/find-root.svg?branch=master)](https://travis-ci.org/js-n/find-root)
  4. ## usage
  5. Say you want to check if the directory name of a project matches its
  6. module name in package.json:
  7. ```js
  8. const path = require('path')
  9. const findRoot = require('find-root')
  10. // from a starting directory, recursively search for the nearest
  11. // directory containing package.json
  12. const root = findRoot('/Users/jsdnxx/Code/find-root/tests')
  13. // => '/Users/jsdnxx/Code/find-root'
  14. const dirname = path.basename(root)
  15. console.log('is it the same?')
  16. console.log(dirname === require(path.join(root, 'package.json')).name)
  17. ```
  18. You can also pass in a custom check function (by default, it checks for the
  19. existence of `package.json` in a directory). In this example, we traverse up
  20. to find the root of a git repo:
  21. ```js
  22. const fs = require('fs')
  23. const gitRoot = findRoot('/Users/jsdnxx/Code/find-root/tests', function (dir) {
  24. return fs.existsSync(path.resolve(dir, '.git'))
  25. })
  26. ```
  27. ## api
  28. ### `findRoot: (startingPath : string, check?: (dir: string) => boolean) => string`
  29. Returns the path for the nearest directory to `startingPath` containing
  30. a `package.json` file, eg `/foo/module`.
  31. If `check` is provided, returns the path for the closest parent directory
  32. where `check` returns true.
  33. Throws an error if no `package.json` is found at any level in the
  34. `startingPath`.
  35. ## installation
  36. ```sh
  37. > npm install find-root
  38. ```
  39. ## running the tests
  40. From package root:
  41. ```sh
  42. > npm install
  43. > npm test
  44. ```
  45. ## contributors
  46. - jsdnxx
  47. ## license
  48. MIT. (c) 2017 jsdnxx