Skip to content

Commit 92c9d60

Browse files
petterikdnolen
authored andcommitted
CLJS-2681: Accepting multiple paths to the --watch option for cljs.main
1 parent e231080 commit 92c9d60

File tree

2 files changed

+52
-16
lines changed

2 files changed

+52
-16
lines changed

src/main/clojure/cljs/cli.clj

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -145,20 +145,36 @@ classpath. Classpath-relative paths have prefix of @ or @/")
145145
[cfg value]
146146
(assoc-in cfg [:options :verbose] (= value "true")))
147147

148+
(defn- validate-watch-paths [[path :as paths]]
149+
(when (or (nil? path)
150+
(and (not (.exists (io/file path)))
151+
(or (string/blank? path)
152+
(string/starts-with? path "-"))))
153+
(throw
154+
(ex-info
155+
(str "Missing watch path(s)")
156+
{:cljs.main/error :invalid-arg})))
157+
(when-let [non-existent (seq (remove #(.exists (io/file %)) paths))]
158+
(throw
159+
(ex-info
160+
(if (== 1 (count non-existent))
161+
(str "Watch path "
162+
(first non-existent)
163+
" does not exist")
164+
(str "Watch paths "
165+
(string/join ", " (butlast non-existent))
166+
" and "
167+
(last non-existent)
168+
" does not exist"))
169+
{:cljs.main/error :invalid-arg}))))
170+
148171
(defn- watch-opt
149-
[cfg path]
150-
(when-not (.exists (io/file path))
151-
(if (or (string/starts-with? path "-")
152-
(string/blank? path))
153-
(throw
154-
(ex-info
155-
(str "Missing watch path")
156-
{:cljs.main/error :invalid-arg}))
157-
(throw
158-
(ex-info
159-
(str "Watch path " path " does not exist")
160-
{:cljs.main/error :invalid-arg}))))
161-
(assoc-in cfg [:options :watch] path))
172+
[cfg paths]
173+
(let [paths (util/split-paths paths)]
174+
(validate-watch-paths paths)
175+
(assoc-in cfg [:options :watch] (cond-> paths
176+
(== 1 (count paths))
177+
first))))
162178

163179
(defn- optimize-opt
164180
[cfg level]
@@ -454,7 +470,9 @@ present"
454470
(not (:output-dir opts))
455471
(assoc :output-dir "out")
456472
(not (contains? opts :aot-cache))
457-
(assoc :aot-cache true)))
473+
(assoc :aot-cache true)
474+
(sequential? (:watch opts))
475+
(update :watch cljs.closure/compilable-input-paths)))
458476
convey (into [:output-dir] repl/known-repl-opts)
459477
cfg (update cfg :options merge (select-keys opts convey))
460478
source (when (and (= :none (:optimizations opts :none)) main-ns)
@@ -533,8 +551,10 @@ present"
533551
"will be used to set ClojureScript compiler "
534552
"options") }
535553
["-w" "--watch"] {:group ::compile :fn watch-opt
536-
:arg "path"
537-
:doc "Continuously build, only effective with the --compile main option"}
554+
:arg "paths"
555+
:doc (str "Continuously build, only effective with the "
556+
"--compile main option. Specifies a system-dependent "
557+
"path-separated list of directories to watch.")}
538558
["-o" "--output-to"] {:group ::compile :fn output-to-opt
539559
:arg "file"
540560
:doc "Set the output compiled file"}

src/main/clojure/cljs/closure.clj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,22 @@
542542
(-compile [this opts] "Returns one or more IJavaScripts.")
543543
(-find-sources [this opts] "Returns one or more IJavascripts, without compiling them."))
544544

545+
(defn compilable-input-paths
546+
"Takes a coll of inputs as strings or files and returns a
547+
single Inputs and Compilable object."
548+
[paths]
549+
(reify
550+
cljs.closure/Inputs
551+
(-paths [_]
552+
(mapcat cljs.closure/-paths paths))
553+
cljs.closure/Compilable
554+
(-compile [_ opts]
555+
(mapcat #(cljs.closure/-compile % opts)
556+
paths))
557+
(-find-sources [_ opts]
558+
(mapcat #(cljs.closure/-find-sources % opts)
559+
paths))))
560+
545561
(defn compile-form-seq
546562
"Compile a sequence of forms to a JavaScript source string."
547563
([forms]

0 commit comments

Comments
 (0)