From d5cfbeaa45b7dae40a4307de1f02ece0d2681781 Mon Sep 17 00:00:00 2001 From: Nick Alexander Date: Mon, 11 Jul 2016 21:57:40 -0700 Subject: [PATCH] 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. --- package.json | 1 + project.clj | 3 ++- src/datomish/node_tempfile.cljs | 12 ++++++++++++ src/datomish/node_tempfile_macros.cljc | 18 ++++++++++++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/datomish/node_tempfile.cljs create mode 100644 src/datomish/node_tempfile_macros.cljc diff --git a/package.json b/package.json index 5f64ee8c..42c61bf3 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "ws": "1.1.1" }, "devDependencies": { + "tmp": "0.0.28" }, "repository": { "type": "git", diff --git a/project.clj b/project.clj index d3bad6df..12ba1dfd 100644 --- a/project.clj +++ b/project.clj @@ -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"]] diff --git a/src/datomish/node_tempfile.cljs b/src/datomish/node_tempfile.cljs new file mode 100644 index 00000000..a0ede232 --- /dev/null +++ b/src/datomish/node_tempfile.cljs @@ -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. diff --git a/src/datomish/node_tempfile_macros.cljc b/src/datomish/node_tempfile_macros.cljc new file mode 100644 index 00000000..0fa10446 --- /dev/null +++ b/src/datomish/node_tempfile_macros.cljc @@ -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"))))