Skip to content

Commit c03e429

Browse files
committed
feat-dev: auto-npm i with branches
\### Rationale Lower change for flaws and weird errors with old dependencies or missing dependencies after a change of branch.
1 parent b550ccc commit c03e429

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

.husky/post-checkout

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
4+
LOG_PREFIX="[.husky/post-checkout]"
5+
6+
. "$(dirname "$0")/hook-utils.sh"
7+
8+
HEAD_PREV="$1"
9+
HEAD_NEW="$2"
10+
CKOUT_TYPE_FLAG="$3" # 0 = retrieve file from index, 1 = changing branches
11+
12+
# check if this is a post-checkout after a `git clone`
13+
IS_CLONING=$( [ -z "$HEAD_PREV" ] && echo true || echo false )
14+
15+
update_npm_dependencies() {
16+
# Only run if chaning branches and not performing the initial `git clone`
17+
if [ $CKOUT_TYPE_FLAG == 1 ] && [ $IS_CLONING == false ]; then
18+
local changed_files=""
19+
# derived from https://gist.github.com/taurus227/28960de89e6c43bb3d492125368f1224
20+
changed_files="$(git diff-tree -r --name-only --no-commit-id $HEAD_PREV $HEAD_NEW)"
21+
22+
if echo "$changed_files" | grep --quiet "package-lock.json"; then
23+
log "CHANGE DETECTED: 'package-lock.json'"
24+
log "Dependency requirements changed! This will take a few seconds..."
25+
26+
local cmd="npm install --prefer-offline"
27+
log "$> $cmd"
28+
eval "$cmd"
29+
fi
30+
fi
31+
}
32+
33+
update_npm_dependencies

.husky/post-merge

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/bin/sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
4+
[ -z "$LOG_PREFIX" ] && LOG_PREFIX="[.husky/post-merge]"
5+
6+
. "$(dirname "$0")/hook-utils.sh"
7+
8+
update_npm_dependencies() {
9+
# derived from https://gist.github.com/taurus227/28960de89e6c43bb3d492125368f1224
10+
local changed_files=""
11+
changed_files="$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD)"
12+
13+
local NEEDS_INSTALL=true
14+
15+
if echo -e "$changed_files" | grep --quiet "package-lock.json"; then
16+
log "CHANGE DETECTED: 'package-lock.json'"
17+
log "Lock files rarely merge properly. Deleting and re-generating it..."
18+
19+
local output="$changed_files"
20+
output="$(echo -e "$output" | grep "package-lock.json")"
21+
local lockfiles=("$output") # make array of filepaths
22+
rm ${lockfiles[@]}
23+
24+
elif echo -e "$changed_files" | grep --quiet "package.json"; then
25+
log "CHANGE DETECTED: 'package.json'"
26+
log "Dependencies might of changed, lets make sure we are g2g!"
27+
else
28+
NEEDS_INSTALL=false
29+
fi
30+
31+
if [ "$NEEDS_INSTALL" == true ]; then
32+
cmd="npm install"
33+
log "$> $cmd"
34+
eval "$cmd"
35+
36+
changed_files="$(git diff --name-only --diff-filter=d)"
37+
if echo -e "$changed_files" | grep --quiet "package-lock.json"; then
38+
log "ALL FIXED! package-lock.json needed a refresh."
39+
40+
local lockfiles=($(echo -e "$changed_files" | grep "package-lock.json"))
41+
git add -- "${lockfiles[@]}"
42+
43+
for lockfile in "${lockfiles[@]}"; do
44+
log "STAGED: $lockfile"
45+
done
46+
47+
local filenoun="file"
48+
if [ "${#lockfiles[@]}" -gt 1 ]; then
49+
filenoun="${filenoun}s"
50+
fi
51+
log "Please commit the newly generated $filenoun for the team!"
52+
fi
53+
fi
54+
}
55+
56+
update_npm_dependencies

.husky/post-rewrite

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
4+
CALLER_CMD="$1" # rebase || amend
5+
export LOG_PREFIX="[.husky/post-rewrite]"
6+
7+
. "$(dirname "$0")/hook-utils.sh"
8+
9+
# derived from https://gist.github.com/taurus227/28960de89e6c43bb3d492125368f1224
10+
if [ "$CALLER_CMD" == "rebase" ]; then
11+
log "DETECTED: git-rebase.";
12+
$(dirname "$0")/post-merge
13+
fi

0 commit comments

Comments
 (0)