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
620zz_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
923if [ -z " $plugins " ]; then
1024 zz_log w " No plugins found at key {B $json_key } in {U $json_file }"
1428# Load the config file
1529config=$( 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
1844for 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
2546done
2647
2748# Reload the plugins list
2849plugins=$( 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
0 commit comments