Skip to content

Instantly share code, notes, and snippets.

@Olical
Created March 6, 2018 12:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Olical/7dc37e102b0ff38c05a0e9ee64f6428e to your computer and use it in GitHub Desktop.
Save Olical/7dc37e102b0ff38c05a0e9ee64f6428e to your computer and use it in GitHub Desktop.
ClojureScript (node) to discover namespaces in a directory
(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)))
{: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