mc-gitpulse 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/server.js ADDED
@@ -0,0 +1,107 @@
1
+ const express = require('express');
2
+ const path = require('path');
3
+ const http = require('http');
4
+ const WebSocket = require('ws');
5
+ const {openResource} = require('open-resource');
6
+ const chalk = require('chalk');
7
+ const { GitAnalyzer } = require('./analyzer');
8
+
9
+ async function startServer(repoPath, port, openBrowser = true) {
10
+ const app = express();
11
+ const server = http.createServer(app);
12
+ const wss = new WebSocket.Server({ server });
13
+
14
+ const analyzer = new GitAnalyzer(repoPath);
15
+
16
+ // Serve static files
17
+ app.use(express.static(path.join(__dirname, 'public')));
18
+ app.use(express.json());
19
+
20
+ // API endpoints
21
+ app.get('/api/analyze', async (req, res) => {
22
+ try {
23
+ const analysis = await analyzer.analyze();
24
+ res.json(analysis);
25
+ } catch (error) {
26
+ res.status(500).json({ error: error.message });
27
+ }
28
+ });
29
+
30
+ app.get('/api/tree', async (req, res) => {
31
+ try {
32
+ const tree = await analyzer.getFileTree();
33
+ res.json(tree);
34
+ } catch (error) {
35
+ res.status(500).json({ error: error.message });
36
+ }
37
+ });
38
+
39
+ app.get('/api/graph', async (req, res) => {
40
+ try {
41
+ const limit = parseInt(req.query.limit) || 100;
42
+ const graph = await analyzer.getCommitGraph(limit);
43
+ res.json(graph);
44
+ } catch (error) {
45
+ res.status(500).json({ error: error.message });
46
+ }
47
+ });
48
+
49
+ app.get('/api/file/:ref/*', async (req, res) => {
50
+ try {
51
+ const ref = req.params.ref;
52
+ const filepath = req.params[0];
53
+ const content = await analyzer.git.show([`${ref}:${filepath}`]);
54
+ res.json({ content });
55
+ } catch (error) {
56
+ res.status(500).json({ error: error.message });
57
+ }
58
+ });
59
+
60
+ app.post('/api/gc', async (req, res) => {
61
+ try {
62
+ await analyzer.git.raw(['gc', '--aggressive']);
63
+ res.json({ success: true, message: 'Garbage collection completed' });
64
+ } catch (error) {
65
+ res.status(500).json({ error: error.message });
66
+ }
67
+ });
68
+
69
+ // WebSocket for real-time updates
70
+ wss.on('connection', (ws) => {
71
+ console.log(chalk.green('🔌 Client connected'));
72
+
73
+ ws.on('message', async (message) => {
74
+ try {
75
+ const data = JSON.parse(message);
76
+
77
+ if (data.type === 'refresh') {
78
+ const analysis = await analyzer.analyze();
79
+ ws.send(JSON.stringify({ type: 'update', data: analysis }));
80
+ }
81
+ } catch (error) {
82
+ ws.send(JSON.stringify({ type: 'error', error: error.message }));
83
+ }
84
+ });
85
+
86
+ ws.on('close', () => {
87
+ console.log(chalk.yellow('🔌 Client disconnected'));
88
+ });
89
+ });
90
+
91
+ // Start server
92
+ server.listen(port, async () => {
93
+ const url = `http://localhost:${port}`;
94
+
95
+ console.log(chalk.green('✓ Server started'));
96
+ console.log(chalk.cyan(`\n 🌐 ${url}\n`));
97
+
98
+ if (openBrowser) {
99
+ console.log(chalk.dim('Opening browser...'));
100
+ openResource(url);
101
+ }
102
+ });
103
+
104
+ return server;
105
+ }
106
+
107
+ module.exports = { startServer };