Skip to content

Instantly share code, notes, and snippets.

@mearns
Created January 8, 2020 16:00
Show Gist options
  • Save mearns/06e21f6e9ee3d7b199ee6a4ff2b4b832 to your computer and use it in GitHub Desktop.
Save mearns/06e21f6e9ee3d7b199ee6a4ff2b4b832 to your computer and use it in GitHub Desktop.

Say you have an npm project with a start script in package.json. You can run this and pass commands to it like:

npm start --silent -- ARG1 ARG2 ARG3

(using --silent because you probably don't want to see NPM's boilerplate error message if your script fails).

But that's a little tedious to add all those extra bits, and for people trying to use your tool who aren't real familiar with npm, they might not know to -- before the script arguments. So just drop this bash script into the root of your directory instead, and then you can run it like:

./npm-start.bash ARG1 ARG2 ARG3

I recommend you rename the bash script to the name of your project/tool.

And you don't even have to run the script from the same directory!

#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
if [ ! -d "$DIR/node_modules" ]
then
npm --prefix "$DIR" install
fi
npm --prefix "$DIR" --silent start -- $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment