Skip to content

Commit 5918e5d

Browse files
authored
Robustify Git-Hooks for use with GitHub Desktop (#499)
git: try to make pre-push hook more robust with sourcing npm
1 parent 5306718 commit 5918e5d

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

.githooks/pre-push

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,30 @@
22

33
set -eu
44

5-
if ! [ -x "$(command -v npm)" ]; then
6-
echo 'Error: npm not found. Make it available to the host shell (e.g., with "nvm use --lts").'
7-
exit 2
5+
NPM_CMD="npm"
6+
7+
if ! (type $NPM_CMD >> /dev/null); then
8+
echo "npm not found, trying to make it available using nvm..."
9+
if type nvm >> /dev/null; then
10+
echo "nvm found, using it to install the latest lts node"
11+
nvm use --lts
12+
else
13+
echo "nvm not found, trying to make it available using the nvm.sh"
14+
# try to make it available based on https://github.com/typicode/husky/issues/912#issuecomment-817522060
15+
export NVM_DIR="$HOME/.nvm/nvm.sh"
16+
. "$(dirname $NVM_DIR)/nvm.sh"
17+
18+
export NVM_DIR="$HOME/.nvm"
19+
a=$(nvm ls --no-colors | grep 'node')
20+
v=$(echo "$a" | sed -E 's/.*\(-> ([^ ]+).*/\1/')
21+
22+
export PATH="$NVM_DIR/versions/node/$v/bin:$PATH"
23+
24+
if ! (type $NPM_CMD >> /dev/null); then
25+
echo "no variant of npm or nvm found, trying to use the npm.cmd"
26+
NPM_CMD="npm.cmd"
27+
fi
28+
fi
829
fi
930

1031

@@ -37,7 +58,7 @@ if [ -n "$(git status --porcelain)" ]; then
3758
fi
3859

3960
echo "Linting project (local mode)..."
40-
npm run lint-local
61+
$NPM_CMD run lint-local
4162

4263

4364
# shellcheck disable=SC2124 # we want the argument splitting

0 commit comments

Comments
 (0)