From 80095ceed3fc9ad989705ccdb79114518d6e9a8b Mon Sep 17 00:00:00 2001 From: John Wiseman Date: Mon, 25 May 2020 15:36:38 -0700 Subject: [PATCH] Added edn2json. --- shadow-cljs.edn | 6 ++++- src/main/lemondronor/edn2json.cljs | 39 ++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/main/lemondronor/edn2json.cljs diff --git a/shadow-cljs.edn b/shadow-cljs.edn index fe57cb7..439ba08 100644 --- a/shadow-cljs.edn +++ b/shadow-cljs.edn @@ -17,4 +17,8 @@ {:target :node-test :output-to "out/node-tests.js" ;;:ns-regexp "-spec$" - :autorun true}}} + :autorun true} + :edn2json + {:target :node-script + :main lemondronor.edn2json/main + :output-to "out/edn2json.js"}}} diff --git a/src/main/lemondronor/edn2json.cljs b/src/main/lemondronor/edn2json.cljs new file mode 100644 index 0000000..be1b368 --- /dev/null +++ b/src/main/lemondronor/edn2json.cljs @@ -0,0 +1,39 @@ +(ns lemondronor.edn2json + (:require + ["fs" :as fs] + [kitchen-async.promise :as p] + [cljs.reader :as reader] + [fipp.edn :as fippedn])) + +(def fs-promises (.-promises fs)) + + +(defn read-file [path options] + (.readFile fs-promises path (clj->js options))) + + +(defn write-file [path data options] + (.writeFile fs-promises path data (clj->js options))) + + +(defn read-file-json [path] + (p/let [json-str (read-file path {:encoding "utf-8"}) + db (js->clj (.parse js/JSON json-str))] + db)) + +(defn write-file-json [db path] + (write-file path (.stringify js/JSON (clj->js db) nil " ") {:encoding "utf-8"})) + +(defn read-file-edn [path] + (p/let [edn-str (read-file path {:encoding "utf-8"}) + db (reader/read-string edn-str)] + db)) + + +(defn now [] + (.getTime (js/Date.))) + + +(defn main [& args] + (p/let [db (read-file-edn (nth args 0)) + done (write-file-json db (nth args 1))]))