hook.js
This file
Path
examples/hook.js
64 lines · 2.1 KiB · JavaScript
Last changed by
Use standard import in examples (#2168)John Geebefore adoption41b12cf
History of this fileTake it
The walk stopped after 200 commits: lines older than 0b0fbbe point at it.
Update dependencies (#1524)John Geebefore adoption0b0fbbe
lines 1–2why, one step back1#!/usr/bin/env node2Use standard import in examples (#2168)John Geebefore adoption41b12cf
line 3why, one step back3const { Command, Option } = require('commander');feat: add preSubcommand hook (#1763)鸿则before adoption2c7f687
line 4why, one step back4const program = new Command();Update dependencies (#1524)John Geebefore adoption0b0fbbe
lines 5–9why, one step back56// This example shows using some hooks for life cycle events.78const timeLabel = 'command duration';9programfeat: add preSubcommand hook (#1763)鸿则before adoption2c7f687
line 10why, one step back10 .option('--profile', 'show how long command takes')Update dependencies (#1524)John Geebefore adoption0b0fbbe
lines 11–22why, one step back11 .hook('preAction', (thisCommand) => {12 if (thisCommand.opts().profile) {13 console.time(timeLabel);14 }15 })16 .hook('postAction', (thisCommand) => {17 if (thisCommand.opts().profile) {18 console.timeEnd(timeLabel);19 }20 });2122programfeat: add preSubcommand hook (#1763)鸿则before adoption2c7f687
line 23why, one step back23 .option('--trace', 'display trace statements for commands')Update dependencies (#1524)John Geebefore adoption0b0fbbe
lines 24–33why, one step back24 .hook('preAction', (thisCommand, actionCommand) => {25 if (thisCommand.opts().trace) {26 console.log('>>>>');27 console.log(`About to call action handler for subcommand: ${actionCommand.name()}`);28 console.log('arguments: %O', actionCommand.args);29 console.log('options: %o', actionCommand.opts());30 console.log('<<<<');31 }32 });33feat: add preSubcommand hook (#1763)鸿则before adoption2c7f687
lines 34–43why, one step back34program35 .option('--env <filename>', 'specify environment file')36 .hook('preSubcommand', (thisCommand, subcommand) => {37 if (thisCommand.opts().env) {38 // One use case for this hook is modifying environment variables before39 // parsing the subcommand, say by reading .env file.40 console.log(`Reading ${thisCommand.opts().env}...`);41 process.env.PORT = 80;42 console.log(`About to call subcommand: ${subcommand.name()}`);43 }Update dependencies (#1524)John Geebefore adoption0b0fbbe
lines 44–45why, one step back44 });45feat: add preSubcommand hook (#1763)鸿则before adoption2c7f687
lines 46–55why, one step back46program.command('start')47 .argument('[script]', 'script name', 'server.js')48 .option('-d, --delay <seconds>', 'how long to delay before starting')49 .addOption(new Option('-p, --port <number>', 'port number').default(8080).env('PORT'))50 .action(async(script, options) => {51 if (options.delay) {52 await new Promise(resolve => setTimeout(resolve, parseInt(options.delay) * 1000));53 }54 console.log(`Starting ${script} on port ${options.port}`);55 });Update dependencies (#1524)John Geebefore adoption0b0fbbe
lines 56–60why, one step back5657// Some of the hooks or actions are async, so call parseAsync rather than parse.58program.parseAsync().then(() => {});5960// Try the following:feat: add preSubcommand hook (#1763)鸿则before adoption2c7f687
lines 61–64why, one step back61// node hook.js start62// node hook.js --trace start --port 9000 test.js63// node hook.js --profile start --delay 564// node hook.js --env=production.env start