UNPKG

@codacy/codacy-mcp

Version:

Codacy MCP server

46 lines (45 loc) 2.02 kB
import { MacCodacyCli } from './MacCodacyCli.js'; export class WinWSLCodacyCli extends MacCodacyCli { constructor(rootPath, provider, organization, repository) { const winRootPath = rootPath.startsWith('/mnt/') ? WinWSLCodacyCli.fromWSLPath(rootPath) : rootPath; super(winRootPath, provider, organization, repository); } static toWSLPath(path) { // First, remove outer quotes if present const cleanPath = path.replace(/^["']|["']$/g, ''); // Convert backslashes to slashes and handle drive letter const wslPath = cleanPath .replace(/\\/g, '/') .replace(/^([a-zA-Z]):/, (match, letter) => `/mnt/${letter.toLowerCase()}`); return wslPath; } static fromWSLPath(path) { // Convert WSL path to Windows path // Example: /mnt/c/Users/user/project -> C:\Users\user\project const windowsPath = path .replace(/^'\/mnt\/([a-zA-Z])/, (match, letter) => `'${letter.toUpperCase()}:`) .replace(/^\/mnt\/([a-zA-Z])/, (match, letter) => `${letter.toUpperCase()}:`) .replace(/\//g, '\\'); return windowsPath; } preparePathForExec(path) { // Convert WSL path to Windows format for validation const winFilePath = path.startsWith('/mnt/') ? WinWSLCodacyCli.fromWSLPath(path) : path; // Validate path security before escaping if (!this.isPathSafe(winFilePath)) { throw new Error(`Unsafe file path rejected: ${winFilePath}`); } // Convert to WSL format and escape special characters const wslPath = WinWSLCodacyCli.toWSLPath(winFilePath); const escapedPath = wslPath.replace(/([\s'"\\;&|`$()[\]{}*?~<>])/g, '\\$1'); return `'${escapedPath}'`; } async execAsync(command, args) { return await super.execAsync(`wsl bash -c "${command}"`, args); } getCliCommand() { return WinWSLCodacyCli.toWSLPath(super.getCliCommand()); } }