@thorprovider/create-thor-app 3.1.3 → 4.0.1
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.
- package/README.md +6 -55
- package/dist/index.js +154 -626
- package/package.json +2 -2
- package/templates/default/.agents/AGENTS.md +27 -0
- package/templates/default/.env.example +8 -30
- package/templates/default/.eslintrc.json +20 -27
- package/templates/default/README.md +52 -123
- package/templates/default/ai-validator.json +47 -29
- package/templates/default/app/api/revalidate/route.ts +27 -36
- package/templates/default/app/backend-unavailable/page.tsx +3 -7
- package/templates/default/app/error.tsx +1 -2
- package/templates/default/app/global-error.tsx +3 -3
- package/templates/default/app/globals.css +1 -71
- package/templates/default/app/layout.tsx +16 -38
- package/templates/default/app/not-found.tsx +1 -1
- package/templates/default/app/page.tsx +10 -119
- package/templates/default/components/providers/app-providers.tsx +19 -68
- package/templates/default/components/providers/cart-provider.tsx +143 -0
- package/templates/default/components/providers/commerce-provider.tsx +33 -0
- package/templates/default/components/providers/region-provider.tsx +105 -0
- package/templates/default/docs/README.md +46 -37
- package/templates/default/docs/commerce-api.md +58 -125
- package/templates/default/docs/patterns.md +128 -67
- package/templates/default/docs/project-structure.md +58 -50
- package/templates/default/docs/rules.md +43 -42
- package/templates/default/docs/types.md +105 -1024
- package/templates/default/lib/commerce.ts +33 -132
- package/templates/default/lib/env.ts +24 -94
- package/templates/default/lib/utils.ts +18 -0
- package/templates/default/next-env.d.ts +1 -1
- package/templates/default/next.config.ts +7 -0
- package/templates/default/package.json +2 -8
- package/templates/default/registry.json +6 -0
- package/templates/default/styles/tokens.css +81 -0
- package/templates/default/components/providers/navigation-provider.tsx +0 -37
- package/templates/default/config/site.config.ts +0 -156
- package/templates/default/docs/components.md +0 -282
- package/templates/default/docs/dependencies.md +0 -61
- package/templates/default/lib/auth-context.tsx +0 -213
- package/templates/default/lib/cart-provider.tsx +0 -53
- package/templates/default/lib/commerce-provider.tsx +0 -48
- package/templates/default/lib/region-detection.ts +0 -73
- package/templates/default/lib/region-provider.tsx +0 -53
- package/templates/default/lib/regions-config.ts +0 -48
- package/templates/default/lib/storefront-config.ts +0 -54
- package/templates/default/next.config.mjs +0 -51
- package/templates/default/proxy.ts +0 -155
- package/templates/default/scripts/generate-regions-config.ts +0 -498
package/dist/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
// src/index.ts
|
|
4
4
|
import { program } from "commander";
|
|
5
|
-
import { resolve, relative as
|
|
5
|
+
import { resolve, relative as relative2 } from "path";
|
|
6
6
|
import { createInterface as createInterface2 } from "readline/promises";
|
|
7
7
|
|
|
8
8
|
// src/helpers/template.ts
|
|
@@ -65,11 +65,11 @@ function createCopyFilter(sourceRoot) {
|
|
|
65
65
|
// src/create-app.ts
|
|
66
66
|
import {
|
|
67
67
|
cpSync,
|
|
68
|
-
existsSync as
|
|
68
|
+
existsSync as existsSync8,
|
|
69
69
|
mkdirSync as mkdirSync3,
|
|
70
70
|
rmSync
|
|
71
71
|
} from "fs";
|
|
72
|
-
import { join as
|
|
72
|
+
import { join as join8, relative } from "path";
|
|
73
73
|
|
|
74
74
|
// src/helpers/logger.ts
|
|
75
75
|
var c = {
|
|
@@ -86,7 +86,7 @@ var bold = (msg) => `${c.bold}${msg}${c.reset}`;
|
|
|
86
86
|
|
|
87
87
|
// src/helpers/package-json.ts
|
|
88
88
|
import { existsSync as existsSync2, readFileSync, readdirSync, writeFileSync } from "fs";
|
|
89
|
-
import { join as join2
|
|
89
|
+
import { join as join2 } from "path";
|
|
90
90
|
function getPackageVersions(monorepoRoot) {
|
|
91
91
|
const versions = {};
|
|
92
92
|
const packagesDir = join2(monorepoRoot, "packages");
|
|
@@ -138,27 +138,12 @@ function updatePackageJson(opts) {
|
|
|
138
138
|
pkg.dependencies = resolveWorkspaceDeps(pkg.dependencies, versions);
|
|
139
139
|
pkg.devDependencies = resolveWorkspaceDeps(pkg.devDependencies, versions);
|
|
140
140
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
...pkg.scripts,
|
|
146
|
-
prebuild: generateCmd,
|
|
147
|
-
predev: generateCmd,
|
|
148
|
-
"type-check": `${generateCmd} && tsc --noEmit`,
|
|
149
|
-
"generate:regions": generateCmd
|
|
150
|
-
};
|
|
151
|
-
} else {
|
|
152
|
-
const generateCmd = "tsx scripts/generate-regions-config.ts --package-dir=.";
|
|
153
|
-
pkg.scripts = {
|
|
154
|
-
...pkg.scripts,
|
|
155
|
-
prebuild: generateCmd,
|
|
156
|
-
predev: generateCmd,
|
|
157
|
-
"type-check": `${generateCmd} && tsc --noEmit`,
|
|
158
|
-
"generate:regions": generateCmd
|
|
159
|
-
};
|
|
160
|
-
}
|
|
141
|
+
pkg.scripts = {
|
|
142
|
+
...pkg.scripts,
|
|
143
|
+
"type-check": "tsc --noEmit"
|
|
144
|
+
};
|
|
161
145
|
delete pkg.scripts?.["generate:agent-index"];
|
|
146
|
+
delete pkg.repository;
|
|
162
147
|
pkg.packageManager = "yarn@4.5.1";
|
|
163
148
|
writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + "\n");
|
|
164
149
|
}
|
|
@@ -377,648 +362,240 @@ function createEnvFile(targetDir, overrides) {
|
|
|
377
362
|
}
|
|
378
363
|
}
|
|
379
364
|
}
|
|
380
|
-
function createNpmrc(targetDir) {
|
|
381
|
-
const npmrcContent = [
|
|
382
|
-
"# Thor Commerce packages are published to NPM (private, scoped to @thorprovider).",
|
|
383
|
-
"# Set NPM_TOKEN in your environment or CI/CD (e.g., Vercel).",
|
|
384
|
-
"# Token should be an NPM Automation token with read access to @thorprovider org.",
|
|
385
|
-
"@thorprovider:registry=https://registry.npmjs.org",
|
|
386
|
-
"//registry.npmjs.org/:_authToken=${NPM_TOKEN}",
|
|
387
|
-
""
|
|
388
|
-
].join("\n");
|
|
389
|
-
writeFileSync2(join4(targetDir, ".npmrc"), npmrcContent);
|
|
390
|
-
log(" \u2713 .npmrc created (NPM registry)", c.green);
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
// src/helpers/site-config.ts
|
|
394
|
-
import { existsSync as existsSync5, readFileSync as readFileSync4, writeFileSync as writeFileSync3 } from "fs";
|
|
395
|
-
import { join as join5 } from "path";
|
|
396
|
-
function patchSiteConfig(targetDir, overrides) {
|
|
397
|
-
const filePath = join5(targetDir, "config", "site.config.ts");
|
|
398
|
-
if (!existsSync5(filePath)) return;
|
|
399
|
-
let content = readFileSync4(filePath, "utf-8");
|
|
400
|
-
let patched = false;
|
|
401
|
-
if (overrides.preset) {
|
|
402
|
-
const updated = content.replace(
|
|
403
|
-
/preset:\s*'[^']*'/,
|
|
404
|
-
`preset: '${overrides.preset}'`
|
|
405
|
-
);
|
|
406
|
-
if (updated !== content) {
|
|
407
|
-
content = updated;
|
|
408
|
-
patched = true;
|
|
409
|
-
log(` \u2713 site.config.ts: preset \u2192 '${overrides.preset}'`, c.green);
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
if (overrides.siteUrl) {
|
|
413
|
-
const updated = content.replace(
|
|
414
|
-
/url:\s*'[^']*'/,
|
|
415
|
-
`url: '${overrides.siteUrl}'`
|
|
416
|
-
);
|
|
417
|
-
if (updated !== content) {
|
|
418
|
-
content = updated;
|
|
419
|
-
patched = true;
|
|
420
|
-
log(` \u2713 site.config.ts: url \u2192 '${overrides.siteUrl}'`, c.green);
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
if (patched) {
|
|
424
|
-
writeFileSync3(filePath, content, "utf-8");
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
365
|
|
|
428
366
|
// src/helpers/agents-dir.ts
|
|
429
|
-
import { existsSync as
|
|
430
|
-
import { join as
|
|
367
|
+
import { existsSync as existsSync5, mkdirSync, readFileSync as readFileSync4, writeFileSync as writeFileSync3 } from "fs";
|
|
368
|
+
import { join as join5 } from "path";
|
|
431
369
|
function write(filePath, content) {
|
|
432
|
-
|
|
370
|
+
writeFileSync3(filePath, content, "utf-8");
|
|
433
371
|
}
|
|
434
372
|
function readSource(sourceDir, relPath) {
|
|
435
|
-
const abs =
|
|
436
|
-
if (!
|
|
437
|
-
return
|
|
373
|
+
const abs = join5(sourceDir, relPath);
|
|
374
|
+
if (!existsSync5(abs)) return "";
|
|
375
|
+
return readFileSync4(abs, "utf-8");
|
|
438
376
|
}
|
|
439
377
|
function buildAgentsMd(name) {
|
|
440
|
-
return `# ${name} \u2014
|
|
378
|
+
return `# ${name} \u2014 Thor Commerce Storefront
|
|
441
379
|
|
|
442
|
-
|
|
443
|
-
|
|
380
|
+
## CRITICAL
|
|
381
|
+
ALWAYS import from @thorprovider/types \u2014 NEVER define Product, Cart, Collection, Money.
|
|
444
382
|
|
|
445
|
-
##
|
|
383
|
+
## Imports
|
|
384
|
+
- Types: import type { Product, Cart, Collection, Money } from '@thorprovider/types'
|
|
385
|
+
- Data: import { commerce } from '@/lib/commerce' (server)
|
|
386
|
+
- Prices: formatPrice(money) from '@/lib/utils' \u2014 NEVER render amount raw
|
|
387
|
+
- Nav: next/link and next/image directly
|
|
446
388
|
|
|
447
|
-
|
|
448
|
-
|
|
389
|
+
## Type Facts
|
|
390
|
+
- Product.title (NOT name), Product.featuredImage?.url (NOT thumbnail)
|
|
391
|
+
- Money.amount is decimal STRING ("29.99"), currencyCode "USD"
|
|
392
|
+
- variant.price is ONE Money (not array). product.handle for URLs.
|
|
449
393
|
|
|
450
|
-
|
|
394
|
+
## Client Hooks
|
|
395
|
+
- useCart() \u2014 @/components/providers/cart-provider
|
|
396
|
+
- useRegion() \u2014 @/components/providers/region-provider
|
|
397
|
+
- useCommerce() \u2014 @/components/providers/commerce-provider
|
|
451
398
|
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
## Read Order (Required Before Implementing Anything)
|
|
457
|
-
|
|
458
|
-
| # | File | What you get |
|
|
459
|
-
|---|------|-------------|
|
|
460
|
-
| 1 | \`.agents/context/architecture.md\` | L1\u2013L5 layer rules, import constraints, frozen files |
|
|
461
|
-
| 2 | \`.agents/context/project-structure.md\` | Folder map, where to create files |
|
|
462
|
-
| 3 | \`.agents/context/dependencies.md\` | @thorprovider/* packages, 55-component inventory |
|
|
463
|
-
| 4 | \`.agents/context/components.md\` | Full @thorprovider/components API with props and examples |
|
|
464
|
-
| 5 | \`.agents/context/types.md\` | All domain types: Product, Cart, Order, Customer, Address, Money |
|
|
465
|
-
| 6 | \`.agents/context/commerce-api.md\` | Every \`commerce.*()\` method + circuit breaker rules |
|
|
466
|
-
| 7 | \`.agents/context/patterns.md\` | Server/client boundaries, data fetching, styling patterns |
|
|
467
|
-
| 8 | \`.agents/context/rules.md\` | Hard constraints \u2014 what you must NEVER do |
|
|
468
|
-
|
|
469
|
-
---
|
|
399
|
+
## Stack
|
|
400
|
+
Next.js 16, React 19, TS strict, Tailwind 4, shadcn/ui.
|
|
401
|
+
Server Components default. 'use client' only for interactivity.
|
|
470
402
|
|
|
471
|
-
##
|
|
472
|
-
|
|
473
|
-
- Add new pages in \`app/\`
|
|
474
|
-
- Add new components in \`components/\`
|
|
475
|
-
- Extend \`config/site.config.ts\` (values only \u2014 shape is frozen)
|
|
476
|
-
- Add API routes in \`app/api/\`
|
|
477
|
-
- Use any component from \`@thorprovider/components\` (check \`dependencies.md\` first)
|
|
478
|
-
- Call any \`commerce.*\` method (see \`commerce-api.md\` for signatures)
|
|
479
|
-
|
|
480
|
-
## What You MUST NOT Do
|
|
481
|
-
|
|
482
|
-
- Modify \`lib/\` files (frozen infrastructure \u2014 see \`architecture.md\`)
|
|
483
|
-
- Modify \`components/providers/\` (frozen provider chain)
|
|
484
|
-
- Modify \`app/layout.tsx\` (frozen root layout)
|
|
485
|
-
- Import \`next/link\` or \`next/image\` inside \`components/\` (use \`useNavigation()\`)
|
|
486
|
-
- Use hardcoded hex colors or \`rgb()\` (use CSS tokens: \`var(--foreground)\`, etc.)
|
|
487
|
-
- Import backend SDKs directly (\`@medusajs/js-sdk\`, \`@shopify/api\`, etc.)
|
|
488
|
-
- Call \`commerce.*\` read methods without \`backendCircuitBreaker.execute()\`
|
|
489
|
-
|
|
490
|
-
---
|
|
491
|
-
|
|
492
|
-
## Available Skills
|
|
493
|
-
|
|
494
|
-
Skills are playbooks for common tasks. Load them when you need step-by-step guidance:
|
|
495
|
-
|
|
496
|
-
| Skill | When to use |
|
|
497
|
-
|-------|-------------|
|
|
498
|
-
| \`.agents/skills/generate-page.md\` | Creating a new Next.js page |
|
|
499
|
-
| \`.agents/skills/generate-component.md\` | Creating a new React component |
|
|
500
|
-
| \`.agents/skills/generate-module.md\` | Creating an L4 feature module |
|
|
501
|
-
| \`.agents/skills/add-commerce-feature.md\` | Adding a feature that uses commerce.* methods |
|
|
502
|
-
| \`.agents/skills/add-route.md\` | Adding an API route with circuit breaker |
|
|
503
|
-
| \`.agents/skills/audit-changes.md\` | Validating generated code before committing |
|
|
504
|
-
|
|
505
|
-
---
|
|
506
|
-
|
|
507
|
-
## Quick Rules (Memorize These)
|
|
508
|
-
|
|
509
|
-
- **Types**: \`Product.handle\` (not \`slug\`), \`Cart.cost.totalAmount\` (not \`total\`), \`Money.amount\` is a string
|
|
510
|
-
- **Methods**: \`getProducts({ query, sortKey, first, skip })\` \u2014 see \`commerce-api.md\` for exact signatures
|
|
511
|
-
- **Components**: Check \`dependencies.md\` before creating any UI \u2014 55 components already exist
|
|
512
|
-
- **Optional methods**: Guard with \`if (commerce.method)\` before calling
|
|
513
|
-
- **All reads**: Wrap with \`backendCircuitBreaker.execute(() => commerce.method(), fallback)\`
|
|
403
|
+
## Docs
|
|
404
|
+
docs/types.md (types), docs/patterns.md (examples), docs/rules.md (constraints).
|
|
514
405
|
`;
|
|
515
406
|
}
|
|
516
407
|
function buildArchitectureMd() {
|
|
517
408
|
return `---
|
|
518
409
|
title: "Architecture"
|
|
519
|
-
purpose: "
|
|
410
|
+
purpose: "Current V0-native Thor Commerce scaffold rules"
|
|
520
411
|
---
|
|
521
412
|
|
|
522
413
|
# Architecture
|
|
523
414
|
|
|
524
|
-
##
|
|
415
|
+
## Layers
|
|
525
416
|
|
|
526
417
|
\`\`\`
|
|
527
|
-
L1: @thorprovider/types
|
|
528
|
-
L2: @thorprovider/adapters
|
|
529
|
-
|
|
530
|
-
L3: @thorprovider/components \u2192 UI components. Imports L1. NO Next.js.
|
|
531
|
-
L4: modules/ \u2192 Feature modules. Imports L3, L2 via props. NO Next.js.
|
|
532
|
-
L5: this app \u2192 Next.js app. Imports everything. Orchestrates L4.
|
|
533
|
-
\`\`\`
|
|
534
|
-
|
|
535
|
-
**Rule**: Imports flow L5 \u2192 L4 \u2192 L3 \u2192 L2 \u2192 L1. Never the reverse.
|
|
536
|
-
**Rule**: L3 and L4 NEVER import \`next/link\`, \`next/image\`, or any \`next/*\`.
|
|
537
|
-
**Rule**: L2 NEVER imports React or any UI library.
|
|
538
|
-
|
|
539
|
-
---
|
|
540
|
-
|
|
541
|
-
## Frozen Files (Do NOT Modify)
|
|
542
|
-
|
|
543
|
-
These files define the app's infrastructure. Changing them breaks the architecture.
|
|
544
|
-
|
|
545
|
-
| File | Why frozen |
|
|
546
|
-
|------|-----------|
|
|
547
|
-
| \`lib/commerce.ts\` | Commerce singleton \u2014 backend is swappable via adapters |
|
|
548
|
-
| \`lib/commerce-provider.tsx\` | ClientCommerceProvider + useCommerce hook |
|
|
549
|
-
| \`lib/region-provider.tsx\` | ClientRegionProvider + useRegion hook |
|
|
550
|
-
| \`lib/auth-context.tsx\` | AuthProvider + useAuth hook |
|
|
551
|
-
| \`lib/cart-provider.tsx\` | ClientCartProvider + useCart hook |
|
|
552
|
-
| \`lib/storefront-config.ts\` | Backend-driven theme accent fetch |
|
|
553
|
-
| \`lib/region-detection.ts\` | IP-based region detection |
|
|
554
|
-
| \`components/providers/app-providers.tsx\` | Provider chain composite (order is critical) |
|
|
555
|
-
| \`components/providers/navigation-provider.tsx\` | ClientNavigationProvider wrapper |
|
|
556
|
-
| \`app/layout.tsx\` | Root layout: ThemeProvider + AppProviders |
|
|
557
|
-
| \`config/site.config.ts\` | Shape is frozen; values can change |
|
|
558
|
-
|
|
559
|
-
---
|
|
560
|
-
|
|
561
|
-
## Provider Chain (Frozen Order)
|
|
562
|
-
|
|
563
|
-
\`\`\`tsx
|
|
564
|
-
// app/layout.tsx
|
|
565
|
-
<ThemeProvider preset={siteConfig.preset}>
|
|
566
|
-
<AppProviders initialRegionId={initialRegionId}>
|
|
567
|
-
{children}
|
|
568
|
-
</AppProviders>
|
|
569
|
-
</ThemeProvider>
|
|
570
|
-
|
|
571
|
-
// components/providers/app-providers.tsx
|
|
572
|
-
<ClientCommerceProvider>
|
|
573
|
-
<ClientRegionProvider initialRegionId={initialRegionId}>
|
|
574
|
-
<AuthProvider>
|
|
575
|
-
<ClientCartProvider>
|
|
576
|
-
<ClientNavigationProvider>
|
|
577
|
-
{children}
|
|
578
|
-
</ClientNavigationProvider>
|
|
579
|
-
</ClientCartProvider>
|
|
580
|
-
</AuthProvider>
|
|
581
|
-
</ClientRegionProvider>
|
|
582
|
-
</ClientCommerceProvider>
|
|
418
|
+
L1: @thorprovider/types \u2192 Domain types only
|
|
419
|
+
L2: @thorprovider/adapters \u2192 Backend abstraction
|
|
420
|
+
L5: this app \u2192 Next.js app + local providers
|
|
583
421
|
\`\`\`
|
|
584
422
|
|
|
585
|
-
|
|
586
|
-
To add a custom provider \u2192 wrap \`{children}\` **inside** \`ClientNavigationProvider\`.
|
|
587
|
-
|
|
588
|
-
---
|
|
589
|
-
|
|
590
|
-
## Backend Swappability
|
|
423
|
+
## Rules
|
|
591
424
|
|
|
592
|
-
|
|
425
|
+
- Import types from \`@thorprovider/types\`
|
|
426
|
+
- Import commerce access from \`@/lib/commerce\`
|
|
427
|
+
- Use \`next/link\` and \`next/image\` directly
|
|
428
|
+
- Local providers live in \`components/providers/\`
|
|
429
|
+
- Do not import \`@thorprovider/components\` or \`@thorprovider/providers\`
|
|
430
|
+
- Do not import backend SDKs directly
|
|
593
431
|
|
|
594
|
-
|
|
595
|
-
// \u2705 Correct \u2014 backend is swappable
|
|
596
|
-
import { commerce } from '@/lib/commerce';
|
|
432
|
+
## Frozen Files
|
|
597
433
|
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
434
|
+
- \`lib/commerce.ts\`
|
|
435
|
+
- \`lib/env.ts\`
|
|
436
|
+
- \`components/providers/app-providers.tsx\`
|
|
601
437
|
`;
|
|
602
438
|
}
|
|
603
439
|
function buildGeneratePageSkill() {
|
|
604
440
|
return `---
|
|
605
441
|
name: generate-page
|
|
606
|
-
description:
|
|
442
|
+
description: Create a new server-first Next.js page in a Thor Commerce storefront.
|
|
607
443
|
---
|
|
608
444
|
|
|
609
445
|
# Skill: Generate Page
|
|
610
446
|
|
|
611
|
-
##
|
|
612
|
-
|
|
613
|
-
1. Read \`.agents/context/project-structure.md\` \u2014 confirm where the page goes
|
|
614
|
-
2. Read \`.agents/context/commerce-api.md\` \u2014 identify which \`commerce.*\` methods you need
|
|
615
|
-
3. Read \`.agents/context/dependencies.md\` \u2014 check if needed components already exist
|
|
616
|
-
|
|
617
|
-
## Steps
|
|
618
|
-
|
|
619
|
-
### 1. Create the page file
|
|
447
|
+
## Checklist
|
|
620
448
|
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
449
|
+
1. Read \`.agents/context/types.md\`
|
|
450
|
+
2. Read \`.agents/context/commerce-api.md\`
|
|
451
|
+
3. Read \`.agents/context/patterns.md\`
|
|
624
452
|
|
|
625
|
-
|
|
453
|
+
## Pattern
|
|
626
454
|
|
|
627
455
|
\`\`\`tsx
|
|
628
|
-
// app/products/page.tsx
|
|
629
|
-
import { backendCircuitBreaker } from '@thorprovider/adapters';
|
|
630
456
|
import { commerce } from '@/lib/commerce';
|
|
631
|
-
import {
|
|
457
|
+
import type { Product } from '@thorprovider/types';
|
|
632
458
|
|
|
633
459
|
export default async function ProductsPage() {
|
|
634
|
-
const products = await
|
|
635
|
-
() => commerce.getProducts({ first: 20 }),
|
|
636
|
-
[] // fallback when backend is unreachable
|
|
637
|
-
);
|
|
460
|
+
const products: Product[] = await commerce.getProducts({ first: 20 });
|
|
638
461
|
|
|
639
462
|
return (
|
|
640
|
-
<
|
|
641
|
-
{products.map((
|
|
642
|
-
<
|
|
643
|
-
key={p.id}
|
|
644
|
-
title={p.title}
|
|
645
|
-
price={parseFloat(p.priceRange.minVariantPrice.amount)}
|
|
646
|
-
image={p.featuredImage?.url ?? ''}
|
|
647
|
-
href={\`/products/\${p.handle}\`}
|
|
648
|
-
/>
|
|
463
|
+
<main>
|
|
464
|
+
{products.map((product) => (
|
|
465
|
+
<div key={product.id}>{product.title}</div>
|
|
649
466
|
))}
|
|
650
|
-
</
|
|
467
|
+
</main>
|
|
651
468
|
);
|
|
652
469
|
}
|
|
653
470
|
\`\`\`
|
|
654
471
|
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
-
|
|
658
|
-
- \u2705 Always wrap reads with \`backendCircuitBreaker.execute(fn, fallback)\`
|
|
659
|
-
- \u2705 Use \`product.handle\` for URLs (not \`id\` or \`slug\`)
|
|
660
|
-
- \u274C Never \`useEffect\` + \`fetch\` in pages \u2014 causes loading flash + worse LCP
|
|
661
|
-
- \u274C Never import \`next/link\` or \`next/image\` in \`components/\`
|
|
662
|
-
|
|
663
|
-
### 4. After generating
|
|
664
|
-
|
|
665
|
-
Run \`.agents/skills/audit-changes.md\` checklist before committing.
|
|
472
|
+
- Server Components fetch data directly.
|
|
473
|
+
- Use \`product.handle\` for URLs.
|
|
474
|
+
- Pass data to client components via props.
|
|
666
475
|
`;
|
|
667
476
|
}
|
|
668
477
|
function buildGenerateComponentSkill() {
|
|
669
478
|
return `---
|
|
670
479
|
name: generate-component
|
|
671
|
-
description:
|
|
480
|
+
description: Create a client component for the V0-native storefront.
|
|
672
481
|
---
|
|
673
482
|
|
|
674
483
|
# Skill: Generate Component
|
|
675
484
|
|
|
676
|
-
##
|
|
677
|
-
|
|
678
|
-
1. Check \`.agents/context/dependencies.md\` \u2014 the component may already exist in \`@thorprovider/components\`
|
|
679
|
-
2. If it exists \u2192 import it, don't recreate it
|
|
680
|
-
3. If it doesn't \u2192 follow this playbook
|
|
681
|
-
|
|
682
|
-
## Steps
|
|
683
|
-
|
|
684
|
-
### 1. Create the component file
|
|
685
|
-
|
|
686
|
-
\`\`\`
|
|
687
|
-
components/[feature-name].tsx
|
|
688
|
-
\`\`\`
|
|
689
|
-
|
|
690
|
-
### 2. Client component pattern
|
|
485
|
+
## Pattern
|
|
691
486
|
|
|
692
487
|
\`\`\`tsx
|
|
693
|
-
// components/product-card-custom.tsx
|
|
694
488
|
'use client';
|
|
695
|
-
import { useNavigation } from '@thorprovider/providers';
|
|
696
|
-
import { Button, Card } from '@thorprovider/components';
|
|
697
|
-
import type { Product } from '@thorprovider/types';
|
|
698
|
-
|
|
699
|
-
interface ProductCardCustomProps {
|
|
700
|
-
product: Product;
|
|
701
|
-
onAddToCart?: (variantId: string) => void;
|
|
702
|
-
}
|
|
703
489
|
|
|
704
|
-
|
|
705
|
-
|
|
490
|
+
import Link from 'next/link';
|
|
491
|
+
import Image from 'next/image';
|
|
492
|
+
import type { Product } from '@thorprovider/types';
|
|
493
|
+
import { formatPrice } from '@/lib/utils';
|
|
706
494
|
|
|
495
|
+
export function ProductCard({ product }: { product: Product }) {
|
|
707
496
|
return (
|
|
708
|
-
<
|
|
709
|
-
<
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
{onAddToCart && (
|
|
719
|
-
<Button onClick={() => onAddToCart(product.variants[0]?.id ?? '')}>
|
|
720
|
-
Add to cart
|
|
721
|
-
</Button>
|
|
722
|
-
)}
|
|
723
|
-
</Card>
|
|
497
|
+
<Link href={\`/products/\${product.handle}\`}>
|
|
498
|
+
<Image
|
|
499
|
+
src={product.featuredImage?.url ?? ''}
|
|
500
|
+
alt={product.featuredImage?.altText ?? product.title}
|
|
501
|
+
width={300}
|
|
502
|
+
height={300}
|
|
503
|
+
/>
|
|
504
|
+
<div>{product.title}</div>
|
|
505
|
+
<div>{formatPrice(product.priceRange.minVariantPrice)}</div>
|
|
506
|
+
</Link>
|
|
724
507
|
);
|
|
725
508
|
}
|
|
726
509
|
\`\`\`
|
|
727
510
|
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
-
|
|
731
|
-
- \u2705 Use CSS tokens for colors: \`var(--foreground)\`, \`var(--background)\`, \`var(--primary-9)\`
|
|
732
|
-
- \u2705 Accept data via props \u2014 components never fetch
|
|
733
|
-
- \u2705 Compose using \`@thorprovider/components\` primitives inside
|
|
734
|
-
- \u274C Never hardcode \`#hex\` or \`rgb()\` colors
|
|
735
|
-
- \u274C Never call \`commerce.*\` or \`fetch()\` inside a component
|
|
736
|
-
|
|
737
|
-
### 4. After generating
|
|
738
|
-
|
|
739
|
-
Run \`.agents/skills/audit-changes.md\` checklist before committing.
|
|
511
|
+
- Use \`next/link\` and \`next/image\` directly.
|
|
512
|
+
- Use CSS tokens / semantic classes, not hardcoded colors.
|
|
513
|
+
- Never fetch commerce data inside the component.
|
|
740
514
|
`;
|
|
741
515
|
}
|
|
742
516
|
function buildGenerateModuleSkill() {
|
|
743
517
|
return `---
|
|
744
518
|
name: generate-module
|
|
745
|
-
description:
|
|
519
|
+
description: Build a larger feature by composing local components with typed props.
|
|
746
520
|
---
|
|
747
521
|
|
|
748
|
-
# Skill: Generate Module
|
|
522
|
+
# Skill: Generate Module
|
|
749
523
|
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
-
|
|
754
|
-
- Composes L3 components (\`@thorprovider/components\`)
|
|
755
|
-
- Receives data and callbacks via props from L5 (the page)
|
|
756
|
-
- Never fetches data itself
|
|
757
|
-
|
|
758
|
-
## Pattern: Variant/Composition
|
|
759
|
-
|
|
760
|
-
\`\`\`tsx
|
|
761
|
-
// components/modules/checkout/checkout-module.tsx
|
|
762
|
-
'use client';
|
|
763
|
-
import { CheckoutStepIndicator, CartSummary } from '@thorprovider/components';
|
|
764
|
-
import type { Cart } from '@thorprovider/types';
|
|
765
|
-
|
|
766
|
-
type CheckoutVariant = 'simple' | 'wizard' | 'one-page';
|
|
767
|
-
|
|
768
|
-
interface CheckoutModuleProps {
|
|
769
|
-
cart: Cart;
|
|
770
|
-
variant?: CheckoutVariant;
|
|
771
|
-
onPayment: (cartId: string) => Promise<void>;
|
|
772
|
-
onAddress: (cartId: string, address: unknown) => Promise<void>;
|
|
773
|
-
}
|
|
774
|
-
|
|
775
|
-
export function CheckoutModule({
|
|
776
|
-
cart,
|
|
777
|
-
variant = 'wizard',
|
|
778
|
-
onPayment,
|
|
779
|
-
onAddress,
|
|
780
|
-
}: CheckoutModuleProps) {
|
|
781
|
-
const steps = ['Address', 'Shipping', 'Payment'];
|
|
782
|
-
|
|
783
|
-
return (
|
|
784
|
-
<div>
|
|
785
|
-
<CheckoutStepIndicator steps={steps} currentStep={0} completedSteps={[]} />
|
|
786
|
-
<CartSummary
|
|
787
|
-
subtotal={parseFloat(cart.cost.subtotalAmount.amount)}
|
|
788
|
-
total={parseFloat(cart.cost.totalAmount.amount)}
|
|
789
|
-
onCheckout={() => onPayment(cart.id ?? '')}
|
|
790
|
-
/>
|
|
791
|
-
</div>
|
|
792
|
-
);
|
|
793
|
-
}
|
|
794
|
-
\`\`\`
|
|
795
|
-
|
|
796
|
-
## Rules
|
|
797
|
-
|
|
798
|
-
- \u2705 Accept all data via props \u2014 never fetch inside a module
|
|
799
|
-
- \u2705 Use Variant/Composition pattern \u2014 configurable via props, not hardcoded flows
|
|
800
|
-
- \u2705 Domain values (status colors, filter options) come from L5 as props
|
|
801
|
-
- \u274C Never hardcode business logic like \`case 'pending': return 'yellow'\`
|
|
802
|
-
- \u274C Never import \`next/*\` \u2014 use \`useNavigation()\`
|
|
803
|
-
|
|
804
|
-
## After generating
|
|
805
|
-
|
|
806
|
-
Run \`.agents/skills/audit-changes.md\` checklist before committing.
|
|
524
|
+
- Keep data fetching in the page or route.
|
|
525
|
+
- Pass typed props into the module.
|
|
526
|
+
- Use local providers from \`components/providers/\` when client state is needed.
|
|
527
|
+
- Reuse \`formatPrice()\`, \`product.handle\`, and semantic Tailwind classes.
|
|
807
528
|
`;
|
|
808
529
|
}
|
|
809
530
|
function buildAddCommerceFeatureSkill() {
|
|
810
531
|
return `---
|
|
811
532
|
name: add-commerce-feature
|
|
812
|
-
description:
|
|
533
|
+
description: Add commerce-backed UI or server logic using the local commerce singleton.
|
|
813
534
|
---
|
|
814
535
|
|
|
815
536
|
# Skill: Add Commerce Feature
|
|
816
537
|
|
|
817
|
-
##
|
|
818
|
-
|
|
819
|
-
1. Read \`.agents/context/commerce-api.md\` \u2014 find the exact method signatures
|
|
820
|
-
2. Read \`.agents/context/types.md\` \u2014 understand the data shapes
|
|
821
|
-
3. Identify: is this a **read** (needs circuit breaker) or a **mutation** (direct call)?
|
|
822
|
-
|
|
823
|
-
## Read Operations (Server Components / API Routes)
|
|
824
|
-
|
|
825
|
-
Always wrap with \`backendCircuitBreaker.execute()\`:
|
|
826
|
-
|
|
827
|
-
\`\`\`ts
|
|
828
|
-
import { commerce } from '@/lib/commerce';
|
|
829
|
-
import { backendCircuitBreaker } from '@thorprovider/adapters';
|
|
830
|
-
|
|
831
|
-
// In a server component or API route
|
|
832
|
-
const products = await backendCircuitBreaker.execute(
|
|
833
|
-
() => commerce.getProducts({ query: 'shoes', first: 20 }),
|
|
834
|
-
[] // fallback value when backend is down
|
|
835
|
-
);
|
|
836
|
-
\`\`\`
|
|
837
|
-
|
|
838
|
-
## Mutation Operations (Client Components)
|
|
839
|
-
|
|
840
|
-
Call directly \u2014 adapters handle retry internally:
|
|
841
|
-
|
|
842
|
-
\`\`\`ts
|
|
843
|
-
// In a client component
|
|
844
|
-
const { addItem } = useCart(); // from '@/lib/cart-provider'
|
|
845
|
-
await addItem(variantId, quantity);
|
|
846
|
-
|
|
847
|
-
// Or directly via commerce instance
|
|
848
|
-
const cart = await commerce.addToCart(cartId, [{ merchandiseId: variantId, quantity: 1 }]);
|
|
849
|
-
\`\`\`
|
|
850
|
-
|
|
851
|
-
## Optional Methods
|
|
852
|
-
|
|
853
|
-
Methods marked with \`?\` in \`commerce-api.md\` may not exist. Always guard:
|
|
854
|
-
|
|
855
|
-
\`\`\`ts
|
|
856
|
-
// \u2705 Guard optional methods
|
|
857
|
-
if (commerce.getAdminOrder) {
|
|
858
|
-
const order = await backendCircuitBreaker.execute(
|
|
859
|
-
() => commerce.getAdminOrder!(orderId),
|
|
860
|
-
null
|
|
861
|
-
);
|
|
862
|
-
}
|
|
863
|
-
\`\`\`
|
|
864
|
-
|
|
865
|
-
## Common Patterns
|
|
866
|
-
|
|
867
|
-
### Product listing page
|
|
868
|
-
\`\`\`ts
|
|
869
|
-
const products = await backendCircuitBreaker.execute(
|
|
870
|
-
() => commerce.getProducts({ query, sortKey: 'title', first: 20 }),
|
|
871
|
-
[]
|
|
872
|
-
);
|
|
873
|
-
\`\`\`
|
|
874
|
-
|
|
875
|
-
### Single product page
|
|
876
|
-
\`\`\`ts
|
|
877
|
-
const product = await backendCircuitBreaker.execute(
|
|
878
|
-
() => commerce.getProduct(handle), // handle = URL slug
|
|
879
|
-
undefined
|
|
880
|
-
);
|
|
881
|
-
if (!product) notFound();
|
|
882
|
-
\`\`\`
|
|
883
|
-
|
|
884
|
-
### Cart operations (client)
|
|
885
|
-
\`\`\`ts
|
|
886
|
-
const { cart, addItem, removeItem, updateQuantity } = useCart();
|
|
887
|
-
\`\`\`
|
|
888
|
-
|
|
889
|
-
### Auth (client)
|
|
890
|
-
\`\`\`ts
|
|
891
|
-
const { customer, isAuthenticated, login, logout } = useAuth();
|
|
892
|
-
\`\`\`
|
|
893
|
-
|
|
894
|
-
## After generating
|
|
538
|
+
## Rules
|
|
895
539
|
|
|
896
|
-
|
|
540
|
+
- Import \`commerce\` from \`@/lib/commerce\`
|
|
541
|
+
- Never import vendor SDKs directly
|
|
542
|
+
- Auth lives under \`commerce.auth.*\`
|
|
543
|
+
- Reads typically happen in server components or route handlers
|
|
544
|
+
- Client mutations should use local provider hooks when available
|
|
897
545
|
`;
|
|
898
546
|
}
|
|
899
547
|
function buildAddRouteSkill() {
|
|
900
548
|
return `---
|
|
901
549
|
name: add-route
|
|
902
|
-
description:
|
|
550
|
+
description: Create an App Router route handler for storefront data.
|
|
903
551
|
---
|
|
904
552
|
|
|
905
553
|
# Skill: Add API Route
|
|
906
554
|
|
|
907
|
-
## Pattern
|
|
908
|
-
|
|
909
555
|
\`\`\`ts
|
|
910
|
-
// app/api/products/route.ts
|
|
911
|
-
import { backendCircuitBreaker } from '@thorprovider/adapters';
|
|
912
|
-
import { commerce } from '@/lib/commerce';
|
|
913
|
-
|
|
914
|
-
export async function GET(request: Request) {
|
|
915
|
-
try {
|
|
916
|
-
const { searchParams } = new URL(request.url);
|
|
917
|
-
const query = searchParams.get('q') ?? undefined;
|
|
918
|
-
|
|
919
|
-
const products = await backendCircuitBreaker.execute(
|
|
920
|
-
() => commerce.getProducts({ query, first: 20 }),
|
|
921
|
-
[]
|
|
922
|
-
);
|
|
923
|
-
|
|
924
|
-
return Response.json(products);
|
|
925
|
-
} catch {
|
|
926
|
-
return Response.json({ error: 'Service unavailable' }, { status: 503 });
|
|
927
|
-
}
|
|
928
|
-
}
|
|
929
|
-
\`\`\`
|
|
930
|
-
|
|
931
|
-
## Rules
|
|
932
|
-
|
|
933
|
-
- \u2705 Always wrap reads with \`backendCircuitBreaker.execute(fn, fallback)\`
|
|
934
|
-
- \u2705 Return \`Response.json()\` (not \`NextResponse\`)
|
|
935
|
-
- \u2705 Catch errors and return \`503\` when backend is unavailable
|
|
936
|
-
- \u2705 Use \`commerce\` from \`@/lib/commerce\` (not a new instance)
|
|
937
|
-
- \u274C Never import backend SDKs directly (\`@medusajs/js-sdk\`, etc.)
|
|
938
|
-
- \u274C Never use raw \`fetch(COMMERCE_API_URL + '...')\`
|
|
939
|
-
|
|
940
|
-
## Admin Routes
|
|
941
|
-
|
|
942
|
-
Admin routes live in \`app/api/admin/\` and use \`adminFetch\` for internal calls:
|
|
943
|
-
|
|
944
|
-
\`\`\`ts
|
|
945
|
-
// app/api/admin/orders/route.ts
|
|
946
|
-
import { backendCircuitBreaker } from '@thorprovider/adapters';
|
|
947
556
|
import { commerce } from '@/lib/commerce';
|
|
948
557
|
|
|
949
558
|
export async function GET() {
|
|
950
|
-
const
|
|
951
|
-
|
|
952
|
-
{ orders: [], count: 0 }
|
|
953
|
-
);
|
|
954
|
-
return Response.json({ orders, count });
|
|
559
|
+
const products = await commerce.getProducts({ first: 20 });
|
|
560
|
+
return Response.json(products);
|
|
955
561
|
}
|
|
956
562
|
\`\`\`
|
|
957
563
|
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
564
|
+
- Use \`Response.json()\`
|
|
565
|
+
- Use \`commerce\` from \`@/lib/commerce\`
|
|
566
|
+
- Validate request input when needed
|
|
961
567
|
`;
|
|
962
568
|
}
|
|
963
569
|
function buildAuditChangesSkill() {
|
|
964
570
|
return `---
|
|
965
571
|
name: audit-changes
|
|
966
|
-
description:
|
|
572
|
+
description: Validate generated code against the current storefront rules.
|
|
967
573
|
---
|
|
968
574
|
|
|
969
575
|
# Skill: Audit Changes
|
|
970
576
|
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
- [ ] No \`
|
|
976
|
-
- [ ]
|
|
977
|
-
- [ ]
|
|
978
|
-
- [ ]
|
|
979
|
-
|
|
980
|
-
## Data Fetching
|
|
981
|
-
|
|
982
|
-
- [ ] All \`commerce.get*()\` reads wrapped in \`backendCircuitBreaker.execute(fn, fallback)\`
|
|
983
|
-
- [ ] Mutations (\`addToCart\`, \`checkout\`, etc.) called directly (no circuit breaker)
|
|
984
|
-
- [ ] Optional methods (\`commerce.method?\`) guarded with \`if (commerce.method)\`
|
|
985
|
-
- [ ] Server components fetch data; client components receive data via props
|
|
986
|
-
|
|
987
|
-
## Styling
|
|
988
|
-
|
|
989
|
-
- [ ] No hardcoded \`#hex\` colors in components
|
|
990
|
-
- [ ] No \`rgb()\` or \`hsl()\` inline colors in components
|
|
991
|
-
- [ ] Colors use CSS tokens: \`var(--foreground)\`, \`var(--background)\`, \`var(--primary-9)\`, etc.
|
|
992
|
-
|
|
993
|
-
## Components
|
|
994
|
-
|
|
995
|
-
- [ ] Checked \`@thorprovider/components\` inventory before creating new UI (see \`dependencies.md\`)
|
|
996
|
-
- [ ] No duplicate implementations of existing components (Button, ProductCard, CartItem, etc.)
|
|
997
|
-
- [ ] \`useNavigation()\` used for Link and Image (not \`next/link\` / \`next/image\`)
|
|
998
|
-
|
|
999
|
-
## Provider Chain
|
|
1000
|
-
|
|
1001
|
-
- [ ] \`components/providers/app-providers.tsx\` order unchanged
|
|
1002
|
-
- [ ] \`app/layout.tsx\` structure unchanged
|
|
1003
|
-
|
|
1004
|
-
## Build
|
|
1005
|
-
|
|
1006
|
-
- [ ] \`yarn type-check\` passes
|
|
1007
|
-
- [ ] No TypeScript errors in generated files
|
|
577
|
+
- [ ] Types imported from \`@thorprovider/types\`
|
|
578
|
+
- [ ] No \`@thorprovider/components\` imports
|
|
579
|
+
- [ ] No \`@thorprovider/providers\` imports
|
|
580
|
+
- [ ] No backend SDK imports
|
|
581
|
+
- [ ] No raw \`fetch(COMMERCE_API_URL...)\`
|
|
582
|
+
- [ ] Prices rendered with \`formatPrice()\`
|
|
583
|
+
- [ ] URLs use \`product.handle\`
|
|
584
|
+
- [ ] No hardcoded hex/rgb colors
|
|
585
|
+
- [ ] \`tsc --noEmit\` passes
|
|
1008
586
|
`;
|
|
1009
587
|
}
|
|
1010
588
|
function generateAgentsDir(name, targetDir, sourceDir) {
|
|
1011
|
-
const agentsDir =
|
|
1012
|
-
const contextDir =
|
|
1013
|
-
const skillsDir =
|
|
589
|
+
const agentsDir = join5(targetDir, ".agents");
|
|
590
|
+
const contextDir = join5(agentsDir, "context");
|
|
591
|
+
const skillsDir = join5(agentsDir, "skills");
|
|
1014
592
|
mkdirSync(contextDir, { recursive: true });
|
|
1015
593
|
mkdirSync(skillsDir, { recursive: true });
|
|
1016
|
-
write(
|
|
1017
|
-
write(
|
|
594
|
+
write(join5(agentsDir, "AGENTS.md"), buildAgentsMd(name));
|
|
595
|
+
write(join5(contextDir, "architecture.md"), buildArchitectureMd());
|
|
1018
596
|
const docFiles = [
|
|
597
|
+
"README.md",
|
|
1019
598
|
"project-structure.md",
|
|
1020
|
-
"dependencies.md",
|
|
1021
|
-
"components.md",
|
|
1022
599
|
"types.md",
|
|
1023
600
|
"commerce-api.md",
|
|
1024
601
|
"patterns.md",
|
|
@@ -1027,20 +604,20 @@ function generateAgentsDir(name, targetDir, sourceDir) {
|
|
|
1027
604
|
for (const docFile of docFiles) {
|
|
1028
605
|
const content = readSource(sourceDir, `docs/${docFile}`);
|
|
1029
606
|
if (content) {
|
|
1030
|
-
write(
|
|
607
|
+
write(join5(contextDir, docFile), content);
|
|
1031
608
|
}
|
|
1032
609
|
}
|
|
1033
|
-
write(
|
|
1034
|
-
write(
|
|
1035
|
-
write(
|
|
1036
|
-
write(
|
|
1037
|
-
write(
|
|
1038
|
-
write(
|
|
610
|
+
write(join5(skillsDir, "generate-page.md"), buildGeneratePageSkill());
|
|
611
|
+
write(join5(skillsDir, "generate-component.md"), buildGenerateComponentSkill());
|
|
612
|
+
write(join5(skillsDir, "generate-module.md"), buildGenerateModuleSkill());
|
|
613
|
+
write(join5(skillsDir, "add-commerce-feature.md"), buildAddCommerceFeatureSkill());
|
|
614
|
+
write(join5(skillsDir, "add-route.md"), buildAddRouteSkill());
|
|
615
|
+
write(join5(skillsDir, "audit-changes.md"), buildAuditChangesSkill());
|
|
1039
616
|
}
|
|
1040
617
|
|
|
1041
618
|
// src/helpers/vercel.ts
|
|
1042
|
-
import { existsSync as
|
|
1043
|
-
import { join as
|
|
619
|
+
import { existsSync as existsSync6, mkdirSync as mkdirSync2, writeFileSync as writeFileSync4 } from "fs";
|
|
620
|
+
import { join as join6 } from "path";
|
|
1044
621
|
var VERCEL_API = "https://api.vercel.com";
|
|
1045
622
|
async function createVercelProject(name, token, teamSlug, gitRepo) {
|
|
1046
623
|
const url = new URL(`${VERCEL_API}/v11/projects`);
|
|
@@ -1071,22 +648,21 @@ async function createVercelProject(name, token, teamSlug, gitRepo) {
|
|
|
1071
648
|
return await res.json();
|
|
1072
649
|
}
|
|
1073
650
|
function writeProjectJson(targetDir, orgId, projectId) {
|
|
1074
|
-
const vercelDir =
|
|
1075
|
-
if (!
|
|
1076
|
-
|
|
1077
|
-
|
|
651
|
+
const vercelDir = join6(targetDir, ".vercel");
|
|
652
|
+
if (!existsSync6(vercelDir)) mkdirSync2(vercelDir, { recursive: true });
|
|
653
|
+
writeFileSync4(
|
|
654
|
+
join6(vercelDir, "project.json"),
|
|
1078
655
|
JSON.stringify({ orgId, projectId }, null, 2) + "\n"
|
|
1079
656
|
);
|
|
1080
657
|
}
|
|
1081
658
|
function generateDeployWorkflow(targetDir) {
|
|
1082
|
-
const workflowDir =
|
|
659
|
+
const workflowDir = join6(targetDir, ".github", "workflows");
|
|
1083
660
|
mkdirSync2(workflowDir, { recursive: true });
|
|
1084
661
|
const workflow = `# Vercel deploy workflow \u2014 generated by create-thor-app.
|
|
1085
662
|
# Required GitHub Actions secrets:
|
|
1086
663
|
# VERCEL_TOKEN \u2014 Vercel Access Token
|
|
1087
664
|
# VERCEL_ORG_ID \u2014 From .vercel/project.json (accountId)
|
|
1088
665
|
# VERCEL_PROJECT_ID \u2014 From .vercel/project.json (projectId)
|
|
1089
|
-
# NPM_TOKEN \u2014 NPM Automation Token with read access to @thorprovider org
|
|
1090
666
|
|
|
1091
667
|
name: Vercel Deploy
|
|
1092
668
|
|
|
@@ -1120,8 +696,6 @@ jobs:
|
|
|
1120
696
|
|
|
1121
697
|
- name: Build Project
|
|
1122
698
|
run: vercel build \${{ github.ref == 'refs/heads/main' && '--prod' || '' }} --token=\${{ secrets.VERCEL_TOKEN }}
|
|
1123
|
-
env:
|
|
1124
|
-
NPM_TOKEN: \${{ secrets.NPM_TOKEN }}
|
|
1125
699
|
|
|
1126
700
|
- name: Deploy to Vercel
|
|
1127
701
|
id: deploy
|
|
@@ -1141,7 +715,7 @@ jobs:
|
|
|
1141
715
|
body: \`\u{1F517} Preview: \${{ steps.deploy.outputs.url }}\`
|
|
1142
716
|
})
|
|
1143
717
|
`;
|
|
1144
|
-
|
|
718
|
+
writeFileSync4(join6(workflowDir, "vercel-deploy.yml"), workflow);
|
|
1145
719
|
}
|
|
1146
720
|
async function linkVercel(name, targetDir, githubRepo) {
|
|
1147
721
|
let token = process.env.VERCEL_TOKEN;
|
|
@@ -1232,16 +806,16 @@ async function linkVercel(name, targetDir, githubRepo) {
|
|
|
1232
806
|
}
|
|
1233
807
|
|
|
1234
808
|
// src/helpers/ai-validator.ts
|
|
1235
|
-
import { existsSync as
|
|
1236
|
-
import { join as
|
|
809
|
+
import { existsSync as existsSync7, readFileSync as readFileSync5, writeFileSync as writeFileSync5 } from "fs";
|
|
810
|
+
import { join as join7 } from "path";
|
|
1237
811
|
function verifyValidatorConfig(targetDir) {
|
|
1238
|
-
const validatorPath =
|
|
1239
|
-
if (!
|
|
812
|
+
const validatorPath = join7(targetDir, "ai-validator.json");
|
|
813
|
+
if (!existsSync7(validatorPath)) {
|
|
1240
814
|
log(` \u26A0\uFE0F ai-validator.json not found at ${validatorPath}`, c.yellow);
|
|
1241
815
|
return false;
|
|
1242
816
|
}
|
|
1243
817
|
try {
|
|
1244
|
-
const content =
|
|
818
|
+
const content = readFileSync5(validatorPath, "utf-8");
|
|
1245
819
|
const config = JSON.parse(content);
|
|
1246
820
|
if (!config.version || !config.maxRetries || !config.retryModelId) {
|
|
1247
821
|
log(" \u26A0\uFE0F ai-validator.json is missing required fields", c.yellow);
|
|
@@ -1261,20 +835,6 @@ function verifyValidatorConfig(targetDir) {
|
|
|
1261
835
|
}
|
|
1262
836
|
|
|
1263
837
|
// src/create-app.ts
|
|
1264
|
-
function bundleRegionsScript(monorepoRoot, targetDir) {
|
|
1265
|
-
if (!monorepoRoot) return;
|
|
1266
|
-
const regionsScriptSrc = join9(
|
|
1267
|
-
monorepoRoot,
|
|
1268
|
-
"scripts",
|
|
1269
|
-
"generate-regions-config.ts"
|
|
1270
|
-
);
|
|
1271
|
-
if (existsSync9(regionsScriptSrc)) {
|
|
1272
|
-
const scriptsDir = join9(targetDir, "scripts");
|
|
1273
|
-
mkdirSync3(scriptsDir, { recursive: true });
|
|
1274
|
-
cpSync(regionsScriptSrc, join9(scriptsDir, "generate-regions-config.ts"));
|
|
1275
|
-
log(" \u2713 Bundled generate-regions-config.ts", c.green);
|
|
1276
|
-
}
|
|
1277
|
-
}
|
|
1278
838
|
async function createApp(opts) {
|
|
1279
839
|
const {
|
|
1280
840
|
name,
|
|
@@ -1289,16 +849,13 @@ async function createApp(opts) {
|
|
|
1289
849
|
noValidator,
|
|
1290
850
|
commerceProvider,
|
|
1291
851
|
commerceApiUrl,
|
|
1292
|
-
commerceApiKey
|
|
1293
|
-
preset,
|
|
1294
|
-
siteUrl,
|
|
1295
|
-
generateRegions
|
|
852
|
+
commerceApiKey
|
|
1296
853
|
} = opts;
|
|
1297
854
|
if (monorepoRoot) {
|
|
1298
855
|
loadRootEnv(monorepoRoot);
|
|
1299
856
|
}
|
|
1300
857
|
const templatePath = resolveTemplatePath(mode, monorepoRoot);
|
|
1301
|
-
if (!
|
|
858
|
+
if (!existsSync8(templatePath)) {
|
|
1302
859
|
log(`
|
|
1303
860
|
\u274C Template not found: ${templatePath}`, c.red);
|
|
1304
861
|
if (mode === "standalone") {
|
|
@@ -1315,7 +872,7 @@ async function createApp(opts) {
|
|
|
1315
872
|
log(` Target : ${c.cyan}${outputDir}${c.reset}`);
|
|
1316
873
|
log(` Package : ${c.cyan}@thorprovider/${name}${c.reset}
|
|
1317
874
|
`);
|
|
1318
|
-
if (
|
|
875
|
+
if (existsSync8(outputDir)) {
|
|
1319
876
|
log(`\u274C Directory already exists: ${outputDir}`, c.red);
|
|
1320
877
|
log(
|
|
1321
878
|
" Choose a different name or delete the existing directory.",
|
|
@@ -1346,8 +903,8 @@ async function createApp(opts) {
|
|
|
1346
903
|
log(" \u2713 Files copied", c.green);
|
|
1347
904
|
log("\u{1F9F9} Removing build artifacts...", c.cyan);
|
|
1348
905
|
for (const p of REMOVE_AFTER_COPY) {
|
|
1349
|
-
const fullPath =
|
|
1350
|
-
if (
|
|
906
|
+
const fullPath = join8(outputDir, p);
|
|
907
|
+
if (existsSync8(fullPath)) {
|
|
1351
908
|
rmSync(fullPath, { recursive: true, force: true });
|
|
1352
909
|
log(` \u2713 Removed ${p}`, c.gray);
|
|
1353
910
|
}
|
|
@@ -1360,8 +917,6 @@ async function createApp(opts) {
|
|
|
1360
917
|
mode
|
|
1361
918
|
});
|
|
1362
919
|
log(" \u2713 package.json updated", c.green);
|
|
1363
|
-
bundleRegionsScript(monorepoRoot, outputDir);
|
|
1364
|
-
createNpmrc(outputDir);
|
|
1365
920
|
const envOverrides = {
|
|
1366
921
|
...commerceProvider && { NEXT_PUBLIC_COMMERCE_PROVIDER: commerceProvider },
|
|
1367
922
|
...commerceApiUrl && { NEXT_PUBLIC_COMMERCE_API_URL: commerceApiUrl },
|
|
@@ -1371,13 +926,9 @@ async function createApp(opts) {
|
|
|
1371
926
|
outputDir,
|
|
1372
927
|
Object.keys(envOverrides).length > 0 ? envOverrides : void 0
|
|
1373
928
|
);
|
|
1374
|
-
if (preset || siteUrl) {
|
|
1375
|
-
log("\u2699\uFE0F Applying site configuration...", c.cyan);
|
|
1376
|
-
patchSiteConfig(outputDir, { preset, siteUrl });
|
|
1377
|
-
}
|
|
1378
929
|
if (!noContext) {
|
|
1379
930
|
log("\u{1F4C1} Generating .agents/ directory...", c.cyan);
|
|
1380
|
-
const contextSourceDir = mode === "monorepo" && monorepoRoot ?
|
|
931
|
+
const contextSourceDir = mode === "monorepo" && monorepoRoot ? join8(monorepoRoot, "packages", "core") : outputDir;
|
|
1381
932
|
generateAgentsDir(name, outputDir, contextSourceDir);
|
|
1382
933
|
log(" \u2713 .agents/ generated (AGENTS.md + context/ + skills/)", c.green);
|
|
1383
934
|
}
|
|
@@ -1400,26 +951,6 @@ async function createApp(opts) {
|
|
|
1400
951
|
if (!skipInstall) {
|
|
1401
952
|
installDependencies(outputDir);
|
|
1402
953
|
}
|
|
1403
|
-
if (generateRegions) {
|
|
1404
|
-
if (skipInstall) {
|
|
1405
|
-
log(
|
|
1406
|
-
"\n\u26A0\uFE0F --generate-regions skipped: requires dependencies to be installed.",
|
|
1407
|
-
c.yellow
|
|
1408
|
-
);
|
|
1409
|
-
log(' Run "yarn generate:regions" manually after running "yarn install".', c.yellow);
|
|
1410
|
-
} else {
|
|
1411
|
-
log("\n\u{1F5FA}\uFE0F Generating regions configuration from backend...", c.cyan);
|
|
1412
|
-
try {
|
|
1413
|
-
const { execSync: execSync3 } = await import("child_process");
|
|
1414
|
-
execSync3("yarn generate:regions", { cwd: outputDir, stdio: "inherit" });
|
|
1415
|
-
} catch {
|
|
1416
|
-
log(
|
|
1417
|
-
'\n\u26A0\uFE0F Region generation failed. You can retry with "yarn generate:regions".',
|
|
1418
|
-
c.yellow
|
|
1419
|
-
);
|
|
1420
|
-
}
|
|
1421
|
-
}
|
|
1422
|
-
}
|
|
1423
954
|
let remoteCreated = false;
|
|
1424
955
|
let githubRepo;
|
|
1425
956
|
if (!skipGit) {
|
|
@@ -1474,8 +1005,8 @@ async function createApp(opts) {
|
|
|
1474
1005
|
} else {
|
|
1475
1006
|
log("\u23ED\uFE0F Skipping Vercel integration (--skip-vercel)", c.gray);
|
|
1476
1007
|
}
|
|
1477
|
-
const isInsideMonorepo = monorepoRoot && !
|
|
1478
|
-
const relPath = isInsideMonorepo ?
|
|
1008
|
+
const isInsideMonorepo = monorepoRoot && !relative(monorepoRoot, outputDir).startsWith("..");
|
|
1009
|
+
const relPath = isInsideMonorepo ? relative(monorepoRoot, outputDir) : name;
|
|
1479
1010
|
let step = 1;
|
|
1480
1011
|
log("\n" + bold("\u2705 Project created successfully!"), c.green);
|
|
1481
1012
|
log("\n" + bold("Next steps:"), c.bold);
|
|
@@ -1492,7 +1023,7 @@ ${step++}. Configure your backend:`, c.cyan);
|
|
|
1492
1023
|
const vis = process.env.GITHUB_REPO_VISIBILITY === "public" ? "--public" : "--private";
|
|
1493
1024
|
const org = process.env.GITHUB_ORG;
|
|
1494
1025
|
const repoName = org ? `${org}/${name}` : name;
|
|
1495
|
-
const githubHint = monorepoRoot ? `once GITHUB_TOKEN is in ${
|
|
1026
|
+
const githubHint = monorepoRoot ? `once GITHUB_TOKEN is in ${relative(monorepoRoot, join8(monorepoRoot, ".env"))}` : "once GITHUB_TOKEN is set";
|
|
1496
1027
|
log(
|
|
1497
1028
|
`
|
|
1498
1029
|
${step++}. Push to GitHub (${githubHint}):`,
|
|
@@ -1561,7 +1092,7 @@ if (nodeMajor < NODE_MIN_MAJOR || nodeMajor === NODE_MIN_MAJOR && nodeMinor < NO
|
|
|
1561
1092
|
);
|
|
1562
1093
|
process.exit(1);
|
|
1563
1094
|
}
|
|
1564
|
-
program.name("create-thor-app").description("Create a new Thor Commerce application").argument("[name]", "Project name (kebab-case)").option("--name <name>", "Project name (alternative to positional arg)").option("-o, --output <dir>", "Output directory (default: ./<name>)").option("--skip-git", "Skip git init").option("--skip-install", "Skip yarn install").option("--skip-remote", "Skip GitHub remote repo creation").option("--skip-vercel", "Skip Vercel project linking").option("--no-context", "Skip .agents/ directory generation").option("--no-validator", "Skip ai-validator.json injection (V0 Stop Hooks)").option("--commerce-provider <provider>", "Commerce backend provider (e.g., medusa, shopify)").option("--commerce-api-url <url>", "Commerce backend API URL").option("--commerce-api-key <key>", "Commerce publishable API key").
|
|
1095
|
+
program.name("create-thor-app").description("Create a new Thor Commerce application").argument("[name]", "Project name (kebab-case)").option("--name <name>", "Project name (alternative to positional arg)").option("-o, --output <dir>", "Output directory (default: ./<name>)").option("--skip-git", "Skip git init").option("--skip-install", "Skip yarn install").option("--skip-remote", "Skip GitHub remote repo creation").option("--skip-vercel", "Skip Vercel project linking").option("--no-context", "Skip .agents/ directory generation").option("--no-validator", "Skip ai-validator.json injection (V0 Stop Hooks)").option("--commerce-provider <provider>", "Commerce backend provider (e.g., medusa, shopify)").option("--commerce-api-url <url>", "Commerce backend API URL").option("--commerce-api-key <key>", "Commerce publishable API key").action(async (positionalName, opts) => {
|
|
1565
1096
|
let rawName = positionalName || opts.name || "";
|
|
1566
1097
|
if (!rawName) {
|
|
1567
1098
|
rawName = await promptForName();
|
|
@@ -1578,7 +1109,7 @@ program.name("create-thor-app").description("Create a new Thor Commerce applicat
|
|
|
1578
1109
|
const mode = monorepoRoot ? "monorepo" : "standalone";
|
|
1579
1110
|
if (mode === "standalone") {
|
|
1580
1111
|
}
|
|
1581
|
-
const isInsideMonorepo = monorepoRoot != null && !
|
|
1112
|
+
const isInsideMonorepo = monorepoRoot != null && !relative2(monorepoRoot, process.cwd()).startsWith("..");
|
|
1582
1113
|
const outputDir = opts.output ? resolve(opts.output) : isInsideMonorepo ? resolve(monorepoRoot, "projects", name) : resolve(process.cwd(), name);
|
|
1583
1114
|
await createApp({
|
|
1584
1115
|
name,
|
|
@@ -1593,10 +1124,7 @@ program.name("create-thor-app").description("Create a new Thor Commerce applicat
|
|
|
1593
1124
|
noValidator: !!opts.noValidator,
|
|
1594
1125
|
commerceProvider: opts.commerceProvider,
|
|
1595
1126
|
commerceApiUrl: opts.commerceApiUrl,
|
|
1596
|
-
commerceApiKey: opts.commerceApiKey
|
|
1597
|
-
preset: opts.preset,
|
|
1598
|
-
siteUrl: opts.siteUrl,
|
|
1599
|
-
generateRegions: !!opts.generateRegions
|
|
1127
|
+
commerceApiKey: opts.commerceApiKey
|
|
1600
1128
|
});
|
|
1601
1129
|
});
|
|
1602
1130
|
program.parse();
|