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
.