joy
joy copied to clipboard
Reload code on refresh without server restart
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.
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.
Are there any updates on this? Maybe there is a way to at least refresh a browser tab when the code changes.
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.
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