Skip to main content
Glama
nrwl

Nx MCP Server

Official
by nrwl
watcher.ts3.13 kB
import { lspLogger } from '@nx-console/language-server-utils'; import { getNxVersion } from '@nx-console/shared-nx-workspace-info'; import { debounce } from '@nx-console/shared-utils'; import { DaemonWatcher, NativeWatcher } from '@nx-console/shared-watcher'; import { gte } from '@nx-console/nx-version'; let _daemonWatcher: DaemonWatcher | undefined; let _nativeWatcher: NativeWatcher | undefined; export async function cleanupAllWatchers(): Promise<void> { const cleanupPromises: Promise<void>[] = []; if (_nativeWatcher) { cleanupPromises.push( _nativeWatcher.stop().catch((e) => { lspLogger.log( 'Error stopping native watcher during global cleanup: ' + e, ); }), ); _nativeWatcher = undefined; } if (_daemonWatcher) { cleanupPromises.push( Promise.resolve(_daemonWatcher.stop()).catch((e) => { lspLogger.log( 'Error stopping daemon watcher during global cleanup: ' + e, ); }), ); _daemonWatcher = undefined; } await Promise.all(cleanupPromises); } export async function languageServerWatcher( workspacePath: string, callback: () => unknown, ): Promise<() => Promise<void>> { const version = await getNxVersion(workspacePath); const debouncedCallback = debounce(callback, 1000); if (gte(version, '16.4.0')) { if (process.platform === 'win32') { if (_nativeWatcher) { try { await _nativeWatcher.stop(); } catch (e) { lspLogger.log('Error stopping previous native watcher: ' + e); } _nativeWatcher = undefined; } const nativeWatcher = new NativeWatcher( workspacePath, debouncedCallback, lspLogger, ); _nativeWatcher = nativeWatcher; return async () => { lspLogger.log('Unregistering file watcher'); try { await nativeWatcher.stop(); } catch (e) { lspLogger.log('Error stopping native watcher during cleanup: ' + e); } if (_nativeWatcher === nativeWatcher) { _nativeWatcher = undefined; } }; } else { if (_daemonWatcher) { try { _daemonWatcher.stop(); } catch (e) { lspLogger.log('Error stopping previous daemon watcher: ' + e); } _daemonWatcher = undefined; } const daemonWatcher = new DaemonWatcher( workspacePath, version, debouncedCallback, lspLogger, ); _daemonWatcher = daemonWatcher; await daemonWatcher.start(); return async () => { lspLogger.log('Unregistering file watcher'); try { await daemonWatcher.stop(); } catch (e) { lspLogger.log('Error stopping daemon watcher during cleanup: ' + e); } if (_daemonWatcher === daemonWatcher) { _daemonWatcher = undefined; } }; } } else { lspLogger.log( 'File watching is not supported for Nx versions below 16.4.0.', ); return async () => { lspLogger.log('unregistering empty watcher'); }; } }

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