yargs / yargs/yargs

Nested `commandDir` with Typescript requires `visit` function to work

Open
#2,267 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug typescript
Dominant language
JavaScript
Stars
11.5k
Forks
1k
PR merge metrics
No merged PRs in 30d

Description

Using Yargs v17.6.2 with ts-node:
I'm trying to nest a couple of directories of commands using commandDir

level_1.ts

import yargs from 'yargs';

yargs(process.argv.slice(2))
  .commandDir('./level_1_commands', {
    extensions: ['ts'],
  })
  .demandCommand(1)
  .recommendCommands().argv;

level_2.ts

import { command } from 'yargs';

command('level_2 <command>', 'run all level 2 actions commands', (yargs) =>
  yargs.commandDir('./level_2_commands', {
    extensions: ['ts'],
    visit: () => {}, // required for it to work
  })
)
  .recommendCommands()
  .parseAsync();

level_3.ts

import { command } from 'yargs';

command('level_3 <command>', 'run all level 3 commands', (yargs) =>
  yargs.commandDir('./level_3_commands', {
    extensions: ['ts'],
    visit: () => {}, // required
  })
).parseAsync();

The only way for yargs to find the commands more than one level down is to include the visit param in the commandDir section. Without it the command would fail silently on the level 3 executions such as cli level_2 level_3 level3_command. I discovered the visit would make a difference while trying to debug.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the nested command setup from level_1.ts, level_2.ts, and level_3.ts using yargs v17.6.2 with ts-node. Compare commandDir behavior with and without the visit option, especially for cli level_2 level_3 level3_command; done means nested commands are discovered without the workaround and the failure is covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
cli
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.