Skip to content

Commit 1c93ed6

Browse files
Start to refactor
1 parent c575453 commit 1c93ed6

File tree

2 files changed

+27
-28
lines changed

2 files changed

+27
-28
lines changed

src/commands/interactive.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import WebSocket from 'ws';
21
import {Args, Command, Flags} from '@oclif/core'
32

43
import {getDisco} from '../config.js'
@@ -22,24 +21,6 @@ export default class Interactive extends Command {
2221
}
2322

2423
public async run(): Promise<void> {
25-
const ws = new WebSocket('wss://app1.antoineleclair.ca/api/projects/flask/runs-ws');
26-
ws.on('error', console.error);
27-
ws.on('message', (data) => {
28-
if (data instanceof Buffer && data.length >= 2) {
29-
const prefix = data.subarray(0, 2).toString('utf8');
30-
const restOfMessage = data.subarray(2);
31-
if (prefix === 'o:') {
32-
process.stdout.write(restOfMessage);
33-
} else if (prefix === 'e:') {
34-
process.stderr.write(restOfMessage);
35-
}
36-
}
37-
});
38-
process.stdin.setRawMode(true);
39-
process.stdin.resume();
40-
process.stdin.on( 'data', (key) => {
41-
ws.send(key, {binary: true});
42-
});
4324
}
4425
}
4526

src/commands/run.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import WebSocket from 'ws';
12
import {Args, Command, Flags} from '@oclif/core'
23

34
import {getDisco} from '../config.js'
@@ -30,14 +31,31 @@ export default class Run extends Command {
3031
timeout: flags.timeout,
3132
}
3233
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+
});
4260
}
4361
}

0 commit comments

Comments
 (0)