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,8 +19,7 @@ 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
@@ -40,25 +40,34 @@ 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 s " 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
0 commit comments