Skip to content

Commit ceaa293

Browse files
committed
Support directories in the watcher interface
1 parent f40585f commit ceaa293

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/lambdaisland/launchpad/watcher.clj

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
(defn canonical-path [p]
2222
(.getCanonicalPath (io/file p)))
2323

24-
(defn parent-path [p]
25-
(.getParent (io/file (canonical-path p))))
24+
(defn dir-path [p]
25+
(if (.isDirectory (io/file p))
26+
(canonical-path p)
27+
(.getParent (io/file (canonical-path p)))))
2628

2729
(require 'clojure.pprint)
2830

@@ -53,7 +55,7 @@
5355
[file->handler]
5456
(let [file->handler (swap! handlers merge file->handler)
5557
file->handler (update-keys file->handler canonical-path)
56-
directories (distinct (map parent-path (keys file->handler)))
58+
directories (distinct (map dir-path (keys file->handler)))
5759
;; in case of nested directories, only watch the top-most one
5860
directories (remove (fn [d]
5961
(some #(and (not= d %)
@@ -67,11 +69,14 @@
6769
(for [dir directories]
6870
(watch
6971
(fn [{:keys [type path] :as event}]
70-
(if-let [f (get file->handler (str path))]
71-
(try
72-
(f event)
73-
(catch Exception e
74-
(prn e)))))
72+
(loop [path path]
73+
(if-let [f (get file->handler (str path))]
74+
(try
75+
(f event)
76+
(catch Exception e
77+
(prn e)))
78+
(when-let [p (.getParent path)]
79+
(recur p)))))
7580
(str dir))))))))
7681

7782
(comment

0 commit comments

Comments
 (0)