0

I am using shelljs to git clone in my node app. I want to only do something once the clone is successful. So wanted to so something like this:

shell.exec(`git clone https://myrepo.git; echo "cloned"`);

This returns cloned once my repo is cloned on Google Cloud functions. How do I do something like this:

if echo === 'cloned' {
   //do something
} else {
      //do something else
}
nb_nb_nb
  • 1,243
  • 11
  • 36
  • 1
    It would probably be simpler to just chain your commands. WIth `git clone https://myrepo.git && echo "cloned"`, the `echo` will not be executed if `git clone` returns an error. – Romain Valeri Dec 06 '22 at 16:32
  • @RomainValeri, can you give me an example please? Sorry, I am new to this. – nb_nb_nb Dec 06 '22 at 16:32
  • This probably is a valuable starting point for you to read about how to use that function: https://stackabuse.com/executing-shell-commands-with-node-js/ – arkascha Dec 06 '22 at 18:19
  • I would like to point out that you need to add a test condition on the return code for the git command, to make sure that it was successful. You shouldn't send that "cloned" echo unless it was a fully cloned branch, not act when git terminates badly. – Eric Marceau Dec 08 '22 at 02:45

2 Answers2

0

use && instead of ; like this:

shell.exec(`git clone https://myrepo.git && echo "cloned"`);

&& means echo "cloned" won't be executed until git clone https://myrepo.git finish successfully.

mar0ne
  • 148
  • 1
  • 10
0

I ended up using this:

if (shell.exec(`git clone https://myrepo.git`).code === 0) {
    //do something
} else {
   //do something else
}

This worked perfectly.

nb_nb_nb
  • 1,243
  • 11
  • 36