Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
fix: fix async exit cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
hugomrdias committed Jul 19, 2019
1 parent 2e4379a commit 56843ba
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 42 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"async-iterator-to-stream": "^1.1.0",
"base32.js": "~0.1.0",
"bignumber.js": "^9.0.0",
"async-exit-hook": "^2.0.1",
"binary-querystring": "~0.1.2",
"bl": "^3.0.0",
"bs58": "^4.0.1",
Expand Down
77 changes: 40 additions & 37 deletions src/cli/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,52 +25,55 @@ if (!semver.satisfies(process.versions.node, pkg.engines.node)) {

const YargsPromise = require('yargs-promise')
const updateNotifier = require('update-notifier')
const onExit = require('signal-exit')
const onExit = require('async-exit-hook')
const utils = require('./utils')
const print = utils.print
const debug = require('debug')('ipfs:cli')
const parser = require('./parser')
const commandAlias = require('./command-alias')

function main (args) {
const oneWeek = 1000 * 60 * 60 * 24 * 7
updateNotifier({ pkg, updateCheckInterval: oneWeek }).notify()
const oneWeek = 1000 * 60 * 60 * 24 * 7
updateNotifier({ pkg, updateCheckInterval: oneWeek }).notify()

const cli = new YargsPromise(parser)
const cli = new YargsPromise(parser)

let getIpfs = null
let getIpfs = null

// Apply command aliasing (eg `refs local` -> `refs-local`)
args = commandAlias(args)

cli
.parse(args)
.then(({ data, argv }) => {
getIpfs = argv.getIpfs
if (data) {
print(data)
}
})
.catch(({ error, argv }) => {
getIpfs = argv && argv.getIpfs
debug(error)
// the argument can have a different shape depending on where the error came from
if (error.message || (error.error && error.error.message)) {
print(error.message || error.error.message)
} else {
print('Unknown error, please re-run the command with DEBUG=ipfs:cli to see debug output')
}
process.exit(1)
})

onExit(() => {
// If an IPFS instance was used in the handler then clean it up here
if (getIpfs && getIpfs.instance) {
const cleanup = getIpfs.rest[0]

cleanup().catch(err => debug(err))
// Apply command aliasing (eg `refs local` -> `refs-local`)
const args = commandAlias(process.argv.slice(2))
cli
.parse(args)
.then(({ data, argv }) => {
getIpfs = argv.getIpfs
if (data) {
print(data)
}
})
}
.catch(({ error, argv }) => {
getIpfs = argv.getIpfs
if (error) {
throw error
}
throw new Error('Unknown error, please re-run the command with DEBUG=ipfs:cli to see debug output')
})

onExit(cb => {
// If an IPFS instance was used in the handler then clean it up here
if (getIpfs && getIpfs.instance) {
const cleanup = getIpfs.rest[0]

return cleanup()
.then(() => cb())
.catch(err => {
print(err.message)
debug(err)
cb()
})
}
cb()
})

main(process.argv.slice(2))
onExit.unhandledRejectionHandler(err => {
print(err.message)
debug(err)
})
14 changes: 9 additions & 5 deletions test/cli/id.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const runOnAndOff = require('../utils/on-and-off')

describe('id', () => runOnAndOff((thing) => {
let ipfs
const cli = new YargsPromise(parser)

before(function () {
this.timeout(60 * 1000)
Expand All @@ -25,14 +24,19 @@ describe('id', () => runOnAndOff((thing) => {
expect(id).to.have.property('addresses')
})
})
}))

describe('id', () => {
const cli = new YargsPromise(parser)

it('get the id with cli parser', () => {
return cli.parse('id --silent')
.then(({ data }) => {
expect(data).to.have.property('id')
expect(data).to.have.property('publicKey')
expect(data).to.have.property('addresses')
const id = JSON.parse(data)
expect(id).to.have.property('id')
expect(id).to.have.property('publicKey')
expect(id).to.have.property('addresses')
return data
})
})
}))
})

0 comments on commit 56843ba

Please sign in to comment.