-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnpm-r.sh
executable file
·48 lines (37 loc) · 934 Bytes
/
npm-r.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/bin/bash
# check that `jq` is installed
if ! type jq &> /dev/null; then
echo "[npm-r] missing dependency jq"
exit 1
fi
# confirm that a first argument was passed
if [ -z "$1" ]; then
echo "usage: npm-r <subcommand>"
exit 1
fi
# define the first argument as the subcommand
SUBCMD="$1"
# traverse the filesystem upward looking for package.json
while [ ! -f ./package.json ]
do
# if we get to root, give up
if [ "$PWD" == "/" ]; then
echo "[npm-r] unable to find package.json"
exit 1
fi
# keep looking
cd ..
done
# read the command from package.json
CMD=$(jq -r ".scripts.$SUBCMD" < package.json)
if [ "$CMD" == "null" ]; then
echo "[npm-r] no such subcommand '$1'"
exit 1
fi
# add additional arguments
CMD="$CMD ${@:2}"
echo -e '\033[37m$ '$CMD'\033[39m'
# add `./node_modules/.bin` to $PATH
PATH=$PATH:./node_modules/.bin
# exec the command
exec bash -c "$CMD"