joy icon indicating copy to clipboard operation
joy copied to clipboard

Reload code on refresh without server restart

Open swlkr opened this issue 5 years ago • 6 comments
trafficstars

swlkr avatar Dec 18 '19 00:12 swlkr

Does this implemented? I tried and it didn't work. However I can use external tool like entr - ls *.janet | entr -r janet main.janet, but I wouldn't want to rely on external tools if it could be avoided.

Ok. LOL. Found in the code that do the same thing with the same tool.

onemanstartup avatar Sep 17 '20 19:09 onemanstartup

Yeah entr is great!

I'm still not quite sure how I'd do this. It would be nice to start a websocket server and push updated html to the page you're working on, something like "hot reload" but I'm not sure if I care about the server reloading without restarting since restarts are so fast.

swlkr avatar Sep 17 '20 19:09 swlkr

Are there any updates on this? Maybe there is a way to at least refresh a browser tab when the code changes.

aliaksandr-s avatar Nov 27 '20 16:11 aliaksandr-s

I did manage to whip something up to refresh the tab, but the websockets thing would be cross platform/cross browser.

Here is what I did on macOS. I created two files, watch with chmod +x and refresh-tab.scptd:

watch looks like this:

#!/usr/bin/env sh

pkill -xf "janet src/app.janet"
sleep .5
janet src/app.janet &
osascript refresh-tab.scptd

refresh-tab looks like this:

#!/usr/bin/osascript

tell application "Safari"
    set docUrl to URL of document 1
    set URL of document 1 to docUrl
end tell

and then it gets called via fswatch which I wrapped up in a phony in project.janet:

(phony "watch" []
  (os/shell "fswatch -o . --exclude='.git' --exclude='.sqlite3' | xargs -n1 -I{} ./watch"))

I think I will take another shot at using websockets to reload html on the page with https://github.com/joy-framework/halo2 now that janet master has some async things so it might be possible to get websockets working in pure janet.

swlkr avatar Nov 30 '20 17:11 swlkr

I noticed that generating a fresh app contains entr in the server command. I was surprised to find that the app wasn't reloading when I made changes though.

(phony "server" []
  (if (= "development" (os/getenv "JOY_ENV"))
    (os/shell "find . -name '*.janet' | entr janet main.janet")
    (os/shell "janet main.janet")))

I'm pretty sure that it is because when it calls os/getenv the .env file hasn't been loaded yet so it always evaluates to false. I got it working by changing it to this

(use joy)

(phony "server" []
  (if (= "development" (env :joy_env))
    (os/shell "find . -name '*.janet' | entr -r janet main.janet")
    (os/shell "janet main.janet")))

Instead of using os/getenv I use joy's env and there was a missing -r flag on the entr command

matthewmcgarvey avatar Jan 30 '21 16:01 matthewmcgarvey