Skip to content

Commit

Permalink
Get dynamic route importing working
Browse files Browse the repository at this point in the history
  • Loading branch information
swlkr committed Apr 30, 2020
1 parent 83f3740 commit 7a83be7
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/joy/router.janet
Expand Up @@ -87,9 +87,17 @@
~(set *route-table* (merge *route-table* (route-table ,;args)))))


(defn present? [val]
(and (truthy? val)
(not (empty? val))))


(defn namespace [val]
(when (keyword? val)
(first (string/split "/" val))))
(let [arr (string/split "/" val)
len (dec (length arr))
ns-array (array/slice arr 0 len)]
(string/join ns-array "/"))))


(defmacro defroutes [& args]
Expand All @@ -101,13 +109,13 @@
files (as-> rest ?
(map |(get $ 2) ?)
(map namespace ?)
(filter truthy? ?)
(filter present? ?)
(distinct ?))

# import all distinct file names from routes
_ (loop [file :in files]
(try
(import* (string "./routes/" file))
(import* (string "./routes/" file) :as file)
([err]
(print (string "Route file src/routes/" file ".janet does not exist.")))))

Expand Down

0 comments on commit 7a83be7

Please sign in to comment.