2019-08-03 10:19:56 +08:00
|
|
|
const ws = require('nodejs-websocket')
|
2019-11-12 20:45:57 +08:00
|
|
|
const { exec, spawn } = require('child_process')
|
2019-08-03 10:19:56 +08:00
|
|
|
|
2019-11-13 15:29:56 +08:00
|
|
|
var clientConnection = null
|
|
|
|
var debuggerConnection = null
|
|
|
|
|
2019-08-03 10:19:56 +08:00
|
|
|
const createServer = () => {
|
|
|
|
let server = ws.createServer(connection => {
|
2019-11-13 15:29:56 +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({
|
|
|
|
cmd: 'SWITCH_TO_DEBUG'
|
|
|
|
}), function() {
|
|
|
|
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
console.log("client " + connection.key + " attached to dev kit")
|
|
|
|
}
|
|
|
|
|
2019-08-03 10:19:56 +08:00
|
|
|
connection.on('text', function (result) {
|
2019-08-13 17:39:07 +08:00
|
|
|
console.log('text', result)
|
2019-11-12 10:44:43 +08:00
|
|
|
let resultObject = JSON.parse(result)
|
|
|
|
switch(resultObject.cmd) {
|
2019-11-12 16:34:11 +08:00
|
|
|
case 'DEBUG':
|
2019-11-13 15:29:56 +08:00
|
|
|
clientConnection = connection
|
|
|
|
|
2019-11-12 16:34:11 +08:00
|
|
|
let contextId = resultObject.data.contextId
|
|
|
|
let projectHome = resultObject.data.projectHome
|
2019-11-20 15:50:19 +08:00
|
|
|
let source = resultObject.data.source
|
2019-11-13 15:29:56 +08:00
|
|
|
console.log(connection.key + " request debug, project home: " + projectHome)
|
|
|
|
|
2019-11-20 15:50:19 +08:00
|
|
|
spawn('code', [projectHome, projectHome + "/src/" + source])
|
2019-11-13 15:29:56 +08:00
|
|
|
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-11-12 20:04:18 +08:00
|
|
|
})
|
2019-11-13 15:29:56 +08:00
|
|
|
}, 3000)
|
2019-11-12 16:43:30 +08:00
|
|
|
|
2019-11-12 10:44:43 +08:00
|
|
|
break
|
|
|
|
}
|
2019-08-03 10:19:56 +08:00
|
|
|
})
|
|
|
|
connection.on('connect', function (code) {
|
2019-08-13 17:39:07 +08:00
|
|
|
console.log('connect', code)
|
2019-08-03 10:19:56 +08:00
|
|
|
})
|
|
|
|
connection.on('close', function (code) {
|
2019-11-13 15:29:56 +08:00
|
|
|
console.log('close: code = ' + code, connection.key)
|
2019-08-03 10:19:56 +08:00
|
|
|
})
|
|
|
|
connection.on('error', function (code) {
|
2019-08-13 17:39:07 +08:00
|
|
|
console.log('error', code)
|
2019-08-03 10:19:56 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
return server
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = createServer()
|