Skip to content

Commit 5d7f93f

Browse files
committed
feat: ✨ add global installation option for npm plugins
1 parent 6cc450d commit 5d7f93f

4 files changed

Lines changed: 18 additions & 11 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 -- '[.config.commitizen.path // "", .commitlint.extends // ""]'
10+
git hook run install-plugins -- -g '[.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: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
eval $(
66
zz_args "Install npm plugins from package.json configuration" $0 "$@" <<-help
77
f file json_file Package.json file path (default: ./package.json)
8+
g - global Install plugins globally
89
- key json_key JSON key path to extract plugins from
910
help
1011
)
@@ -18,8 +19,7 @@ if [ -z "$json_key" ]; then
1819
fi
1920

2021
zz_log i "Using file {B $json_file}..."
21-
plugins=$(jq -r "$json_key" "$json_file" | tr -d "'[]:,\"")
22-
22+
plugins=$(jq -r "$json_key" "$json_file" | tr -d "'[]:,\"" |sort -u | tr '\n' ' ' | sed 's/ */ /g;s/^ *//;s/ *$//')
2323
if [ -z "$plugins" ]; then
2424
zz_log w "No plugins found at key {B $json_key} in {U $json_file}"
2525
exit 0
@@ -40,25 +40,34 @@ if [ ! -f "$config" ]; then
4040
touch $config
4141
fi
4242

43-
# foreach plugin, check if it is already installed
43+
# List installed plugins from the config file
44+
declared=$(grep -v '^#' $config | tr '\n' ' ')
45+
zz_log i "Currently declared plugins: {B $declared}"
46+
zz_log i "Currently required plugins: {B $plugins}"
47+
48+
# foreach plugin, if it is not already in the config file, add it to the config file
4449
for plugin in $plugins; do
4550
echo "$plugin" | sed 's/^ *//;s/ *$//' | grep -v --file=$config >>$config
4651
done
4752

4853
# Reload the plugins list
4954
plugins=$(cat $config | grep -v '^$' | tr '\n' ' ')
5055

56+
# List plugins to be installed
57+
zz_log i "Plugins to be installed: {B $plugins}"
58+
5159
# For each plugin, check if it is already installed
5260
for plugin in $plugins; do
53-
if npm list --depth=0 | grep -q "$plugin@"; then
61+
if npm list $global --depth=0 | grep -q "$plugin@"; then
5462
plugins=$(echo $plugins | sed "s#$plugin##g" | tr -s ' ')
63+
zz_log s "Plugin {B $plugin} is already installed, skipping."
5564
fi
5665
done
5766

5867
# Install the plugins if there are any to install
5968
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
69+
zz_log i "Installing missing plugins {B $plugins} ..."
70+
if ! npm install $global --no-save $plugins 2>/dev/null 1>&2; then
6271
zz_log e "Failed to install one of plugins {B $plugins}!"
6372
exit 1
6473
fi

src/githooks/_pre-commit.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ if git diff --name-only ${@:---cached} | grep -q "composer.json"; then
5959
fi
6060

6161
# Install Prettier plugins if they are not already installed
62-
git hook run install-plugins -- '.prettier.plugins//""'
62+
git hook run install-plugins -- -g '.prettier.plugins//""'
6363

6464
# Run pre-commit checks
6565
npx --yes git-precommit-checks

src/githooks/_prepare-commit-msg.sh

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

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

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

0 commit comments

Comments
 (0)