()
| 185 | } |
| 186 | |
| 187 | protected connectToConsole(): Promise<void> { |
| 188 | return new Promise<void>((resolve, reject) => { |
| 189 | const socket = new net.Socket(); |
| 190 | socket.on ('data', (data) => { |
| 191 | try { |
| 192 | this.process.stdin.write(data, 'utf8'); |
| 193 | } |
| 194 | catch (e) { |
| 195 | console.error(`stdin write failed ${e}`); |
| 196 | } |
| 197 | }); |
| 198 | socket.once('close', () => { |
| 199 | this.consoleSocket = null; |
| 200 | }); |
| 201 | socket.on ('error', (e) => { |
| 202 | const code: string = (e as any).code; |
| 203 | if (code !== 'ECONNRESET') { |
| 204 | // Can happen if extension exited while we are still running. Rare, generally a bug in VSCode or frontend |
| 205 | const msg = `Error: unexpected socket error ${e}. Please report this problem`; |
| 206 | this.emit('output', msg + '\n'); |
| 207 | console.error(msg); |
| 208 | if (!this.consoleSocket) { // We were already connected |
| 209 | reject(e); |
| 210 | } |
| 211 | } else if (this.consoleSocket) { |
| 212 | // Adapter died/crashed |
| 213 | this.consoleSocket.destroy(); |
| 214 | this.consoleSocket = null; |
| 215 | } |
| 216 | }); |
| 217 | |
| 218 | // It is possible that the server is not ready |
| 219 | socket.connect(this.consolePort, '127.0.0.1', () => { |
| 220 | socket.write(greenFormat(quoteShellCmdLine([this.application, ...this.args]) + '\n')); |
| 221 | this.consoleSocket = socket; |
| 222 | resolve(); |
| 223 | }); |
| 224 | }); |
| 225 | } |
| 226 | |
| 227 | private sendToConsole(data: string|Buffer) { |
| 228 | if (this.consoleSocket) { |
no test coverage detected