UNPKG

@jussimirvfx/meta-pixel-tracking

Version:

Sistema completo de tracking do Meta Pixel (Pixel + CAPI) com proteção anti-adblock para landing pages

163 lines (137 loc) 4.17 kB
#!/usr/bin/env node /** * Servidor de desenvolvimento para Meta Pixel/CAPI * Integra Vite com API routes */ const { spawn } = require('child_process'); const path = require('path'); const fs = require('fs'); // Cores para console const colors = { green: '\x1b[32m', blue: '\x1b[34m', yellow: '\x1b[33m', red: '\x1b[31m', reset: '\x1b[0m', bold: '\x1b[1m' }; function log(message, color = 'blue') { console.log(`${colors[color]}${message}${colors.reset}`); } function logSuccess(message) { log(`✅ ${message}`, 'green'); } function logInfo(message) { log(`ℹ️ ${message}`, 'blue'); } function logWarning(message) { log(`⚠️ ${message}`, 'yellow'); } function logError(message) { log(`❌ ${message}`, 'red'); } // Verificar se o servidor Express existe function checkDevServer() { const devServerPath = path.join(process.cwd(), 'dev-server-full.js'); if (!fs.existsSync(devServerPath)) { logError('Servidor Express não encontrado. Execute npm run setup:meta primeiro.'); process.exit(1); } return devServerPath; } // Verificar se .env existe function checkEnvFile() { const envPath = path.join(process.cwd(), '.env'); const envExamplePath = path.join(process.cwd(), '.env.example'); if (!fs.existsSync(envPath)) { if (fs.existsSync(envExamplePath)) { logWarning('.env não encontrado. Copiando de .env.example...'); fs.copyFileSync(envExamplePath, envPath); logSuccess('.env criado a partir de .env.example'); logWarning('Configure suas credenciais do Meta Pixel no arquivo .env'); } else { logError('.env não encontrado e .env.example não existe.'); logError('Execute npm run setup:meta primeiro.'); process.exit(1); } } } // Verificar dependências function checkDependencies() { const packagePath = path.join(process.cwd(), 'package.json'); const packageJson = JSON.parse(fs.readFileSync(packagePath, 'utf8')); const requiredDeps = ['express', 'http-proxy-middleware']; const missingDeps = requiredDeps.filter(dep => !packageJson.dependencies?.[dep] && !packageJson.devDependencies?.[dep] ); if (missingDeps.length > 0) { logWarning(`Dependências faltando: ${missingDeps.join(', ')}`); logInfo('Instalando dependências...'); const installProcess = spawn('npm', ['install', ...missingDeps], { stdio: 'inherit', shell: true }); installProcess.on('close', (code) => { if (code === 0) { logSuccess('Dependências instaladas'); startDevServer(); } else { logError('Erro ao instalar dependências'); process.exit(1); } }); } else { startDevServer(); } } // Iniciar servidor de desenvolvimento function startDevServer() { const devServerPath = checkDevServer(); checkEnvFile(); log('🚀 Iniciando servidor de desenvolvimento...', 'bold'); log('📡 API Routes disponíveis:', 'blue'); log(' GET /api/get-ip'); log(' GET /api/meta/conversions (health check)'); log(' POST /api/meta/conversions'); log('🌐 Frontend: http://localhost:3000'); log('', 'bold'); // Iniciar servidor Express const serverProcess = spawn('node', [devServerPath], { stdio: 'inherit', shell: true, env: { ...process.env, NODE_ENV: 'development' } }); serverProcess.on('close', (code) => { if (code !== 0) { logError(`Servidor encerrado com código ${code}`); } }); // Capturar sinais para encerrar graciosamente process.on('SIGINT', () => { logInfo('Encerrando servidor...'); serverProcess.kill('SIGINT'); }); process.on('SIGTERM', () => { logInfo('Encerrando servidor...'); serverProcess.kill('SIGTERM'); }); } // Função principal function main() { log('🔧 Servidor de desenvolvimento Meta Pixel/CAPI', 'bold'); log('============================================'); try { checkDependencies(); } catch (error) { logError(`Erro ao iniciar servidor: ${error.message}`); process.exit(1); } } // Executar se chamado diretamente if (require.main === module) { main(); } module.exports = { main };