UNPKG

@re-shell/cli

Version:

Full-stack development platform uniting microservices and microfrontends. Build complete applications with .NET (ASP.NET Core Web API, Minimal API), Java (Spring Boot, Quarkus, Micronaut, Vert.x), Rust (Actix-Web, Warp, Rocket, Axum), Python (FastAPI, Dja

178 lines (177 loc) 7.41 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.serveMicrofrontend = serveMicrofrontend; const fs = __importStar(require("fs-extra")); const path = __importStar(require("path")); const child_process_1 = require("child_process"); const chalk_1 = __importDefault(require("chalk")); /** * Starts development server for one or all microfrontends * * @param name - Name of the microfrontend to serve (optional, serves all if omitted) * @param options - Server options * @version 0.1.0 */ async function serveMicrofrontend(name, options = {}) { const { spinner } = options; // Set default options const port = options.port || '3000'; const host = options.host || 'localhost'; const open = options.open || false; try { // Validate we're in a Re-Shell project if (spinner) { spinner.setText('Validating Re-Shell project...'); } // Determine if we're in a Re-Shell project const isInReshellProject = fs.existsSync('package.json') && (fs.existsSync('apps') || fs.existsSync('packages')); if (!isInReshellProject) { if (spinner) { spinner.stop(); } throw new Error('Not in a Re-Shell project. Please run this command from the root of a Re-Shell project.'); } if (name) { // Serve a specific microfrontend if (spinner) { spinner.setText(`Validating microfrontend "${name}"...`); } const mfPath = path.resolve(process.cwd(), 'apps', name); if (!fs.existsSync(mfPath)) { if (spinner) { spinner.stop(); } throw new Error(`Microfrontend "${name}" not found in apps directory.`); } const packageJsonPath = path.join(mfPath, 'package.json'); if (!fs.existsSync(packageJsonPath)) { if (spinner) { spinner.stop(); } throw new Error(`package.json not found for microfrontend "${name}".`); } // Change to the microfrontend directory and serve process.chdir(mfPath); if (spinner) { spinner.setText(`Starting development server for "${name}"...`); } console.log(chalk_1.default.cyan(`Starting development server for microfrontend "${name}"...`)); // Build the serve command with options let serveCommand = `npm run dev -- --port ${port} --host ${host}`; if (open) { serveCommand += ' --open'; } try { if (spinner) { spinner.stop(); } const childProcess = (0, child_process_1.exec)(serveCommand); // Handle process output childProcess.stdout?.on('data', (data) => { console.log(data.toString()); }); childProcess.stderr?.on('data', (data) => { console.error(data.toString()); }); // Handle process exit childProcess.on('exit', (code) => { if (code !== 0) { console.error(chalk_1.default.red(`Development server exited with code ${code}`)); } }); // Keep the process running console.log(chalk_1.default.green(`Development server started at http://${host}:${port}`)); console.log(chalk_1.default.yellow('Press Ctrl+C to stop the server')); // Prevent the Node.js process from exiting process.stdin.resume(); } catch (error) { throw new Error(`Failed to start development server: ${error.message}`); } } else { // Serve all microfrontends using the workspace manager if (spinner) { spinner.setText('Starting development servers for all applications...'); } console.log(chalk_1.default.cyan('Starting development servers for all applications...')); // Use the project's package manager if possible let devCommand = 'npm run dev'; if (fs.existsSync('pnpm-lock.yaml')) { devCommand = 'pnpm run dev'; } else if (fs.existsSync('yarn.lock')) { devCommand = 'yarn dev'; } try { if (spinner) { spinner.stop(); } const childProcess = (0, child_process_1.exec)(devCommand); // Handle process output childProcess.stdout?.on('data', (data) => { console.log(data.toString()); }); childProcess.stderr?.on('data', (data) => { console.error(data.toString()); }); // Handle process exit childProcess.on('exit', (code) => { if (code !== 0) { console.error(chalk_1.default.red(`Development servers exited with code ${code}`)); } }); // Keep the process running console.log(chalk_1.default.green('Development servers started')); console.log(chalk_1.default.yellow('Press Ctrl+C to stop all servers')); // Prevent the Node.js process from exiting process.stdin.resume(); } catch (error) { throw new Error(`Failed to start development servers: ${error.message}`); } } } catch (error) { if (spinner) { spinner.stop(); } throw error; } }