Creating Custom Commands
Extend Xec's functionality by creating custom commands that integrate seamlessly with the built-in command system.
Overview
Xec supports dynamic command loading from .xec/commands/
directories. Custom commands are JavaScript or TypeScript files that export command definitions compatible with the Commander.js framework used internally by Xec.
Command Structure
Custom commands must follow this basic structure:
/**
* Command description (optional)
* This will be available as: xec my-command [args...]
*/
export default function command(program) {
program
.command('my-command [args...]')
.description('A custom command')
.option('-v, --verbose', 'Enable verbose output')
.action(async (args, options) => {
// Your command logic here
});
}
Loading Mechanism
Discovery Process
Xec discovers commands using the following search pattern:
-
Primary locations (in order):
.xec/commands/
in current directory.xec/cli/
in current directory- Parent directories (up to 3 levels)
-
Environment paths:
- Additional paths from
XEC_COMMANDS_PATH
environment variable (colon-separated)
- Additional paths from
-
File patterns:
.js
,.mjs
,.ts
,.tsx
extensions- Excludes test files (
.test.js
,.spec.ts
, etc.) - Excludes hidden files and type definition files
Command Registration
Commands are loaded in this order:
- Built-in commands (from Xec core)
- Dynamic commands (from directories above)
- Dynamic commands override built-in commands if they share the same name