HouseinIsProgramming a3d3f40569 refactor(cli): Separate long- and shorthands for options 7 months ago
..
commands a3d3f40569 refactor(cli): Separate long- and shorthands for options 7 months ago
shared a3d3f40569 refactor(cli): Separate long- and shorthands for options 7 months ago
utilities 9379d7329c fix(cli): Fix relative import path for parent dirs 1 year ago
README.md 92f71ea1fa refactor(cli): Refactor cli to use declarative array approach 7 months ago
cli.ts 92f71ea1fa refactor(cli): Refactor cli to use declarative array approach 7 months ago
constants.ts 4ea7711f49 fix(cli): Various fixes to CLI add commands 1 year ago
types.ts 795b013c51 feat(cli): Implement "add" command for ui extensions 1 year ago

README.md

Vendure CLI Command Structure

This document describes the new array-based CLI command structure that allows for easy declaration and management of CLI commands.

Overview

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.

Command Definition Interface

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
}

Option Definition Interface

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
}

Adding New Commands

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);
        },
    },
];

Examples

Simple Command (No Options)

{
    name: 'add',
    description: 'Add a feature to your Vendure project',
    action: async () => {
        const { addCommand } = await import('./add/add');
        await addCommand();
        process.exit(0);
    },
}

Command with Options

{
    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);
    },
}

Benefits

  1. Centralized Management: All commands are defined in one place
  2. Type Safety: Full TypeScript support with proper interfaces
  3. Easy Extension: Adding new commands is straightforward
  4. Consistent Structure: All commands follow the same pattern
  5. Option Support: Built-in support for command options with validation

File Structure

  • packages/cli/src/shared/cli-command-definition.ts - Interface definitions
  • packages/cli/src/shared/command-registry.ts - Command registration utility
  • packages/cli/src/commands/command-declarations.ts - Command declarations array
  • packages/cli/src/cli.ts - Main CLI entry point