This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-07-23
Channels
- # babashka (1)
- # beginners (26)
- # clj-kondo (15)
- # cljdoc (2)
- # clojure (14)
- # clojure-europe (13)
- # clojure-norway (6)
- # conjure (2)
- # etaoin (4)
- # events (2)
- # expound (2)
- # fulcro (1)
- # hyperfiddle (6)
- # london-clojurians (2)
- # malli (3)
- # meander (10)
- # minecraft (3)
- # nbb (34)
- # off-topic (10)
- # pathom (13)
- # reagent (7)
- # vim (3)
is there a way to make a "self contained" script assuming that the person running it has node installed on their system?
i'm writing a helper script for a project that i'm putting in my project/scripts
dir. to keep things tidy, i'd like to have a single file like project/scripts/do-thing
that would (if necessary) install any npm libs necessary and then do the thing
i don't think i want to generate a binary with e.g. caxa since i want the artifact to be the source code
@lilactown There's two ways of doing this. Either you bundle nbb + your script via this: https://github.com/babashka/nbb/tree/main/doc/bundle or you publish your nbb project on npm: https://github.com/babashka/nbb/tree/main/doc/publish
There's also the option to bundle your project with caxa into a standalone executable: https://github.com/babashka/nbb/tree/main/doc/caxa but this will be more challenging if you want to support multiple operating systems, since this bundles your own Node.js
yeah like i said above, i don't want any artifacts other than the source code. i also don't want to deal with publishing
i want to point to some other developer and say, "go run this script i committed to the repo" and it Just Works(tm)
well, you asked for a standalone script, which is what bundle does. The other option is to use the npm approach, which "just works" with npx
, like e.g. npx clojure-quiz
here's the setup i have created right now:
project/
script/
do-thing
do-thing-src/
package.json
main.cljs
do-thing
is just
#!/bin/bash
cd script/do-thing-src
npm install
npx nbb -m main "$@"
and main.cljs
contains all the source code for the script. so now all another developer has to do is run ./script/do-thing arg1 arg2
and it will handle the restyeah that works. you could also make an index.mjs which imports nbb and then calls loadFile on your script
you could skip the npm install step by checking if .node_modules/nbb is already there
nbb can execute script/do-thing-src/main.cljs also from the root, it will pick up on the node_modules in that dir
i seem to remember there being a way that you could have fully self contained bb scripts (that would somehow specify their deps inline in the file, and download them) and was hoping to replicate that with nbb
what i'm curious about (and it sounds like there's no easy way to do this, and that's fine) is whether i can get rid of the do-thing
wrapper and even package.json
and have it specified in some other way e.g. at the top of the file
yes, bb has babashka.deps/add-deps and this calls deps.clj which then gives you a classpath which bb dynamically adds to its own classpath. you could do something similar in a self-bootstrapping index.mjs file (similar to your bash script)
I think your bash wrapper is fine though, you could just optimize it to not call npm install more than once and skip npx since it takes a while
Or you could use #!/usr/bin/env nbb
and ask your colleague to install nbb globally (usually it's best to keep npm things local though)
Running npx nbb
also works if you don't have it installed btw. npx will ask you to install it