0

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)

Van Hemer
  • 1
  • 1

1 Answers1

1

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 the project/two directory then run the script in project/one while still in the context of project/two. Upon this being completed, the subshell will automatically terminate itself. Throughout this entire process, your main shell is still in the previous context.

Dumbass
  • 110
  • 6
  • Not really, I'm trying to run a command from project One in project Two so it applies to project two. Rather than just run a command to project One from another folder – Van Hemer Sep 07 '22 at 03:26
  • @VanHemer I see, in that case you want to spawn a subshell and run everything within that. In the linux shell, it would look something like this `(cd ./two && sh ../one/command.sh)`. If this is what you are looking for, I will update my answer to go more in depth as to what this does. – Dumbass Sep 07 '22 at 14:46
  • yes, logically you understood correctly, that what I'm looking for – Van Hemer Sep 08 '22 at 02:34
  • @VanHemer Alright, I've updated my answer accordingly. – Dumbass Sep 08 '22 at 02:46
  • Ok, i tried this logic, so running from node exec, but node.js trying to launch script from projectOne, it's changed only thing, from where to run script. But the logic is to make the script work for projectTwo. Maybe need to work around spawn or fork. – Van Hemer Sep 08 '22 at 02:56
  • Hm, not sure how it would work with node exec. I've only tested this with the linux shell. – Dumbass Sep 08 '22 at 02:58