I’m writing a bash script intended to be run via
npx. The script automates the process of setting up a “starter site” for me – runs snowpack’s version of CRA, sets up tailwind, puts some config files and a snippets file in place. From this script, I’d like to be able to directly access all the files in the NPM published tarball, so that I can copy them (e.g. config files, snippets, etc) to the right place.
I spent a lot of time on google trying to find a good way to accomplish this, but I gave up, and figured out a not-good way of accomplishing this (via the special bash variable
$0). See the script in its entirety
here by following the link to my project in the first paragraph:
# If we're running locally, $BASE is CWD: BASE="$(dirname "$0")" # If we're running via npx, $BASE # is $CWD/../lib/node_modules/[package name]: if [ ! -f "$BASE"/package.json ]; then BASE="$(dirname "$BASE")"/lib/node_modules/"$(basename "$0")" fi # Make sure we have absolute path BASE="$(cd "$BASE" && pwd)"
The idea is, at least on my mac, when I run npx, it seems to unpack the tarball under
and the main file indicated by
package.json is copied to and invoked via
But this is fragile, given
- I can’t find this ~npx/lib/bin directory scheme documented anywhere
- The relative locations of the running bin and the rest of the files changes based on whether I’m running the file locally or via
The script works just fine in its current form, but, can anyone tell me a more sane way to refer to the path of the extracted tarball contents of a running
npx script/package, from inside the running script?
Edit: I had a pretty reasonable four or so inline links for ease of reading/reference, but then am told “Sorry, new users can only put 2 links in a post.”