mariotti
mariotti

Reputation: 408

node js: I to get the output of an (child) exec and set a variable to be retuned?

I have been searching for a truly example, but I could not find any. I m fully new to node js.

I am setting up a service which gets passwords using a command line tool.

The command line "pw get key" returns the password associated with the key. The command line "pw set key password" sets the password associated with the key.

The partial code I have till now is:

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

async function cmdPwGet(key) {
  const { stdout, stderr } = await exec(`pw get ${key}`);
  console.log('stdout:', stdout); 
  console.log('stderr:', stderr);
}

async function cmdPwPut(key, passwd) {
  const { stdout, stderr } = await exec(`pw set ${key} ${passwd}`);
  console.log('stdout:', stdout);
  console.log('stderr:', stderr);
}

class PwService {

    constructor (KEY){
    this.basekey = KEY;
    this.pwd = "";
    }

    setPw (key, passwd) {
      key = this.basekey + "." + key;
      var res = cmdPwPut(key, passwd);
      /* missing bits */
    }

    getPw (key) {
      key = this.basekey + "." + key;
      var res = cmdPwGet(key);
      /* missing bit */
    }
}

module.exports = PwService;

This will be used inside a testcafe environment. Here I define a Role.

testRole() {
  let pwService = new PwService('a-key-');
  let pw = pwService.getPw('testPW');
  //console.log('pw: '+pw)

  return Role('https://www.example.com/', async t => {
    await t
    .typeText(this.firstInput, 'testPW')
    .typeText(this.secondInput, pw<??>)
    .click(this.selectButton);
    }, { preserveUrl: true });
}

The testcafe code works if I use literal strings for pw.

The /missing bits/ is left empty as I tried many different things but no one worked!

I think I can make it working with the *Sync version of the child. But as this is inside a testcafe which might run in parallel I would prefer an async version.

Any suggestion? I know it is really to understand promises and the like in node.js, but I could not make my way out of this.

It seems it should be a standard exercise for node.js experts.

Upvotes: 1

Views: 2476

Answers (1)

ufxmeng
ufxmeng

Reputation: 2600

Async/Await just make async code looks like sync code, your code still executed async. And the return result of the Async\Await function cmdPwGet is a Promise, not the password as you think.

the execute result of cmdPwGet is a promise

async function cmdPwGet(key) {
  const { stdout, stderr } = await exec(`pw get ${key}`);
  return stdout;
}

make getPw Async/Await

  async getPw(key) {
        key = this.basekey + "." + key;
        var res = await cmdPwGet(key);
        return res;
    }

get the password

testRole() {
  let pwService = new PwService('a-key-');

  return Role('https://www.example.com/', async t => {
    // get your password with await
    let pw = await pwService.getPw('testPW');
    await t
    .typeText(this.firstInput, 'testPW')
    .typeText(this.secondInput, pw<??>)
    .click(this.selectButton);
    }, { preserveUrl: true });
}

Upvotes: 6

Related Questions