From 6a898bf8c70ddb53b8ba36d1c6b8a9d6756e1397 Mon Sep 17 00:00:00 2001 From: Brandon Olivier Date: Fri, 31 Mar 2023 14:34:35 -0500 Subject: [PATCH] Add `nbb.core/invoked-script` fn (#317) --- CHANGELOG.md | 4 ++++ script/nbb_tests.clj | 17 +++++++++++++++++ src/nbb/api.cljs | 1 + src/nbb/core.cljs | 7 +++++++ test-scripts/invoked-file-test/nbb.edn | 1 + test-scripts/invoked-file-test/src/core.cljs | 8 ++++++++ test-scripts/invoked-file-test/src/script.cljs | 7 +++++++ 7 files changed, 45 insertions(+) create mode 100644 test-scripts/invoked-file-test/nbb.edn create mode 100644 test-scripts/invoked-file-test/src/core.cljs create mode 100644 test-scripts/invoked-file-test/src/script.cljs diff --git a/CHANGELOG.md b/CHANGELOG.md index cdbcfd3c..766da541 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/script/nbb_tests.clj b/script/nbb_tests.clj index 3ad397f3..698ee6d1 100644 --- a/script/nbb_tests.clj +++ b/script/nbb_tests.clj @@ -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" diff --git a/src/nbb/api.cljs b/src/nbb/api.cljs index 1ed2379d..8a3cbc10 100644 --- a/src/nbb/api.cljs +++ b/src/nbb/api.cljs @@ -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))))) diff --git a/src/nbb/core.cljs b/src/nbb/core.cljs index f676b661..1bd5cb44 100644 --- a/src/nbb/core.cljs +++ b/src/nbb/core.cljs @@ -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)) @@ -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)} diff --git a/test-scripts/invoked-file-test/nbb.edn b/test-scripts/invoked-file-test/nbb.edn new file mode 100644 index 00000000..ccd9a316 --- /dev/null +++ b/test-scripts/invoked-file-test/nbb.edn @@ -0,0 +1 @@ +{:paths ["src"]} diff --git a/test-scripts/invoked-file-test/src/core.cljs b/test-scripts/invoked-file-test/src/core.cljs new file mode 100644 index 00000000..eba27049 --- /dev/null +++ b/test-scripts/invoked-file-test/src/core.cljs @@ -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))) diff --git a/test-scripts/invoked-file-test/src/script.cljs b/test-scripts/invoked-file-test/src/script.cljs new file mode 100644 index 00000000..304927eb --- /dev/null +++ b/test-scripts/invoked-file-test/src/script.cljs @@ -0,0 +1,7 @@ +(ns script + (:require [nbb.core :refer [invoked-file *file*]])) + +(prn + (if (= *file* (invoked-file)) + :invoked + :not-invoked))