commands/db/health.ts
db health, no registration
A command.ts per directory
One test checks the whole tree
Less registration. More building.
Add a file.
Get a command.
Folders become groups and dotted names keep small trees compact. defineCommand infers the handler arguments from your Yargs builder, so argv.port is a number without a cast.
commands/
├── db/
│ ├── health.ts → db health
│ └── migration/
│ └── command.ts → db migration
├── studio.start.ts → studio start
└── $default.ts → default command
// studio.start.ts
export const command = defineCommand({
describe: 'Start the studio',
builder: (yargs) =>
yargs.option('port', { type: 'number', default: 3000 }),
handler: (argv) => console.log(argv.port),
});Built for CLIs that keep growing
Load what runs.
Test what ships.
The tree is scanned once, but a group imports its commands only when Yargs enters it. Because a broken module in an unvisited group would otherwise hide, validateCommands imports every command in a test and checks the positional arguments, without running a handler.
import { validateCommands } from 'yargs-file-commands';
it('validates the complete command tree', async () => {
await expect(
validateCommands({
commandDirs: [import.meta.dirname + '/commands'],
}),
).resolves.toBeUndefined();
});Keep reading
Find your way around.
Short guides for the conventions, plus an API reference generated from the library source.
Start here
Getting started
Install the package, register the tree with Yargs and write your first typed command.
Read more →Naming
File conventions
How directories, dotted names, $default.ts and command.ts become commands, plus scanning and lazy loading.
Read more →Structure
Groups and overlays
Describe groups, share typed options, and layer hand-written commands over generated ones.
Read more →Reference
API reference
Generated from the TSDoc comments: fileCommands, defineCommand, validateCommands and their options.
Read more →Install
From a folder of files to a working CLI.
npm install yargs-file-commands yargsESM only. Requires Node.js 22 or newer and Yargs 18.