From c8fe24e69dc98f5033027bc7de1a6cf624f3e96b Mon Sep 17 00:00:00 2001 From: Schlauer-Hax Date: Wed, 29 Nov 2023 22:35:03 +0100 Subject: [PATCH] fix helper crash --- .gitignore | 1 + helper/index.ts | 21 ++++++++++++--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 674b02d..d9c7448 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ node_modules/ .DS_Store helper/.env helper/testinput.txt +deno.lock \ No newline at end of file diff --git a/helper/index.ts b/helper/index.ts index b61c421..c197c8e 100644 --- a/helper/index.ts +++ b/helper/index.ts @@ -1,4 +1,5 @@ import "https://deno.land/x/dotenv@v3.2.0/load.ts"; +import { exists } from "https://deno.land/std@0.208.0/fs/mod.ts"; const date = new Date() if (date.getMonth() === 11) { @@ -25,7 +26,7 @@ function websocket() { const ws = new WebSocket('ws://localhost:9000/wss'); let latestsolution: string; - ws.onmessage = (event) => { + ws.onmessage = async (event) => { const json = JSON.parse(event.data); if (json.type === 'solutions') { console.log('Solutions received, triggering latest solution...'); @@ -34,18 +35,20 @@ function websocket() { type: 'data', name: latestsolution, })); - const testinput = Deno.readTextFileSync('./testinput.txt'); - if (testinput !== '') { - ws.send(JSON.stringify({ - type: 'run', - solution: latestsolution, - input: testinput, - })); + if (await exists('./testinput.txt')) { + const testinput = Deno.readTextFileSync('./testinput.txt'); + if (testinput !== '') { + ws.send(JSON.stringify({ + type: 'run', + solution: latestsolution, + input: testinput, + })); + } } } else if (json.type === 'result') { console.log(`${json.data}`); } else if (json.type === 'data') { - ws.send(JSON.stringify({type: 'run',solution: latestsolution,input: json.data})); + ws.send(JSON.stringify({ type: 'run', solution: latestsolution, input: json.data })); } };