Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: onFinishCommand handler #1473

Merged
merged 4 commits into from Dec 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo, should be command not comand

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @dapplion. Mind doing a small PR to fix this typo?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @dapplion. Mind doing a small PR to fix this typo?

Sure @mleguen: #1506

await this.model.find()
return Promise.resolve('result value')
})
.onFinishCommand(async (resultValue) => {
mleguen marked this conversation as resolved.
Show resolved Hide resolved
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) => {
petrgrishin marked this conversation as resolved.
Show resolved Hide resolved
commandResult.should.eql(result)
petrgrishin marked this conversation as resolved.
Show resolved Hide resolved
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