hook.js
This file
Path
examples/hook.js
65 lines · 2.2 KiB · JavaScript
Last changed by
feat: add preSubcommand hook (#1763)鸿则before adoption2c7f687
History of this fileTake it
Add hooks for life cycle events: using .hook() (#1514)John Geebefore adoptionc20284d
lines 1–3why, one step back1#!/usr/bin/env node23// const commander = require('commander'); // (normal include)feat: add preSubcommand hook (#1763)鸿则before adoption2c7f687
lines 4–5why, one step back4const { Command, Option } = require('../'); // include commander in git clone of commander repo5const program = new Command();Add hooks for life cycle events: using .hook() (#1514)John Geebefore adoptionc20284d
lines 6–10why, one step back67// This example shows using some hooks for life cycle events.89const timeLabel = 'command duration';10programfeat: add preSubcommand hook (#1763)鸿则before adoption2c7f687
line 11why, one step back11 .option('--profile', 'show how long command takes')Add hooks for life cycle events: using .hook() (#1514)John Geebefore adoptionc20284d
lines 12–23why, one step back12 .hook('preAction', (thisCommand) => {13 if (thisCommand.opts().profile) {14 console.time(timeLabel);15 }16 })17 .hook('postAction', (thisCommand) => {18 if (thisCommand.opts().profile) {19 console.timeEnd(timeLabel);20 }21 });2223programfeat: add preSubcommand hook (#1763)鸿则before adoption2c7f687
line 24why, one step back24 .option('--trace', 'display trace statements for commands')Add hooks for life cycle events: using .hook() (#1514)John Geebefore adoptionc20284d
lines 25–34why, one step back25 .hook('preAction', (thisCommand, actionCommand) => {26 if (thisCommand.opts().trace) {27 console.log('>>>>');28 console.log(`About to call action handler for subcommand: ${actionCommand.name()}`);29 console.log('arguments: %O', actionCommand.args);30 console.log('options: %o', actionCommand.opts());31 console.log('<<<<');32 }33 });34feat: add preSubcommand hook (#1763)鸿则before adoption2c7f687
lines 35–44why, one step back35program36 .option('--env <filename>', 'specify environment file')37 .hook('preSubcommand', (thisCommand, subcommand) => {38 if (thisCommand.opts().env) {39 // One use case for this hook is modifying environment variables before40 // parsing the subcommand, say by reading .env file.41 console.log(`Reading ${thisCommand.opts().env}...`);42 process.env.PORT = 80;43 console.log(`About to call subcommand: ${subcommand.name()}`);44 }Add hooks for life cycle events: using .hook() (#1514)John Geebefore adoptionc20284d
lines 45–46why, one step back45 });46feat: add preSubcommand hook (#1763)鸿则before adoption2c7f687
lines 47–56why, one step back47program.command('start')48 .argument('[script]', 'script name', 'server.js')49 .option('-d, --delay <seconds>', 'how long to delay before starting')50 .addOption(new Option('-p, --port <number>', 'port number').default(8080).env('PORT'))51 .action(async(script, options) => {52 if (options.delay) {53 await new Promise(resolve => setTimeout(resolve, parseInt(options.delay) * 1000));54 }55 console.log(`Starting ${script} on port ${options.port}`);56 });Add hooks for life cycle events: using .hook() (#1514)John Geebefore adoptionc20284d
lines 57–61why, one step back5758// Some of the hooks or actions are async, so call parseAsync rather than parse.59program.parseAsync().then(() => {});6061// Try the following:feat: add preSubcommand hook (#1763)鸿则before adoption2c7f687
lines 62–65why, one step back62// node hook.js start63// node hook.js --trace start --port 9000 test.js64// node hook.js --profile start --delay 565// node hook.js --env=production.env start