From 7518696543876d07251a9be9e91ad48edbb10215 Mon Sep 17 00:00:00 2001 From: Joe Danziger Date: Fri, 9 May 2025 02:11:49 -0400 Subject: [PATCH] fix formatting --- scripts/modules/mcp-utils.js | 115 ++++++++++++++++++++++------ scripts/modules/rule-transformer.js | 95 +++++++++++++---------- 2 files changed, 145 insertions(+), 65 deletions(-) diff --git a/scripts/modules/mcp-utils.js b/scripts/modules/mcp-utils.js index d0219232..7aea792f 100644 --- a/scripts/modules/mcp-utils.js +++ b/scripts/modules/mcp-utils.js @@ -3,28 +3,95 @@ import path from 'path'; import { log } from './utils.js'; // Structure matches project conventions (see scripts/init.js) -export function setupMCPConfiguration(brandDir) { - const mcpPath = path.join(brandDir, 'mcp.json'); - const mcpConfig = { - "task-master-ai": { - command: "npx", - args: ["-y", "--package=task-master-ai", "task-master-ai"], - env: { - ANTHROPIC_API_KEY: "ANTHROPIC_API_KEY_HERE", - PERPLEXITY_API_KEY: "PERPLEXITY_API_KEY_HERE", - OPENAI_API_KEY: "OPENAI_API_KEY_HERE", - GOOGLE_API_KEY: "GOOGLE_API_KEY_HERE", - XAI_API_KEY: "XAI_API_KEY_HERE", - OPENROUTER_API_KEY: "OPENROUTER_API_KEY_HERE", - MISTRAL_API_KEY: "MISTRAL_API_KEY_HERE", - AZURE_OPENAI_API_KEY: "AZURE_OPENAI_API_KEY_HERE" - } - } - }; - try { - fs.writeFileSync(mcpPath, JSON.stringify(mcpConfig, null, 2)); - log('success', `Created MCP configuration: ${mcpPath}`); - } catch (e) { - log('warn', `Failed to create MCP configuration at ${mcpPath}: ${e.message}`); - } +export function setupMCPConfiguration(configDir) { + const mcpPath = path.join(configDir, 'mcp.json'); + // New MCP config to be added - references the installed package + const newMCPServer = { + 'task-master-ai': { + command: 'npx', + args: ['-y', '--package=task-master-ai', 'task-master-ai'], + env: { + ANTHROPIC_API_KEY: 'ANTHROPIC_API_KEY_HERE', + PERPLEXITY_API_KEY: 'PERPLEXITY_API_KEY_HERE', + OPENAI_API_KEY: 'OPENAI_API_KEY_HERE', + GOOGLE_API_KEY: 'GOOGLE_API_KEY_HERE', + XAI_API_KEY: 'XAI_API_KEY_HERE', + OPENROUTER_API_KEY: 'OPENROUTER_API_KEY_HERE', + MISTRAL_API_KEY: 'MISTRAL_API_KEY_HERE', + AZURE_OPENAI_API_KEY: 'AZURE_OPENAI_API_KEY_HERE', + OLLAMA_API_KEY: 'OLLAMA_API_KEY_HERE' + } + } + }; + // Create config directory if it doesn't exist + if (!fs.existsSync(configDir)) { + fs.mkdirSync(configDir, { recursive: true }); + } + if (fs.existsSync(mcpPath)) { + log( + 'info', + 'MCP configuration file already exists, checking for existing task-master-ai...' + ); + try { + // Read existing config + const mcpConfig = JSON.parse(fs.readFileSync(mcpPath, 'utf8')); + // Initialize mcpServers if it doesn't exist + if (!mcpConfig.mcpServers) { + mcpConfig.mcpServers = {}; + } + // Check if any existing server configuration already has task-master-ai in its args + const hasMCPString = Object.values(mcpConfig.mcpServers).some( + (server) => + server.args && + server.args.some( + (arg) => typeof arg === 'string' && arg.includes('task-master-ai') + ) + ); + if (hasMCPString) { + log( + 'info', + 'Found existing task-master-ai MCP configuration in mcp.json, leaving untouched' + ); + return; // Exit early, don't modify the existing configuration + } + // Add the task-master-ai server if it doesn't exist + if (!mcpConfig.mcpServers['task-master-ai']) { + mcpConfig.mcpServers['task-master-ai'] = newMCPServer['task-master-ai']; + log( + 'info', + 'Added task-master-ai server to existing MCP configuration' + ); + } else { + log('info', 'task-master-ai server already configured in mcp.json'); + } + // Write the updated configuration + fs.writeFileSync(mcpPath, JSON.stringify(mcpConfig, null, 4)); + log('success', 'Updated MCP configuration file'); + } catch (error) { + log('error', `Failed to update MCP configuration: ${error.message}`); + // Create a backup before potentially modifying + const backupPath = `${mcpPath}.backup-${Date.now()}`; + if (fs.existsSync(mcpPath)) { + fs.copyFileSync(mcpPath, backupPath); + log('info', `Created backup of existing mcp.json at ${backupPath}`); + } + // Create new configuration + const newMCPConfig = { + mcpServers: newMCPServer + }; + fs.writeFileSync(mcpPath, JSON.stringify(newMCPConfig, null, 4)); + log( + 'warn', + 'Created new MCP configuration file (backup of original file was created if it existed)' + ); + } + } else { + // If mcp.json doesn't exist, create it + const newMCPConfig = { + mcpServers: newMCPServer + }; + fs.writeFileSync(mcpPath, JSON.stringify(newMCPConfig, null, 4)); + log('success', 'Created MCP configuration file'); + } + log('info', 'MCP server will use the installed task-master-ai package'); } diff --git a/scripts/modules/rule-transformer.js b/scripts/modules/rule-transformer.js index 94a2fe99..5ce01697 100644 --- a/scripts/modules/rule-transformer.js +++ b/scripts/modules/rule-transformer.js @@ -12,7 +12,6 @@ import { log } from './utils.js'; // Import the shared MCP configuration helper import { setupMCPConfiguration } from './mcp-utils.js'; - // Import Roo Code conversionConfig and fileMap from profiles import { conversionConfig, fileMap } from '../profiles/roo.js'; @@ -100,7 +99,11 @@ function updateFileReferences(content) { * Main transformation function that applies all conversions */ // Main transformation function that applies all conversions, now brand-generic -function transformCursorToBrandRules(content, conversionConfig, globalReplacements = []) { +function transformCursorToBrandRules( + content, + conversionConfig, + globalReplacements = [] +) { // Apply all transformations in appropriate order let result = content; result = replaceBasicTerms(result, conversionConfig); @@ -137,7 +140,11 @@ function convertCursorRuleToBrandRule(sourcePath, targetPath, profile) { const content = fs.readFileSync(sourcePath, 'utf8'); // Transform content - const transformedContent = transformCursorToBrandRules(content, conversionConfig, globalReplacements); + const transformedContent = transformCursorToBrandRules( + content, + conversionConfig, + globalReplacements + ); // Ensure target directory exists const targetDir = path.dirname(targetPath); @@ -168,7 +175,7 @@ function convertCursorRuleToBrandRule(sourcePath, targetPath, profile) { function convertAllCursorRulesToBrandRules(projectDir, profile) { const { fileMap, brandName, rulesDir } = profile; // Use assets/rules as the source of rules instead of .cursor/rules -const cursorRulesDir = path.join(projectDir, 'assets', 'rules'); + const cursorRulesDir = path.join(projectDir, 'assets', 'rules'); const brandRulesDir = path.join(projectDir, rulesDir); if (!fs.existsSync(cursorRulesDir)) { @@ -220,42 +227,48 @@ const cursorRulesDir = path.join(projectDir, 'assets', 'rules'); * @returns {boolean} - True if removal succeeded, false otherwise */ function removeBrandRules(projectDir, profile) { - const { brandName, rulesDir } = profile; - const brandRulesDir = path.join(projectDir, rulesDir); - const brandDir = path.dirname(brandRulesDir); - // Also remove the mcp.json file if it exists in the brand directory - const mcpPath = path.join(brandDir, 'mcp.json'); - if (fs.existsSync(mcpPath)) { - try { - fs.unlinkSync(mcpPath); - log('info', `Removed MCP configuration: ${mcpPath}`); - } catch (e) { - log('warn', `Failed to remove MCP configuration at ${mcpPath}: ${e.message}`); - } - } - // Do not allow removal of the default Cursor rules directory - if (brandName.toLowerCase() === 'cursor') { - log('warn', 'Cannot remove default Cursor rules directory. Skipping.'); - return false; - } - if (fs.existsSync(brandRulesDir)) { - fs.rmSync(brandRulesDir, { recursive: true, force: true }); - log('info', `Removed rules directory: ${brandRulesDir}`); - // Check if parent brand folder is empty - if ( - fs.existsSync(brandDir) && - path.basename(brandDir) !== '.cursor' && - fs.readdirSync(brandDir).length === 0 - ) { - fs.rmdirSync(brandDir); - log('info', `Removed empty brand folder: ${brandDir}`); - } - return true; - } else { - log('warn', `Rules directory not found: ${brandRulesDir}`); - return false; - } + const { brandName, rulesDir } = profile; + const brandRulesDir = path.join(projectDir, rulesDir); + const brandDir = path.dirname(brandRulesDir); + // Also remove the mcp.json file if it exists in the brand directory + const mcpPath = path.join(brandDir, 'mcp.json'); + if (fs.existsSync(mcpPath)) { + try { + fs.unlinkSync(mcpPath); + log('info', `Removed MCP configuration: ${mcpPath}`); + } catch (e) { + log( + 'warn', + `Failed to remove MCP configuration at ${mcpPath}: ${e.message}` + ); + } + } + // Do not allow removal of the default Cursor rules directory + if (brandName.toLowerCase() === 'cursor') { + log('warn', 'Cannot remove default Cursor rules directory. Skipping.'); + return false; + } + if (fs.existsSync(brandRulesDir)) { + fs.rmSync(brandRulesDir, { recursive: true, force: true }); + log('info', `Removed rules directory: ${brandRulesDir}`); + // Check if parent brand folder is empty + if ( + fs.existsSync(brandDir) && + path.basename(brandDir) !== '.cursor' && + fs.readdirSync(brandDir).length === 0 + ) { + fs.rmdirSync(brandDir); + log('info', `Removed empty brand folder: ${brandDir}`); + } + return true; + } else { + log('warn', `Rules directory not found: ${brandRulesDir}`); + return false; + } } -export { convertAllCursorRulesToBrandRules, convertCursorRuleToBrandRule, removeBrandRules }; - +export { + convertAllCursorRulesToBrandRules, + convertCursorRuleToBrandRule, + removeBrandRules +};