@ikas/component-cli 0.83.0 → 0.86.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/dist/commands/create.d.ts.map +1 -1
  2. package/dist/commands/create.js +34 -793
  3. package/dist/commands/create.js.map +1 -1
  4. package/dist/utils/component-helpers.d.ts.map +1 -1
  5. package/dist/utils/component-helpers.js +9 -65
  6. package/dist/utils/component-helpers.js.map +1 -1
  7. package/dist/utils/observer-runtime.d.ts.map +1 -1
  8. package/dist/utils/observer-runtime.js +2 -37
  9. package/dist/utils/observer-runtime.js.map +1 -1
  10. package/dist/utils/template.d.ts +8 -0
  11. package/dist/utils/template.d.ts.map +1 -0
  12. package/dist/utils/template.js +24 -0
  13. package/dist/utils/template.js.map +1 -0
  14. package/package.json +3 -2
  15. package/templates/add/component.css +13 -0
  16. package/templates/add/component.tsx +12 -0
  17. package/templates/add/section.css +17 -0
  18. package/templates/add/section.tsx +14 -0
  19. package/templates/create/README.md +50 -0
  20. package/templates/create/claude-md +258 -0
  21. package/templates/create/cursorrules +107 -0
  22. package/templates/create/gitignore +5 -0
  23. package/templates/create/ikas.config.json +95 -0
  24. package/templates/create/mcp.json +10 -0
  25. package/templates/create/package.json +21 -0
  26. package/templates/create/src/components/ExampleComponent/index.tsx +22 -0
  27. package/templates/create/src/components/ExampleComponent/styles.css +36 -0
  28. package/templates/create/src/components/ExampleComponent/types.ts +7 -0
  29. package/templates/create/src/components/ExampleSection/index.tsx +14 -0
  30. package/templates/create/src/components/ExampleSection/styles.css +29 -0
  31. package/templates/create/src/components/ExampleSection/types.ts +6 -0
  32. package/templates/create/src/components/index.ts +2 -0
  33. package/templates/create/src/global-types.ts +5 -0
  34. package/templates/create/src/global.css +12 -0
  35. package/templates/create/src/ikas-component-utils.d.ts +3 -0
  36. package/templates/create/tsconfig.json +30 -0
  37. package/templates/create/vite.config.ts +15 -0
  38. package/templates/observer-runtime.js +34 -0
@@ -5,6 +5,7 @@ import inquirer from "inquirer";
5
5
  import ora from "ora";
6
6
  import * as path from "path";
7
7
  import { generateComponentId, generateProjectId } from "../utils/component-helpers.js";
