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/scripts/server.js

70 lines
2.5 KiB
JavaScript
Raw Normal View History

2019-12-04 14:16:57 +08:00
const ws = require('nodejs-websocket')
const { exec, spawn } = require('child_process')
const fs = require('fs')
2019-12-11 15:14:29 +08:00
var server
2019-12-10 20:54:49 +08:00
var contextId = null
2019-12-04 14:16:57 +08:00
var clientConnection = null
var debuggerConnection = null
const createServer = () => {
2019-12-11 15:14:29 +08:00
server = ws.createServer(connection => {
2019-12-04 14:16:57 +08:00
console.log('connected', connection.headers.host)
if (connection.headers.host.startsWith("localhost")) {
console.log("debugger " + connection.key + " attached to dev kit")
debuggerConnection = connection
clientConnection.sendText(JSON.stringify({
2019-12-10 20:54:49 +08:00
cmd: 'SWITCH_TO_DEBUG',
contextId: contextId
2019-12-04 14:16:57 +08:00
}), function() {
})
} else {
console.log("client " + connection.key + " attached to dev kit")
}
connection.on('text', function (result) {
console.log('text', result)
let resultObject = JSON.parse(result)
switch(resultObject.cmd) {
case 'DEBUG':
clientConnection = connection
2019-12-11 15:14:29 +08:00
server.debugging = true
console.log("enter debugging")
2019-12-10 20:54:49 +08:00
contextId = resultObject.data.contextId
let projectHome = '.'
2019-12-04 14:16:57 +08:00
fs.writeFileSync(projectHome + '/build/context', contextId, 'utf8')
let source = resultObject.data.source
console.log(connection.key + " request debug, project home: " + projectHome)
spawn('code', [projectHome, projectHome + "/src/" + source])
setTimeout(() => {
exec('osascript -e \'tell application "System Events"\ntell application "Visual Studio Code" to activate\nkey code 96\nend tell\'', (err, stdout, stderr) => {
if (err) {
console.log(`stdout: ${err}`)
}
})
2019-12-10 20:54:49 +08:00
}, 1500)
2019-12-04 14:16:57 +08:00
break
}
})
connection.on('connect', function (code) {
console.log('connect', code)
})
connection.on('close', function (code) {
console.log('close: code = ' + code, connection.key)
2019-12-11 15:14:29 +08:00
console.log("quit debugging")
server.debugging = false
2019-12-04 14:16:57 +08:00
})
connection.on('error', function (code) {
console.log('error', code)
})
})
return server
}
module.exports = createServer()