UNPKG

component-tagger

Version:

A Vite plugin that automatically adds comprehensive component tagging attributes to JSX/TSX elements for debugging and testing

111 lines (91 loc) โ€ข 3.66 kB
#!/usr/bin/env node import { readFileSync, writeFileSync, existsSync } from 'fs'; import { join, dirname } from 'path'; import { fileURLToPath } from 'url'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); console.log('๐Ÿงน Component Tagger: Cleaning up configuration...'); // Find the project root function findProjectRoot() { let currentDir = process.cwd(); // If we're in node_modules, go up to find the project root if (currentDir.includes('node_modules')) { const parts = currentDir.split('/'); const nodeModulesIndex = parts.lastIndexOf('node_modules'); if (nodeModulesIndex !== -1) { currentDir = parts.slice(0, nodeModulesIndex).join('/'); } } return currentDir; } const projectRoot = findProjectRoot(); const viteConfigPath = join(projectRoot, 'vite.config.ts'); const viteConfigJsPath = join(projectRoot, 'vite.config.js'); // Check which config file exists let configPath = null; if (existsSync(viteConfigPath)) { configPath = viteConfigPath; } else if (existsSync(viteConfigJsPath)) { configPath = viteConfigJsPath; } else { console.log('โœ… No Vite config found - nothing to clean up'); process.exit(0); } try { // Read the current config let configContent = readFileSync(configPath, 'utf8'); // Check if componentTagger is configured if (!configContent.includes('componentTagger') && !configContent.includes('component-tagger')) { console.log('โœ… Component Tagger not found in Vite config - nothing to clean up'); process.exit(0); } let hasChanges = false; // Remove the import statement const importPatterns = [ /import\s*{\s*componentTagger\s*}\s*from\s*['"]component-tagger['"]\s*;?\s*\n?/g, /import\s+componentTagger\s+from\s*['"]component-tagger['"]\s*;?\s*\n?/g, ]; for (const pattern of importPatterns) { if (pattern.test(configContent)) { configContent = configContent.replace(pattern, ''); hasChanges = true; console.log('โœ… Removed componentTagger import'); } } // Remove componentTagger from plugins array // Handle various formats: componentTagger(), componentTagger({}), etc. const pluginPatterns = [ /,\s*componentTagger\([^)]*\)\s*,?/g, /componentTagger\([^)]*\)\s*,/g, /componentTagger\([^)]*\)/g, ]; for (const pattern of pluginPatterns) { if (pattern.test(configContent)) { configContent = configContent.replace(pattern, ''); hasChanges = true; console.log('โœ… Removed componentTagger from plugins array'); } } // Clean up any empty lines or trailing commas configContent = configContent .replace(/,\s*\]/g, '\n ]') // Remove trailing commas before closing bracket .replace(/\n\s*\n\s*\n/g, '\n\n') // Remove multiple empty lines .trim(); if (hasChanges) { // Write the updated config writeFileSync(configPath, configContent); console.log('โœ… Component Tagger removed from Vite config'); console.log('๐Ÿ‘‹ Thanks for using Component Tagger!'); } else { console.log('โœ… Component Tagger configuration already clean'); } } catch (error) { console.log('โš ๏ธ Could not automatically clean up Component Tagger from Vite config.'); console.log('๐Ÿ“ Please manually remove the following from your vite.config.ts:'); console.log(''); console.log('1. Remove import:'); console.log(" import { componentTagger } from 'component-tagger'"); console.log(''); console.log('2. Remove from plugins array:'); console.log(' componentTagger()'); }