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
JavaScript
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()');
}