-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hooks for life cycle events: using .hook() (#1514)
* First cut at action hooks * First cut at promise support in hooks * Move static routines out of class * Add error for unexpport event type * Add typings * Add typings * Support multiple hooks on same command * Add some synchronous tests * Add async tests * Add tests that options and args are available from context in hooks * Expand test names * Spell life cycle as two words * Spell life cycle as two words * Fix typo in JSDoc * Add example for hooks * Make comment more accurate * First cut at README * Small README improvements * Add note about async and multiple hooks * Rename hooks and pass parameters rather than context hash * Make example scan better * Fix stale comment
- Loading branch information
1 parent
6c06528
commit c20284d
Showing
7 changed files
with
525 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#!/usr/bin/env node | ||
|
||
// const commander = require('commander'); // (normal include) | ||
const commander = require('../'); // include commander in git clone of commander repo | ||
const program = new commander.Command(); | ||
|
||
// This example shows using some hooks for life cycle events. | ||
|
||
const timeLabel = 'command duration'; | ||
program | ||
.option('-p, --profile', 'show how long command takes') | ||
.hook('preAction', (thisCommand) => { | ||
if (thisCommand.opts().profile) { | ||
console.time(timeLabel); | ||
} | ||
}) | ||
.hook('postAction', (thisCommand) => { | ||
if (thisCommand.opts().profile) { | ||
console.timeEnd(timeLabel); | ||
} | ||
}); | ||
|
||
program | ||
.option('-t, --trace', 'display trace statements for commands') | ||
.hook('preAction', (thisCommand, actionCommand) => { | ||
if (thisCommand.opts().trace) { | ||
console.log('>>>>'); | ||
console.log(`About to call action handler for subcommand: ${actionCommand.name()}`); | ||
console.log('arguments: %O', actionCommand.args); | ||
console.log('options: %o', actionCommand.opts()); | ||
console.log('<<<<'); | ||
} | ||
}); | ||
|
||
program.command('delay') | ||
.option('--message <value>', 'custom message to display', 'Thanks for waiting') | ||
.argument('[seconds]', 'how long to delay', '1') | ||
.action(async(waitSeconds, options) => { | ||
await new Promise(resolve => setTimeout(resolve, parseInt(waitSeconds) * 1000)); | ||
console.log(options.message); | ||
}); | ||
|
||
program.command('hello') | ||
.option('-e, --example') | ||
.action(() => console.log('Hello, world')); | ||
|
||
// Some of the hooks or actions are async, so call parseAsync rather than parse. | ||
program.parseAsync().then(() => {}); | ||
|
||
// Try the following: | ||
// node hook.js hello | ||
// node hook.js --profile hello | ||
// node hook.js --trace hello --example | ||
// node hook.js delay | ||
// node hook.js --trace delay 5 --message bye | ||
// node hook.js --profile delay |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.