aboutsummaryrefslogtreecommitdiff
path: root/scripts/cli.ts
blob: be7a81bfcdac875c9980c8ab3e3b2ca3f5f5bee2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const ws = new WebSocket(Deno.args[0])


function do_stuff() {

    switch (Deno.args[1]) {
        case "enqueue":
            if (Deno.args.length >= 4)
                ws.send(JSON.stringify({ t: "metadata", key: Deno.args[2], data: JSON.parse(Deno.args[3]) }))
            ws.send(JSON.stringify({ t: "enqueue", key: Deno.args[2], ignore_complete: true }))
            break;
        case "save":
            break;
        default:
            break
    }
    ws.send(JSON.stringify({ t: "save" }))
}

ws.onerror = () => console.error("ws error")
ws.onclose = () => console.error("ws closed")
ws.onopen = () => {
    console.log("ws open");
    ws.send(JSON.stringify({ t: "register", name: "cli", task_kinds: [] }))
    do_stuff()
    setTimeout(() => Deno.exit(0), 200) // not sure if websockets are flushed since they're non-blocking
}
ws.onmessage = ev => {
    if (typeof ev.data != "string") return
    const p = JSON.parse(ev.data)
    if (p.t == "config") return
    if (p.t == "error") console.error(`error: ${p.message}`);
}