Skip to content

Instantly share code, notes, and snippets.

@YurySolovyov
Created September 11, 2016 15:30
Show Gist options
  • Save YurySolovyov/17b566e5a4af8c1ae1629fe86461f242 to your computer and use it in GitHub Desktop.
Save YurySolovyov/17b566e5a4af8c1ae1629fe86461f242 to your computer and use it in GitHub Desktop.
(ns eion.directories.core
(:require-macros [cljs.core.async.macros :refer [go]])
(:require [cljs.core.async :refer [put! close! chan <! >!]]))
(def path (js/require "path"))
(def fs (js/require "fs"))
(def concurrency 4)
(defn handle-error [e]
(.warn js/console e))
(defn read-directory [dir-path]
(let [out (chan)]
(.readdir fs dir-path
(fn [err items]
(put! out (if err err (js->clj items)))
(close! out)))
out))
(defn stat-file [file-path]
(let [out (chan)]
(.stat fs file-path
(fn [err stat]
(put! out (if err err (js->clj stat)))
(.log js/console stat)
(close! out)))
out))
(defn map-items-to-stats [items]
(go
(let [stats (chan)]
(doseq [item items]
(put! stats (<! (stat-file item))))
stats
)))
(defn directory-contents [dir-path]
(go
(let [res (<! (read-directory dir-path))]
(if (instance? js/Error res)
(handle-error res)
(into (vector) (<! (map-items-to-stats res)))
))))
(defn init-directory [dir-path]
(go
(let [items (<! (directory-contents dir-path))]
(.log js/console items)
)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment