|
| 1 | +# Page Designer Decorator Tool |
| 2 | + |
| 3 | +Tool for adding Page Designer decorators to React components using native TypeScript template literals. |
| 4 | + |
| 5 | +## 🎯 Overview |
| 6 | + |
| 7 | +This tool analyzes React components and generates Page Designer decorators (`@Component`, `@AttributeDefinition`, `@RegionDefinition`) to make components available in Page Designer for Storefront Next. |
| 8 | + |
| 9 | +## ✨ Key Features |
| 10 | + |
| 11 | +- **Name-Based Lookup**: Find components by name (e.g., "ProductCard") without knowing paths |
| 12 | +- **Auto-Discovery**: Automatically searches common component directories |
| 13 | +- **Type-Safe**: Full TypeScript type inference for all contexts |
| 14 | +- **Fast**: Direct function execution, no file I/O or compilation overhead |
| 15 | +- **Flexible Input**: Supports component names or file paths |
| 16 | +- **Two Modes**: Auto mode for quick setup, Interactive mode for fine-tuned control |
| 17 | + |
| 18 | +## 📁 File Structure |
| 19 | + |
| 20 | +``` |
| 21 | +page-designer-decorator/ |
| 22 | +├── analyzer.ts # Component parsing and analysis |
| 23 | +├── rules.ts # Rule loader and exports |
| 24 | +├── index.ts # Main tool implementation |
| 25 | +├── rules/ |
| 26 | +│ ├── 1-mode-selection.ts # Entry point |
| 27 | +│ ├── 2a-auto-mode.ts # Auto mode workflow |
| 28 | +│ ├── 2b-0-interactive-overview.ts # Interactive workflow overview |
| 29 | +│ ├── 2b-1-interactive-analyze.ts # Step 1: Analysis |
| 30 | +│ ├── 2b-2-interactive-select-props.ts # Step 2: Selection |
| 31 | +│ ├── 2b-3-interactive-configure-attrs.ts # Step 3: Configuration |
| 32 | +│ ├── 2b-4-interactive-configure-regions.ts # Step 4: Regions |
| 33 | +│ └── 2b-5-interactive-confirm-generation.ts # Step 5: Generation |
| 34 | +└── templates/ |
| 35 | + └── decorator-generator.ts # Decorator code generation |
| 36 | +``` |
| 37 | + |
| 38 | +## 🚀 Usage |
| 39 | + |
| 40 | +### Basic Usage (Name-Based - Recommended) |
| 41 | + |
| 42 | +```bash |
| 43 | +# By component name (automatically finds the file) |
| 44 | +add_page_designer_decorator({ |
| 45 | + component: "ProductCard", |
| 46 | + autoMode: true |
| 47 | +}) |
| 48 | + |
| 49 | +# Interactive mode |
| 50 | +add_page_designer_decorator({ |
| 51 | + component: "Hero", |
| 52 | + conversationContext: { step: "analyze" } |
| 53 | +}) |
| 54 | + |
| 55 | +# With custom search paths (for unusual locations) |
| 56 | +add_page_designer_decorator({ |
| 57 | + component: "ProductCard", |
| 58 | + searchPaths: ["packages/retail/src", "app/features"], |
| 59 | + autoMode: true |
| 60 | +}) |
| 61 | +``` |
| 62 | + |
| 63 | +### Path-Based Usage |
| 64 | + |
| 65 | +```bash |
| 66 | +# If you prefer to specify the exact path |
| 67 | +add_page_designer_decorator({ |
| 68 | + component: "src/components/ProductCard.tsx", |
| 69 | + autoMode: true |
| 70 | +}) |
| 71 | +``` |
| 72 | + |
| 73 | +### Workflow |
| 74 | + |
| 75 | +1. **Component Discovery**: Provide name (e.g., "ProductCard") or path |
| 76 | +2. **Mode Selection**: Choose Auto or Interactive mode |
| 77 | +3. **Analysis** (Interactive only): Review component props |
| 78 | +4. **Selection** (Interactive only): Select which props to expose |
| 79 | +5. **Configuration** (Interactive only): Configure types and defaults |
| 80 | +6. **Regions** (Interactive only): Configure nested content areas |
| 81 | +7. **Generation**: Get decorator code |
| 82 | + |
| 83 | +### Component Discovery |
| 84 | + |
| 85 | +The tool automatically searches for components in these locations (in order): |
| 86 | + |
| 87 | +1. `src/components/**` (PascalCase and kebab-case) |
| 88 | +2. `app/components/**` |
| 89 | +3. `components/**` |
| 90 | +4. `src/**` (broader search) |
| 91 | +5. Custom paths (if provided via `searchPaths`) |
| 92 | + |
| 93 | +**Examples:** |
| 94 | + |
| 95 | +- `"ProductCard"` → finds `src/components/product-tile/ProductCard.tsx` |
| 96 | +- `"Hero"` → finds `src/components/hero/Hero.tsx` or `app/components/hero.tsx` |
| 97 | +- `"product-card"` → finds `src/components/product-card.tsx` or `product-card/index.tsx` |
| 98 | + |
| 99 | +**Tips:** |
| 100 | + |
| 101 | +- Use component name for portability |
| 102 | +- Use path for unusual locations |
| 103 | +- Add `searchPaths` for monorepos or non-standard structures |
| 104 | + |
| 105 | +## 🏗️ Architecture |
| 106 | + |
| 107 | +### Rule Rendering |
| 108 | + |
| 109 | +Rules are pure TypeScript functions that return strings: |
| 110 | + |
| 111 | +```typescript |
| 112 | +${context.hasEditableProps |
| 113 | + ? context.editableProps.map(prop => |
| 114 | + `- \`${prop.name}\` (${prop.type})` |
| 115 | + ).join('\n') |
| 116 | + : '' |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +### Type Safety |
| 121 | + |
| 122 | +Every rule has a strongly-typed context interface: |
| 123 | + |
| 124 | +```typescript |
| 125 | +export interface AnalyzeStepContext { |
| 126 | + componentName: string; |
| 127 | + file: string; |
| 128 | + hasEditableProps: boolean; |
| 129 | + editableProps: PropInfo[]; |
| 130 | + // ... more fields |
| 131 | +} |
| 132 | + |
| 133 | +export function renderAnalyzeStep(context: AnalyzeStepContext): string { |
| 134 | + // TypeScript checks all variable access at compile time |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +### Template Generation |
| 139 | + |
| 140 | +Code generation uses pure functions: |
| 141 | + |
| 142 | +```typescript |
| 143 | +export function generateDecoratorCode(context: MetadataContext): string { |
| 144 | + const imports = generateImports(context); |
| 145 | + const decorator = generateComponentDecorator(context); |
| 146 | + const attributes = generateAttributes(context); |
| 147 | + |
| 148 | + return `${imports}${decorator}\nexport class ${context.metadataClassName} {\n${attributes}\n}`; |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | +## 📦 Build Process |
| 153 | + |
| 154 | +All rules and templates are compiled into the JavaScript output: |
| 155 | + |
| 156 | +```json |
| 157 | +{ |
| 158 | + "scripts": { |
| 159 | + "build": "tsc" |
| 160 | + } |
| 161 | +} |
| 162 | +``` |
| 163 | + |
| 164 | +## 🎯 When to Use This Tool |
| 165 | + |
| 166 | +Use this tool when: |
| 167 | + |
| 168 | +- ✅ You need to add Page Designer support to React components |
| 169 | +- ✅ You want automatic component discovery by name |
| 170 | +- ✅ You prefer type-safe decorator generation |
| 171 | +- ✅ You need both quick auto-mode and detailed interactive workflows |
| 172 | + |
| 173 | +## 🔧 Development |
| 174 | + |
| 175 | +### Adding a New Rule |
| 176 | + |
| 177 | +1. Create a new file in `rules/`: |
| 178 | + |
| 179 | +```typescript |
| 180 | +// rules/my-new-rule.ts |
| 181 | +export interface MyRuleContext { |
| 182 | + message: string; |
| 183 | +} |
| 184 | + |
| 185 | +export function renderMyRule(context: MyRuleContext): string { |
| 186 | + return `# My Rule\n\n${context.message}`; |
| 187 | +} |
| 188 | +``` |
| 189 | + |
| 190 | +2. Export it from `rules.ts`: |
| 191 | + |
| 192 | +```typescript |
| 193 | +import {renderMyRule, type MyRuleContext} from './rules/my-new-rule.js'; |
| 194 | + |
| 195 | +export const pageDesignerDecoratorRules = { |
| 196 | + // ... existing rules |
| 197 | + getMyRule(context: MyRuleContext): string { |
| 198 | + return renderMyRule(context); |
| 199 | + }, |
| 200 | +}; |
| 201 | +``` |
| 202 | + |
| 203 | +3. Use it in `index.ts`: |
| 204 | + |
| 205 | +```typescript |
| 206 | +const instructions = pageDesignerDecoratorRules.getMyRule({ |
| 207 | + message: 'Hello World', |
| 208 | +}); |
| 209 | +``` |
| 210 | + |
| 211 | +### Modifying Code Generation |
| 212 | + |
| 213 | +Edit `templates/decorator-generator.ts` directly. Changes require recompilation. |
| 214 | + |
| 215 | +## 📊 Performance |
| 216 | + |
| 217 | +The tool uses direct function execution with no file I/O or compilation overhead. Typical tool invocations complete in under 1ms. |
| 218 | + |
| 219 | +## ✅ Testing |
| 220 | + |
| 221 | +```bash |
| 222 | +pnpm build |
| 223 | +pnpm test |
| 224 | +``` |
| 225 | + |
| 226 | +Comprehensive test suite covers all workflow modes, component discovery, and error handling. |
| 227 | + |
| 228 | +## 🎓 Learning Resources |
| 229 | + |
| 230 | +- [Template Literals (MDN)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) |
| 231 | +- [TypeScript Handbook](https://www.typescriptlang.org/docs/handbook/intro.html) |
| 232 | +- [MCP Tools Documentation](https://modelcontextprotocol.io/docs) |
| 233 | + |
| 234 | +## 📝 License |
| 235 | + |
| 236 | +Apache-2.0 - Copyright (c) 2025, Salesforce, Inc. |
0 commit comments