Search code examples
gitbashosascript

Concurrent execution of script in different tab windows


I'm trying clone down git repo's concurrently in different Mac terminal tabs as they take a long time to clone down.

I've tried a lot of variations of the below but can't seem to get each separate clone and then following commands in 3 separate terminal tabs, running at the same time, any ideas on how I can change the below to make this happen without installing something external such as ttab?

cwd=$(pwd)
osascript -e 'tell application "Terminal" to activate' -e 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down' -e 'tell application "Terminal" to do script "cd '$cwd' && git clone git@github.com:me/myrepo1.git && cd myrepo1 && git pull && nvm use && npm install &" in selected tab of the front window' &
osascript -e 'tell application "Terminal" to activate' -e 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down' -e 'tell application "Terminal" to do script "cd '$cwd' && git clone git@github.com:me/myrepo3.git && cd myrepo2 && git pull && nvm use && npm install" in selected tab of the front window' &
git clone git@github.com:me/myrepo3.git && cd myrepo3 && git pull && nvm use && npm install

Solution

  • This will do it.

    #!/bin/bash
    
    declare -a repos=("myrepo1" "myrepo2" "myrepo3")
    me="git@github.com:me"
    
    pwd=`pwd`
    for i in "${repos[@]}"
    do
      osascript -e "tell application \"Terminal\"" -e "tell application \"System Events\" to keystroke \"t\" using {command down}" -e "do script \"cd $pwd; git clone $me/$i.git && cd $i && git pull && nvm use && npm install\" in front window" -e "end tell" > /dev/null
    done
    

    Notes:

    • You shouldn't have to use git pull after cloning a repo, but I kept it in the script anyways since it doesn't do any harm.
    • If you use nvm use, make sure that you have an .nvmrc file with your specified node version in the root of each project. Otherwise nvm use will not work.
      • You probably already know this, but I wanted to state it again in case your script doesn't run.