1.1.0 • Published 7 years ago

find-root v1.1.0

Weekly downloads
4,913,775
License
MIT
Repository
github
Last release
7 years ago

find-root

recursively find the closest package.json

Build Status

usage

Say you want to check if the directory name of a project matches its module name in package.json:

const path = require('path')
const findRoot = require('find-root')

// from a starting directory, recursively search for the nearest
// directory containing package.json
const root = findRoot('/Users/jsdnxx/Code/find-root/tests')
// => '/Users/jsdnxx/Code/find-root'

const dirname = path.basename(root)
console.log('is it the same?')
console.log(dirname === require(path.join(root, 'package.json')).name)

You can also pass in a custom check function (by default, it checks for the existence of package.json in a directory). In this example, we traverse up to find the root of a git repo:

const fs = require('fs')

const gitRoot = findRoot('/Users/jsdnxx/Code/find-root/tests', function (dir) {
  return fs.existsSync(path.resolve(dir, '.git'))
})

api

findRoot: (startingPath : string, check?: (dir: string) => boolean) => string

Returns the path for the nearest directory to startingPath containing a package.json file, eg /foo/module.

If check is provided, returns the path for the closest parent directory where check returns true.

Throws an error if no package.json is found at any level in the startingPath.

installation

> npm install find-root

running the tests

From package root:

> npm install
> npm test

contributors

  • jsdnxx

license

MIT. (c) 2017 jsdnxx

babel-plugin-emotionarchetype-library@kevindurb/react-toolboxkilli8n-react-native-fast-imagern-device-informationpackage-assetsburra-cling-x-climetrovue-cli@steven-torres/jsxrgui-jvale-dawn-cliwebflow-reactgui-jvale-cligui-jvale-dawn-toolsseed-cli-tools@c11/engine.cli-service-web@broken-css/webpack-loadere2rayngtscogoportutilszikkurat@nansen/stylemark@infinitebrahmanuniverse/nolb-findreact-native-alias@everything-registry/sub-chunk-1669@magic-pod/appium@anonybit-modules/videoreconstructionneural_compressor_ext_lab_customizedneural_compressor_ext_lab_customized_2nm-prunengx-testbeddernpm-run-promptnpm-idempotent-rebuildnodefrnodupdepsnois-react-toastnoknpm-list-scriptsdotnet-settingsxbuild-mgrappversion-mgrnest-shopifyneutrino-middleware-esbuildneutrino-middleware-esnextnewfractalcompnexus-hooknext-page-testernext-page-tester-rewnewed@jacksongross/eslint-import-resolver-jsconfig@jinghuan/cachequickfix@jojoxd/vite-plugin-faviconqubic-test@kaltura-ng/dev-workspace@kalkanisys/vue-select@l.systems/config.io@kontorol-ng/dev-workspacereact-chunky-cliorigami-core-lib@gdeioannes/appfairy@gdeioannes/bappfairyowdit@golocalinteractive/storage-essentials-components@git-streamer/cli@getty.io/taskapppadded-standardpickupbiz-npm-packageparkpaths.macro@hiro-ui/eslint-configpatternplate-clientpatternplate-server@hodzitsky/themeplify@habemus/env-optionspkg-configpkgswapnovacap-componentsnodebook-igniteng-testbeddernode-config-loadernoynpm-autoinit@lyn4ed/strapi-admin@madccc/duplicate-package-checker-webpack-plugin@lynched-test/strapi-admin@marionebl/esbuild-dev@marionebl/git-cz@markjm/duplicate-package-checker-webpack-plugin@manta-style/cli@mamba-le/utilsreact-native-typescript-transformer@leny/banye@liquid-labs/liq-test-lib@liquid-labs/npm-toolkit@liquid-labs/plugable-express@liquid-labs/liq-core@liquid-labs/liq-projects-lib@liquid-labs/sdlc-lib-build@liquid-labs/sdlc-resource-babel-and-rollup@liquid-labs/catalyst-resource-babel-and-rollup
1.1.0

7 years ago

1.0.0

8 years ago

0.1.2

8 years ago

0.1.1

10 years ago

0.1.0

10 years ago