最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I read an EDN file from ClojureScript running on NodeJS? - Stack Overflow

programmeradmin5浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 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.

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


发布评论

评论列表(0)

  1. 暂无评论