Skip to content

Commit 95e35b9

Browse files
committed
feat: ✨ add global installation option for npm plugins
1 parent 039813e commit 95e35b9

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

src/githooks/_install-plugins.sh

Lines changed: 10 additions & 4 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 (npm -g)
89
- key json_key JSON key path to extract plugins from
910
help
1011
)
@@ -18,15 +19,15 @@ 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+
plugins=$(jq -r "$json_key" "$json_file" | tr -d "'[]:,\"" | sort -u | tr '\n' ' ' | sed 's/ */ /g;s/^ *//;s/ *$//')
2223

2324
if [ -z "$plugins" ]; then
2425
zz_log w "No plugins found at key {B $json_key} in {U $json_file}"
2526
exit 0
2627
fi
2728

2829
# Load the config file
29-
config=$(dirname $0)/.ci-plugins
30+
config=$(dirname $0)/PLUGINS
3031

3132
# if config file only contains comments and empty lines, make it empty
3233
if [ -f "$config" ] && [ -z "$(grep -v -e '^#' -e '^$' $config)" ]; then
@@ -48,19 +49,24 @@ done
4849
# Reload the plugins list
4950
plugins=$(cat $config | grep -v '^$' | tr '\n' ' ')
5051

52+
# List of plugins to install
53+
zz_log i "Plugins to install: {B $plugins}"
54+
5155
# For each plugin, check if it is already installed
5256
for plugin in $plugins; do
53-
if npm list --depth=0 | grep -q "$plugin@"; then
57+
if npm list $global --depth=0 | grep -q "$plugin@"; then
5458
plugins=$(echo $plugins | sed "s#$plugin##g" | tr -s ' ')
5559
fi
5660
done
5761

5862
# Install the plugins if there are any to install
5963
if [ -n "$plugins" ]; then
6064
zz_log i "Installing plugins {B $plugins} ..."
61-
if ! npm install --no-save $plugins 2>/dev/null 1>&2; then
65+
if ! npm install $global --no-save $plugins 2>/dev/null 1>&2; then
6266
zz_log e "Failed to install one of plugins {B $plugins}!"
6367
exit 1
6468
fi
6569
zz_log s "Plugins {B $plugins} installed successfully!"
70+
else
71+
zz_log s "All plugins are already installed."
6672
fi

0 commit comments

Comments
 (0)