-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
127 lines (92 loc) · 3.23 KB
/
index.ts
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { ServerWebSocket } from "bun"
import {exec} from "child_process"
import fs from "fs";
function execute(command: string, callback: any, ws: ServerWebSocket<unknown>) {
let ls = exec(command,callback);
if (ls.stdout === null || ls.stderr === null){
console.log("Stdout is null");
return;
}
let buffer = '';
ls.stdout.on('data', (data) => {
buffer += data.toString();
// Split the buffer into lines
const lines = buffer.split('\n');
// Process complete lines, keeping the last incomplete line in the buffer
for (let i = 0; i < lines.length - 1; i++) {
console.log(`stdout: ${lines[i]}`);
}
// Save the last incomplete line in the buffer for the next iteration
buffer = lines[lines.length - 1];
});
ls.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
ls.on('close', (code) => {
// Process any remaining incomplete line in the buffer
if (buffer.trim() !== '') {
console.log(`stdout: ${buffer}`);
}
console.log(`child process exited with code ${code}`);
});
ws.send(buffer);
}
function logResult(output: any) {
output.stdout?.on("data", (data:any) => {
// ws.send(data.toString());
console.log("LOG: ",data.toString());
});
}
const server = Bun.serve({
port: 3001,
fetch (request, server) {
if (server.upgrade(request)){
return;
}
return new Response("Hello Bun");
},
websocket: {
open(ws) {
console.log("connection open");
ws.send("Connected");
},
message(ws, message) {
let body = JSON.parse(String(message));
console.log("incoming message",body.emit);
switch (body.emit) {
case "new-project":
execute(
"mkdir projects && cd projects && cargo stylus new project > output.txt 2>&1",
logResult,
ws
);
ws.send("code compiled");
break;
case "compile-project":
// fs.writeFileSync("./projects/project/src/main.rs", body.main);
// fs.writeFileSync("./projects/project/src/lib.rs", body.lib);
execute(
"cd projects && cd project && cargo stylus check > output.txt 2>&1",
logResult,
ws
);
ws.send("Completed");
break;
case "get-abi":
execute(
"cd projects && cd project && cargo stylus export-abi --json > abi.json 2>&1",
logResult,
ws
);
ws.send("Created");
break;
default:
ws.send("Invalid Event");
}
},
close(ws){
console.log("connection closed");
ws.send("Closed");
}
}
})