Add tempfile and with-tempfile to CLJS.

We already have a nice library like this for CLJ (tempfile); this builds
the same thing for CLJS, using Node.js's tmp.
This commit is contained in:
Nick Alexander 2016-07-11 21:57:40 -07:00
parent 0a312b4f40
commit d5cfbeaa45
4 changed files with 33 additions and 1 deletions

View file

@ -13,6 +13,7 @@
"ws": "1.1.1"
},
"devDependencies": {
"tmp": "0.0.28"
},
"repository": {
"type": "git",

View file

@ -41,7 +41,8 @@
}
:profiles {:dev {:dependencies [[com.cemerick/piggieback "0.2.1"]
[org.clojure/tools.nrepl "0.2.10"]]
[org.clojure/tools.nrepl "0.2.10"]
[tempfile "0.2.0"]]
:repl-options {:nrepl-middleware [cemerick.piggieback/wrap-cljs-repl]}
:plugins [[lein-cljsbuild "1.1.2"]
[lein-doo "0.1.6"]]

View file

@ -0,0 +1,12 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
(ns datomish.node-tempfile
(:require
[cljs.nodejs :as nodejs]))
(def tmp (nodejs/require "tmp"))
(defn tempfile []
(.fileSync tmp)) ;; Cleaned up on process exit.

View file

@ -0,0 +1,18 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
(ns datomish.node-tempfile-macros)
(defmacro with-tempfile
"Uses a tempfile for some content and delete it immediately"
[bindings & body]
(cond
(= (count bindings) 0) `(do ~@body)
(symbol? (bindings 0)) `(let ~(subvec bindings 0 2)
(try
(with-tempfile ~(subvec bindings 2) ~@body)
(finally
(.removeCallback ~(bindings 0))))) ;; See Node.js tmp module.
:else (throw (java.lang.IllegalArgumentException.
"with-tempfile only allows Symbols in bindings"))))