Skip to main content
Glama
nrwl

Nx MCP Server

Official
by nrwl
get-generator-context-v2.ts2.09 kB
import { GeneratorContext } from '@nx-console/shared-generate-ui-types'; import { existsSync, lstatSync } from 'fs'; import { normalize, parse } from 'path'; import { getProjectByPath } from './get-project-by-path'; import { nxWorkspace } from '@nx-console/shared-nx-workspace-info'; import { lspLogger } from '@nx-console/language-server-utils'; export async function getGeneratorContextV2( path: string | undefined, workspacePath: string, ): Promise<GeneratorContext> { let projectName: string | undefined = undefined; let directory: string | undefined = undefined; let normalizedDirectory: string | undefined = undefined; const { workspaceLayout, nxVersion } = await nxWorkspace( workspacePath, lspLogger, ); if (path) { const normalizedPath = normalize(path); const project = await getProjectByPath(normalizedPath, workspacePath); projectName = (project && project.name) || undefined; directory = getDirectory(normalizedPath, workspacePath); normalizedDirectory = getDirectoryWithoutAppsDirLibsDir( normalizedPath, workspaceLayout, workspacePath, ); } return { project: projectName, directory, normalizedDirectory: normalizedDirectory, nxVersion, }; } function getDirectory(path: string, workspacePath: string): string | undefined { let dir: string | undefined = undefined; if (existsSync(path)) { if (lstatSync(path).isDirectory()) { dir = path; } else { dir = parse(path).dir; } } dir = dir?.replace(normalize(workspacePath), ''); return dir; } function getDirectoryWithoutAppsDirLibsDir( path: string, { appsDir, libsDir }: { appsDir?: string; libsDir?: string }, workspacePath: string, ) { let dir = getDirectory(path, workspacePath); if (!dir) { return; } dir = dir.replace(/\\/g, '/').replace(/^\//, ''); if (appsDir && dir.startsWith(appsDir)) { dir = dir.replace(appsDir, '').replace(/^\//, ''); } if (libsDir && dir.startsWith(libsDir)) { dir = dir.replace(libsDir, '').replace(/^\//, ''); } return dir; }

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/nrwl/nx-console'

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