@ordojs/dev-tools
Version:
Advanced developer tools for OrdoJS with component inspector, AST explorer, and performance profiling
248 lines (246 loc) • 6.13 kB
JavaScript
import { EventEmitter } from 'events';
// src/development-server/index.ts
var DevelopmentServer = class extends EventEmitter {
config;
isRunning;
server;
// Express server instance
/**
* Create a new DevelopmentServer instance
*
* @param config - Development server configuration
*/
constructor(config) {
super();
this.config = config;
this.isRunning = false;
this.server = null;
}
/**
* Start the development server
*/
async start() {
if (this.isRunning) {
console.warn("Development server is already running");
return;
}
try {
await this.initializeServer();
await this.startServer();
this.isRunning = true;
console.log(`Development server started on http://${this.config.host}:${this.config.port}`);
this.emit("started");
} catch (error) {
console.error("Failed to start development server:", error);
this.emit("error", error);
throw error;
}
}
/**
* Stop the development server
*/
async stop() {
if (!this.isRunning) {
console.warn("Development server is not running");
return;
}
try {
await this.stopServer();
this.isRunning = false;
console.log("Development server stopped");
this.emit("stopped");
} catch (error) {
console.error("Failed to stop development server:", error);
this.emit("error", error);
throw error;
}
}
/**
* Get server status
*
* @returns Server status information
*/
getStatus() {
return {
isRunning: this.isRunning,
port: this.config.port,
host: this.config.host,
config: this.config
};
}
/**
* Update server configuration
*
* @param newConfig - New configuration
*/
updateConfig(newConfig) {
this.config = { ...this.config, ...newConfig };
this.emit("configUpdated", this.config);
}
/**
* Initialize Express server
*/
async initializeServer() {
try {
const express = await import('express');
const cors = await import('cors');
const compression = await import('compression');
const serveStatic = await import('serve-static');
const morgan = await import('morgan');
const helmet = await import('helmet');
const app = express.default();
if (this.config.cors) {
app.use(cors.default());
}
if (this.config.compression) {
app.use(compression.default());
}
app.use(helmet.default());
app.use(morgan.default("combined"));
app.use(serveStatic.default(this.config.staticDir));
this.setupDevToolsRoutes(app);
app.use(this.errorHandler.bind(this));
this.server = app;
} catch (error) {
console.error("Failed to initialize Express server:", error);
throw error;
}
}
/**
* Start the server
*/
async startServer() {
return new Promise((resolve, reject) => {
if (!this.server) {
reject(new Error("Server not initialized"));
return;
}
const server = this.server.listen(this.config.port, this.config.host, () => {
console.log(`Development server listening on http://${this.config.host}:${this.config.port}`);
resolve();
});
server.on("error", (error) => {
console.error("Server error:", error);
reject(error);
});
});
}
/**
* Stop the server
*/
async stopServer() {
return new Promise((resolve, reject) => {
if (!this.server) {
resolve();
return;
}
const server = this.server;
server.close((error) => {
if (error) {
console.error("Error stopping server:", error);
reject(error);
} else {
console.log("Server stopped");
resolve();
}
});
});
}
/**
* Setup dev tools API routes
*
* @param app - Express app instance
*/
setupDevToolsRoutes(app) {
app.get("/api/dev-tools/health", (req, res) => {
res.json({
status: "healthy",
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
config: this.config
});
});
app.get("/api/dev-tools/inspector", (req, res) => {
res.json({
components: [],
stats: {
totalComponents: 0,
totalRenderCount: 0,
averageRenderTime: 0,
peakMemoryUsage: 0
}
});
});
app.get("/api/dev-tools/profiler", (req, res) => {
res.json({
profiles: [],
stats: {
totalProfiles: 0,
activeProfiles: 0,
totalMeasurements: 0,
averageProfileDuration: 0
}
});
});
app.get("/api/dev-tools/ast-explorer", (req, res) => {
res.json({
asts: [],
stats: {
totalASTs: 0,
totalNodes: 0,
averageDepth: 0
}
});
});
app.get("/api/dev-tools/bundle-analyzer", (req, res) => {
res.json({
analyses: [],
stats: {
totalAnalyses: 0,
totalSize: 0,
averageSize: 0
}
});
});
app.get("/api/dev-tools/error-overlay", (req, res) => {
res.json({
errors: [],
stats: {
totalErrors: 0,
errorsByType: {},
errorsByFile: {}
}
});
});
app.get("/api/dev-tools/hmr", (req, res) => {
res.json({
updates: [],
stats: {
totalUpdates: 0,
updatesByType: {},
updatesByFile: {},
averageUpdateTime: 0
}
});
});
}
/**
* Error handling middleware
*
* @param err - Error object
* @param req - Request object
* @param res - Response object
* @param next - Next function
*/
errorHandler(err, req, res, next) {
console.error("Server error:", err);
res.status(500).json({
error: {
message: err.message,
stack: process.env.NODE_ENV === "development" ? err.stack : void 0
}
});
}
};
export { DevelopmentServer };
//# sourceMappingURL=index.mjs.map
//# sourceMappingURL=index.mjs.map