@hyperfrontend/project-scope/project/root

root

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

§function

findGitRoot(startPath: string): string

Find nearest .git directory (repo root).

Parameters

NameTypeDescription
§startPath
string
Starting path

Returns

string
Git 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'
§function

findProjectRoot(startPath: string): string

Find the project root from a starting path. Project root is the nearest directory containing package.json with source files.

Parameters

NameTypeDescription
§startPath
string
Starting path

Returns

string
Project 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')
§function

findRootDirectory(startPath: string, markers: string[] | unknown): string

Generic root finder - walk up looking for any marker file.

Parameters

NameTypeDescription
§startPath
string
Starting path
§markers
string[] | unknown
Files to search for

Returns

string
Root 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'
§function

findWorkspaceRoot(startPath: string): string

Find workspace root (monorepo root). Searches up for workspace markers like nx.json, turbo.json, etc.

Parameters

NameTypeDescription
§startPath
string
Starting path

Returns

string
Workspace 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'
}

Variables

§type

ROOT_MARKERS

Files indicating project root.
§type

WORKSPACE_MARKERS

Files indicating workspace/monorepo root.