Skip to content

Commit d33d4ba

Browse files
committed
Add babashka.fs as built-in library, bump babashka.cli
babashka.fs compiles to CLJS/Node via reader conditionals. Register it as a lazily-loaded module (nbb_fs), copying its public vars into SCI. Its with-temp-dir macro is a compile-time macro that copy-ns cannot copy as a runtime var, so reinterpret it as an SCI macro over create-temp-dir and delete-tree. Bump babashka.cli 0.8.60 -> 0.12.75.
1 parent 05c5ad0 commit d33d4ba

6 files changed

Lines changed: 65 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ For a list of breaking changes, check [here](#breaking-changes).
44

55
[Nbb](https://github.com/babashka/nbb): Scripting in Clojure on Node.js using [SCI](https://github.com/babashka/sci)
66

7+
## Unreleased
8+
9+
- Add `babashka.fs` as a built-in library, including the `with-temp-dir` macro
10+
- Bump `babashka.cli` to 0.12.75
11+
712
## 1.4.209 (2026-07-12)
813

914
- Bump SCI to 0.14.55. Support implementing CLJS protocols (e.g. `ILookup`, etc) on `deftype` and `defrecord`

deps.edn

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
com.cognitect/transit-cljs {:mvn/version "0.8.280"}
2222
#_#_prismatic/schema {:mvn/version "1.3.0"}
2323
#_#_metosin/malli {:mvn/version "0.8.8"}
24-
org.babashka/cli {:mvn/version "0.8.60"}
24+
org.babashka/cli {:mvn/version "0.12.75"}
25+
babashka/fs {:mvn/version "0.5.34"}
2526
#_{:local/root "/Users/borkdude/dev/babashka/sci"}
2627
io.github.babashka/sci.configs #_{:local/root "../sci.configs"}
2728
{:git/url "https://github.com/babashka/sci.configs"

shadow-cljs.edn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
:depends-on #{:nbb_core}}
4747
:nbb_tools_cli {:init-fn nbb.impl.tools-cli/init
4848
:depends-on #{:nbb_core :nbb_goog_string}}
49+
:nbb_fs {:init-fn nbb.impl.fs/init
50+
:depends-on #{:nbb_core}}
4951
:nbb_transit {:init-fn nbb.impl.transit/init
5052
:depends-on #{:nbb_core}}
5153
:nbb_nrepl_server {:init-fn nbb.impl.nrepl-server/init

src/nbb/impl/fs.cljs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
(ns nbb.impl.fs
2+
{:no-doc true}
3+
(:require [babashka.fs]
4+
[nbb.core :as nbb]
5+
[sci.core :as sci]))
6+
7+
(def fns (sci/create-ns 'babashka.fs nil))
8+
9+
;; babashka.fs/with-temp-dir is a compile-time macro, so copy-ns can't copy it as
10+
;; a runtime var. Reinterpret it here as an SCI macro that expands to the copied
11+
;; create-temp-dir / delete-tree vars.
12+
(defn ^:macro with-temp-dir
13+
"Evaluates body with `temp-dir` bound to the result of `(create-temp-dir opts)`.
14+
15+
By default, the `temp-dir` will be removed with `delete-tree` on exit from the
16+
scope.
17+
18+
Options:
19+
* see `create-temp-dir` for options that control directory creation
20+
* `:keep` - if `true` does not delete the directory on exit from macro scope."
21+
[_ _ [temp-dir opts & more] & body]
22+
(assert (empty? more) "with-temp-dir binding takes at most 2 forms")
23+
(assert (symbol? temp-dir) "with-temp-dir needs a symbol to bind")
24+
`(let [opts# ~opts
25+
~temp-dir (babashka.fs/create-temp-dir opts#)]
26+
(try
27+
~@body
28+
(finally
29+
(when-not (:keep opts#)
30+
(babashka.fs/delete-tree ~temp-dir {:force true}))))))
31+
32+
(def fs-namespace
33+
(assoc (sci/copy-ns babashka.fs fns {:exclude [with-temp-dir]})
34+
'with-temp-dir (sci/copy-var with-temp-dir fns)))
35+
36+
(defn init []
37+
(nbb/register-plugin!
38+
::fs
39+
{:namespaces {'babashka.fs fs-namespace}}))

src/nbb/macros.clj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
clojure.test "./nbb_test.js"
4040
nbb.repl "./nbb_repl.js"
4141
clojure.tools.cli "./nbb_tools_cli.js"
42+
babashka.fs "./nbb_fs.js"
4243
goog.string "./nbb_goog_string.js"
4344
goog.string.format "./nbb_goog_string.js"
4445
goog.crypt "./nbb_goog_crypt.js"

test/nbb/main_test.cljs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,22 @@
109109
(.then (fn [v]
110110
(is (= :hello v))))))
111111

112+
(deftest-async babashka-fs-test
113+
(-> (nbb/load-string "(require '[babashka.fs :as bfs])
114+
[(bfs/exists? \"deps.edn\") (bfs/file-name \"/a/b.txt\")]")
115+
(.then (fn [v]
116+
(is (= [true "b.txt"] v))))))
117+
118+
(deftest-async babashka-fs-with-temp-dir-test
119+
(-> (nbb/load-string "(require '[babashka.fs :as bfs])
120+
(let [captured (atom nil)]
121+
(bfs/with-temp-dir [d]
122+
(reset! captured (str d))
123+
{:inside (bfs/exists? d)})
124+
{:inside true :after (bfs/exists? @captured)})")
125+
(.then (fn [v]
126+
(is (= {:inside true :after false} v))))))
127+
112128
(deftest-async as-alias
113129
(-> (nbb/load-string "(require '[rando.ns :as-alias dude]) ::dude/foo")
114130
(.then (fn [v]

0 commit comments

Comments
 (0)