This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Doric/doric-cli/src/server.ts

106 lines
5.2 KiB
TypeScript
Raw Normal View History

2021-02-05 18:20:42 +08:00
import fs from "fs";
2021-02-19 17:57:15 +08:00
import WebSocket from "ws"
2021-02-05 18:20:42 +08:00
import "colors";
2021-02-19 15:43:12 +08:00
import path from "path";
import { delay, glob } from "./util";
import { Shell } from "./shell";
2021-02-05 18:20:42 +08:00
2021-02-20 17:58:09 +08:00
export type MSG = {
type: "D2C" | "C2D" | "C2S" | "D2S" | "S2C" | "S2D",
cmd: string,
payload: { [index: string]: string }
}
2021-02-05 18:20:42 +08:00
export async function createServer() {
let contextId: string = "0"
2021-02-19 17:57:15 +08:00
let clientConnection: WebSocket | undefined = undefined
let debuggerConnection: WebSocket | undefined = undefined
2021-02-07 16:33:31 +08:00
let deviceId = 0
2021-02-19 17:57:15 +08:00
return new WebSocket.Server({ port: 7777 })
.on("connection", (ws, request) => {
2021-02-20 17:58:09 +08:00
let thisDeviceId: string
2021-02-19 17:57:15 +08:00
console.log('Connected', request.headers.host)
if (request.headers.host?.startsWith("localhost")) {
2021-02-20 17:58:09 +08:00
thisDeviceId = `Debugger#${deviceId++}`
2021-02-19 17:57:15 +08:00
console.log(`Debugger ${thisDeviceId} attached to dev kit`.green)
debuggerConnection = ws
clientConnection?.send(JSON.stringify({
cmd: 'SWITCH_TO_DEBUG',
contextId: contextId
}))
} else {
2021-02-20 17:58:09 +08:00
thisDeviceId = `Client#${deviceId++}`
2021-02-19 17:57:15 +08:00
console.log(`${thisDeviceId} attached to dev kit`.green)
}
2021-02-20 17:58:09 +08:00
ws.on('message', async function (result: string) {
const resultObject = JSON.parse(result) as MSG
if (resultObject.type === "D2C" || resultObject.type === "C2D") {
ws.send(result);
} else if (resultObject.type === "C2S") {
switch (resultObject.cmd) {
case 'DEBUG':
clientConnection = ws;
(ws as any).debugging = true;
console.log("Enter debugging");
contextId = resultObject.payload.contextId;
const projectHome = '.';
await fs.promises.writeFile(path.resolve(projectHome, "build", "context"), contextId, "utf-8");
let source = resultObject.payload.source as string;
if (source.startsWith(".js")) {
source = source.replace(".js", ".ts");
} else if (!source.startsWith(".ts")) {
source = source + ".ts"
2021-02-19 17:57:15 +08:00
}
2021-02-20 17:58:09 +08:00
let sourceFile = path.resolve(projectHome, "src", source);
if (!fs.existsSync(sourceFile)) {
const tsFiles = await glob(source, {
cwd: path.resolve(projectHome, "src")
})
if (!!!tsFiles || tsFiles.length === 0) {
console.error(`Cannot find ${source} in ${path.resolve(projectHome)}`);
}
sourceFile = tsFiles[0];
}
console.log(thisDeviceId + " request debug, project home: " + projectHome);
await Shell.exec("code", [projectHome, sourceFile]);
await delay(1500);
break;
case 'EXCEPTION':
console.log(resultObject.payload.source.red);
console.log(resultObject.payload.exception.red);
break;
case 'LOG':
const date = new Date
const format = function (num: number) {
return (Array(2).join("0") + num).slice(-2);
};
const timeStr = `${format(date.getHours())}:${format(date.getMinutes())}:${format(date.getSeconds())}.${(Array(3).join("0") + date.getMilliseconds()).slice(-3)}`
const logContent = resultObject.payload.message
if (resultObject.payload.type === 'DEFAULT') {
console.log(`${timeStr} ${thisDeviceId} ${"[I]".green} ${logContent.green}`.bgBlue);
} else if (resultObject.payload.type === 'ERROR') {
console.log(`${timeStr} ${thisDeviceId} ${"[E]".green} ${logContent.green}`.bgRed);
} else if (resultObject.payload.type === 'WARN') {
console.log(`${timeStr.black} ${thisDeviceId.black} ${"[W]".green} ${logContent.green}`.bgYellow);
}
break
}
2021-02-19 17:57:15 +08:00
}
})
ws.on('connect', function (code: number) {
console.log('connect', code)
})
ws.on('close', function (code: number) {
console.log('close: code = ' + code, thisDeviceId)
console.log("quit debugging");
(ws as any).debugging = false
})
ws.on('error', function (code: number) {
console.log('error', code)
})
2021-02-05 18:20:42 +08:00
})
}