@hyperfrontend/project-scope/project/rootroot
Root-directory detection using marker files for git roots, project roots, and workspace roots.
findGitRoot walks up from a starting path until it finds a .git directory. findProjectRoot looks for the first ancestor containing a package.json (or another entry from ROOT_MARKERS). findWorkspaceRoot is similar but uses WORKSPACE_MARKERS (Nx, Lerna, Rush, pnpm/Yarn/npm workspace declarations) so it stops at the monorepo root rather than at any individual package. findRootDirectory is the generalized helper that takes a custom marker list. The marker constants are exposed for downstream tools that need to extend or customize the search.
API Reference
ƒ Functions
Find nearest .git directory (repo root).
Parameters
| Name | Type | Description |
|---|---|---|
§startPath | string | Starting path |
Returns
stringGit root path or null
Example
Finding Git repository root
import { findGitRoot } from '@hyperfrontend/project-scope'
const gitRoot = findGitRoot('./src/deep/nested/file.ts')
// => '/path/to/repository'Find the project root from a starting path. Project root is the nearest directory containing package.json with source files.
Parameters
| Name | Type | Description |
|---|---|---|
§startPath | string | Starting path |
Returns
stringProject root path or null
Example
Finding project root
import { findProjectRoot } from '@hyperfrontend/project-scope'
// Find project root from current directory
const root = findProjectRoot(process.cwd())
if (root) {
console.log('Project root:', root)
}
// Find root from a deeply nested file
const root2 = findProjectRoot('./libs/my-lib/src/utils/helper.ts')Generic root finder - walk up looking for any marker file.
Parameters
Returns
stringRoot directory path or null
Example
Finding root by marker files
import { findRootDirectory } from '@hyperfrontend/project-scope'
// Find monorepo root by looking for nx.json or lerna.json
const root = findRootDirectory('./libs/my-lib', ['nx.json', 'lerna.json'])
// => '/path/to/monorepo'Find workspace root (monorepo root). Searches up for workspace markers like nx.json, turbo.json, etc.
Parameters
| Name | Type | Description |
|---|---|---|
§startPath | string | Starting path |
Returns
stringWorkspace root path or null
Example
Finding workspace root
import { findWorkspaceRoot } from '@hyperfrontend/project-scope'
const root = findWorkspaceRoot('./libs/my-lib')
if (root) {
console.log('Monorepo root:', root) // e.g., '/home/user/my-monorepo'
}