| 176 | } |
| 177 | |
| 178 | function IDEMessageHandler(client:SocketRuntimeClient, message) { |
| 179 | let ws = client.socket; |
| 180 | let data = JSON.parse(message); |
| 181 | |
| 182 | if(data.type === "init") { |
| 183 | let {editor, runtimeOwner, controlOwner, internal, mode} = config; |
| 184 | let {hash} = data; |
| 185 | |
| 186 | let content:string; |
| 187 | let path:string; |
| 188 | let isLocal = hash.indexOf("gist:") === 0; |
| 189 | |
| 190 | // If we're in file mode, the only valid file to serve is the one specified in `config.path`. |
| 191 | if(mode === Mode.file) { |
| 192 | content = eveSource.find(config.path); |
| 193 | path = config.path; |
| 194 | } |
| 195 | |
| 196 | // Otherwise, anything goes. First we check if the client has requested a specific file in the URL hash. |
| 197 | if(!isLocal && mode === Mode.workspace && hash) { |
| 198 | // @FIXME: This code to strip the editor hash segment out really needs to be abstacted. |
| 199 | let filepath = hash.split("#")[0]; |
| 200 | if(filepath[filepath.length - 1] === "/") filepath = filepath.slice(0, -1); |
| 201 | |
| 202 | content = filepath && eveSource.find(filepath); |
| 203 | path = hash; |
| 204 | } |
| 205 | |
| 206 | // If we've got a path to run with, use it as the default. |
| 207 | if(!isLocal && !content && config.path) { |
| 208 | let workspace = "root"; |
| 209 | // @FIXME: This hard-coding isn't technically wrong right now, but it's brittle and poor practice. |
| 210 | content = eveSource.get(config.path, workspace); |
| 211 | path = eveSource.getRelativePath(config.path, workspace); |
| 212 | } |
| 213 | |
| 214 | // If we can't find the config path in a workspace, try finding it on disk. |
| 215 | if(!isLocal && !content && config.path && fs.existsSync("." + path)) { |
| 216 | content = fs.readFileSync("." + path).toString(); |
| 217 | } |
| 218 | |
| 219 | ws.send(JSON.stringify({type: "initProgram", path, code: content, config, workspaces: eveSource.workspaces})); |
| 220 | if(runtimeOwner === Owner.server) { |
| 221 | client.load(content, "user"); |
| 222 | } |
| 223 | } else if(data.type === "save"){ |
| 224 | eveSource.save(data.path, data.code); |
| 225 | } else if(data.type === "ping") { |
| 226 | // we don't need to do anything with pings, they're just to make sure hosts like |
| 227 | // Heroku don't shutdown our server. |
| 228 | } else { |
| 229 | client.handleEvent(message); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | function MessageHandler(client:SocketRuntimeClient, message) { |
| 234 | let ws = client.socket; |