Skip to content

Commit

Permalink
feat: use execa instead of subcomandante
Browse files Browse the repository at this point in the history
  • Loading branch information
hugomrdias authored and daviddias committed Oct 27, 2018
1 parent 5feea50 commit 388b401
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 48 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
"debug": "^4.1.0",
"detect-node": "^2.0.3",
"dexie": "^2.0.4",
"execa": "^1.0.0",
"hapi": "^16.6.2",
"hat": "~0.0.3",
"ipfs-api": "^25.0.0",
Expand All @@ -108,7 +109,6 @@
"rimraf": "^2.6.2",
"safe-json-parse": "^4.0.0",
"safe-json-stringify": "^1.1.0",
"subcomandante": "^1.0.5",
"superagent": "^3.8.2"
},
"devDependencies": {
Expand Down
28 changes: 10 additions & 18 deletions src/utils/exec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
'use strict'

const run = require('subcomandante')
const once = require('once')
const debug = require('debug')
const log = debug('ipfsd-ctl:exec')
const path = require('path')
const execa = require('execa')
const noop = () => {}

function exec (cmd, args, opts, callback) {
Expand All @@ -13,30 +12,23 @@ function exec (cmd, args, opts, callback) {
opts = {}
}

callback = once(callback)

opts = Object.assign({}, {
stdout: noop,
stderr: noop
}, opts)

const done = (code) => {
// if process exits with non-zero code, subcomandante will cause
// an error event to be emitted which will call the passed
// callback so we only need to handle the happy path
if (code === 0) {
callback()
}
}

log(path.basename(cmd), args.join(' '))

const command = run(cmd, args, opts)
command.on('error', callback)
command.on('close', done)
command.on('exit', done)
command.stdout.on('data', opts.stdout)
const command = execa(cmd, args, { env: opts.env })
command.stderr.on('data', opts.stderr)
command.stdout.on('data', opts.stdout)
command
.then(r => {
callback(null, r)
})
.catch(err => {
callback(err)
})

return command
}
Expand Down
37 changes: 8 additions & 29 deletions test/exec.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const isrunning = require('is-running')
const cp = require('child_process')
const path = require('path')
const exec = require('../src/utils/exec')
const os = require('os')
Expand All @@ -32,19 +31,6 @@ function psExpect (pid, shouldBeRunning, grace, callback) {
}, 200)
}

function isRunningGrep (pattern, callback) {
const cmd = 'ps aux'
cp.exec(cmd, { maxBuffer: 1024 * 500 }, (err, stdout, stderr) => {
if (err) {
return callback(err)
}

const running = stdout.match(pattern) !== null

callback(null, running)
})
}

function makeCheck (n, done) {
let i = 0

Expand Down Expand Up @@ -98,7 +84,6 @@ describe('exec', () => {
exec(process.execPath, [
path.resolve(path.join(__dirname, 'fixtures', 'error.js'))
], {}, (error) => {
expect(error.message).to.contain('non-zero exit code 1')
expect(error.message).to.contain('Goodbye cruel world!')

done()
Expand All @@ -113,13 +98,10 @@ describe('exec', () => {
const args = hang.concat(tok)

const p = exec(args[0], args.slice(1), {}, (err) => {
expect(err).to.not.exist()

isRunningGrep(token, (err, running) => {
expect(err).to.not.exist()
expect(running).to.not.be.ok()
check()
})
expect(err).to.exist()
expect(err.killed).to.be.ok()
expect(err.signal).to.equal('SIGTERM')
check()
})

psExpect(p.pid, true, 10, (err, running) => {
Expand All @@ -141,13 +123,10 @@ describe('exec', () => {
const tok = token()

const p = exec(survivor, [tok], {}, (err) => {
expect(err).to.not.exist()

isRunningGrep(token, (err, running) => {
expect(err).to.not.exist()
expect(running).to.not.be.ok()
check()
})
expect(err).to.exist()
expect(err.killed).to.be.ok()
expect(err.signal).to.equal('SIGKILL')
check()
})

psExpect(p.pid, true, 10, (err, running) => {
Expand Down

0 comments on commit 388b401

Please sign in to comment.