Fork me on GitHub
#babashka
<
2023-08-19
>
Richie20:08:11

Hey! I'm using bb to automate things for cljs rn development and one of the things I need to do is call npx expo start --android. I got something working using :inherit true and now I'm all set but I was curious how I would make it work without :inherit true i.e. if I wanted to capture output.

Richie20:08:44

script/bb/android.clj

(ns bb.android
  (:require [ babashka.process :refer [process]]))

(defn launch []
  (process {:inherit true} "npm" "run" "android"))
script/bb/watch.clj
(ns bb.watch
  (:require [ babashka.process :refer [shell process exec]]
            [ :as io]))

(defn launch []
  (shell {:out :string :err :string} "adb" "devices")
  (let [p (promise)]
    (doto
     (Thread.
      (fn []
        (doseq [line (line-seq (io/reader (:out (process "npm" "run" "watch"))))]
          (println (str "[watch] " line))
          (when (seq (re-seq #"shadow-cljs - watching build :app" line))
            (deliver p "booted")))))
      (.start))
    p))
bb.edn
{:paths ["script"]
 :tasks {:requires ([bb.emulator :as emulator]
                    [bb.watch :as watch]
                    [bb.android :as android])
         dev {:task (do
                      (let [watch (watch/launch)
                            emulator (emulator/launch)]
                        (deref watch)
                        (deref emulator)
                        (deref (android/launch))))}
         emulator {:task (do
                           (shell "adb" "devices")
                           (deref (emulator/launch)))}
         watch {:task (deref (watch/launch))}
         android {:task (deref (android/launch))}}}

borkdude20:08:15

probably something like this yes

Richie21:08:27

I'm able to call npm run watch and re-seq on the lines for shadow-cljs - watching build :app but I wasn't able to figure out how to do something similar for npx expo start --android because that starts an interactive thing where I can hit a to launch it or r to reload it. I have a package.json that aliases android to npx expo start --android... I'm sure I'm not asking a clear question and I'm sorry about that. I just wanted ideas on how I could run an interactive program and re-seq the output. I'm guessing I would have to get rid of :inherit but I don't know what to use instead.

borkdude21:08:39

You can do {:in :inherit} instead

Richie21:08:57

Oh, that's an interesting point.

Richie21:08:15

On another note, the first thing that I tried was this:

(defn launch []
  (process {:in *in*
            :out *out*
            :err *out*}
           "npm" "run" "android"))
Why doesn't that work?

borkdude21:08:47

it's related to buffering

Richie21:08:29

Ok, so it would work if pass some kind of InputStream to :in and copy and flush each byte from *in*?

borkdude21:08:50

there's a discussion in the process repo on this somewhere