Skip to content

Handle file paths gracefully #15

@jyn514

Description

@jyn514

Currently, beholder can only handle directories, not files (i.e. it inherits gmethvin/directory-watcher#95). It would be nice to work around this upstream. Here is some code I use in my own project—maybe this fits into the API?

(require  [babashka.fs :as fs])

(defn on-file-change
  [cb paths event]
    ; behold doesn't support file filters, only directory filters. implement them ourselves.
    (when (contains? paths (-> event :path))
         (cb event)))

(defn to-dir [path]
  (let [dir (if (fs/directory? path) path (fs/parent path))]
    (-> dir fs/real-path str)))

(defn watch-files
  [cb paths]
  (let [abs-paths (set (map fs/real-path paths))
        dirs (set (map to-dir abs-paths))]
    (apply behold/watch #(on-file-change cb abs-paths %) dirs)))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions