(statusBarItem: vscode.StatusBarItem)
| 23 | const WebSocketServer = Server; |
| 24 | |
| 25 | export const startDevTools = async (statusBarItem: vscode.StatusBarItem) => { |
| 26 | if (wss) { |
| 27 | wss.clients.forEach((client) => { |
| 28 | client.close(); |
| 29 | }); |
| 30 | wss.close(); |
| 31 | wss = undefined; |
| 32 | return setStatusBarToStopped(statusBarItem); |
| 33 | } |
| 34 | const config = getConfig(); |
| 35 | const port = config.get<number>("devToolsPort"); |
| 36 | wss = new WebSocketServer({ port: port || 3003 }); |
| 37 | setStatusBarToRunning(statusBarItem); |
| 38 | |
| 39 | const workspaceUri = getWorkspaceUri(); |
| 40 | if (!workspaceUri) { |
| 41 | return; |
| 42 | } |
| 43 | let rootDir = workspaceUri; |
| 44 | |
| 45 | if (!(await isRemixDir(rootDir))) { |
| 46 | const currentFile = vscode.window.activeTextEditor?.document.uri; |
| 47 | |
| 48 | if (!currentFile) { |
| 49 | return; |
| 50 | } |
| 51 | const remixDir = await getRemixRootFromFileUri(currentFile); |
| 52 | if (!remixDir) { |
| 53 | return; |
| 54 | } |
| 55 | rootDir = remixDir; |
| 56 | } |
| 57 | |
| 58 | const isTS = await isTSConfigExists(rootDir); |
| 59 | |
| 60 | // Handle WebSocket connection |
| 61 | wss.on("connection", (socket: WebSocket) => { |
| 62 | // Handle messages received from the React app |
| 63 | socket.onmessage = async (event: MessageEvent) => { |
| 64 | const message = JSON.parse(event.data.toString()); |
| 65 | |
| 66 | if (message.type === "plugin") { |
| 67 | if (message.subtype === "read_file") { |
| 68 | const file = await tryReadFile(joinPath(rootDir, message.path).path); |
| 69 | if (file) { |
| 70 | return socket.send(JSON.stringify({ type: "plugin", subtype: "read_file", error: false, data: file })); |
| 71 | } |
| 72 | socket.send(JSON.stringify({ type: "plugin", subtype: "read_file", error: true, data: "File not found" })); |
| 73 | } |
| 74 | if (message.subtype === "open_file") { |
| 75 | try { |
| 76 | await openFileInEditor(joinPath(rootDir, message.path).path); |
| 77 | socket.send(JSON.stringify({ type: "plugin", subtype: "open_file", error: false, data: "success" })); |
| 78 | } catch (err) { |
| 79 | socket.send( |
| 80 | JSON.stringify({ type: "plugin", subtype: "open_file", error: true, data: (err as any)?.message }), |
| 81 | ); |
| 82 | } |
no test coverage detected