Lance Shi
Lance Shi

Reputation: 1187

Node js: run cmd and get the output in error condition

I am trying to get node js to run a cmd command. The problem is, I really want to get the command's output regardless of whether it is a data or an error condition. I am currently using node-cmd to do this job and my code looks like:

const cmd = require('node-cmd');
const Promise = require('bluebird');

const getAsync = Promise.promisify(cmd.get, { multiArgs: true, context: cmd });

getAsync('sfdx force:source:status --json').then(data => {
    console.log('success');
    console.dir(data);
}).catch(err => {
    console.log('error');
    console.log(err.message);
});

When the result is success, I am getting the correct data. However, I still want to get the json output from my cli tool when error happens, But in err condition, err.message seem to be a pretty long string. Is there any way I can still retrieve just the data in error condition?

Upvotes: 0

Views: 400

Answers (1)

mfkw1
mfkw1

Reputation: 1151

You can add || true to your command. That will ensure your command will always return success. I'm assuming your server is running on Linux-based OS.

Upvotes: 1

Related Questions