Skip to content

Commit

Permalink
feat(command): add onFinishCommand handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr Grishin committed Nov 7, 2019
1 parent c10c38c commit 03e2507
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
18 changes: 12 additions & 6 deletions lib/command.js
Expand Up @@ -242,13 +242,19 @@ module.exports = function command (yargs, usage, validation, globalMiddleware) {

if (isPromise(handlerResult)) {
yargs.getUsageInstance().cacheHelpMessage()
handlerResult.catch(error => {
try {
yargs.getUsageInstance().fail(null, error)
} catch (err) {
handlerResult
.then(value => {
yargs._onFinishCommand && yargs._onFinishCommand(value)
})
.catch(error => {
try {
yargs.getUsageInstance().fail(null, error)
} catch (err) {
// fail's throwing would cause an unhandled rejection.
}
})
}
})
} else {
yargs._onFinishCommand && yargs._onFinishCommand(handlerResult)
}
}

Expand Down
6 changes: 6 additions & 0 deletions yargs.js
Expand Up @@ -483,6 +483,12 @@ function Yargs (processArgs, cwd, parentRequire) {
return self
}

self.onFinishCommand = function (f) {
argsert('<function>', [f], arguments.length)
self._onFinishCommand = f
return self
}

self.check = function (f, _global) {
argsert('<function> [boolean]', [f, _global], arguments.length)
validation.check(f, _global !== false)
Expand Down

0 comments on commit 03e2507

Please sign in to comment.