ancesdir
v6.0.0
Published
Find a specific ancestor/root directory given a starting location and a search parameter
Downloads
6,045
Maintainers
Readme
ancesdir
Find a specific ancestor/root directory given a starting location and a search parameter
There are a few packages out there that already support finding the root directory of a project based off assumptions like that directory containing package.json
or node_modules
. However, this is not always the case. I needed a way to find an ancestor directory that may not always have these markers. So, this provides the means to specify a custom marker file or directory as the means to identify the ancestor that you may need.
This may be useful in a variety of situations. For example, a monorepo where you want to differentiate in development scripts between the root folder of each package, and the root folder of the entire repository.
Getting Started
yarn
yarn add ancesdir
npm
npm install ancesdir
Usage
Default
import ancesdir from "ancesdir";
console.log(ancesdir());
Outputs the absolute path of the first parent directory to the ancesdir
package that contains package.json
.
In most cases, this is likely all you need.
From Specific Location
import ancesdir from "ancesdir";
console.log(ancesdir(__dirname));
Outputs the absolute path of the first parent directory to __dirname
that contains package.json
.
Custom Target From Specific Location
import ancesdir from "ancesdir";
console.log(ancesdir(__dirname, ".mymarkerfile");
Outputs the absolute path of the first parent directory that contains a file or directory called .mymarkerfile
.
This is useful if you don't have a classic file hierarchy or you want to use this for more advanced use cases where having control over the file system item that identifies your ancestor is useful.