Fork me on GitHub
#nbb
<
2022-07-23
>
borkdude11:07:16

If you haven't listened, recommended!

1
👍 2
🙏 1
lilactown17:07:27

is there a way to make a "self contained" script assuming that the person running it has node installed on their system?

lilactown17:07:50

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

lilactown17:07:56

(because ultimately i want to be able to do ./scripts/do-thing arg1 arg2)

lilactown17:07:42

i don't think i want to generate a binary with e.g. caxa since i want the artifact to be the source code

borkdude18:07:29

@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

borkdude18:07:37

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

lilactown18:07:43

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

borkdude18:07:15

then the first option should work: bundle

lilactown18:07:47

i still have to run the bundle command first, right?

lilactown18:07:35

that doesn't help me any more than just running nbb itself

lilactown18:07:16

i want to point to some other developer and say, "go run this script i committed to the repo" and it Just Works(tm)

borkdude18:07:12

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

👍 1
🙂 1
lilactown18:07:48

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 rest

borkdude18:07:47

yeah that works. you could also make an index.mjs which imports nbb and then calls loadFile on your script

borkdude18:07:36

you could skip the npm install step by checking if .node_modules/nbb is already there

borkdude18:07:26

also node_modules/.bin/nbb is faster to invoke than via npx

borkdude18:07:53

minor enhancements ;)

borkdude18:07:42

nbb can execute script/do-thing-src/main.cljs also from the root, it will pick up on the node_modules in that dir

borkdude18:07:58

but of course you need to execute npm install from inside that dir

lilactown18:07:27

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

lilactown18:07:50

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

borkdude18:07:29

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)

borkdude18:07:31

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

borkdude18:07:12

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)

lilactown18:07:36

yeah i'm not sure i can ask everyone who might run this to have nbb installed

borkdude18:07:30

Running npx nbb also works if you don't have it installed btw. npx will ask you to install it

borkdude18:07:58

maybe the least cumbersome will be to have a package.json in the root of your project, and then nbb script/main.cljs in "scripts" of your package.json so you can do npm run do-thing

borkdude18:07:53

nbb could of course just be in your devDependencies

lilactown18:07:04

hmm ok i'll play with it. i have a few other npm libs that the script depends on

lilactown18:07:22

thanks for the help!

👍 1
borkdude18:07:04

those npm libs can also be in devDependencies