|
|
7 months ago | |
|---|---|---|
| .. | ||
| commands | 7 months ago | |
| shared | 7 months ago | |
| utilities | 1 year ago | |
| README.md | 7 months ago | |
| cli.ts | 7 months ago | |
| constants.ts | 1 year ago | |
| types.ts | 1 year ago | |
This document describes the new array-based CLI command structure that allows for easy declaration and management of CLI commands.
The CLI now uses a structured approach where all commands are defined in an array of CliCommandDefinition objects, making it easy to add, remove, and modify commands.
interface CliCommandDefinition {
name: string; // The command name (e.g., 'add', 'migrate')
description: string; // Command description shown in help
options?: CliCommandOption[]; // Optional array of command options
action: (options?: Record<string, any>) => Promise<void>; // Command implementation
}
interface CliCommandOption {
flag: string; // Option flag (e.g., '-f, --file <path>')
description: string; // Option description
required?: boolean; // Whether the option is required
defaultValue?: any; // Default value for the option
}
To add a new command, simply add it to the cliCommands array in packages/cli/src/commands/command-declarations.ts:
export const cliCommands: CliCommandDefinition[] = [
// ... existing commands ...
{
name: 'new-command',
description: 'Description of the new command',
options: [
{
flag: '-o, --option <value>',
description: 'Description of the option',
required: true,
},
],
action: async (options) => {
// Command implementation
console.log('Command executed with options:', options);
process.exit(0);
},
},
];
{
name: 'add',
description: 'Add a feature to your Vendure project',
action: async () => {
const { addCommand } = await import('./add/add');
await addCommand();
process.exit(0);
},
}
{
name: 'example',
description: 'Example command with options',
options: [
{
flag: '-f, --file <path>',
description: 'Path to the file',
required: true,
},
{
flag: '-v, --verbose',
description: 'Enable verbose output',
required: false,
defaultValue: false,
},
],
action: async (options) => {
if (options) {
console.log('File path:', options.file);
console.log('Verbose mode:', options.verbose);
}
process.exit(0);
},
}
packages/cli/src/shared/cli-command-definition.ts - Interface definitionspackages/cli/src/shared/command-registry.ts - Command registration utilitypackages/cli/src/commands/command-declarations.ts - Command declarations arraypackages/cli/src/cli.ts - Main CLI entry point