Skip to content

Commit

Permalink
feat: replace subcommandante with execa
Browse files Browse the repository at this point in the history
Thanks to the great work from @JGAntunes
  • Loading branch information
dignifiedquire committed Dec 20, 2016
1 parent 8d43043 commit 7b09529
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 187 deletions.
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,13 @@
"license": "MIT",
"dependencies": {
"async": "^2.1.4",
"bl": "^1.1.2",
"execa": "^0.5.0",
"go-ipfs-dep": "0.4.4",
"ipfs-api": "^12.0.3",
"multiaddr": "^2.1.0",
"once": "^1.4.0",
"pump": "^1.0.2",
"rimraf": "^2.5.4",
"shutdown": "^0.2.4",
"subcomandante": "^1.0.5"
"shutdown": "^0.2.4"
},
"devDependencies": {
"aegir": "^9.3.0",
Expand Down
67 changes: 67 additions & 0 deletions src/exec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
'use strict'

const execa = require('execa')
const once = require('once')

function exec (cmd, args, opts, handlers) {
opts = opts || {}
let err = ''
let result = ''
let callback

// Handy method if we just want the result and err returned in a callback
if (typeof handlers === 'function') {
callback = once(handlers)
handlers = {
error: callback,
data (data) {
result += data
},
done () {
if (err) {
return callback(new Error(err))
}
callback(null, result.trim())
}
}
}

// The listeners that will actually be set on the process
const listeners = {
data: handlers.data,
error (data) {
err += data
},
done (code) {
if (code !== 0) {
return handlers.error(
new Error(`non-zero exit code ${code}\n
while running: ${cmd} ${args.join(' ')}\n\n
${err}`)
)
}
if (handlers.done) {
handlers.done()
}
}
}

const command = execa(cmd, args, opts)

if (listeners.data) {
command.stdout.on('data', listeners.data)
}

command.stderr.on('data', listeners.error)

// If command fails to execute return directly to the handler
command.on('error', handlers.error)

command.on('close', listeners.done)

command.catch(handlers.error)

return command
}

module.exports = exec
41 changes: 23 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ const join = require('path').join

const Node = require('./node')

const defaultOptions = {
'Addresses.Swarm': ['/ip4/0.0.0.0/tcp/0'],
'Addresses.Gateway': '',
'Addresses.API': '/ip4/127.0.0.1/tcp/0',
disposable: true,
init: true
}

function tempDir () {
return join(os.tmpdir(), `ipfs_${String(Math.random()).substr(2)}`)
}
Expand Down Expand Up @@ -34,7 +42,10 @@ module.exports = {
opts = {}
}
this.disposable(opts, (err, node) => {
if (err) return done(err)
if (err) {
return done(err)
}

node.startDaemon(done)
})
},
Expand All @@ -43,28 +54,22 @@ module.exports = {
done = opts
opts = {}
}
opts['Addresses.Swarm'] = ['/ip4/0.0.0.0/tcp/0']
opts['Addresses.Gateway'] = ''
opts['Addresses.API'] = '/ip4/127.0.0.1/tcp/0'

if (opts.apiAddr) {
opts['Addresses.API'] = opts.apiAddr
}
let options = {}
Object.assign(options, defaultOptions, opts || {})

if (opts.gatewayAddr) {
opts['Addresses.Gateway'] = opts.gatewayAddr
}
const repoPath = options.repoPath || tempDir()
const disposable = options.disposable
delete options.disposable
delete options.repoPath

const node = new Node(opts.repoPath || tempDir(), opts, true)
const node = new Node(repoPath, options, disposable)

if (typeof opts.init === 'boolean' && opts.init === false) {
process.nextTick(() => {
done(null, node)
})
if (typeof options.init === 'boolean' &&
options.init === false) {
process.nextTick(() => done(null, node))
} else {
node.init((err) => {
done(err, node)
})
node.init((err) => done(err, node))
}
}
}
Loading

0 comments on commit 7b09529

Please sign in to comment.