55eval $(
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
910help
1011)
@@ -18,15 +19,14 @@ if [ -z "$json_key" ]; then
1819fi
1920
2021zz_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/ *$//' )
2323if [ -z " $plugins " ]; then
2424 zz_log w " No plugins found at key {B $json_key } in {U $json_file }"
2525 exit 0
2626fi
2727
2828# Load the config file
29- config=$( dirname $0 ) /.ci-plugins
29+ config=$( dirname $0 ) /PLUGINS
3030
3131# if config file only contains comments and empty lines, make it empty
3232if [ -f " $config " ] && [ -z " $( grep -v -e ' ^#' -e ' ^$' $config ) " ]; then
@@ -40,27 +40,38 @@ if [ ! -f "$config" ]; then
4040 touch $config
4141fi
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
4449for plugin in $plugins ; do
4550 echo " $plugin " | sed ' s/^ *//;s/ *$//' | grep -v --file=$config >> $config
4651done
4752
4853# Reload the plugins list
4954plugins=$( 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
5260for 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 i "Plugin {B $plugin} is already installed, skipping."
5564 fi
5665done
5766
5867# Install the plugins if there are any to install
5968if [ -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
6574 zz_log s " Plugins {B $plugins } installed successfully!"
75+ else
76+ zz_log s " All plugins are already installed."
6677fi
0 commit comments