Trigger82 commited on
Commit
5c9db77
Β·
verified Β·
1 Parent(s): 73f3430

Create server.js

Browse files
Files changed (1) hide show
  1. server.js +148 -0
server.js ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const express = require('express');
2
+ const { spawn } = require('child_process');
3
+ const fs = require('fs');
4
+ const path = require('path');
5
+ const app = express();
6
+ const http = require('http').createServer(app);
7
+ const io = require('socket.io')(http);
8
+
9
+ // Middleware
10
+ app.use(express.static('public'));
11
+ app.use(express.json());
12
+
13
+ // User directories base path
14
+ const USERS_DIR = path.join(__dirname, 'users');
15
+
16
+ // Create users directory if it doesn't exist
17
+ if (!fs.existsSync(USERS_DIR)) {
18
+ fs.mkdirSync(USERS_DIR, { recursive: true });
19
+ }
20
+
21
+ // Socket.io connection handler
22
+ io.on('connection', (socket) => {
23
+ console.log('A user connected');
24
+
25
+ // Handle repository cloning
26
+ socket.on('cloneRepo', ({ userId, repoUrl }) => {
27
+ const userDir = path.join(USERS_DIR, userId);
28
+
29
+ // Create user directory if it doesn't exist
30
+ if (!fs.existsSync(userDir)) {
31
+ fs.mkdirSync(userDir, { recursive: true });
32
+ }
33
+
34
+ // Clone the repository
35
+ const gitClone = spawn('git', ['clone', repoUrl, '.'], { cwd: userDir });
36
+
37
+ gitClone.stdout.on('data', (data) => {
38
+ socket.emit('output', data.toString());
39
+ });
40
+
41
+ gitClone.stderr.on('data', (data) => {
42
+ socket.emit('output', data.toString());
43
+ });
44
+
45
+ gitClone.on('close', (code) => {
46
+ if (code === 0) {
47
+ socket.emit('output', 'βœ… Repository cloned successfully!');
48
+ socket.emit('repoCloned', { success: true });
49
+ } else {
50
+ socket.emit('output', '❌ Failed to clone repository');
51
+ socket.emit('repoCloned', { success: false });
52
+ }
53
+ });
54
+ });
55
+
56
+ // Handle npm/yarn install
57
+ socket.on('installDeps', ({ userId, packageManager }) => {
58
+ const userDir = path.join(USERS_DIR, userId);
59
+ const command = packageManager === 'yarn' ? 'yarn' : 'npm';
60
+ const args = ['install'];
61
+
62
+ const installProcess = spawn(command, args, { cwd: userDir });
63
+
64
+ installProcess.stdout.on('data', (data) => {
65
+ socket.emit('output', data.toString());
66
+ });
67
+
68
+ installProcess.stderr.on('data', (data) => {
69
+ socket.emit('output', data.toString());
70
+ });
71
+
72
+ installProcess.on('close', (code) => {
73
+ if (code === 0) {
74
+ socket.emit('output', 'βœ… Dependencies installed successfully!');
75
+ } else {
76
+ socket.emit('output', '❌ Failed to install dependencies');
77
+ }
78
+ });
79
+ });
80
+
81
+ // Handle starting the application
82
+ socket.on('startApp', ({ userId, startCommand }) => {
83
+ const userDir = path.join(USERS_DIR, userId);
84
+ const [command, ...args] = startCommand.split(' ');
85
+
86
+ const startProcess = spawn(command, args, { cwd: userDir });
87
+
88
+ startProcess.stdout.on('data', (data) => {
89
+ socket.emit('output', data.toString());
90
+ });
91
+
92
+ startProcess.stderr.on('data', (data) => {
93
+ socket.emit('output', data.toString());
94
+ });
95
+
96
+ startProcess.on('close', (code) => {
97
+ socket.emit('output', `Process exited with code ${code}`);
98
+ });
99
+
100
+ // Store the process reference so we can kill it later
101
+ socket.process = startProcess;
102
+ });
103
+
104
+ // Handle stopping the application
105
+ socket.on('stopApp', () => {
106
+ if (socket.process) {
107
+ socket.process.kill();
108
+ socket.emit('output', 'πŸ›‘ Application stopped');
109
+ } else {
110
+ socket.emit('output', 'No running process to stop');
111
+ }
112
+ });
113
+
114
+ // Handle file operations
115
+ socket.on('readFile', ({ userId, filePath }) => {
116
+ const fullPath = path.join(USERS_DIR, userId, filePath);
117
+
118
+ fs.readFile(fullPath, 'utf8', (err, data) => {
119
+ if (err) {
120
+ socket.emit('output', `❌ Error reading file: ${err.message}`);
121
+ } else {
122
+ socket.emit('fileContent', { filePath, content: data });
123
+ }
124
+ });
125
+ });
126
+
127
+ socket.on('writeFile', ({ userId, filePath, content }) => {
128
+ const fullPath = path.join(USERS_DIR, userId, filePath);
129
+
130
+ fs.writeFile(fullPath, content, 'utf8', (err) => {
131
+ if (err) {
132
+ socket.emit('output', `❌ Error writing file: ${err.message}`);
133
+ } else {
134
+ socket.emit('output', `βœ… File ${filePath} saved successfully`);
135
+ }
136
+ });
137
+ });
138
+
139
+ socket.on('disconnect', () => {
140
+ console.log('User disconnected');
141
+ if (socket.process) {
142
+ socket.process.kill();
143
+ }
144
+ });
145
+ });
146
+
147
+ const PORT = process.env.PORT || 7860;
148
+ http.listen(PORT, () => console.log(`🌐 Server running on port ${PORT}`));