8
+ import { readTemplate } from "../utils/template.js";
8
9
  const PROP_TYPES = [
9
10
  "TEXT",
10
11
  "NUMBER",
@@ -41,801 +42,41 @@ async function createProject(projectName) {
41
42
  fs.mkdirSync(projectPath, { recursive: true });
42
43
  fs.mkdirSync(path.join(projectPath, "src/components/ExampleComponent"), { recursive: true });
43
44
  fs.mkdirSync(path.join(projectPath, "src/components/ExampleSection"), { recursive: true });
44
- // Create package.json
45
- const packageJson = {
46
- name: projectName,
47
- version: "1.0.0",
48
- type: "module",
49
- scripts: {
50
- dev: "ikas-component dev",
51
- build: "ikas-component build",
52
- add: "ikas-component add",
53
- publish: "ikas-component publish"
54
- },
55
- dependencies: {
56
- preact: "^10.19.3"
57
- },
58
- devDependencies: {
59
- "@ikas/component-cli": "*",
60
- "@ikas/bp-storefront": "*",
61
- "@ikas/code-components-mcp": "*",
62
- typescript: "^5.3.0",
63
- vite: "^5.0.0"
64
- }
65
- };
66
- fs.writeFileSync(path.join(projectPath, "package.json"), JSON.stringify(packageJson, null, 2));
67
- // Create tsconfig.json
68
- const tsConfig = {
69
- compilerOptions: {
70
- target: "ES2022",
71
- lib: ["DOM", "DOM.Iterable", "ES2022"],
72
- module: "ESNext",
73
- moduleResolution: "bundler",
74
- jsx: "react-jsx",
75
- jsxImportSource: "preact",
76
- strict: true,
77
- esModuleInterop: true,
78
- skipLibCheck: true,
79
- forceConsistentCasingInFileNames: true,
80
- resolveJsonModule: true,
81
- declaration: true,
82
- declarationMap: true,
83
- outDir: "./dist",
84
- rootDir: "./src"
85
- },
86
- include: ["src/**/*"],
87
- exclude: ["node_modules", "dist"]
88
- };
89
- fs.writeFileSync(path.join(projectPath, "tsconfig.json"), JSON.stringify(tsConfig, null, 2));
90
- // Create vite.config.ts
91
- const viteConfig = `import { defineConfig } from "vite";
92
- import preact from "@preact/preset-vite";
93
-
94
- export default defineConfig({
95
- plugins: [preact()],
96
- build: {
97
- lib: {
98
- entry: "./src/components/index.ts",
99
- formats: ["es"]
100
- },
101
- rollupOptions: {
102
- external: ["preact", "preact/hooks", "@ikas/component-utils"]
103
- }
104
- }
105
- });
106
- `;
107
- fs.writeFileSync(path.join(projectPath, "vite.config.ts"), viteConfig);
108
- // Create ikas.config.json
45
+ // Generate IDs for the example config
109
46
  const projectId = generateProjectId();
110
- const ikasConfig = {
111
- name: projectName,
112
- version: "1.0.0",
113
- projectId,
114
- globalStyles: "./src/global.css",
115
- components: [
116
- {
117
- id: generateComponentId(projectId, "example-component"),
118
- name: "Example Component",
119
- entry: "./src/components/ExampleComponent/index.tsx",
120
- styles: "./src/components/ExampleComponent/styles.css",
121
- props: [
122
- {
123
- name: "title",
124
- displayName: "Title",
125
- type: "TEXT",
126
- required: true,
127
- defaultValue: "Hello World",
128
- description: "The main title text"
129
- },
130
- {
131
- name: "description",
132
- displayName: "Description",
133
- type: "TEXT",
134
- required: false,
135
- defaultValue: "This is an example ikas code component built with Preact.",
136
- description: "The description text below the title"
137
- },
138
- {
139
- name: "showButton",
140
- displayName: "Show Button",
141
- type: "BOOLEAN",
142
- required: false,
143
- defaultValue: true,
144
- description: "Whether to show the action button"
145
- },
146
- {
147
- name: "buttonText",
148
- displayName: "Button Text",
149
- type: "TEXT",
150
- required: false,
151
- defaultValue: "Click Me",
152
- description: "The text displayed on the button"
153
- }
154
- ]
155
- },
156
- {
157
- id: generateComponentId(projectId, "example-section"),
158
- name: "Example Section",
159
- entry: "./src/components/ExampleSection/index.tsx",
160
- styles: "./src/components/ExampleSection/styles.css",
161
- type: "section",
162
- props: [
163
- {
164
- name: "heading",
165
- displayName: "Heading",
166
- type: "TEXT",
167
- required: true,
168
- defaultValue: "Welcome to Our Store",
169
- description: "The section heading text",
170
- groupId: "content"
171
- },
172
- {
173
- name: "subtitle",
174
- displayName: "Subtitle",
175
- type: "TEXT",
176
- required: false,
177
- defaultValue: "Discover our latest products and collections.",
178
- description: "The subtitle text below the heading",
179
- groupId: "content"
180
- },
181
- {
182
- name: "backgroundColor",
183
- displayName: "Background Color",
184
- type: "COLOR",
185
- required: false,
186
- description: "The section background color",
187
- groupId: "appearance"
188
- }
189
- ],
190
- propGroups: [
191
- { id: "content", name: "Content", description: "Text and copy settings" },
192
- { id: "appearance", name: "Appearance", description: "Colors and visual style" }
193
- ]
194
- }
195
- ]
47
+ const exampleComponentId = generateComponentId(projectId, "example-component");
48
+ const exampleSectionId = generateComponentId(projectId, "example-section");
49
+ const vars = {
50
+ PROJECT_NAME: projectName,
51
+ PROJECT_ID: projectId,
52
+ EXAMPLE_COMPONENT_ID: exampleComponentId,
53
+ EXAMPLE_SECTION_ID: exampleSectionId,
196
54
  };
197
- fs.writeFileSync(path.join(projectPath, "ikas.config.json"), JSON.stringify(ikasConfig, null, 2));
198
- // Create ExampleComponent
199
- const exampleComponentTsx = `import { Props } from "./types";
200
-
201
- export function ExampleComponent({
202
- title,
203
- description = "This is an example ikas code component built with Preact.",
204
- showButton,
205
- buttonText = "Click Me",
206
- }: Props) {
207
- return (
208
- <div className="example-component">
209
- <h1 className="example-title">{title}</h1>
210
- <p className="example-description">{description}</p>
211
- {showButton && (
212
- <button className="example-button">
213
- {buttonText}
214
- </button>
215
- )}
216
- </div>
217
- );
218
- }
219
-
220
- export default ExampleComponent;
221
- `;
222
- fs.writeFileSync(path.join(projectPath, "src/components/ExampleComponent/index.tsx"), exampleComponentTsx);
223
- // Create component types
224
- const componentTypes = `// Auto-generated types based on ikas.config.json props
225
- export interface Props {
226
- title: string;
227
- description?: string;
228
- showButton?: boolean;
229
- buttonText?: string;
230
- }
231
- `;
232
- fs.writeFileSync(path.join(projectPath, "src/components/ExampleComponent/types.ts"), componentTypes);
233
- // Create component styles
234
- const componentStyles = `.example-component {
235
- padding: 24px;
236
- border-radius: 8px;
237
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
238
- color: white;
239
- font-family: system-ui, -apple-system, sans-serif;
240
- }
241
-
242
- .example-title {
243
- margin: 0 0 12px 0;
244
- font-size: 28px;
245
- font-weight: 700;
246
- }
247
-
248
- .example-description {
249
- margin: 0 0 20px 0;
250
- font-size: 16px;
251
- opacity: 0.9;
252
- }
253
-
254
- .example-button {
255
- padding: 12px 24px;
256
- border: none;
257
- border-radius: 6px;
258
- background: white;
259
- color: #667eea;
260
- font-size: 16px;
261
- font-weight: 600;
262
- cursor: pointer;
263
- transition: transform 0.2s, box-shadow 0.2s;
264
- }
265
-
266
- .example-button:hover {
267
- transform: translateY(-2px);
268
- box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
269
- }
270
- `;
271
- fs.writeFileSync(path.join(projectPath, "src/components/ExampleComponent/styles.css"), componentStyles);
272
- // Create ExampleSection
273
- const exampleSectionTsx = `import { Props } from "./types";
274
-
275
- export function ExampleSection({ heading, subtitle, backgroundColor }: Props) {
276
- return (
277
- <section className="example-section" style={backgroundColor ? { backgroundColor } : undefined}>
278
- <div className="example-section-inner">
279
- <h2 className="example-section-heading">{heading}</h2>
280
- {subtitle && <p className="example-section-subtitle">{subtitle}</p>}
281
- </div>
282
- </section>
283
- );
284
- }
285
-
286
- export default ExampleSection;
287
- `;
288
- fs.writeFileSync(path.join(projectPath, "src/components/ExampleSection/index.tsx"), exampleSectionTsx);
289
- // Create section types
290
- const sectionTypes = `// Auto-generated types based on ikas.config.json props
291
- export interface Props {
292
- heading: string;
293
- subtitle?: string;
294
- backgroundColor?: string;
295
- }
296
- `;
297
- fs.writeFileSync(path.join(projectPath, "src/components/ExampleSection/types.ts"), sectionTypes);
298
- // Create section styles
299
- const sectionStyles = `.example-section {
300
- width: 100%;
301
- padding: 64px 24px;
302
- background: #f8f9fa;
303
- }
304
-
305
- .example-section-inner {
306
- max-width: 1200px;
307
- margin: 0 auto;
308
- text-align: center;
309
- }
310
-
311
- .example-section-heading {
312
- margin: 0 0 16px 0;
313
- font-size: 36px;
314
- font-weight: 700;
315
- color: #1a1a2e;
316
- font-family: system-ui, -apple-system, sans-serif;
317
- }
318
-
319
- .example-section-subtitle {
320
- margin: 0;
321
- font-size: 18px;
322
- color: #6b7280;
323
- font-family: system-ui, -apple-system, sans-serif;
324
- max-width: 600px;
325
- margin-left: auto;
326
- margin-right: auto;
327
- }
328
- `;
329
- fs.writeFileSync(path.join(projectPath, "src/components/ExampleSection/styles.css"), sectionStyles);
330
- // Create global CSS file
331
- const globalCss = `/*
332
- * Global styles for your ikas code components project.
333
- * These styles are NOT scoped — they apply across all components.
334
- * Use this for CSS resets, base typography, utility classes, or CSS variables.
335
- *
336
- * Loading order:
337
- * 1. Global CSS (this file) — lowest specificity
338
- * 2. Shared chunk CSS — multi-scope (.cc_a .sel, .cc_b .sel)
339
- * 3. Component CSS — single scope (.cc_id .sel) — highest specificity
340
- *
341
- * Component-scoped styles naturally override these globals.
342
- */
343
- `;
344
- fs.writeFileSync(path.join(projectPath, "src/global.css"), globalCss);
345
- // Create empty global types file
346
- const globalTypes = `// Auto-generated global type definitions for ikas code components.
347
- // Custom enum types from the editor will appear here.
348
- // This file is regenerated automatically — do not edit manually.
349
-
350
- export {};
351
- `;
352
- fs.writeFileSync(path.join(projectPath, "src/global-types.ts"), globalTypes);
353
- // Create components index
354
- const componentsIndex = `export { ExampleComponent } from "./ExampleComponent/index";
355
- export { ExampleSection } from "./ExampleSection/index";
356
- `;
357
- fs.writeFileSync(path.join(projectPath, "src/components/index.ts"), componentsIndex);
358
- // Create README
359
- const readme = `# ${projectName}
360
-
361
- An ikas code components project.
362
-
363
- ## Getting Started
364
-
365
- 1. Install dependencies:
366
- \`\`\`bash
367
- npm install
368
- \`\`\`
369
-
370
- 2. Start the development server:
371
- \`\`\`bash
372
- npm run dev
373
- \`\`\`
374
-
375
- 3. Open the ikas editor and connect to the dev server from the Dev Components panel.
376
-
377
- ## Commands
378
-
379
- - \`npm run dev\` - Start development server with live editor updates
380
- - \`npm run build\` - Build components for production
381
- - \`npm run add\` - Add a new component to the project
382
-
383
- ## Project Structure
384
-
385
- \`\`\`
386
- ${projectName}/
387
- ├── src/
388
- │ ├── components/
389
- │ │ ├── ExampleComponent/ # A child component (type: "component")
390
- │ │ │ ├── index.tsx
391
- │ │ │ ├── styles.css
392
- │ │ │ └── types.ts
393
- │ │ ├── ExampleSection/ # A page-level section (type: "section")
394
- │ │ │ ├── index.tsx
395
- │ │ │ ├── styles.css
396
- │ │ │ └── types.ts
397
- │ │ └── index.ts
398
- │ ├── global.css # Unscoped global styles
399
- │ └── global-types.ts # Auto-generated shared enum types
400
- ├── ikas.config.json
401
- ├── package.json
402
- ├── tsconfig.json
403
- └── vite.config.ts
404
- \`\`\`
405
-
406
- ## Sections vs Components
407
-
408
- There are two types of code components:
409
-
410
- - **Sections** are page-level, full-width containers (e.g. Header, Hero Banner, Footer).
411
- Set \`"type": "section"\` on the component entry in \`ikas.config.json\`.
412
- - **Components** are child elements placed inside sections (e.g. buttons, cards, badges).
413
- No \`type\` field needed — it defaults to \`"component"\`.
414
-
415
- ## Adding Props
416
-
417
- Edit \`ikas.config.json\` to add props to your components. Available prop types:
418
-
419
- - \`TEXT\` - String input
420
- - \`NUMBER\` - Number input
421
- - \`BOOLEAN\` - Checkbox toggle
422
- - \`IMAGE\` - Image picker
423
- - \`LINK\` - Link/URL input
424
- - \`LIST_OF_LINK\` - List of navigation links
425
- - \`COLOR\` - Color picker
426
- - \`PRODUCT\` - Single product selector
427
- - \`PRODUCT_LIST\` - Multiple products selector
428
- - \`CATEGORY\` - Single category selector
429
- - \`CATEGORY_LIST\` - Multiple categories selector
430
- - \`BRAND\` - Single brand selector
431
- - \`BRAND_LIST\` - Multiple brands selector
432
- - \`BLOG\` - Single blog post selector
433
- - \`BLOG_LIST\` - Multiple blog posts selector
434
- - \`BLOG_CATEGORY\` - Single blog category selector
435
- - \`BLOG_CATEGORY_LIST\` - Multiple blog categories selector
436
-
437
- ## Building for Production
438
-
439
- Run \`npm run build\` to compile your components. The output will be ready to upload to the ikas editor.
440
- `;
441
- fs.writeFileSync(path.join(projectPath, "README.md"), readme);
442
- // Create .gitignore
443
- const gitignore = `node_modules/
444
- dist/
445
- .DS_Store
446
- *.log
447
- `;
448
- fs.writeFileSync(path.join(projectPath, ".gitignore"), gitignore);
449
- // Create type declarations for virtual modules
450
- const componentUtilsTypes = `declare module "@ikas/component-utils" {
451
- export function observer<T extends (props: any) => any>(component: T): T;
452
- }
453
- `;
454
- fs.writeFileSync(path.join(projectPath, "src/ikas-component-utils.d.ts"), componentUtilsTypes);
455
- // Create CLAUDE.md for Claude Code AI assistant
456
- const claudeMd = `# ikas Code Components Project
457
-
458
- You are building **Preact + TypeScript components for an e-commerce storefront**.
459
- This project uses the ikas Code Components framework with an MCP server for API documentation.
460
-
461
- **ALWAYS query MCP tools before writing code that uses storefront APIs.**
462
- Never guess function signatures or type shapes — the MCP server is the source of truth.
463
-
464
- ## CRITICAL: Auto-Generated Files
465
-
466
- **NEVER manually create or edit \`types.ts\` or \`global-types.ts\`** — they are auto-generated by CLI commands.
467
- Use \`npx ikas-component config add-component --props '[...]'\` or \`npx ikas-component config add-prop\` to manage props.
468
- These commands update \`ikas.config.json\`, \`types.ts\`, and \`global-types.ts\` automatically.
469
-
470
- ## MCP Tools (12 tools)
471
-
472
- ### Starting a New Section
473
- | Tool | Purpose |
474
- |------|---------|
475
- | \`get_section_template(sectionType)\` | **Start here** — production-ready starter template (index.tsx + types.ts + styles.css + config) for common sections: header, footer, product-detail, product-list, cart, login, register, hero-banner, blog-post, faq, etc. |
476
- | \`get_framework_guide(topic)\` | Framework docs — "ai-workflow", "common-pitfalls", "prop-types", "css-scoping", "form-handling", "imports", "sections-vs-components" |
477
- | \`get_code_example(task)\` | Code examples — "add to cart", "variant selection", "image-handling", "product-detail-section", "cart-section" |
478
-
479
- ### Looking Up APIs
480
- | Tool | Purpose |
481
- |------|---------|
482
- | \`search_docs(query)\` | Search across all storefront API docs and framework guides |
483
- | \`get_function_doc(name)\` | Full docs for a specific function (e.g. "addItemToCart", "Router.navigate") |
484
- | \`list_functions(category?)\` | List functions by category: ProductDetail, Cart, ProductList, Navigation, Customer, Order, Image, Blog, Brand, Category, Pricing, Form, Validation, Pagination, Filtering |
485
- | \`get_prop_types()\` | All available ikas.config.json prop types with TS types and examples |
486
-
487
- ### Exploring Types
488
- | Tool | Purpose |
489
- |------|---------|
490
- | \`get_model_guide(model)\` | **One-stop-shop** — type definition + all utility functions + examples + related types for a model (e.g. "IkasProduct", "IkasOrder") |
491
- | \`get_type_definition(name)\` | Full definition of a type or enum (e.g. "IkasProduct", "IkasOrderStatus") |
492
- | \`get_functions_for_type(typeName)\` | All utility functions for a type (e.g. "IkasImage") |
493
- | \`search_types(query)\` | Search types/enums by keyword (e.g. "price", "address", "status") |
494
- | \`list_types(domain?, kind?)\` | List all types, optionally filtered by domain ("product", "cart", "order") or kind ("type", "enum") |
495
-
496
- ## CLI Commands
497
-
498
- **Never edit ikas.config.json or types.ts manually** — use the CLI commands:
499
-
500
- | Command | Purpose |
501
- |---------|---------|
502
- | \`npx ikas-component config add-component --name "Name" --type section --props '[...]'\` | **Primary** — create component with all props in one command |
503
- | \`npx ikas-component config add-component --name "Name" --type section\` | Create component with no props (add later) |
504
- | \`npx ikas-component config add-prop --component "Name" --name title --displayName "Title" --type TEXT --required [--group content]\` | Add a single prop incrementally |
505
- | \`npx ikas-component config update-prop --component "Name" --prop title --type RICH_TEXT [--group colors]\` | Update a prop's type or group |
506
- | \`npx ikas-component config remove-prop --component "Name" --prop title\` | Remove a prop |
507
- | \`npx ikas-component config remove-component --name "Name"\` | Remove a component |
508
- | \`npx ikas-component config add-prop-group --component "Name" --id colors --name "Colors" [--description "..."] [--parent style]\` | Create a prop group |
509
- | \`npx ikas-component config update-prop-group --component "Name" --id colors [--name "..."] [--description "..."]\` | Update a prop group |
510
- | \`npx ikas-component config remove-prop-group --component "Name" --id colors\` | Remove a prop group |
511
- | \`npx ikas-component config list\` | List all components and their props |
512
- | \`npx ikas-component check --json\` | Type-check all components, output errors as JSON |
513
- | \`npx ikas-component build\` | Compile server.js + client.js + styles.css per component |
514
- | \`npx ikas-component dev\` | Start dev server (Vite 5200 + WebSocket 5201) |
515
-
516
- ## Workflow: Building a New Section
517
-
518
- 1. \`get_section_template("product-detail")\` → get starter files + CLI command with --props
519
- 2. Run the CLI command (creates component + props + types.ts in one step)
520
- 3. Write \`index.tsx\` and \`styles.css\` using the template (do NOT edit types.ts — it's already generated)
521
- 4. Look up APIs → \`get_model_guide("IkasProduct")\`, \`get_function_doc("addItemToCart")\`
522
- 5. \`npx ikas-component check --json\` → fix type errors
523
- 6. \`npx ikas-component build\` → verify clean build
524
-
525
- ## Sub-Component Structure
526
-
527
- **ALWAYS create sub-components in \`src/sub-components/\` with their own folder containing \`index.tsx\` and \`styles.css\`. NEVER create flat .tsx files inside a component folder.**
528
-
529
- \`\`\`
530
- src/
531
- ├── components/
532
- │ ├── ProductList/
533
- │ │ ├── index.tsx # imports from ../../sub-components/...
534
- │ │ ├── types.ts # auto-generated (CLI-managed)
535
- │ │ └── styles.css # @import "../../sub-components/.../styles.css"
536
- │ └── index.ts
537
- ├── sub-components/
538
- │ ├── ProductCard/
539
- │ │ ├── index.tsx # sub-component with inline Props
540
- │ │ └── styles.css
541
- │ └── FilterSidebar/
542
- │ ├── index.tsx
543
- │ └── styles.css
544
- ├── global.css # unscoped global styles
545
- └── global-types.ts # auto-generated shared enum types
546
- \`\`\`
547
-
548
- **Key rules:**
549
- - \`src/components/\` = registered in ikas.config.json. \`src/sub-components/\` = internal helpers (NOT in ikas.config.json)
550
- - Sub-components do NOT have \`types.ts\` — define \`Props\` inline in \`index.tsx\`
551
- - Custom enum types are exported from \`src/global-types.ts\` (auto-generated). Sub-components can import them: \`import type { MyEnum } from "../../global-types";\`
552
- - CSS: use \`@import "../../sub-components/ProductCard/styles.css";\` in parent styles.css
553
- - TSX: use \`import ProductCard from "../../sub-components/ProductCard";\`
554
- - Sub-components that read MobX stores need \`observer()\`, others don't
555
- - Sub-components can be shared across multiple parent sections
556
-
557
- \`\`\`tsx
558
- // src/sub-components/ProductCard/index.tsx — inline Props, no types.ts
559
- import { IkasProduct, getSelectedProductVariant } from "@ikas/bp-storefront";
560
-
561
- interface Props { product: IkasProduct; }
562
-
563
- export default function ProductCard({ product }: Props) { ... }
564
- \`\`\`
565
-
566
- ## Key Patterns
567
-
568
- ### Component Exports & Reactivity
569
- \`\`\`tsx
570
- // Root components are automatically reactive — the ikas runtime wraps them in autorun().
571
- // Do NOT import or use observer() on root exports.
572
- import { Props } from "./types";
573
-
574
- export function MySection({ product }: Props) { ... }
575
- export default MySection;
576
- \`\`\`
577
-
578
- \`\`\`tsx
579
- // Only use observer() on extracted SUB-COMPONENTS that independently read MobX stores:
580
- import { observer } from "@ikas/component-utils";
581
-
582
- const CartBadge = observer(function CartBadge() {
583
- const count = cartStore.order?.orderLineItems?.length ?? 0;
584
- return <span>{count}</span>;
585
- });
586
- \`\`\`
587
-
588
- ### Null Safety
589
- \`\`\`tsx
590
- // Storefront models can be null — always check before accessing
591
- if (!product) return null;
592
- const price = product.selectedVariant?.price?.formattedPrice ?? "";
593
- \`\`\`
594
-
595
- ### Mutations
596
- \`\`\`tsx
597
- // Functions like addItemToCart() mutate the model in-place (MobX observable)
598
- // No need to capture return value — root components re-render automatically via autorun()
599
- await addItemToCart({ product });
600
- \`\`\`
601
-
602
- ### COMPONENT & COMPONENT_LIST Props
603
- These props let you create **slot-based** architectures where store owners can drag child components into your section/component from the editor.
604
-
605
- - \`COMPONENT\` — a single child component slot (TS type: \`any\`)
606
- - \`COMPONENT_LIST\` — a list of child components (TS type: \`any\`)
607
-
608
- **Rendering:** Use the \`<IkasComponentRenderer>\` wrapper to render child components:
609
- \`\`\`tsx
610
- import { IkasComponentRenderer } from "@ikas/bp-storefront";
611
- import { Props } from "./types";
612
-
613
- export function MySection({ title, cardList, ...props }: Props) {
614
- return (
615
- <section>
616
- <h2>{title}</h2>
617
- <div className="cards">
618
- {/* COMPONENT_LIST — render a list of child components */}
619
- <IkasComponentRenderer
620
- components={cardList as any[]}
621
- parentProps={props}
622
- />
623
- </div>
624
- </section>
625
- );
626
- }
627
- export default MySection;
628
- \`\`\`
629
-
630
- **Key rules:**
631
- - Always pass \`parentProps={props}\` so child components can access parent data via dynamic values
632
- - Cast the prop value: \`components={myList as any[]}\` for COMPONENT_LIST, \`components={[myComp] as any[]}\` for COMPONENT
633
- - \`<IkasComponentRenderer>\` handles rendering, styling, and reactivity of child components automatically
634
-
635
- **Config example:**
636
- \`\`\`json
637
- {
638
- "name": "cardList",
639
- "displayName": "Card List",
640
- "type": "COMPONENT_LIST"
641
- }
642
- \`\`\`
643
-
644
- ### CSS Scoping
645
- All CSS is auto-scoped with \`.cc_{componentId}\` prefix. Write plain class names — they are scoped at build time.
646
-
647
- ### Preact Event Handling
648
- Use \`onInput\` instead of \`onChange\` for text inputs (Preact behavior).
649
-
650
- ### Import Source
651
- \`\`\`tsx
652
- import { addItemToCart, IkasProduct, IkasImage } from "@ikas/bp-storefront";
653
- \`\`\`
654
-
655
- ## No Static Text Rule
656
-
657
- **CRITICAL: Never hardcode user-visible text in JSX.** All text strings (headings, button labels,
658
- empty states, loading messages, form labels) MUST be TEXT props with defaultValues.
659
-
660
- Wrong: \`<h1>Sign In</h1>\`
661
- Correct: \`<h1>{title}</h1>\` with \`title\` as TEXT prop (defaultValue: "Sign In")
662
-
663
- For button loading states, use two separate props:
664
- - \`submitButtonText\` (default: "Sign In")
665
- - \`submittingButtonText\` (default: "Signing in...")
666
-
667
- Group text props under a "Texts" propGroup when the component has 5+ props.
668
-
669
- ## Sections vs Components
670
-
671
- - **Sections** = page-level, full-width containers (Header, Hero, Product Grid, Footer).
672
- Set \`"type": "section"\` via CLI. Use \`<section>\` root element. Props interface: \`Props\`.
673
- **Sections MUST always include a \`backgroundColor\` COLOR prop** (default: \`#ffffff\`).
674
- Apply via inline style: \`style={backgroundColor ? { backgroundColor } : undefined}\`
675
- Consider also adding \`textColor\` COLOR props for text elements directly on the section background.
676
- - **Components** = child elements placed inside sections (buttons, cards, badges).
677
- Defaults to \`"component"\`. Use \`<div>\` root element. Props interface: \`Props\`.
678
-
679
- ## Prop Groups
680
-
681
- Organize props into collapsible groups in the editor sidebar. Use prop groups when a component has 5+ props to improve UX.
682
-
683
- ### Config Format
684
- - Define groups in \`propGroups\` array on the component in \`ikas.config.json\`
685
- - Assign props to groups via \`groupId\` on each prop
686
- - Groups can nest 1 level deep via \`children\`
687
- - Props without \`groupId\` appear ungrouped at root level
688
- - Group IDs must be unique within a component
689
-
690
- ### CLI Commands
691
- - \`config add-prop-group --component "Name" --id content --name "Content"\` — create group
692
- - \`config add-prop-group --component "Name" --id colors --name "Colors" --parent style\` — create nested group
693
- - \`config add-prop --component "Name" --name title --displayName "Title" --type TEXT --group content\` — add prop to group
694
- - \`config update-prop --component "Name" --prop title --group colors\` — move prop to different group
695
- - \`config remove-prop-group --component "Name" --id content\` — remove group (props become ungrouped)
696
-
697
- ### When to Use
698
- - 5+ props → group related props
699
- - Sections with content + style props → separate "Content" and "Appearance" groups
700
- - Complex sections → use nested groups (e.g., "Style" → "Colors", "Typography")
701
-
702
- ## Workflow Addition
703
-
704
- When building a section with 5+ props, organize into prop groups after defining props:
705
- \`\`\`bash
706
- npx ikas-component config add-prop-group --component "Name" --id content --name "Content"
707
- npx ikas-component config update-prop --component "Name" --prop title --group content
708
- \`\`\`
709
-
710
- ## Build Verification
711
-
712
- IMPORTANT: After completing any task, always run \`npx ikas-component build\` (or \`npm run build\`) to check for
713
- TypeScript and build errors. Fix any errors before considering the task done.
714
- `;
715
- fs.writeFileSync(path.join(projectPath, "CLAUDE.md"), claudeMd);
716
- // Create .cursorrules for Cursor AI assistant
717
- const cursorrules = `# ikas Code Components
718
-
719
- You are building **Preact + TypeScript components for an e-commerce storefront**.
720
- **ALWAYS query MCP tools before writing code that uses storefront APIs.**
721
-
722
- ## CRITICAL: Auto-Generated Files
723
-
724
- **NEVER manually create or edit \`types.ts\` or \`global-types.ts\`** — they are auto-generated by CLI commands.
725
- Use \`npx ikas-component config add-component --props '[...]'\` or \`npx ikas-component config add-prop\` to manage props.
726
- These commands update \`ikas.config.json\`, \`types.ts\`, and \`global-types.ts\` automatically.
727
-
728
- ## MCP Tools (12 tools)
729
-
730
- ### Starting a New Section
731
- - get_section_template(sectionType) — **Start here** — production-ready starter template for common sections (header, footer, product-detail, product-list, cart, login, hero-banner, blog-post, faq, etc.)
732
- - get_framework_guide(topic) — Framework docs ("ai-workflow", "common-pitfalls", "prop-types", "css-scoping", "form-handling", "imports")
733
- - get_code_example(task) — Code examples ("add to cart", "variant selection", "image-handling")
734
-
735
- ### Looking Up APIs
736
- - search_docs(query) — Search all storefront API docs and framework guides
737
- - get_function_doc(name) — Full docs for a function (e.g. "addItemToCart", "Router.navigate")
738
- - list_functions(category?) — List functions by category (ProductDetail, Cart, ProductList, Navigation, Customer, Order, Image, Blog, Brand, Pricing, Form, Validation, Pagination, Filtering)
739
- - get_prop_types() — All available ikas.config.json prop types
740
-
741
- ### Exploring Types
742
- - get_model_guide(model) — **One-stop-shop** — type def + utility functions + examples for a model (e.g. "IkasProduct", "IkasOrder")
743
- - get_type_definition(name) — Full definition of a type or enum
744
- - get_functions_for_type(typeName) — All utility functions for a type
745
- - search_types(query) — Search types/enums by keyword ("price", "address", "status")
746
- - list_types(domain?, kind?) — List all types, filter by domain or kind ("type"/"enum")
747
-
748
- ## CLI Commands (never edit ikas.config.json or types.ts manually)
749
- - npx ikas-component config add-component --name "Name" --type section --props '[...]' — **Primary** — create with all props in one command
750
- - npx ikas-component config add-component --name "Name" --type section — Create component with no props
751
- - npx ikas-component config add-prop --component "Name" --name title --displayName "Title" --type TEXT --required [--group content] — Add a prop incrementally
752
- - npx ikas-component config update-prop --component "Name" --prop title --type RICH_TEXT [--group colors] — Update a prop or group
753
- - npx ikas-component config remove-prop --component "Name" --prop title — Remove a prop
754
- - npx ikas-component config remove-component --name "Name" — Remove a component
755
- - npx ikas-component config add-prop-group --component "Name" --id colors --name "Colors" [--parent style] — Create a prop group
756
- - npx ikas-component config update-prop-group --component "Name" --id colors [--name "..."] — Update a prop group
757
- - npx ikas-component config remove-prop-group --component "Name" --id colors — Remove a prop group
758
- - npx ikas-component config list — List all components and their props
759
- - npx ikas-component check --json — Type-check all components
760
- - npx ikas-component build — Compile server.js + client.js + styles.css per component
761
- - npx ikas-component dev — Start dev server (Vite 5200 + WebSocket 5201)
762
-
763
- ## Workflow: Building a New Section
764
- 1. get_section_template("product-detail") → get starter files + CLI command with --props
765
- 2. Run the CLI command (creates component + props + types.ts in one step)
766
- 3. Write index.tsx and styles.css using the template (do NOT edit types.ts — it's already generated)
767
- 4. Look up APIs: get_model_guide("IkasProduct"), get_function_doc("addItemToCart")
768
- 5. npx ikas-component check --json → fix type errors
769
- 6. npx ikas-component build → verify clean build
770
-
771
- ## Sub-Component Structure
772
- **ALWAYS create sub-components in \`src/sub-components/\` with their own folder containing \`index.tsx\` and \`styles.css\`.**
773
- - \`src/components/\` = registered in ikas.config.json. \`src/sub-components/\` = internal helpers (NOT in ikas.config.json)
774
- - Sub-components do NOT have \`types.ts\` — define \`Props\` inline in \`index.tsx\`
775
- - Custom enum types are exported from \`src/global-types.ts\` (auto-generated). Sub-components can import them: \`import type { MyEnum } from "../../global-types";\`
776
- - CSS: \`@import "../../sub-components/ProductCard/styles.css";\` in parent styles.css
777
- - TSX: \`import ProductCard from "../../sub-components/ProductCard";\`
778
- - Sub-components that read MobX stores need \`observer()\`, others don't
779
- - Sub-components can be shared across multiple parent sections
780
- - NEVER create flat .tsx files inside a component folder
781
-
782
- ## Key Patterns
783
- - Root components are automatically reactive (ikas runtime uses autorun). Do NOT use observer() on root exports.
784
- Pattern: \`export function MySection({ title }: Props) { ... }\` + \`export default MySection;\`
785
- Only use observer() on extracted sub-components that independently read MobX stores.
786
- - Null safety: storefront models can be null — always check before accessing
787
- - Mutations: addItemToCart() etc. mutate MobX observables in-place — no return capture needed
788
- - COMPONENT & COMPONENT_LIST props: slot-based child components rendered via \`<IkasComponentRenderer>\`
789
- \`import { IkasComponentRenderer } from "@ikas/bp-storefront"\`
790
- \`<IkasComponentRenderer components={myList as any[]} parentProps={props} />\`
791
- Always pass \`parentProps={props}\` so children can access parent data. Cast with \`as any[]\`.
792
- - CSS: write plain class names — auto-scoped with .cc_{componentId} at build time
793
- - Events: use onInput (not onChange) for text inputs (Preact behavior)
794
- - Imports: \`import { addItemToCart, IkasProduct } from "@ikas/bp-storefront"\`
795
-
796
- ## No Static Text Rule
797
- **CRITICAL: Never hardcode user-visible text in JSX.** All text strings (headings, button labels,
798
- empty states, loading messages, form labels) MUST be TEXT props with defaultValues.
799
- Wrong: \`<h1>Sign In</h1>\`
800
- Correct: \`<h1>{title}</h1>\` with \`title\` as TEXT prop (defaultValue: "Sign In")
801
- For button loading states, use two separate props (e.g., \`submitButtonText\` + \`submittingButtonText\`).
802
- Group text props under a "Texts" propGroup when the component has 5+ props.
803
-
804
- ## Sections vs Components
805
- - Sections = page-level containers (Header, Hero, Product Grid, Footer)
806
- Set "type": "section" via CLI. Use <section> root element. Props interface: Props.
807
- **Sections MUST always include a \`backgroundColor\` COLOR prop** (default: \`#ffffff\`).
808
- Apply via inline style: \`style={backgroundColor ? { backgroundColor } : undefined}\`
809
- Consider also adding \`textColor\` COLOR props for text elements directly on the section background.
810
- - Components = child elements inside sections (buttons, cards, badges)
811
- Defaults to "component". Use <div> root element. Props interface: Props.
812
-
813
- ## Prop Groups
814
- Organize 5+ props into collapsible groups in editor sidebar.
815
- - Define groups in \`propGroups\` on component in ikas.config.json
816
- - Assign props via \`groupId\` on each prop
817
- - Nest 1 level deep with \`children\`
818
- - Group IDs must be unique within a component
819
- - CLI: add-prop-group, update-prop-group, remove-prop-group
820
- - Add prop to group: add-prop --group <groupId> or update-prop --group <groupId>
821
-
822
- ## Build Verification
823
- IMPORTANT: After completing any task, always run \`npx ikas-component build\` (or \`npm run build\`)
824
- to check for TypeScript and build errors. Fix any errors before considering the task done.
825
- `;
826
- fs.writeFileSync(path.join(projectPath, ".cursorrules"), cursorrules);
827
- // Create .mcp.json for MCP server configuration
828
- // Uses npx which resolves the bin through node_modules hierarchy,
829
- // working both in standalone projects and monorepo workspaces.
830
- const mcpConfig = {
831
- mcpServers: {
832
- "ikas-code-components": {
833
- command: "npx",
834
- args: ["ikas-mcp"]
835
- }
836
- }
837
- };
838
- fs.writeFileSync(path.join(projectPath, ".mcp.json"), JSON.stringify(mcpConfig, null, 2));
55
+ // Write all template files
56
+ const files = [
57
+ { template: "create/package.json", dest: "package.json", vars },
58
+ { template: "create/tsconfig.json", dest: "tsconfig.json" },
59
+ { template: "create/vite.config.ts", dest: "vite.config.ts" },
60
+ { template: "create/ikas.config.json", dest: "ikas.config.json", vars },
61
+ { template: "create/gitignore", dest: ".gitignore" },
62
+ { template: "create/mcp.json", dest: ".mcp.json" },
63
+ { template: "create/README.md", dest: "README.md", vars },
64
+ { template: "create/claude-md", dest: "CLAUDE.md" },
65
+ { template: "create/cursorrules", dest: ".cursorrules" },
66
+ { template: "create/src/global.css", dest: "src/global.css" },
67
+ { template: "create/src/global-types.ts", dest: "src/global-types.ts" },
68
+ { template: "create/src/ikas-component-utils.d.ts", dest: "src/ikas-component-utils.d.ts" },
69
+ { template: "create/src/components/index.ts", dest: "src/components/index.ts" },
70
+ { template: "create/src/components/ExampleComponent/index.tsx", dest: "src/components/ExampleComponent/index.tsx" },
71
+ { template: "create/src/components/ExampleComponent/types.ts", dest: "src/components/ExampleComponent/types.ts" },
72
+ { template: "create/src/components/ExampleComponent/styles.css", dest: "src/components/ExampleComponent/styles.css" },
73
+ { template: "create/src/components/ExampleSection/index.tsx", dest: "src/components/ExampleSection/index.tsx" },
74
+ { template: "create/src/components/ExampleSection/types.ts", dest: "src/components/ExampleSection/types.ts" },
75
+ { template: "create/src/components/ExampleSection/styles.css", dest: "src/components/ExampleSection/styles.css" },
76
+ ];
77
+ for (const file of files) {
78
+ fs.writeFileSync(path.join(projectPath, file.dest), readTemplate(file.template, file.vars));
79
+ }
839
80
  // Create or merge .cursor/mcp.json for Cursor editor
840
81
  // Uses node + ${workspaceFolder} instead of npx because Cursor
841
82
  // doesn't reliably inherit the shell PATH.