Skip to content

Commit 8d55832

Browse files
committed
fix(gitutils): 🐛 correct install-plugins in commit hooks
1 parent ffa8a1e commit 8d55832

3 files changed

Lines changed: 49 additions & 24 deletions

File tree

src/githooks/_commit-msg.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ if [ -t 1 ]; then
77
fi
88

99
# Install commitizen plugins
10-
git hook run install-plugins -- '.commitlint.extends//""'
10+
git hook run install-plugins -- '[.config.commitizen.path // "", .commitlint.extends // ""]'
1111

1212
# Apply commitlint rules to the latest commit message
1313
zz_log i "Applying commitlint rules to the latest commit..."

src/githooks/_install-plugins.sh

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
#!/bin/sh
22

3-
json_key=$1
4-
json_file=${2:-./package.json}
3+
# Function to print help and manage arguments
4+
5+
eval $(
6+
zz_args "Install npm plugins from package.json configuration" $0 "$@" <<-help
7+
f file json_file Package.json file path (default: ./package.json)
8+
- key json_key JSON key path to extract plugins from
9+
help
10+
)
11+
12+
# Set defaults if not provided
13+
json_file=${json_file:-./package.json}
14+
15+
if [ -z "$json_key" ]; then
16+
zz_log e "JSON key is required."
17+
exit 1
18+
fi
519

620
zz_log i "Using file {B $json_file}..."
7-
plugins=$(jq -r "$1" "$json_file" | tr -d "'[]:,\"")
21+
plugins=$(jq -r "$json_key" "$json_file" | tr -d "'[]:,\"")
822

923
if [ -z "$plugins" ]; then
1024
zz_log w "No plugins found at key {B $json_key} in {U $json_file}"
@@ -14,30 +28,39 @@ fi
1428
# Load the config file
1529
config=$(dirname $0)/.ci-plugins
1630

31+
# if config file only contains comments and empty lines, make it empty
32+
if [ -f "$config" ] && [ -z "$(grep -v -e '^#' -e '^$' $config)" ]; then
33+
zz_log w "Config file {B $config} is empty or contains only comments, reset it."
34+
cat /dev/null >$config
35+
fi
36+
37+
# Create the config file if it doesn't exist
38+
if [ ! -f "$config" ]; then
39+
zz_log w "Config file {B $config} does not exist, create it."
40+
touch $config
41+
fi
42+
1743
# foreach plugin, check if it is already installed
1844
for plugin in $plugins; do
19-
20-
if ! npm list $plugin 2>/dev/null 1>&2; then
21-
# Save the plugins to a config file
22-
zz_log i "Adding plugin {B $plugin} to list of CI dependencies ..."
23-
echo "$plugin" | sed 's/^ *//;s/ *$//' | grep -v --file=$config >>$config
24-
fi
45+
echo "$plugin" | sed 's/^ *//;s/ *$//' | grep -v --file=$config >>$config
2546
done
2647

2748
# Reload the plugins list
2849
plugins=$(cat $config | grep -v '^$' | tr '\n' ' ')
2950

30-
# If no plugins are left, exit
31-
if [ -z "$plugins" ]; then
32-
zz_log w "No plugins to install!"
33-
exit 0
34-
fi
35-
36-
# Install the plugins
37-
zz_log i "Installing plugins {B $plugins} ..."
38-
if ! npm install --no-save $plugins 2>/dev/null 1>&2; then
39-
zz_log e "Failed to install one of plugins {B $plugins}!"
40-
exit 1
41-
fi
51+
# For each plugin, check if it is already installed
52+
for plugin in $plugins; do
53+
if npm list --depth=0 | grep -q "$plugin@"; then
54+
plugins=$(echo $plugins | sed "s#$plugin##g" | tr -s ' ')
55+
fi
56+
done
4257

43-
zz_log s "Plugins {B $plugins} installed successfully!"
58+
# Install the plugins if there are any to install
59+
if [ -n "$plugins" ]; then
60+
zz_log i "Installing plugins {B $plugins} ..."
61+
if ! npm install --no-save $plugins 2>/dev/null 1>&2; then
62+
zz_log e "Failed to install one of plugins {B $plugins}!"
63+
exit 1
64+
fi
65+
zz_log s "Plugins {B $plugins} installed successfully!"
66+
fi

src/githooks/_prepare-commit-msg.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ if [ -t 1 ]; then
88
fi
99

1010
# Install commitizen plugins
11-
git hook run install-plugins -- '.config.commitizen.path//""'
11+
git hook run install-plugins -- '[.config.commitizen.path // "", .commitlint.extends // ""]'
12+
13+
1214

1315
# Edit commit message
1416
if [ $(grep -cv -e '^#' -e '^$' .git/COMMIT_EDITMSG) -eq 0 ]; then

0 commit comments

Comments
 (0)