|
| 1 | +import WebSocket from 'ws'; |
1 | 2 | import {Args, Command, Flags} from '@oclif/core' |
2 | 3 |
|
3 | 4 | import {getDisco} from '../config.js' |
@@ -30,14 +31,31 @@ export default class Run extends Command { |
30 | 31 | timeout: flags.timeout, |
31 | 32 | } |
32 | 33 | const res = await request({method: 'POST', url, discoConfig, body, expectedStatuses: [202]}) |
33 | | - const data = (await res.json()) as any |
34 | | - |
35 | | - const outputUrl = `https://${discoConfig.host}/api/projects/${flags.project}/runs/${data.run.number}/output` |
36 | | - readEventSource(outputUrl, discoConfig, { |
37 | | - onMessage(event: MessageEvent) { |
38 | | - const message = JSON.parse(event.data) |
39 | | - process.stdout.write(message.text) |
40 | | - }, |
41 | | - }) |
| 34 | + const respBody = (await res.json()) as {run: { |
| 35 | + id: string, |
| 36 | + number: number, |
| 37 | + }}; |
| 38 | + const wsUrl = `wss://${discoConfig.host}/api/projects/${flags.project}/runs/${respBody.run.id}/ws`; |
| 39 | + const ws = new WebSocket(wsUrl); |
| 40 | + ws.on('error', console.error); |
| 41 | + ws.on('message', (data) => { |
| 42 | + if (data instanceof Buffer && data.length >= 2) { |
| 43 | + const prefix = data.subarray(0, 2).toString('utf8'); |
| 44 | + const restOfMessage = data.subarray(2); |
| 45 | + if (prefix === 'o:') { |
| 46 | + process.stdout.write(restOfMessage); |
| 47 | + } else if (prefix === 'e:') { |
| 48 | + process.stderr.write(restOfMessage); |
| 49 | + } else if (prefix === 's:') { |
| 50 | + const statusCode = Number.parseInt(restOfMessage.toString('utf8'), 10); |
| 51 | + this.exit(statusCode); |
| 52 | + } |
| 53 | + } |
| 54 | + }); |
| 55 | + process.stdin.setRawMode(true); |
| 56 | + process.stdin.resume(); |
| 57 | + process.stdin.on( 'data', (key) => { |
| 58 | + ws.send(key, {binary: true}); |
| 59 | + }); |
42 | 60 | } |
43 | 61 | } |
0 commit comments