Fork me on GitHub
#shadow-cljs
<
2023-04-16
>
witek17:04:37

Usually I have the shadow-cljs watch process in a window which is not visible. I would like to play a sound, when there is a compilation error/warning. And then another sound when the error/warning is gone again. Any ideas how this could be achieved? Thank you.

thheller06:04:01

if this is for a browser and you have the browser also open you can use :devtools {:custom-notify a.namespace/your-function}

thheller06:04:11

which is jsut a (defn your-function [msg] ...)

thheller06:04:25

you can (js/console.log msg) to see what it gets called with

thheller06:04:14

basically it gets all the messages that the hot-reload/repl stuff also gets, so there is a :build-complete and :build-failure

thheller06:04:23

you could use the browser to play a sound and so on

witek06:04:38

this is for node

thheller06:04:52

the node process can also do that I guess

witek06:04:18

so I would like a invoke conmands on these events

thheller06:04:10

and if you want a more involved version you can do a plugin, but its not documented so you have to do some digging. I can give you the barebones if you want

thheller06:04:11

not much code but you can basically subscribe to anything shadow-cljs does

witek06:04:30

I will try the hook first

thheller06:04:20

hook doens't get called for build failures IIRC

witek06:04:23

ok, then you can give me a hint, how I can plug in?

thheller06:04:52

(ns demo.dummy-plugin
  (:require
    [clojure.core.async :as async]
    [shadow.jvm-log :as log]
    [shadow.cljs.model :as m]
    [shadow.cljs.devtools.server.system-bus :as sys-bus]))

(def plugin
  {:requires-server true
   :depends-on [:system-bus]
   :start
   (fn [system-bus]
     (let [chan (async/chan (async/sliding-buffer 10))]
       (sys-bus/sub system-bus ::m/worker-broadcast chan)
       (async/thread
         (loop []
           (when-some [msg (async/<!! chan)]
             (tap> [:plugin msg])
             (recur))))
       chan))
   :stop
   (fn [chan]
     (log/debug ::stop)
     (async/close! chan)
     ::done)})

thheller06:04:17

then :plugins [demo.dummy-plugin] in shadow-cljs.edn

thheller06:04:52

look in the http://localhost:9630/inspect ui to see what kind of messages you get

thheller06:04:01

and replace the tap> with whatever you want to do

thheller06:04:27

there is (:type msg) with :build-complete and :build-failure and so on

witek06:04:49

thank you!

thheller06:04:03

this is regular clojure code, so you can do whatever you want.

thheller06:04:23

please share what you come up with. maybe others are interested too

witek06:04:54

Where do I put my Clojure file?

thheller07:04:18

its a regular clojure namespace, with all the regular rules

witek07:04:46

ok, I got it. First I had a simple namespace myplugin. This did not work. It must be like tools.myplugin.

thheller07:04:19

(ns myplugin) should work just fine?

thheller07:04:43

assuming you have :source-paths ["src/main"] and src/main/myplugin.clj file

witek07:04:17

it was crashing with NullPointerException

witek08:04:19

I get the messages :build-start and :build-complete but there is no message for a build failure...

witek08:04:34

Is there an other topic to subscribe for build failures?

thheller08:04:33

:build-complete is the message you want

thheller08:04:07

the warnings are in the data

thheller08:04:06

this is how you can get all the warnings for example, thats how the client HUD displays them

witek09:04:20

It works. I love it! I will write a post and provide a link. Thank you for providing such a great tool.

👍 2
Ryan22:04:11

@witek if you’re on a mac there’s say "what" or afplay /System/Library/Sounds/Sosumi.aiff maybe pipe watch to something? My shell-fu is weak these days.