Trakkasure / node-expect

An expect-style library for NodeJS
33 stars 3 forks source link

Any way to use with child_process? #1

Open roborourke opened 7 years ago

roborourke commented 7 years ago

Hi, is there a way to use this with child_process.spawn?

I've tried passing stdin and stdout to .monitor(), I've tried using this library: https://github.com/stream-utils/duplex-child-process but I'm not having any luck.

My code looks like this:

import dps from 'duplex-child-process'

export function addKey( password ) {
  return new Promise( (resolve, reject) => {
    const parser = new Expect()
    parser
      .conversation(/ssh-add/)
        .sync()
        .expect(/Enter passphrase/)
          .send(`${password}\n`)
        .branch(/Identity added/)
          .handler(()=> { console.log('added'); resolve() })
        .branch(/Bad passphrase/)
          .handler(()=> { console.log('bad'); reject() })
        .end()
    parser.write("ssh-add\n")
    parser.monitor(dps.spawn('ssh-add', [ '-K', '~/.ssh/id_rsa' ]))
  })
}
Trakkasure commented 7 years ago

I haven't looked at this code in a while. Let me see what I can do to make this work.

Grenadingue commented 7 years ago

Hi, just wanted to bump, I'm very interested by using your project with child_process.spawn() too

I tried to make it work by myself but I'm a bit lost in the code. Anyway, if I can do anything that can accelerate resolving this issue, I'd be happy to help !