-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnpmr
executable file
·94 lines (79 loc) · 1.81 KB
/
npmr
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/sh
# Exit on error
# TODO: Re-enable Exit on error when this sourcing NVM doesn't return non-0 exit status
# set -o errexit
red=$(tput setaf 1)
green=$(tput setaf 2)
bold=$(tput bold)
normal=$(tput sgr0)
nvm_file_path=~/.nvm/nvm.sh
usage() {
echo """
recursive npm
Runs the given NPM command recursively in all the Git repositories being NPM
modules (ie. they have a package.json file at their root) located under the
current directory.
Usage:
$ cd ~/version/project-group
$ $0 NPM-COMMAND
Examples:
$ $0 link
$ $0 install
""" 1>&2
}
checkArgs() {
if [ $# -lt 1 ]; then
usage
exit 3
fi
}
checkArgs $@
npm_command=$@
if ! which npm && [ ! -r $nvm_file_path ] ; then
echo "This script requires either npm or nvm"
exit 1
fi
if [ -r $nvm_file_path ]; then
. $nvm_file_path
nvmautouse() {
if [ -r .nvmrc ]; then
nvm use
else
printf "$red"
printf "Using the last loaded version of Node.js"
printf "$normal"
printf "\n"
fi
}
else
nvmautouse() {
# nvm is not present
return
}
fi
# Store current working directory
cwd=$(pwd)
# find won't descend into .git directories, but it still does descend into git
# repositories, looking for other .git folders. And that could be a costly
# operation.
for dotgit_dir_path in $(find . -name .git -type d -prune)
do
# Restore current working directory for successive run
cd "$cwd"
# Change to the Git repository directory
cd "$dotgit_dir_path/.."
if [ ! -f 'package.json' ]
then
# This is not an NPM module
continue
fi
printf "\n"
printf "$bold"
printf "$(pwd)"
printf "$normal"
printf " ← npm $npm_command"
printf "\n"
nvmautouse
npm $npm_command
printf "\n"
done