title: node js: I to get the output of an (child) exec and set a variable to be retuned?
body: <p>I have been searching for a truly example, but I could not find any. I m fully new to node js.</p>
  
  <p>I am setting up a service which gets passwords using a command line tool.</p>
  
  <p>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.</p>
  
  <p>The partial code I have till now is:</p>
  
  <pre><code>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;
  </code></pre>
  
  <p>This will be used inside a testcafe environment. Here I define a Role.</p>
  
  <pre><code>testRole() {
    let pwService = new PwService('a-key-');
    let pw = pwService.getPw('testPW');
    //console.log('pw: '+pw)
  
    return Role('https://www.example.com/', async t =&gt; {
      await t
      .typeText(this.firstInput, 'testPW')
      .typeText(this.secondInput, pw&lt;??&gt;)
      .click(this.selectButton);
      }, { preserveUrl: true });
  }
  </code></pre>
  
  <p>The testcafe code works if I use literal strings for pw.</p>
  
  <p>The /<em>missing bits</em>/ is left empty as I tried many different things but no one worked!</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>It seems it should be a standard exercise for node.js experts.</p>