Skip to content

Commit

Permalink
Add nbb.core/invoked-script fn (babashka#317)
Browse files Browse the repository at this point in the history
  • Loading branch information
bolivier authored Mar 31, 2023
1 parent 760493f commit 6a898bf
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ For a list of breaking changes, check [here](#breaking-changes).

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

## 1.2.172

- [#95](https://github.com/babashka/nbb/issues/95): add `invoked-file`

## 1.2.171

- [#315](https://github.com/babashka/nbb/issues/315): support `:init` option in repl api
Expand Down
17 changes: 17 additions & 0 deletions script/nbb_tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,23 @@
(is (thrown? Exception
(nbb {:dir "test-scripts/paths-test"} "src/project_dir_not_on_classpath.cljs")))))

(deftest invoked-file-test
(testing "calling as a script"
(is (= :invoked
(nbb
{:dir "test-scripts/invoked-file-test"}
"src/script.cljs"))))
(testing "calling with -m"
(is (= :not-invoked
(nbb {:dir "test-scripts/invoked-file-test"}
"-m" "core"))))
(testing "calling with -e"
(is (= :not-invoked
(nbb "-e" "(require '[nbb.core :refer [*file* invoked-file]])
(if (and (some? *file*) (= *file* (invoked-file)))
:invoked
:not-invoked)")))))

(deftest medley-test
(let [deps '{medley/medley {:git/url "https://github.com/weavejester/medley"
:git/tag "1.4.0"
Expand Down
1 change: 1 addition & 0 deletions src/nbb/api.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@

(defn loadFile [script]
(let [script-path (path/resolve script)]
(reset! nbb/-invoked-file script-path)
(-> (initialize script-path nil)
(.then #(nbb/load-file script-path)))))

Expand Down
7 changes: 7 additions & 0 deletions src/nbb/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,12 @@
([e opts]
(nbb.error/print-error-report e opts)))

(def -invoked-file (atom nil))
(defn invoked-file
"Return an absolute path for the file where nbb was invoked"
[]
@-invoked-file)

(def sns
(sci/create-ns 'sci.core nil))

Expand Down Expand Up @@ -619,6 +625,7 @@
'alter-var-root (sci/copy-var sci/alter-var-root nbb-ns)
'slurp (sci/copy-var slurp nbb-ns)
'*file* sci/file
'invoked-file invoked-file
'version (sci/copy-var version nbb-ns)
'await (sci/copy-var await nbb-ns)
'time (sci/copy-var time* nbb-ns)}
Expand Down
1 change: 1 addition & 0 deletions test-scripts/invoked-file-test/nbb.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{:paths ["src"]}
8 changes: 8 additions & 0 deletions test-scripts/invoked-file-test/src/core.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(ns core
(:require [nbb.core :refer [invoked-file *file*]]))

(defn -main []
(prn (if (and (some? *file*)
(= *file* (invoked-file)))
:invoked
:not-invoked)))
7 changes: 7 additions & 0 deletions test-scripts/invoked-file-test/src/script.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(ns script
(:require [nbb.core :refer [invoked-file *file*]]))

(prn
(if (= *file* (invoked-file))
:invoked
:not-invoked))

0 comments on commit 6a898bf

Please sign in to comment.