Skip to content
Myr

hook.js

This file

Path

examples/hook.js

71 lines · 2.2 KiB · JavaScript

Last changed by

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

History of this file

Take it

The walk stopped after 200 commits: lines older than 3103c6b point at it.

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 1–2why, one step back
1#!/usr/bin/env node
2

Use standard import in examples (#2168)John Geebefore adoption41b12cf

line 3why, one step back
3const { Command, Option } = require('commander');

feat: add preSubcommand hook (#1763)鸿则before adoption2c7f687

line 4why, one step back
4const program = new Command();

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 5–9why, one step back
5
6// This example shows using some hooks for life cycle events.78const timeLabel = 'command duration';9program

feat: add preSubcommand hook (#1763)鸿则before adoption2c7f687

line 10why, one step back
10 .option('--profile', 'show how long command takes')

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 11–22why, one step back
11 .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 });2122program

feat: add preSubcommand hook (#1763)鸿则before adoption2c7f687

line 23why, one step back
23 .option('--trace', 'display trace statements for commands')

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 24–26why, one step back
24 .hook('preAction', (thisCommand, actionCommand) => {
25 if (thisCommand.opts().trace) {26 console.log('>>>>');

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 27–29why, one step back
27 console.log(
28 `About to call action handler for subcommand: ${actionCommand.name()}`,29 );

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 30–35why, one step back
30 console.log('arguments: %O', actionCommand.args);
31 console.log('options: %o', actionCommand.opts());32 console.log('<<<<');33 }34 });35

feat: add preSubcommand hook (#1763)鸿则before adoption2c7f687

lines 36–45why, one step back
36program
37 .option('--env <filename>', 'specify environment file')38 .hook('preSubcommand', (thisCommand, subcommand) => {39 if (thisCommand.opts().env) {40 // One use case for this hook is modifying environment variables before41 // parsing the subcommand, say by reading .env file.42 console.log(`Reading ${thisCommand.opts().env}...`);43 process.env.PORT = 80;44 console.log(`About to call subcommand: ${subcommand.name()}`);45 }

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 46–47why, one step back
46 });
47

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 48–49why, one step back
48program
49 .command('start')

feat: add preSubcommand hook (#1763)鸿则before adoption2c7f687

lines 50–51why, one step back
50 .argument('[script]', 'script name', 'server.js')
51 .option('-d, --delay <seconds>', 'how long to delay before starting')

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 52–55why, one step back
52 .addOption(
53 new Option('-p, --port <number>', 'port number').default(8080).env('PORT'),54 )55 .action(async (script, options) => {

feat: add preSubcommand hook (#1763)鸿则before adoption2c7f687

line 56why, one step back
56 if (options.delay) {

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 57–59why, one step back
57 await new Promise((resolve) =>
58 setTimeout(resolve, parseInt(options.delay) * 1000),59 );

feat: add preSubcommand hook (#1763)鸿则before adoption2c7f687

lines 60–62why, one step back
60 }
61 console.log(`Starting ${script} on port ${options.port}`);62 });

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 63–67why, one step back
63
64// Some of the hooks or actions are async, so call parseAsync rather than parse.65program.parseAsync().then(() => {});6667// Try the following:

feat: add preSubcommand hook (#1763)鸿则before adoption2c7f687

lines 68–71why, one step back
68// node hook.js start
69// node hook.js --trace start --port 9000 test.js70// node hook.js --profile start --delay 571// node hook.js --env=production.env start