Skip to main content
Glama
utils.ts1.43 kB
/** * @fileoverview Utility functions for slash commands module */ import path from 'path'; /** * Resolve project root from a target directory by navigating up * based on a known relative path structure. * * This is useful when lifecycle hooks receive a nested directory * (like `.roo/rules`) and need to get back to the project root * to place commands in the correct location. * * @param targetDir - The target directory (usually rulesDir) * @param relativePath - The relative path from project root (e.g., ".roo/rules") * @returns The project root directory * * @example * ```typescript * // If targetDir is "/project/.roo/rules" and relativePath is ".roo/rules" * const projectRoot = resolveProjectRoot("/project/.roo/rules", ".roo/rules"); * // Returns: "/project" * * // If relativePath is "." then targetDir is already project root * const projectRoot = resolveProjectRoot("/project", "."); * // Returns: "/project" * ``` */ export function resolveProjectRoot( targetDir: string, relativePath: string ): string { // If relativePath is just "." then targetDir is already the project root if (relativePath === '.') { return targetDir; } // Count how many directory levels we need to go up const levels = relativePath.split(path.sep).filter(Boolean).length; let projectRoot = targetDir; for (let i = 0; i < levels; i++) { projectRoot = path.dirname(projectRoot); } return projectRoot; }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/eyaltoledano/claude-task-master'

If you have feedback or need assistance with the MCP directory API, please join our Discord server