I have a simple data file in EDN format I need to read in a ClojureScript cli app running on NodeJS, but none of the relevant core libraries from Clojure seem to be available (core.java.io/read, clojure.edn/read, etc.)
What should I be using instead?
I have a simple data file in EDN format I need to read in a ClojureScript cli app running on NodeJS, but none of the relevant core libraries from Clojure seem to be available (core.java.io/read, clojure.edn/read, etc.)
What should I be using instead?
Share Improve this question asked Jun 20, 2017 at 19:21 shadershader 8411 gold badge7 silver badges26 bronze badges 1- AFAIK, it should be possible: github./logseq/logseq/blob/… – Trylks Commented Apr 16, 2022 at 21:34
2 Answers
Reset to default 6You could use:
(ns app.core
(:require [cljs.reader :as reader]))
(def fs (js/require "fs"))
(defn read-edn [path f]
(.readFile fs path "utf8" (fn [err data] (f (reader/read-string data)))))
(defn process [coll])
(read-edn "/tmp/x.clj" process)
In the example above, process
would receive the data structure that was read from the file. You would need to implement process
and add error handling to read-edn
.
Or even easier use readFileSync
(shadow-cljs
example):
(require '["fs" :as fs]
'[cljs.reader :as reader])
(defn read-edn [path]
(-> (.readFileSync fs path "utf8")
reader/read-string))
(read-edn "/xxx/yyy/zzz/my-data.edn")