There are two or more projects on node js.
For example, the first project is located in the folder: "/project/one", inside one of the js functions we execute:
const projectTwoPath = "/project/two";
exec(`sh ${projectTwoPath}/projectRun.sh`, ...)
exec(`sh ./projectRun.sh`, {cwd: projectTwoPath }, ...)
projectRun.sh contains:
npx prisma migrate dev
pm2 start npm --name "project-two" -- run start
The script is executed, but for project One. Question: how to run this script so that it is executed for project Two, being in "/project/one", so that it is executed for the node in "/project/two"? (Option can be from the Linux console, or can within node exec(or shelljs) example)
In order to accomplish this, you will want to use a subshell such as
(cd ./two && sh ../one/command.sh). What this does is spawn a subshell, cd into theproject/twodirectory then run the script inproject/onewhile still in the context ofproject/two. Upon this being completed, the subshell will automatically terminate itself. Throughout this entire process, your main shell is still in the previous context.