4

Im working on large project and we have multiple npm packages.

I want to install all the packages in parallel which mean that i want all of to run on the same time (to save time) and once the last install has finished to continue with my script.

Example script:

#!/bin/zsh
#...

NPM_FOLDERS=(
    common.library/audit
    common.library/cipher
    common.library/logger
    ...
)

# Get the number of total folders to process
counter=${#NPM_FOLDERS[@]};

# counter for user feedback to the current install folder index
index=1;

# loop and install all the required packages
for folder in $NPM_FOLDERS;
do 
    #  Print the installation message with the folder & couters
    echo "\033[38;5;255m($index/$counter) Executing npm install in: \033[38;5;226m$folder";
    
    # change the folder to the required location    
    cd $ROOT_FOLDER/$folder;
    
    # Execute install on this folder
    npm install ;
   
    # increase current index
    let index++;

done

echo
echo "\033[38;5;11mInstallation completed."
echo 

In not going to accept the fastest answer but the one who will do what i wish to do and do not have the right knowledge on how to do it, so you can tale the time and give a full answer.

Thank you very much in advance.

Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167

1 Answers1

5

Execute npm install in the background with:

npm install &

Then after the done line you can wait for all the background processes to finish with:

wait

This command is explained in the bash manual:

Wait until the child process specified by each process ID pid or job specification jobspec exits and return the exit status of the last command waited for. If a job spec is given, all processes in the job are waited for. If no arguments are given, all currently active child processes are waited for, and the return status is zero.

Barmar
  • 741,623
  • 53
  • 500
  • 612