Skip to content

Commit

Permalink
feat: onFinishCommand handler (yargs#1473)
Browse files Browse the repository at this point in the history
* feat(command): add onFinishCommand handler
  • Loading branch information
petrgrishin authored and mleguen committed Dec 3, 2019
1 parent 63b3dd3 commit fe380cd
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 6 deletions.
14 changes: 14 additions & 0 deletions docs/advanced.md
Expand Up @@ -449,6 +449,20 @@ yargs.parserConfiguration({
See the [yargs-parser](https://github.com/yargs/yargs-parser#configuration) module
for detailed documentation of this feature.

## Command finish hook
### Example
```
yargs
.comand('cmd', ..., async () => {
await this.model.find()
return Promise.resolve('result value')
})
.onFinishCommand(async (resultValue) => {
await this.db.disconnect()
process.exit()
}).argv;
```

## Middleware

Sometimes you might want to transform arguments before they reach the command handler.
Expand Down
23 changes: 17 additions & 6 deletions lib/command.js
Expand Up @@ -240,15 +240,26 @@ module.exports = function command (yargs, usage, validation, globalMiddleware) {
handlerResult = commandHandler.handler(innerArgv)
}

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

Expand Down
36 changes: 36 additions & 0 deletions test/yargs.js
Expand Up @@ -2355,4 +2355,40 @@ describe('yargs dsl tests', () => {
// #1482.
argv['--'].should.eql(['foo'])
})

describe('onFinishCommand', () => {
it('use with promise', (done) => {
const result = 'noop-result'
let calledTimes = 0
yargs(['noop']).command('noop', 'a noop command', noop, async () => {
return result
}).onFinishCommand(async (commandResult) => {
commandResult.should.eql(result)
calledTimes++
})
.parse('noop')

setTimeout(() => {
calledTimes.should.eql(1)
done()
}, 5)
})

it('use without promise', (done) => {
const result = 'noop-result'
let calledTimes = 0
yargs(['noop']).command('noop', 'a noop command', noop, () => {
return result
}).onFinishCommand((commandResult) => {
commandResult.should.eql(result)
calledTimes++
})
.parse('noop')

setTimeout(() => {
calledTimes.should.eql(1)
done()
}, 5)
})
})
})
11 changes: 11 additions & 0 deletions yargs.js
Expand Up @@ -32,6 +32,7 @@ function Yargs (processArgs, cwd, parentRequire) {
let preservedGroups = {}
let usage = null
let validation = null
let handlerFinishCommand = null

const y18n = Y18n({
directory: path.resolve(__dirname, './locales'),
Expand Down Expand Up @@ -172,6 +173,7 @@ function Yargs (processArgs, cwd, parentRequire) {
frozen.parsed = self.parsed
frozen.parseFn = parseFn
frozen.parseContext = parseContext
frozen.handlerFinishCommand = handlerFinishCommand
}
function unfreeze () {
let frozen = frozens.pop()
Expand All @@ -190,6 +192,7 @@ function Yargs (processArgs, cwd, parentRequire) {
completionCommand = frozen.completionCommand
parseFn = frozen.parseFn
parseContext = frozen.parseContext
handlerFinishCommand = frozen.handlerFinishCommand
}

self.boolean = function (keys) {
Expand Down Expand Up @@ -483,6 +486,14 @@ function Yargs (processArgs, cwd, parentRequire) {
return self
}

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

self.getHandlerFinishCommand = () => handlerFinishCommand

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

0 comments on commit fe380cd

Please sign in to comment.