Lance Shi
Lance Shi

Reputation: 1187

Node js - how to execute two windows commands in sequence and get the output

I am trying to execute two windows commands in sequence and get the result of the later one. Something like:

cd ${directory}
sfdx force:source:convert -d outputTmp/ --json

I have browsed and tried a bunch of third-party libraries, like node-cmd. But so far I haven't got any luck yet. As in node-cmd example:

cmd.get(
    `cd ${directory}
    sfdx force:source:convert -d outputTmp/ --json`,
    function(err, data, stderr) {

This works very well on my macOS machine. But on Windows it tends to execute only the first command.

Is there anyway I can resolve this issue? Even some walk around for just cd {directory} + real command can be really helpful

Upvotes: 2

Views: 213

Answers (1)

Andriy
Andriy

Reputation: 15462

You can try this:

const exec = require('child_process').exec;

exec(`cd dir 
      sfdx force:source:convert -d outputTmp/ --json`, (err, stdout, stderr) => {
  if (err) {
    // node couldn't execute the command
    return;
  }
  console.log(stdout);
});

or by using && without backticks:

const exec = require('child_process').exec;

exec('cd dir && sfdx force:source:convert -d outputTmp/ --json', (err, stdout, stderr) => {
  if (err) {
    // node couldn't execute the command
    return;
  }
  console.log(stdout);
});

Upvotes: 2

Related Questions