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