UNPKG

@eventcatalogtest/studio

Version:

A drag and drop UI for distributed systems that keeps your diagrams where they belong – in your repo

176 lines (148 loc) • 5.35 kB
#!/usr/bin/env node import { spawn } from 'child_process'; import { fileURLToPath } from 'url'; import { dirname, join } from 'path'; import { existsSync } from 'fs'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); // Get the package root directory (one level up from bin) const packageRoot = join(__dirname, '..'); console.log('šŸŽØ EventCatalog Studio'); console.log('===================================='); // Check if we have a built version (.next directory exists) const hasBuiltVersion = existsSync(join(packageRoot, '.next')); const hasNodeModules = existsSync(join(packageRoot, 'node_modules')); async function installDependencies() { return new Promise((resolve, reject) => { console.log('šŸ“¦ Installing dependencies...'); // Check if pnpm is available, otherwise fallback to npm const packageManager = existsSync(join(packageRoot, 'pnpm-lock.yaml')) ? 'pnpm' : 'npm'; const installProcess = spawn(packageManager, ['install'], { cwd: packageRoot, stdio: 'inherit', shell: true }); installProcess.on('close', (code) => { if (code !== 0) { reject(new Error('Failed to install dependencies')); } else { console.log('āœ… Dependencies installed successfully'); resolve(); } }); installProcess.on('error', reject); }); } async function buildProject() { return new Promise((resolve, reject) => { console.log('šŸ”Ø Building project...'); const buildProcess = spawn('npm', ['run', 'build'], { cwd: packageRoot, stdio: 'inherit', shell: true }); buildProcess.on('close', (code) => { if (code !== 0) { reject(new Error('Failed to build project')); } else { console.log('āœ… Project built successfully'); resolve(); } }); buildProcess.on('error', reject); }); } function startStudio(mode = 'dev') { const port = process.env.PORT || 3001; const command = mode === 'prod' ? 'start' : 'dev'; const isDebug = process.env.DEBUG === 'true'; console.log(`šŸš€ Starting EventCatalog Studio in ${mode} mode`); console.log(`šŸ“ Server will be available at: http://localhost:${port}`); console.log(''); console.log('Press Ctrl+C to stop the server'); if (!isDebug) { console.log('šŸ’” Set DEBUG=true to see NextJS logs'); } console.log('===================================='); const EVENTCATALOG_DIR = process.env.EVENTCATALOG_DIR || join(process.cwd()); // Configure stdio based on DEBUG environment variable const stdio = isDebug ? 'inherit' : ['ignore', 'pipe', 'pipe']; // Start the Next.js server const studioProcess = spawn('npm', ['run', command], { cwd: packageRoot, stdio: stdio, shell: true, env: { ...process.env, PORT: port, CURRENT_DIR: process.cwd(), EVENTCATALOG_DIR: EVENTCATALOG_DIR } }); // If not in debug mode, still capture and show critical errors if (!isDebug && studioProcess.stderr) { studioProcess.stderr.on('data', (data) => { const message = data.toString(); // Only show critical errors, not all NextJS output if (message.includes('Error:') || message.includes('error') || message.includes('Failed')) { console.error('āŒ', message.trim()); } }); } // Show server ready message even when not in debug mode if (!isDebug && studioProcess.stdout) { studioProcess.stdout.on('data', (data) => { const message = data.toString(); // Show important startup messages if (message.includes('ready') || message.includes('started') || message.includes('Local:')) { console.log('āœ… Server is ready!'); } }); } studioProcess.on('close', (code) => { console.log(`\nEventCatalog Studio exited with code ${code}`); }); studioProcess.on('error', (err) => { console.error('āŒ Error starting EventCatalog Studio:', err); process.exit(1); }); // Handle process termination gracefully process.on('SIGINT', () => { console.log('\nšŸ‘‹ Shutting down EventCatalog Studio...'); studioProcess.kill('SIGINT'); process.exit(0); }); process.on('SIGTERM', () => { console.log('\nšŸ‘‹ Shutting down EventCatalog Studio...'); studioProcess.kill('SIGTERM'); process.exit(0); }); } // Main execution async function main() { try { // Install dependencies if needed if (!hasNodeModules) { await installDependencies(); } try { await startStudio('dev'); } catch (error) { console.error('āŒ Error starting EventCatalog Studio:', error.message); process.exit(1); } // // If we have a built version, start in production mode // if (hasBuiltVersion) { // startStudio('prod'); // } else { // // Check if we can build (only if node_modules exists) // if (hasNodeModules) { // console.log('ā„¹ļø No built version found. Building for development...'); // // For now, just start in dev mode - building takes time // startStudio('dev'); // } else { // console.error('āŒ Missing dependencies and cannot install them'); // process.exit(1); // } // } } catch (error) { console.error('āŒ Error:', error.message); process.exit(1); } } main();