Skip to content
Myr

hook.js

This file

Path

examples/hook.js

71 lines · 2.2 KiB · JavaScript

Last changed by

Switching to esm-only (#2464)John Geebefore adoptionf4bd470

History of this file

Take it

The walk stopped after 200 commits: lines older than 471ec40 point at it.

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

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

Switching to esm-only (#2464)John Geebefore adoptionf4bd470

line 3why, one step back
3import { Command, Option } from 'commander';

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 4–26why, one step back
4const program = new Command();
56// This example shows using some hooks for life cycle events.78const timeLabel = 'command duration';9program10 .option('--profile', 'show how long command takes')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 });2122program23 .option('--trace', 'display trace statements for commands')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 copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 30–47why, one step back
30 console.log('arguments: %O', actionCommand.args);
31 console.log('options: %o', actionCommand.opts());32 console.log('<<<<');33 }34 });3536program37 .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 }46 });47

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

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

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

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) => {

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

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 );

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 60–71why, one step back
60 }
61 console.log(`Starting ${script} on port ${options.port}`);62 });6364// Some of the hooks or actions are async, so call parseAsync rather than parse.65program.parseAsync().then(() => {});6667// Try the following:68// node hook.js start69// node hook.js --trace start --port 9000 test.js70// node hook.js --profile start --delay 571// node hook.js --env=production.env start