0

I have an app creator and, using the Username NPM package, I can get the OS' username with this:

Browser.ExecJS("username.sync()")

Tested on Electron.

So, this is the format my app creator uses to read from JS:

Browser.ExecJS("")

In this example, it's calling username.sync(). And it works.

But when running this:

Browser.ExecJS("const{exec}=require(`child_process`);exec(`jq -r '.next_var' /tmp/eventsheet.json`,(error,stdout,stderr)=>{if(error){console.log(`error:${error.message}`);return}if(stderr){console.log(`stderr:${stderr}`);return}console.log(`${stdout}`);return});")

It gets "0". And then correctly logs the stdout on console. But, it should get the same as it logs on console, instead of "0".

Its inspired by this code about how to run a native shell program directly from Electron/NodeJS: https://stackabuse.com/executing-shell-commands-with-node-js/

So, I think it should replace the console.log by some sort of return. How do I re-factor it? Thanks in advance.

UPDATE: based on this suggested answer (that doesn't works), this is the closest template to put it to work using return instead of console.log as output:

const { promisify } = require('util');
const exec = promisify(require('child_process').exec)

module.exports.getGitUser = async function getGitUser () {
  const name = await exec('git config --global user.name')
  const email = await exec('git config --global user.email')
  return { name, email }
};

But still doesn't works. What I need is this:

const{exec}=require(`child_process`);exec(`jq -r '.next_var' /tmp/eventsheet.json`,(error,stdout,stderr)=>{if(error){console.log(`error:${error.message}`);return}if(stderr){console.log(`stderr:${stderr}`);return}console.log(`${stdout}`);return});

But using return instead of console.log.

  • 2
    It looks like you're using jq to read a value from a json file. You're using javascript so why don't you just open the file, use JSON.parse(), and then pull the value from the resulting object? It has to be a lot easier. – Software Engineer Sep 20 '21 at 06:37
  • Thanks for trying to help on making this simple and enabling it to work; but the purpose of this question isn't just about reading JSON (whether its jq or JSON.parse()), but about directly returning a value (in this case, the ${stdout}) when Electron/NodeJS are executing a shell program. – dani 'SO learn value newbies' Sep 20 '21 at 06:40
  • 2
    Got it, thanks for the clarification. Does this answer your question? [Execute and get the output of a shell command in node.js](https://stackoverflow.com/questions/12941083/execute-and-get-the-output-of-a-shell-command-in-node-js) – Software Engineer Sep 20 '21 at 06:51
  • 2
    A program can only ever return an integer as status code (and its range is OS-dependent). You would need to capture stdout. – CherryDT Sep 20 '21 at 06:52
  • @SoftwareEngineer, this is the closest answer on your referred question/topic and the only that doesn't uses console.log: https://stackoverflow.com/a/50335035/5623661 but it haven't worked (also tested it on NodeJS by running "node test.js"). – dani 'SO learn value newbies' Sep 20 '21 at 07:18

0 Answers0