Reputation: 841
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?
Upvotes: 6
Views: 3235
Reputation: 145
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")
Upvotes: 1
Reputation: 3418
You 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
.
Upvotes: 6