0

while cd ./tools/darwin/ && ./adb works in my mac terminal,

my node.js application with this piece of code

const CHANGE_DIRECTORY_COMMAND = "cd ./tools/darwin/"
const EXECUTE_ADB_COMMAND = "./adb"

if(platform === "darwin") {
  exec(`${CHANGE_DIRECTORY_COMMAND} && ${EXECUTE_ADB_COMMAND}`, (error, stdout, stderr) => {
    if (error) {
      console.error(`Error executing the adb file: ${error}`);
      return;
    }

    if (stdout) {
      console.log(`Standard output: ${stdout}`);
    }

    if (stderr) {
      console.error(`Standard error: ${stderr}`);
    }
  });
}

gives this error,

Error executing the adb file: Error: Command failed: cd ./tools/darwin/ && ./adb

in addition, this code works pretty well,

const { exec } = require("child_process");

let commandOne = "ls -l"; // display all files in current directory with  (-l) long format
let commandTwo = "whoami"; // print the current user
let commandThree = "pwd"; //print the name of current directory

exec(`${commandOne} && ${commandTwo} && ${commandThree}`, (error, stdout, stderr) => {
    if (error) {
        console.log(`error: ${error.message}`);
        return;
    }
    if (stderr) {
        console.log(`stderr: ${stderr}`);
        return;
    }
    console.log(`Output: ${stdout}`);
});

1 Answers1

0

cd ./tools/darwin/ && ./adb are two different commands, every command will executed from where ever you are.

const { exec } = require('child_process');

// const CHANGE_DIRECTORY_COMMAND = "cd ./tools/darwin/"
// const EXECUTE_ADB_COMMAND = "./adb"

const CHANGE_DIRECTORY_COMMAND = '/tools/darwin/'
const EXECUTE_ADB_COMMAND = 'adb'

//  exec(`${CHANGE_DIRECTORY_COMMAND} && ${EXECUTE_ADB_COMMAND}`, (error, stdout, stderr) => {
//  });

exec(EXECUTE_ADB_COMMAND, { cwd: CHANGE_DIRECTORY_COMMAND}, (error, stdout, stderr) => {
});
vr-ee
  • 11
  • 2
  • `cd ./tools/darwin/ && ./adb` will *first* execute the first command and if successful execute the second. In particular this is correct way to navigate to a directory and run a command in said directory. – VLAZ Sep 01 '23 at 11:09
  • you are right for `shell`, but not for `excec`: [nodeJS exec does not work for "cd " shell cmd](https://stackoverflow.com/questions/15629923/nodejs-exec-does-not-work-for-cd-shell-cmd) – vr-ee Sep 01 '23 at 11:27
  • Tried in with `exec` and it worked the same way. I used these steps: https://gist.github.com/PurpleMagick/43a9608062450ba427d1254a3fcadb1f – VLAZ Sep 01 '23 at 11:27