-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathserve.ts
More file actions
101 lines (84 loc) · 3.55 KB
/
Copy pathserve.ts
File metadata and controls
101 lines (84 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { Command } from 'commander';
import path from 'path';
import fs from 'fs';
import chalk from 'chalk';
import { bundleRequire } from 'bundle-require';
export const serveCommand = new Command('serve')
.description('Start ObjectStack server with plugins from configuration')
.argument('[config]', 'Configuration file path', 'objectstack.config.ts')
.option('-p, --port <port>', 'Server port', '3000')
.option('--no-server', 'Skip starting HTTP server plugin')
.action(async (configPath, options) => {
console.log(chalk.bold(`\n🚀 ObjectStack Server`));
console.log(chalk.dim(`------------------------`));
console.log(`📂 Config: ${chalk.blue(configPath)}`);
console.log(`🌐 Port: ${chalk.blue(options.port)}`);
console.log('');
const absolutePath = path.resolve(process.cwd(), configPath);
if (!fs.existsSync(absolutePath)) {
console.error(chalk.red(`\n❌ Configuration file not found: ${absolutePath}`));
process.exit(1);
}
try {
// Load configuration
console.log(chalk.yellow(`📦 Loading configuration...`));
const { mod } = await bundleRequire({
filepath: absolutePath,
});
const config = mod.default || mod;
if (!config) {
throw new Error(`Default export not found in ${configPath}`);
}
console.log(chalk.green(`✓ Configuration loaded`));
// Import ObjectStack runtime
const { ObjectKernel } = await import('@objectstack/core');
// Create kernel instance
console.log(chalk.yellow(`🔧 Initializing ObjectStack kernel...`));
const kernel = new ObjectKernel({
metadata: config.metadata || {},
objects: config.objects || {},
});
// Load plugins from configuration
const plugins = config.plugins || [];
if (plugins.length > 0) {
console.log(chalk.yellow(`📦 Loading ${plugins.length} plugin(s)...`));
for (const plugin of plugins) {
try {
kernel.registerPlugin(plugin);
const pluginName = plugin.name || plugin.constructor?.name || 'unnamed';
console.log(chalk.green(` ✓ Registered plugin: ${pluginName}`));
} catch (e: any) {
console.error(chalk.red(` ✗ Failed to register plugin: ${e.message}`));
}
}
}
// Add HTTP server plugin if not disabled
if (options.server !== false) {
try {
const { HonoServerPlugin } = await import('@objectstack/plugin-hono-server');
const serverPlugin = new HonoServerPlugin({ port: parseInt(options.port) });
kernel.registerPlugin(serverPlugin);
console.log(chalk.green(` ✓ Registered HTTP server plugin (port: ${options.port})`));
} catch (e: any) {
console.warn(chalk.yellow(` ⚠ HTTP server plugin not available: ${e.message}`));
}
}
// Boot the kernel
console.log(chalk.yellow(`\n🚀 Starting ObjectStack...`));
await kernel.boot();
console.log(chalk.green(`\n✅ ObjectStack server is running!`));
console.log(chalk.dim(` Press Ctrl+C to stop\n`));
// Keep process alive
process.on('SIGINT', async () => {
console.log(chalk.yellow(`\n\n⏹ Stopping server...`));
await kernel.shutdown();
console.log(chalk.green(`✅ Server stopped`));
process.exit(0);
});
} catch (error: any) {
console.error(chalk.red(`\n❌ Server Error:`));
console.error(error.message || error);
console.error(error.stack);
process.exit(1);
}
});