A MacOS Trick to Develop Multiple Projects from a Single Command
How to spawn a terminal and start the development process for each project
Published on
Apr 21, 2023
Read time
3 min read
Introduction
The terminal is one of the most important tools in every developer’s arsenal, but I often find myself typing out the same commands multiple times every day. In particular, I typically spend a minute or two every day setting up development processes for the same four-or-so projects.
This is unnecessary, since it could be automated!
After a bit of research, I realised that — without too much effort — I could create a shortcut to help me spawn multiple terminals, each running a separate development process, from a single keyword. In this article, I’ll share how.
The “open” command
On MacOS, the best way I found was to use the open
command. Using the -a
flag, we can open a specific app — like the Terminal or iTerm — and pass it a Bash (or Zsh) file to open, which will run immediately. It’s possible to do something similar using AppleScript, but I found the open
method simpler.
Creating a shell file for each project
First, we need to create a shell file containing the code we want to run for each of our projects or processes. We can create a new file and with the extension “.sh”, and in it we can put the repetitive shell code we want to automate for a single task.
For me, since I work with Node.js, I will typically navigate to a project, run nvm use
to ensure I am running the correct Node version for the project, and then yarn develop
.
My files look something like this, and I’ll have one for every project I want to run locally in my standard development process:
#!/bin/zsh
cd ~/work/my-nodejs-project
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
nvm use
yarn develop
At the top of the file, we have a shebang !#
followed by code that tells the interpreter how the script to run. This could be Bash, though in this case I’m using Zsh. Next, we cd
into my project direction.
The following line is used to tell Zsh to load nvm
, which we then run to select the project’s Node.js version. Finally, we run the development command for the project.
I’ll have one of these files for each project, typically with some minor differences based on the steps needed to run the project in development.
Running the shell files in parallel
Now we have created our shell files, we simply need to run them. Using a single ampersand &
, we can set these processes off in parallel:
#!/bin/zsh
code ~/work/my-workspace.code-workspace \
& open -a iTerm ~/scripts/develop-my-first-project.sh \
& open -a iTerm ~/scripts/develop-my-second-project.sh \
& open -a iTerm ~/scripts/develop-my-third-project.sh
I use iTerm
but any terminal application should work. As part of this command, I also open up a VSCode workspace using the code
command. (To set that up, see the official docs). I named the above file develop-projects.sh
.
Adding an alias
Finally, we can create an alias to run the above script quickly. The exact way to do this will depend on the shell you use, but for Zsh, go to the .zshrc
config file and add something like this:
alias develop="bash ~/scripts/develop-projects.sh"
I hope you found this trick useful. If you’re looking for a similar trick but you’re not using a Mac, I suggest checking out the start
command on Windows and the gnome-terminal
or xterm
commands on Linux.
Related articles
You might also enjoy...
How to Automate Merge Requests with Node.js and Jira
A quick guide to speed up your MR or PR workflow with a simple Node.js script
7 min read
Automate Your Release Notes with AI
How to save time every week using GitLab, OpenAI, and Node.js
11 min read
How to Create a Super Minimal MDX Writing Experience
Learn to create a custom MDX experience so you can focus on writing without worrying about boilerplate or repetition
12 min read