|
| 1 | +You are an expert in creating CLI commands for the Datadog Build Plugins project. |
| 2 | +Your task is to create a new CLI command in `packages/tools/src/commands/` following the project's established patterns and conventions. |
| 3 | + |
| 4 | +If you're unsure about the purpose of the command, ask for clarifications. |
| 5 | + |
| 6 | +## Overview |
| 7 | + |
| 8 | +The project uses [Clipanion framework](https://mael.dev/clipanion/docs/) for CLI commands. |
| 9 | +Each command should be self-contained in its own directory under `packages/tools/src/commands/` with proper TypeScript implementation. |
| 10 | + |
| 11 | +## Step-by-Step Implementation |
| 12 | + |
| 13 | +### 1. Create Command Directory Structure |
| 14 | + |
| 15 | +First, create the directory structure for your new command: |
| 16 | + |
| 17 | +```bash |
| 18 | +mkdir -p packages/tools/src/commands/<command-name> |
| 19 | +``` |
| 20 | + |
| 21 | +The command name should be kebab-case (e.g., `verify-links`, `create-plugin`, `check-deps`). |
| 22 | + |
| 23 | +### 2. Create the Command Implementation |
| 24 | + |
| 25 | +Create `packages/tools/src/commands/<command-name>/index.ts` with this template: |
| 26 | + |
| 27 | +```typescript |
| 28 | +import { Command, Option } from 'clipanion'; |
| 29 | +import path from 'path'; |
| 30 | +import fs from 'fs'; |
| 31 | + |
| 32 | +import { ROOT } from '@dd/core/constants'; |
| 33 | + |
| 34 | +class YourCommandName extends Command { |
| 35 | + static paths = [['<command-name>']]; |
| 36 | + |
| 37 | + static usage = Command.Usage({ |
| 38 | + category: 'The category of the command', |
| 39 | + description: 'Brief description of what this command does in one sentence', |
| 40 | + details: ` |
| 41 | + Detailed description of the command's purpose and behavior. |
| 42 | + `, |
| 43 | + examples: [ |
| 44 | + ['Basic usage', 'yarn cli <command-name>'], |
| 45 | + ['With options', 'yarn cli <command-name> --fix'], |
| 46 | + ], |
| 47 | + }); |
| 48 | + |
| 49 | + // Define command options using Clipanion helpers |
| 50 | + fix = Option.Boolean('--fix', false, { |
| 51 | + description: 'Automatically fix issues when possible', |
| 52 | + }); |
| 53 | + |
| 54 | + async execute() { |
| 55 | + // Implementation of the command's logic. |
| 56 | + // For the dependencies, import the non native ones in the function that needs it: |
| 57 | + const { green } = await import('@dd/tools/helpers'); |
| 58 | + console.log(`Executing ${green('<command-name>')} command...`); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +export default [YourCommandName]; |
| 63 | +``` |
| 64 | + |
| 65 | +### 3. Common Patterns to Follow |
| 66 | + |
| 67 | +#### Dependencies |
| 68 | + |
| 69 | +Only import native modules at the top of the file. |
| 70 | + |
| 71 | +For non-native dependencies, import them inside the method that needs it. |
| 72 | +This helps reduce the initial load time and avoids unnecessary imports when the command is not executed. |
| 73 | + |
| 74 | +```typescript |
| 75 | +const { green } = await import('@dd/tools/helpers'); |
| 76 | +console.log(`Executing ${green('<command-name>')} command...`); |
| 77 | +``` |
| 78 | + |
| 79 | +#### Error Handling |
| 80 | + |
| 81 | +Prefer gathering errors and reporting them at the end of the command execution to avoid breaking the flow. |
| 82 | + |
| 83 | +If the command is grouping multiple workflows or operations, collect errors in a consistent format and throw one at the end listing everything. |
| 84 | + |
| 85 | +```typescript |
| 86 | +const errors: string[] = []; |
| 87 | + |
| 88 | +// Collect errors in a non blocking/breaking way, with consistent formatting |
| 89 | +errors.push(`[${red('Error|Category')}] ${file}:${line} - ${dim(message)}`); |
| 90 | + |
| 91 | +// Report all errors at the end of the execution |
| 92 | +if (errors.length > 0) { |
| 93 | + throw new Error(`Found ${errors.length} error${errors.length > 1 ? 's' : ''}`); |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +#### Progress Indicators |
| 98 | +```typescript |
| 99 | +console.log(` Processing ${green(files.length.toString())} files...`); |
| 100 | + |
| 101 | +// For long operations |
| 102 | +for (const [index, file] of files.entries()) { |
| 103 | + console.log(` [${index + 1}/${files.length}] ${dim(file)}...`); |
| 104 | + // Process file |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +### 4. Testing Your Command |
| 109 | + |
| 110 | +Test your command locally: |
| 111 | + |
| 112 | +```bash |
| 113 | +# Run your command |
| 114 | +yarn cli <command-name> |
| 115 | +yarn cli <command-name> --help |
| 116 | +yarn cli <command-name> --fix |
| 117 | +``` |
| 118 | + |
| 119 | +### 5. Code Quality Checks |
| 120 | + |
| 121 | +Before finalizing: |
| 122 | + |
| 123 | +```bash |
| 124 | +# Format your code |
| 125 | +yarn format packages/tools/src/commands/<command-name> |
| 126 | + |
| 127 | +# Check types |
| 128 | +yarn typecheck:all |
| 129 | +``` |
| 130 | + |
| 131 | +### 6. Documentation |
| 132 | + |
| 133 | +Update the main documentation: |
| 134 | + |
| 135 | +1. Add command to README.md if it's user-facing |
| 136 | +2. Update CONTRIBUTING.md if it's a development tool |
| 137 | +3. Add inline comments for complex logic |
| 138 | + |
| 139 | +## Example Commands for Reference |
| 140 | + |
| 141 | +Look at these existing commands for patterns: |
| 142 | +- `integrity/index.ts` - Complex multi-phase command |
| 143 | +- `create-plugin/index.ts` - Interactive command with prompts |
| 144 | +- `bump/index.ts` - Command with external tool integration |
| 145 | + |
| 146 | +## Best Practices |
| 147 | + |
| 148 | +1. **Keep it focused**: Each command should do one thing well |
| 149 | +2. **Use existing utilities**: Leverage `@dd/core` helpers |
| 150 | +3. **Consistent output**: Use colors consistently (green for success, red for errors, yellow for warnings) available in `@dd/tools/helpers` |
| 151 | +4. **Graceful errors**: Always catch and report errors clearly |
| 152 | +5. **Progress feedback**: Show users what's happening during long operations |
| 153 | +6. **Exit codes**: Return 0 for success, 1 for errors |
| 154 | + |
| 155 | +Remember: CLI commands are the primary interface for developers. Make them intuitive, fast, and reliable. |
0 commit comments