Skip to content

Commit 372bcda

Browse files
authored
Merge pull request #853 from AndrewLPetersonSF/chore/template-docs
W-21198400: (chore): add AI documentation and best practices
2 parents 67cd5dd + d428cc4 commit 372bcda

6 files changed

Lines changed: 690 additions & 0 deletions

File tree

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
---
2+
name: add-template-generator
3+
description: Add a new template generator command to the CLI
4+
---
5+
Use this workflow whenever exposing a generator from salesforcedx-templates to CLI users.
6+
7+
# Add Template Generator Command Workflow
8+
9+
This workflow guides you through adding a new template generator command that exposes a template from `salesforcedx-templates` to the Salesforce CLI.
10+
11+
## Prerequisites
12+
13+
- Template generator already exists in `salesforcedx-templates` repository
14+
- You know the metadata type name and any subtemplates
15+
- You have the repo cloned and dependencies installed
16+
17+
## Step 1: Bootstrap the Command
18+
19+
// turbo
20+
Run the command generator to create the initial command structure:
21+
22+
```bash
23+
sf dev generate command -n template:generate:{metadataType}:{optionalSubTemplate} --no-unit
24+
```
25+
26+
**Notes:**
27+
- Replace `{metadataType}` with your metadata type (e.g., `flexipage`, `apex`)
28+
- Only add `{optionalSubTemplate}` if you need nested generators (e.g., `digital-experience:site`)
29+
- This creates the command file, updates oclif metadata, and adds NUTs
30+
31+
## Step 2: Update package.json Topics
32+
33+
Open `package.json` and locate the `oclif.topics` section. Under `template > generate`, add a description for your new subtopic:
34+
35+
```json
36+
{
37+
"oclif": {
38+
"topics": {
39+
"template": {
40+
"subtopics": {
41+
"generate": {
42+
"subtopics": {
43+
"{metadataType}": {
44+
"description": "Commands for generating {metadataType} metadata"
45+
}
46+
}
47+
}
48+
}
49+
}
50+
}
51+
}
52+
}
53+
```
54+
55+
## Step 3: Set Command State (Optional)
56+
57+
If your command is not GA-ready, add a state to the command class:
58+
59+
```typescript
60+
public static readonly state = 'beta'; // or 'preview'
61+
```
62+
63+
**State options:**
64+
- `beta`: Shows beta warning to users
65+
- `preview`: Shows preview warning to users
66+
- No state: Command is GA (requires backwards compatibility)
67+
68+
If you want to hide the command from docs and autocomplete:
69+
70+
```typescript
71+
public static readonly hidden = true;
72+
```
73+
74+
**Note:** Hidden commands won't be included in release notes.
75+
76+
## Step 4: Verify File Path
77+
78+
Ensure your command file follows the correct path convention:
79+
80+
- **Single top-level generator**: `src/commands/template/generate/{metadataType}/index.ts`
81+
- **Nested generator**: `src/commands/template/generate/{metadataType}/{subTemplate}.ts`
82+
83+
## Step 5: Define CLI Flags
84+
85+
Before defining flags, inspect the generator TypeScript interface in `salesforcedx-templates`. The interface is the source of truth for flag structure. If it is unavailable, request it instead of guessing.
86+
87+
**Important:** Do NOT add flags manually. The `sf dev generate flag` command is interactive and requires the user to run it themselves—the agent cannot respond to its prompts. When adding flags, instruct the user to run the command and provide guidance for the interactive flow.
88+
89+
### Agent instructions for adding flags
90+
91+
1. Tell user to run `sf dev generate flag` from plugin root.
92+
2. **Flow order:** (1) select command, (2) type (string/boolean/etc.), (3) name (kebab-case), (4) summary, (5) short name (optional), (6) required?, (7) multiple?
93+
3. List flags to add with suggested name, type, summary from generator interface.
94+
4. After user completes: (a) add mappings to options object if command passes opts to `runGenerator`; (b) if generator created `messages/template.generate.{metadataType}.md` instead of updating the existing file, merge those entries into `messages/{metadataType}.md` and delete the generated file.
95+
96+
### Running the flag generator
97+
98+
```bash
99+
sf dev generate flag
100+
```
101+
102+
This will:
103+
- Add the flag to your command's `flags` object
104+
- Generate TypeScript types
105+
- Add entries to the `messages.md` file
106+
107+
**Common flags to consider:**
108+
- `--name` / `-n`: Name of the generated item (usually required)
109+
- `--output-dir` / `-d`: Output directory (default: '.')
110+
- `--template` / `-t`: Template type selection (if multiple templates)
111+
- `--api-version`: API version override
112+
113+
## Step 6: Review Message Files
114+
115+
Check `messages/{metadataType}.md` (merge from `template.generate.{metadataType}.md` if generator created a separate file) and ensure:
116+
- Summary is clear and concise
117+
- Description provides helpful context
118+
- Flag descriptions are detailed and explain constraints
119+
- Examples are practical and cover common use cases
120+
121+
**Optional:** Add links to developer.salesforce.com docs in descriptions if helpful.
122+
123+
## Step 7: Implement the run() Method
124+
125+
Update the `run()` method to call `runGenerator`:
126+
127+
```typescript
128+
import { runGenerator } from '../../utils/templateCommand.js';
129+
130+
public async run(): Promise<CreateOutput> {
131+
const { flags } = await this.parse(CommandClass);
132+
133+
// Add any pre-processing or validation here
134+
135+
return runGenerator({
136+
templateType: TemplateType.{YourMetadataType},
137+
opts: flags,
138+
ux: new Ux({ jsonEnabled: this.jsonEnabled() }),
139+
});
140+
}
141+
```
142+
143+
## Step 8: Write/Update NUTs
144+
145+
Review the auto-generated NUTs in `test/commands/template/generate/{metadataType}/`. Add tests to validate:
146+
- Required flags work correctly
147+
- Optional flags are respected
148+
- Correct files are created in the right locations
149+
- Flag combinations work as expected
150+
151+
Most template generators don't require scratch org connections.
152+
153+
## Step 9: Test Locally
154+
155+
// turbo
156+
Build and link the plugin:
157+
158+
```bash
159+
yarn build
160+
sf plugins link .
161+
```
162+
163+
Test your command:
164+
165+
```bash
166+
sf template generate {metadataType} --name TestExample --output-dir ./test-output
167+
```
168+
169+
Verify the generated files are correct.
170+
171+
## Step 10: Run Tests
172+
173+
// turbo
174+
Run the NUTs to ensure everything works:
175+
176+
```bash
177+
yarn test
178+
```
179+
180+
## Local Development with salesforcedx-templates
181+
182+
If you're also working on the template in `salesforcedx-templates`:
183+
184+
1. Ensure you're using yarn 1
185+
2. Update `package.json` to reference your local `salesforcedx-templates`:
186+
```json
187+
"dependencies": {
188+
"salesforcedx-templates": "file:../path/to/salesforcedx-templates"
189+
}
190+
```
191+
3. After template changes: `yarn build` in salesforcedx-templates
192+
4. Then: `yarn install --force && yarn build` in plugin-templates
193+
194+
## Troubleshooting
195+
196+
If your command does not appear:
197+
198+
- confirm file path matches convention
199+
- run yarn build
200+
- ensure oclif topics updated
201+
- relink plugin:
202+
203+
sf plugins unlink @salesforce/plugin-templates
204+
sf plugins link .
205+
206+
## Final Validation Checklist
207+
208+
Before opening PR ensure:
209+
210+
- command runs locally
211+
- files generate correctly
212+
- flags validated
213+
- messages documented
214+
- NUTs pass
215+
- topics updated

.claude/skills/concise/SKILL.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
name: concise
3+
description: when creating/modifying md files in skills/rules for AI
4+
disable-model-invocation: false
5+
---
6+
7+
# Concise
8+
9+
We want to save tokens and preserve context window size.
10+
11+
- use fragments/bullets, not full sentences
12+
- remove as many words as possible without altering meaning
13+
- cut repetition
14+
- prefer shorter words that mean the same thing

0 commit comments

Comments
 (0)