Created
March 6, 2018 12:17
-
-
Save Olical/7dc37e102b0ff38c05a0e9ee64f6428e to your computer and use it in GitHub Desktop.
ClojureScript (node) to discover namespaces in a directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns cljs-namespace-tools | |
(:require [clojure.tools.namespace.parse :as parse] | |
[cljs.tools.reader.reader-types :as reader-types] | |
[cljs-node-io.core :as io] | |
[cljs-node-io.fs :as fs] | |
[cljs.test :as t])) | |
(defn path-kind [path] | |
(cond | |
(fs/file? path) :file | |
(fs/dir? path) :dir)) | |
(defn ls [path] | |
(map #(str path "/" %) (fs/readdir path))) | |
(defn tree [path] | |
(loop [[dir & dirs] [path] | |
files []] | |
(if (nil? dir) | |
files | |
(let [{:keys [file dir]} (group-by path-kind (ls dir))] | |
(recur (concat dirs dir) (concat files file)))))) | |
(defn path->namespace-name [path] | |
(-> (io/slurp path) | |
(reader-types/string-push-back-reader) | |
(parse/read-ns-decl parse/cljs-read-opts) | |
(parse/name-from-ns-decl))) | |
(defn cljs-path? [path] | |
(boolean (re-find #"\.clj(s|c)$" path))) | |
(defn ns-tree [path] | |
(->> (tree path) | |
(filter cljs-path?) | |
(map path->namespace-name))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{:deps | |
{org.clojure/tools.namespace {:mvn/version "0.3.0-alpha4"} | |
org.clojure/tools.reader {:mvn/version "1.2.1"} | |
cljs-node-io {:mvn/version "0.5.0"}}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment