How do I read an EDN file from ClojureScript running on NodeJS?
Asked Answered
C

2

6

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?

Cheerless answered 20/6, 2017 at 19:21 Comment(1)
AFAIK, it should be possible: github.com/logseq/logseq/blob/…Pastime
S
1

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")


Sneaker answered 1/6, 2023 at 12:38 Comment(0)
R
6

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.

Readytowear answered 21/6, 2017 at 7:54 Comment(0)
S
1

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")


Sneaker answered 1/6, 2023 at 12:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.