@thorprovider/create-thor-app 2.1.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.
- package/README.md +470 -0
- package/dist/index.js +1054 -0
- package/package.json +46 -0
- package/templates/default/.env.example +56 -0
- package/templates/default/.eslintrc.json +43 -0
- package/templates/default/README.md +151 -0
- package/templates/default/app/api/revalidate/route.ts +64 -0
- package/templates/default/app/backend-unavailable/page.tsx +85 -0
- package/templates/default/app/error.tsx +58 -0
- package/templates/default/app/global-error.tsx +208 -0
- package/templates/default/app/globals.css +125 -0
- package/templates/default/app/layout.tsx +50 -0
- package/templates/default/app/not-found.tsx +31 -0
- package/templates/default/app/page.tsx +124 -0
- package/templates/default/components/providers/app-providers.tsx +77 -0
- package/templates/default/components/providers/navigation-provider.tsx +37 -0
- package/templates/default/config/site.config.ts +156 -0
- package/templates/default/docs/README.md +46 -0
- package/templates/default/docs/commerce-api.md +243 -0
- package/templates/default/docs/components.md +276 -0
- package/templates/default/docs/dependencies.md +53 -0
- package/templates/default/docs/patterns.md +100 -0
- package/templates/default/docs/project-structure.md +53 -0
- package/templates/default/docs/rules.md +51 -0
- package/templates/default/docs/types.md +329 -0
- package/templates/default/lib/auth-context.tsx +213 -0
- package/templates/default/lib/cart-provider.tsx +53 -0
- package/templates/default/lib/commerce-provider.tsx +48 -0
- package/templates/default/lib/commerce.ts +165 -0
- package/templates/default/lib/env.ts +147 -0
- package/templates/default/lib/region-detection.ts +80 -0
- package/templates/default/lib/region-provider.tsx +53 -0
- package/templates/default/lib/regions-config.ts +48 -0
- package/templates/default/lib/storefront-config.ts +54 -0
- package/templates/default/lib/utils.ts +10 -0
- package/templates/default/next-env.d.ts +6 -0
- package/templates/default/next.config.mjs +51 -0
- package/templates/default/package.json +42 -0
- package/templates/default/postcss.config.mjs +8 -0
- package/templates/default/proxy.ts +155 -0
- package/templates/default/scripts/generate-regions-config.ts +498 -0
- package/templates/default/tsconfig.json +43 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,1054 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
import { program } from "commander";
|
|
5
|
+
import { resolve, relative as relative3 } from "path";
|
|
6
|
+
import { createInterface as createInterface2 } from "readline/promises";
|
|
7
|
+
|
|
8
|
+
// src/helpers/template.ts
|
|
9
|
+
import { dirname, join } from "path";
|
|
10
|
+
import { existsSync } from "fs";
|
|
11
|
+
import { fileURLToPath } from "url";
|
|
12
|
+
var __filename = fileURLToPath(import.meta.url);
|
|
13
|
+
var __dirname = dirname(__filename);
|
|
14
|
+
var TEMPLATE_COPY_EXCLUDE = [
|
|
15
|
+
".next",
|
|
16
|
+
"node_modules",
|
|
17
|
+
".turbo",
|
|
18
|
+
"dist",
|
|
19
|
+
".git",
|
|
20
|
+
".env",
|
|
21
|
+
".env.local",
|
|
22
|
+
"tsconfig.tsbuildinfo",
|
|
23
|
+
"next-env.d.ts",
|
|
24
|
+
"AGENTS.md",
|
|
25
|
+
"CHANGELOG.md"
|
|
26
|
+
];
|
|
27
|
+
var REMOVE_AFTER_COPY = [
|
|
28
|
+
".next",
|
|
29
|
+
"tsconfig.tsbuildinfo",
|
|
30
|
+
"next-env.d.ts",
|
|
31
|
+
"node_modules",
|
|
32
|
+
".turbo",
|
|
33
|
+
"dist",
|
|
34
|
+
"AGENTS.md",
|
|
35
|
+
"CHANGELOG.md"
|
|
36
|
+
];
|
|
37
|
+
function findMonorepoRoot() {
|
|
38
|
+
let dir = process.cwd();
|
|
39
|
+
for (let i = 0; i < 10; i++) {
|
|
40
|
+
if (existsSync(join(dir, "packages", "core", "package.json"))) {
|
|
41
|
+
return dir;
|
|
42
|
+
}
|
|
43
|
+
const parent = dirname(dir);
|
|
44
|
+
if (parent === dir) break;
|
|
45
|
+
dir = parent;
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
function resolveTemplatePath(mode, monorepoRoot) {
|
|
50
|
+
if (mode === "monorepo" && monorepoRoot) {
|
|
51
|
+
return join(monorepoRoot, "packages", "core");
|
|
52
|
+
}
|
|
53
|
+
return join(__dirname, "..", "templates", "default");
|
|
54
|
+
}
|
|
55
|
+
function createCopyFilter(sourceRoot) {
|
|
56
|
+
return (src) => {
|
|
57
|
+
if (src === sourceRoot) return true;
|
|
58
|
+
const rel = src.slice(sourceRoot.length + 1);
|
|
59
|
+
return !TEMPLATE_COPY_EXCLUDE.some(
|
|
60
|
+
(excl) => rel === excl || rel.startsWith(excl + "/")
|
|
61
|
+
);
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// src/create-app.ts
|
|
66
|
+
import {
|
|
67
|
+
cpSync,
|
|
68
|
+
existsSync as existsSync8,
|
|
69
|
+
mkdirSync as mkdirSync2,
|
|
70
|
+
rmSync
|
|
71
|
+
} from "fs";
|
|
72
|
+
import { join as join8, relative as relative2 } from "path";
|
|
73
|
+
|
|
74
|
+
// src/helpers/logger.ts
|
|
75
|
+
var c = {
|
|
76
|
+
reset: "\x1B[0m",
|
|
77
|
+
bold: "\x1B[1m",
|
|
78
|
+
green: "\x1B[32m",
|
|
79
|
+
yellow: "\x1B[33m",
|
|
80
|
+
red: "\x1B[31m",
|
|
81
|
+
cyan: "\x1B[36m",
|
|
82
|
+
gray: "\x1B[90m"
|
|
83
|
+
};
|
|
84
|
+
var log = (msg, color = c.reset) => console.log(`${color}${msg}${c.reset}`);
|
|
85
|
+
var bold = (msg) => `${c.bold}${msg}${c.reset}`;
|
|
86
|
+
|
|
87
|
+
// src/helpers/package-json.ts
|
|
88
|
+
import { existsSync as existsSync2, readFileSync, readdirSync, writeFileSync } from "fs";
|
|
89
|
+
import { join as join2, relative } from "path";
|
|
90
|
+
function getPackageVersions(monorepoRoot) {
|
|
91
|
+
const versions = {};
|
|
92
|
+
const packagesDir = join2(monorepoRoot, "packages");
|
|
93
|
+
if (!existsSync2(packagesDir)) return versions;
|
|
94
|
+
for (const dir of readdirSync(packagesDir, { withFileTypes: true })) {
|
|
95
|
+
if (!dir.isDirectory()) continue;
|
|
96
|
+
const pkgJsonPath = join2(packagesDir, dir.name, "package.json");
|
|
97
|
+
if (!existsSync2(pkgJsonPath)) continue;
|
|
98
|
+
try {
|
|
99
|
+
const pkg = JSON.parse(readFileSync(pkgJsonPath, "utf-8"));
|
|
100
|
+
if (pkg.name) {
|
|
101
|
+
versions[pkg.name] = pkg.version || "0.0.0";
|
|
102
|
+
}
|
|
103
|
+
} catch {
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return versions;
|
|
107
|
+
}
|
|
108
|
+
function resolveWorkspaceDeps(deps, versions) {
|
|
109
|
+
if (!deps) return deps;
|
|
110
|
+
const resolved = {};
|
|
111
|
+
for (const [name, version] of Object.entries(deps)) {
|
|
112
|
+
if (version.startsWith("workspace:")) {
|
|
113
|
+
const realVersion = versions[name];
|
|
114
|
+
if (!realVersion) {
|
|
115
|
+
resolved[name] = "latest";
|
|
116
|
+
} else if (version === "workspace:*" || version === "workspace:^") {
|
|
117
|
+
resolved[name] = `^${realVersion}`;
|
|
118
|
+
} else if (version === "workspace:~") {
|
|
119
|
+
resolved[name] = `~${realVersion}`;
|
|
120
|
+
} else {
|
|
121
|
+
resolved[name] = `^${realVersion}`;
|
|
122
|
+
}
|
|
123
|
+
} else {
|
|
124
|
+
resolved[name] = version;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return resolved;
|
|
128
|
+
}
|
|
129
|
+
function updatePackageJson(opts) {
|
|
130
|
+
const pkgPath = join2(opts.targetDir, "package.json");
|
|
131
|
+
const pkg = JSON.parse(readFileSync(pkgPath, "utf-8"));
|
|
132
|
+
pkg.name = `@thorprovider/${opts.name}`;
|
|
133
|
+
pkg.version = "0.1.0";
|
|
134
|
+
pkg.description = `Thor Commerce app \u2014 ${opts.name}`;
|
|
135
|
+
pkg.private = true;
|
|
136
|
+
if (opts.monorepoRoot) {
|
|
137
|
+
const versions = getPackageVersions(opts.monorepoRoot);
|
|
138
|
+
pkg.dependencies = resolveWorkspaceDeps(pkg.dependencies, versions);
|
|
139
|
+
pkg.devDependencies = resolveWorkspaceDeps(pkg.devDependencies, versions);
|
|
140
|
+
}
|
|
141
|
+
if (opts.mode === "monorepo" && opts.monorepoRoot) {
|
|
142
|
+
const relFromMonorepo = relative(opts.monorepoRoot, opts.targetDir);
|
|
143
|
+
const generateCmd = `tsx ../../scripts/generate-regions-config.ts --package-dir="${relFromMonorepo}"`;
|
|
144
|
+
pkg.scripts = {
|
|
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
|
+
}
|
|
161
|
+
delete pkg.scripts?.["generate:agent-index"];
|
|
162
|
+
delete pkg.packageManager;
|
|
163
|
+
writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + "\n");
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// src/helpers/git.ts
|
|
167
|
+
import { execSync } from "child_process";
|
|
168
|
+
import { existsSync as existsSync3, readFileSync as readFileSync2 } from "fs";
|
|
169
|
+
import { join as join3 } from "path";
|
|
170
|
+
|
|
171
|
+
// src/helpers/prompts.ts
|
|
172
|
+
import { createInterface } from "readline/promises";
|
|
173
|
+
var isInteractive = process.stdin.isTTY === true;
|
|
174
|
+
async function promptConfirm(question, defaultYes = false) {
|
|
175
|
+
if (!isInteractive) return defaultYes;
|
|
176
|
+
const hint = defaultYes ? "Y/n" : "y/N";
|
|
177
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
178
|
+
try {
|
|
179
|
+
const answer = await rl.question(
|
|
180
|
+
`${c.cyan}? ${question} (${hint}):${c.reset} `
|
|
181
|
+
);
|
|
182
|
+
const trimmed = answer.trim().toLowerCase();
|
|
183
|
+
if (!trimmed) return defaultYes;
|
|
184
|
+
return trimmed === "y" || trimmed === "yes";
|
|
185
|
+
} finally {
|
|
186
|
+
rl.close();
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
async function promptInput(question, defaultValue = "") {
|
|
190
|
+
if (!isInteractive) return defaultValue;
|
|
191
|
+
const label = defaultValue ? `${c.cyan}? ${question}${c.reset} [${defaultValue}]: ` : `${c.cyan}? ${question}:${c.reset} `;
|
|
192
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
193
|
+
try {
|
|
194
|
+
const answer = await rl.question(label);
|
|
195
|
+
return answer.trim() || defaultValue;
|
|
196
|
+
} finally {
|
|
197
|
+
rl.close();
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
async function promptSecret(question) {
|
|
201
|
+
const { stdin, stdout } = process;
|
|
202
|
+
if (!stdin.isTTY) return "";
|
|
203
|
+
stdout.write(`${c.cyan}? ${question}:${c.reset} `);
|
|
204
|
+
const chars = [];
|
|
205
|
+
stdin.setRawMode(true);
|
|
206
|
+
stdin.resume();
|
|
207
|
+
stdin.setEncoding("utf8");
|
|
208
|
+
return new Promise((resolve2) => {
|
|
209
|
+
const handler = (ch) => {
|
|
210
|
+
switch (ch) {
|
|
211
|
+
case "\r":
|
|
212
|
+
case "\n":
|
|
213
|
+
case "":
|
|
214
|
+
stdin.setRawMode(false);
|
|
215
|
+
stdin.pause();
|
|
216
|
+
stdin.removeListener("data", handler);
|
|
217
|
+
stdout.write("\n");
|
|
218
|
+
resolve2(chars.join(""));
|
|
219
|
+
break;
|
|
220
|
+
case "":
|
|
221
|
+
stdout.write("\n");
|
|
222
|
+
process.exit(0);
|
|
223
|
+
break;
|
|
224
|
+
case "\x7F":
|
|
225
|
+
if (chars.length > 0) {
|
|
226
|
+
chars.pop();
|
|
227
|
+
stdout.write("\b \b");
|
|
228
|
+
}
|
|
229
|
+
break;
|
|
230
|
+
default:
|
|
231
|
+
chars.push(ch);
|
|
232
|
+
stdout.write("*");
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
stdin.on("data", handler);
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// src/helpers/git.ts
|
|
240
|
+
function loadRootEnv(monorepoRoot) {
|
|
241
|
+
const envPath = join3(monorepoRoot, ".env");
|
|
242
|
+
if (!existsSync3(envPath)) return;
|
|
243
|
+
const lines = readFileSync2(envPath, "utf-8").split("\n");
|
|
244
|
+
for (const line of lines) {
|
|
245
|
+
const trimmed = line.trim();
|
|
246
|
+
if (!trimmed || trimmed.startsWith("#")) continue;
|
|
247
|
+
const eqIdx = trimmed.indexOf("=");
|
|
248
|
+
if (eqIdx < 1) continue;
|
|
249
|
+
const key = trimmed.slice(0, eqIdx).trim();
|
|
250
|
+
const val = trimmed.slice(eqIdx + 1).trim().replace(/^["']|["']$/g, "");
|
|
251
|
+
if (key && !(key in process.env)) {
|
|
252
|
+
process.env[key] = val;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
function initGitRepo(targetDir) {
|
|
257
|
+
try {
|
|
258
|
+
execSync("git init -b main", { cwd: targetDir, stdio: "pipe" });
|
|
259
|
+
execSync("git add -A", { cwd: targetDir, stdio: "pipe" });
|
|
260
|
+
execSync('git commit -m "feat: initial scaffold from create-thor-app"', {
|
|
261
|
+
cwd: targetDir,
|
|
262
|
+
stdio: "pipe",
|
|
263
|
+
env: {
|
|
264
|
+
...process.env,
|
|
265
|
+
GIT_AUTHOR_NAME: process.env.GIT_AUTHOR_NAME || "Thor Commerce",
|
|
266
|
+
GIT_AUTHOR_EMAIL: process.env.GIT_AUTHOR_EMAIL || "noreply@thorcommerce.dev",
|
|
267
|
+
GIT_COMMITTER_NAME: process.env.GIT_AUTHOR_NAME || "Thor Commerce",
|
|
268
|
+
GIT_COMMITTER_EMAIL: process.env.GIT_AUTHOR_EMAIL || "noreply@thorcommerce.dev"
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
return true;
|
|
272
|
+
} catch {
|
|
273
|
+
return false;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
async function createRemoteRepo(name, targetDir, monorepoRoot) {
|
|
277
|
+
let token = process.env.GITHUB_TOKEN;
|
|
278
|
+
let org = process.env.GITHUB_ORG;
|
|
279
|
+
const visibility = process.env.GITHUB_REPO_VISIBILITY === "public" ? "--public" : "--private";
|
|
280
|
+
if (!token) {
|
|
281
|
+
if (!isInteractive) {
|
|
282
|
+
log(
|
|
283
|
+
" \u26A0\uFE0F GITHUB_TOKEN not set \u2014 skipping remote repo creation.",
|
|
284
|
+
c.yellow
|
|
285
|
+
);
|
|
286
|
+
const hint = monorepoRoot ? ` \u2192 Add GITHUB_TOKEN to ${join3(monorepoRoot, ".env")}` : " \u2192 Set the GITHUB_TOKEN environment variable and re-run";
|
|
287
|
+
log(hint, c.gray);
|
|
288
|
+
return false;
|
|
289
|
+
}
|
|
290
|
+
log("\n\u{1F511} GitHub token required for remote repo creation.", c.cyan);
|
|
291
|
+
log(
|
|
292
|
+
" Generate one at: https://github.com/settings/tokens (repo scope)",
|
|
293
|
+
c.gray
|
|
294
|
+
);
|
|
295
|
+
token = await promptSecret("GitHub Personal Access Token");
|
|
296
|
+
if (!token) {
|
|
297
|
+
log(" \u26A0\uFE0F No token provided \u2014 skipping remote repo creation.", c.yellow);
|
|
298
|
+
return false;
|
|
299
|
+
}
|
|
300
|
+
process.env.GITHUB_TOKEN = token;
|
|
301
|
+
}
|
|
302
|
+
if (!org && isInteractive) {
|
|
303
|
+
org = await promptInput(
|
|
304
|
+
"GitHub organization (leave blank for personal account)"
|
|
305
|
+
);
|
|
306
|
+
if (org) process.env.GITHUB_ORG = org;
|
|
307
|
+
}
|
|
308
|
+
const repoName = org ? `${org}/${name}` : name;
|
|
309
|
+
try {
|
|
310
|
+
log(`\u{1F419} Creating GitHub repo ${repoName}...`, c.cyan);
|
|
311
|
+
execSync(
|
|
312
|
+
`gh repo create ${repoName} ${visibility} --source=. --push --remote=origin`,
|
|
313
|
+
{
|
|
314
|
+
cwd: targetDir,
|
|
315
|
+
stdio: "pipe",
|
|
316
|
+
env: { ...process.env, GITHUB_TOKEN: token }
|
|
317
|
+
}
|
|
318
|
+
);
|
|
319
|
+
log(
|
|
320
|
+
` \u2713 Remote repo created: https://github.com/${repoName}`,
|
|
321
|
+
c.green
|
|
322
|
+
);
|
|
323
|
+
log(" \u2713 Code pushed to origin/main", c.green);
|
|
324
|
+
return true;
|
|
325
|
+
} catch (err) {
|
|
326
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
327
|
+
log(` \u26A0\uFE0F Remote repo creation failed: ${msg}`, c.yellow);
|
|
328
|
+
log(" \u2192 You can create it manually later:", c.gray);
|
|
329
|
+
log(` cd ${targetDir}`, c.gray);
|
|
330
|
+
log(
|
|
331
|
+
` gh repo create ${repoName} ${visibility} --source=. --push`,
|
|
332
|
+
c.gray
|
|
333
|
+
);
|
|
334
|
+
return false;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// src/helpers/install.ts
|
|
339
|
+
import { execSync as execSync2 } from "child_process";
|
|
340
|
+
function installDependencies(targetDir) {
|
|
341
|
+
log("\u{1F4E6} Installing dependencies (this may take a minute)...", c.cyan);
|
|
342
|
+
try {
|
|
343
|
+
execSync2("yarn install", { cwd: targetDir, stdio: "inherit" });
|
|
344
|
+
log(" \u2713 Dependencies installed", c.green);
|
|
345
|
+
return true;
|
|
346
|
+
} catch {
|
|
347
|
+
log(" \u26A0 yarn install failed \u2014 run it manually", c.yellow);
|
|
348
|
+
return false;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// src/helpers/env.ts
|
|
353
|
+
import { existsSync as existsSync4, readFileSync as readFileSync3, writeFileSync as writeFileSync2 } from "fs";
|
|
354
|
+
import { join as join4 } from "path";
|
|
355
|
+
function createEnvFile(targetDir, overrides) {
|
|
356
|
+
const envExamplePath = join4(targetDir, ".env.example");
|
|
357
|
+
const envPath = join4(targetDir, ".env");
|
|
358
|
+
if (existsSync4(envExamplePath) && !existsSync4(envPath)) {
|
|
359
|
+
let content = readFileSync3(envExamplePath, "utf-8");
|
|
360
|
+
if (overrides) {
|
|
361
|
+
for (const [key, value] of Object.entries(overrides)) {
|
|
362
|
+
if (value !== void 0) {
|
|
363
|
+
content = content.replace(
|
|
364
|
+
new RegExp(`^(${key})=.*$`, "m"),
|
|
365
|
+
`$1=${value}`
|
|
366
|
+
);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
writeFileSync2(envPath, content, "utf-8");
|
|
371
|
+
log(" \u2713 .env created from .env.example", c.green);
|
|
372
|
+
if (overrides) {
|
|
373
|
+
const applied = Object.entries(overrides).filter(([, v]) => v !== void 0);
|
|
374
|
+
for (const [key] of applied) {
|
|
375
|
+
log(` \u2713 .env: ${key} configured`, c.green);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
function createNpmrc(targetDir) {
|
|
381
|
+
const npmrcContent = [
|
|
382
|
+
"# Thor Commerce packages are published to GitHub Packages.",
|
|
383
|
+
"# Set NODE_AUTH_TOKEN in your environment or CI/CD (e.g., Vercel).",
|
|
384
|
+
"# Token should be a GitHub PAT (classic) with read:packages.",
|
|
385
|
+
"@thorprovider:registry=https://npm.pkg.github.com",
|
|
386
|
+
"//npm.pkg.github.com/:_authToken=${NODE_AUTH_TOKEN}",
|
|
387
|
+
""
|
|
388
|
+
].join("\n");
|
|
389
|
+
writeFileSync2(join4(targetDir, ".npmrc"), npmrcContent);
|
|
390
|
+
log(" \u2713 .npmrc created (GitHub Packages)", 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
|
+
|
|
428
|
+
// src/helpers/v0-context.ts
|
|
429
|
+
import { existsSync as existsSync6, readFileSync as readFileSync5, writeFileSync as writeFileSync4 } from "fs";
|
|
430
|
+
import { join as join6 } from "path";
|
|
431
|
+
var V0_CONTEXT_FILES = [
|
|
432
|
+
"HOW_TO_USE_THIS_PACKAGE.md",
|
|
433
|
+
"docs/components.md",
|
|
434
|
+
"lib/commerce.ts",
|
|
435
|
+
"lib/storefront-config.ts",
|
|
436
|
+
"lib/commerce-provider.tsx",
|
|
437
|
+
"lib/region-provider.tsx",
|
|
438
|
+
"lib/auth-context.tsx",
|
|
439
|
+
"lib/cart-provider.tsx",
|
|
440
|
+
"components/providers/app-providers.tsx",
|
|
441
|
+
"components/providers/navigation-provider.tsx",
|
|
442
|
+
"app/layout.tsx",
|
|
443
|
+
"app/page.tsx",
|
|
444
|
+
"config/site.config.ts"
|
|
445
|
+
];
|
|
446
|
+
function generateV0Context(name, targetDir, sourceDir) {
|
|
447
|
+
let md = `# V0 Context \u2014 ${name}
|
|
448
|
+
|
|
449
|
+
`;
|
|
450
|
+
md += `> Generated by create-thor-app on ${(/* @__PURE__ */ new Date()).toISOString().split("T")[0]}
|
|
451
|
+
|
|
452
|
+
`;
|
|
453
|
+
md += `## Project
|
|
454
|
+
`;
|
|
455
|
+
md += `- **Package**: \`@thorprovider/${name}\`
|
|
456
|
+
`;
|
|
457
|
+
md += `- **Stack**: Next.js 16, React 19, Tailwind CSS 4, @thorprovider/* packages
|
|
458
|
+
`;
|
|
459
|
+
md += `- **Architecture**: L1 (types) \u2192 L2 (adapters) \u2192 L2.5 (providers) \u2192 L3 (components) \u2192 L5 (this app)
|
|
460
|
+
|
|
461
|
+
`;
|
|
462
|
+
md += `---
|
|
463
|
+
|
|
464
|
+
`;
|
|
465
|
+
md += `## Before Implementing \u2014 Read @thorprovider/core Docs First
|
|
466
|
+
|
|
467
|
+
`;
|
|
468
|
+
md += `Read these 7 files **in order** before writing any feature. They are your single source of truth for types, method signatures, component inventory, and patterns.
|
|
469
|
+
|
|
470
|
+
`;
|
|
471
|
+
md += `| Order | File | What you get |
|
|
472
|
+
`;
|
|
473
|
+
md += `|------:|------|-------------|
|
|
474
|
+
`;
|
|
475
|
+
md += `| 1 | \`packages/core/docs/README.md\` | Doc map + entry point |
|
|
476
|
+
`;
|
|
477
|
+
md += `| 2 | \`packages/core/docs/project-structure.md\` | Directory layout + conventions |
|
|
478
|
+
`;
|
|
479
|
+
md += `| 3 | \`packages/core/docs/dependencies.md\` | 55-component inventory (TIER 1\u20134) + which components exist |
|
|
480
|
+
`;
|
|
481
|
+
md += `| 3b | \`packages/core/docs/components.md\` | Complete @thorprovider/components API: import paths, key props, callbacks, usage examples |
|
|
482
|
+
`;
|
|
483
|
+
md += `| 4 | \`packages/core/docs/types.md\` | All domain types: Product, Cart, Order, Customer, Address, Money\u2026 |
|
|
484
|
+
`;
|
|
485
|
+
md += `| 5 | \`packages/core/docs/commerce-api.md\` | Every \`commerce.*()\` method with exact signatures + circuit breaker rules |
|
|
486
|
+
`;
|
|
487
|
+
md += `| 6 | \`packages/core/docs/patterns.md\` | Page patterns, server/client boundaries, data fetching |
|
|
488
|
+
`;
|
|
489
|
+
md += `| 7 | \`packages/core/docs/rules.md\` | Hard constraints \u2014 what V0 must never do |
|
|
490
|
+
|
|
491
|
+
`;
|
|
492
|
+
md += `> **AGENTS.md** (root) is the navigation index for the full monorepo. Use it to discover where anything lives.
|
|
493
|
+
|
|
494
|
+
`;
|
|
495
|
+
md += `### Quick rules from docs
|
|
496
|
+
|
|
497
|
+
`;
|
|
498
|
+
md += `- **Types are in \`types.md\`** \u2014 never guess field names. \`Product\` has \`handle\`, not \`slug\`. \`Cart\` has \`cost.totalAmount\`, not \`total\`.
|
|
499
|
+
`;
|
|
500
|
+
md += `- **Method signatures are in \`commerce-api.md\`** \u2014 \`getProducts({ query, sortKey, first, skip })\`, not \`getProducts(filter)\`.
|
|
501
|
+
`;
|
|
502
|
+
md += `- **Check \`dependencies.md\` before creating UI** \u2014 if a component exists in TIER 1\u20134, use it. Do not create a new \`<Button>\` or \`<ProductCard>\`.
|
|
503
|
+
`;
|
|
504
|
+
md += `- **Optional methods need a guard** \u2014 \`if (commerce.method) { await commerce.method() }\` (see commerce-api.md).
|
|
505
|
+
`;
|
|
506
|
+
md += `- **All backend reads use circuit breaker** \u2014 \`backendCircuitBreaker.execute(() => commerce.getProducts(), [])\`.
|
|
507
|
+
|
|
508
|
+
`;
|
|
509
|
+
md += `---
|
|
510
|
+
|
|
511
|
+
`;
|
|
512
|
+
md += `## Frozen Architecture Files
|
|
513
|
+
|
|
514
|
+
`;
|
|
515
|
+
md += `These files define constraints that **must not be modified** by V0.
|
|
516
|
+
`;
|
|
517
|
+
md += `Paste this entire document as the **first message** in V0.
|
|
518
|
+
|
|
519
|
+
`;
|
|
520
|
+
for (const relPath of V0_CONTEXT_FILES) {
|
|
521
|
+
const absPath = join6(sourceDir, relPath);
|
|
522
|
+
if (!existsSync6(absPath)) continue;
|
|
523
|
+
const content = readFileSync5(absPath, "utf-8");
|
|
524
|
+
const ext = relPath.split(".").pop() || "";
|
|
525
|
+
const lang = ext === "md" ? "markdown" : ext === "ts" || ext === "tsx" ? "typescript" : ext;
|
|
526
|
+
md += `### ${relPath}
|
|
527
|
+
|
|
528
|
+
`;
|
|
529
|
+
md += `\`\`\`${lang}
|
|
530
|
+
`;
|
|
531
|
+
md += content;
|
|
532
|
+
if (!content.endsWith("\n")) md += "\n";
|
|
533
|
+
md += `\`\`\`
|
|
534
|
+
|
|
535
|
+
`;
|
|
536
|
+
}
|
|
537
|
+
writeFileSync4(join6(targetDir, ".v0-context.md"), md, "utf-8");
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
// src/helpers/vercel.ts
|
|
541
|
+
import { existsSync as existsSync7, mkdirSync, writeFileSync as writeFileSync5 } from "fs";
|
|
542
|
+
import { join as join7 } from "path";
|
|
543
|
+
var VERCEL_API = "https://api.vercel.com";
|
|
544
|
+
async function createVercelProject(name, token, teamSlug, gitRepo) {
|
|
545
|
+
const url = new URL(`${VERCEL_API}/v11/projects`);
|
|
546
|
+
if (teamSlug) url.searchParams.set("slug", teamSlug);
|
|
547
|
+
const body = {
|
|
548
|
+
name,
|
|
549
|
+
framework: "nextjs"
|
|
550
|
+
};
|
|
551
|
+
if (gitRepo) {
|
|
552
|
+
body.gitRepository = {
|
|
553
|
+
repo: gitRepo,
|
|
554
|
+
type: "github"
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
const res = await fetch(url.toString(), {
|
|
558
|
+
method: "POST",
|
|
559
|
+
headers: {
|
|
560
|
+
Authorization: `Bearer ${token}`,
|
|
561
|
+
"Content-Type": "application/json"
|
|
562
|
+
},
|
|
563
|
+
body: JSON.stringify(body)
|
|
564
|
+
});
|
|
565
|
+
if (!res.ok) {
|
|
566
|
+
const err = await res.json().catch(() => ({}));
|
|
567
|
+
const msg = err?.error?.message || `HTTP ${res.status}`;
|
|
568
|
+
throw new Error(msg);
|
|
569
|
+
}
|
|
570
|
+
return await res.json();
|
|
571
|
+
}
|
|
572
|
+
function writeProjectJson(targetDir, orgId, projectId) {
|
|
573
|
+
const vercelDir = join7(targetDir, ".vercel");
|
|
574
|
+
if (!existsSync7(vercelDir)) mkdirSync(vercelDir, { recursive: true });
|
|
575
|
+
writeFileSync5(
|
|
576
|
+
join7(vercelDir, "project.json"),
|
|
577
|
+
JSON.stringify({ orgId, projectId }, null, 2) + "\n"
|
|
578
|
+
);
|
|
579
|
+
}
|
|
580
|
+
function generateDeployWorkflow(targetDir) {
|
|
581
|
+
const workflowDir = join7(targetDir, ".github", "workflows");
|
|
582
|
+
mkdirSync(workflowDir, { recursive: true });
|
|
583
|
+
const workflow = `# Vercel deploy workflow \u2014 generated by create-thor-app.
|
|
584
|
+
# Required GitHub Actions secrets:
|
|
585
|
+
# VERCEL_TOKEN \u2014 Vercel Access Token
|
|
586
|
+
# VERCEL_ORG_ID \u2014 From .vercel/project.json (accountId)
|
|
587
|
+
# VERCEL_PROJECT_ID \u2014 From .vercel/project.json (projectId)
|
|
588
|
+
# NODE_AUTH_TOKEN \u2014 GitHub PAT with read:packages (for @thorprovider/*)
|
|
589
|
+
|
|
590
|
+
name: Vercel Deploy
|
|
591
|
+
|
|
592
|
+
env:
|
|
593
|
+
VERCEL_ORG_ID: \${{ secrets.VERCEL_ORG_ID }}
|
|
594
|
+
VERCEL_PROJECT_ID: \${{ secrets.VERCEL_PROJECT_ID }}
|
|
595
|
+
|
|
596
|
+
on:
|
|
597
|
+
push:
|
|
598
|
+
branches: [main]
|
|
599
|
+
pull_request:
|
|
600
|
+
branches: [main]
|
|
601
|
+
|
|
602
|
+
jobs:
|
|
603
|
+
deploy:
|
|
604
|
+
runs-on: ubuntu-latest
|
|
605
|
+
steps:
|
|
606
|
+
- uses: actions/checkout@v4
|
|
607
|
+
|
|
608
|
+
- name: Setup Node.js
|
|
609
|
+
uses: actions/setup-node@v4
|
|
610
|
+
with:
|
|
611
|
+
node-version: '20'
|
|
612
|
+
registry-url: 'https://npm.pkg.github.com'
|
|
613
|
+
|
|
614
|
+
- name: Install Vercel CLI
|
|
615
|
+
run: npm install -g vercel@latest
|
|
616
|
+
|
|
617
|
+
- name: Pull Vercel Environment
|
|
618
|
+
run: vercel pull --yes --environment=\${{ github.ref == 'refs/heads/main' && 'production' || 'preview' }} --token=\${{ secrets.VERCEL_TOKEN }}
|
|
619
|
+
|
|
620
|
+
- name: Build Project
|
|
621
|
+
run: vercel build \${{ github.ref == 'refs/heads/main' && '--prod' || '' }} --token=\${{ secrets.VERCEL_TOKEN }}
|
|
622
|
+
env:
|
|
623
|
+
NODE_AUTH_TOKEN: \${{ secrets.NODE_AUTH_TOKEN }}
|
|
624
|
+
|
|
625
|
+
- name: Deploy to Vercel
|
|
626
|
+
id: deploy
|
|
627
|
+
run: |
|
|
628
|
+
URL=$(vercel deploy --prebuilt \${{ github.ref == 'refs/heads/main' && '--prod' || '' }} --token=\${{ secrets.VERCEL_TOKEN }})
|
|
629
|
+
echo "url=$URL" >> "$GITHUB_OUTPUT"
|
|
630
|
+
|
|
631
|
+
- name: Comment Preview URL
|
|
632
|
+
if: github.event_name == 'pull_request'
|
|
633
|
+
uses: actions/github-script@v7
|
|
634
|
+
with:
|
|
635
|
+
script: |
|
|
636
|
+
github.rest.issues.createComment({
|
|
637
|
+
issue_number: context.issue.number,
|
|
638
|
+
owner: context.repo.owner,
|
|
639
|
+
repo: context.repo.repo,
|
|
640
|
+
body: \`\u{1F517} Preview: \${{ steps.deploy.outputs.url }}\`
|
|
641
|
+
})
|
|
642
|
+
`;
|
|
643
|
+
writeFileSync5(join7(workflowDir, "vercel-deploy.yml"), workflow);
|
|
644
|
+
}
|
|
645
|
+
async function linkVercel(name, targetDir, githubRepo) {
|
|
646
|
+
let token = process.env.VERCEL_TOKEN;
|
|
647
|
+
if (!token) {
|
|
648
|
+
if (!isInteractive) {
|
|
649
|
+
log(
|
|
650
|
+
" \u26A0\uFE0F VERCEL_TOKEN not set \u2014 skipping Vercel integration.",
|
|
651
|
+
c.yellow
|
|
652
|
+
);
|
|
653
|
+
log(
|
|
654
|
+
" \u2192 Set the VERCEL_TOKEN environment variable and re-run",
|
|
655
|
+
c.gray
|
|
656
|
+
);
|
|
657
|
+
return null;
|
|
658
|
+
}
|
|
659
|
+
log("\n\u{1F511} Vercel Access Token required.", c.cyan);
|
|
660
|
+
log(
|
|
661
|
+
" Generate one at: https://vercel.com/account/tokens (Full Account scope)",
|
|
662
|
+
c.gray
|
|
663
|
+
);
|
|
664
|
+
token = await promptSecret("Vercel Access Token");
|
|
665
|
+
if (!token) {
|
|
666
|
+
log(
|
|
667
|
+
" \u26A0\uFE0F No token provided \u2014 skipping Vercel integration.",
|
|
668
|
+
c.yellow
|
|
669
|
+
);
|
|
670
|
+
return null;
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
let teamSlug = process.env.VERCEL_TEAM_SLUG;
|
|
674
|
+
if (!teamSlug && isInteractive) {
|
|
675
|
+
teamSlug = await promptInput(
|
|
676
|
+
"Vercel team slug (leave blank for personal account)"
|
|
677
|
+
);
|
|
678
|
+
}
|
|
679
|
+
try {
|
|
680
|
+
log(`
|
|
681
|
+
\u{1F517} Creating Vercel project "${name}"...`, c.cyan);
|
|
682
|
+
const project = await createVercelProject(
|
|
683
|
+
name,
|
|
684
|
+
token,
|
|
685
|
+
teamSlug || void 0,
|
|
686
|
+
githubRepo
|
|
687
|
+
);
|
|
688
|
+
writeProjectJson(targetDir, project.accountId, project.id);
|
|
689
|
+
log(" \u2713 .vercel/project.json written", c.green);
|
|
690
|
+
generateDeployWorkflow(targetDir);
|
|
691
|
+
log(" \u2713 .github/workflows/vercel-deploy.yml generated", c.green);
|
|
692
|
+
const scope = teamSlug || project.accountId;
|
|
693
|
+
const projectUrl = `https://vercel.com/${scope}/${project.name}`;
|
|
694
|
+
log(` \u2713 Project created: ${projectUrl}`, c.green);
|
|
695
|
+
log("", c.reset);
|
|
696
|
+
log(
|
|
697
|
+
" \u2500\u2500 GitHub Actions secrets (add these in your repo settings) \u2500\u2500",
|
|
698
|
+
c.bold
|
|
699
|
+
);
|
|
700
|
+
log(` VERCEL_TOKEN = ${c.gray}<your vercel token>${c.reset}`);
|
|
701
|
+
log(` VERCEL_ORG_ID = ${c.cyan}${project.accountId}${c.reset}`);
|
|
702
|
+
log(` VERCEL_PROJECT_ID = ${c.cyan}${project.id}${c.reset}`);
|
|
703
|
+
if (githubRepo) {
|
|
704
|
+
log("", c.reset);
|
|
705
|
+
log(
|
|
706
|
+
` Settings \u2192 Secrets: https://github.com/${githubRepo}/settings/secrets/actions`,
|
|
707
|
+
c.gray
|
|
708
|
+
);
|
|
709
|
+
log("", c.reset);
|
|
710
|
+
log(
|
|
711
|
+
" \u2139\uFE0F For preview URLs on pull requests, complete the native integration:",
|
|
712
|
+
c.gray
|
|
713
|
+
);
|
|
714
|
+
log(
|
|
715
|
+
` https://vercel.com/new \u2192 Import Git Repository \u2192 ${githubRepo}`,
|
|
716
|
+
c.gray
|
|
717
|
+
);
|
|
718
|
+
}
|
|
719
|
+
return {
|
|
720
|
+
projectId: project.id,
|
|
721
|
+
orgId: project.accountId,
|
|
722
|
+
projectUrl
|
|
723
|
+
};
|
|
724
|
+
} catch (err) {
|
|
725
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
726
|
+
log(` \u26A0\uFE0F Vercel project creation failed: ${msg}`, c.yellow);
|
|
727
|
+
log(" \u2192 You can link manually later:", c.gray);
|
|
728
|
+
log(` cd ${targetDir} && vercel link`, c.gray);
|
|
729
|
+
return null;
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
// src/create-app.ts
|
|
734
|
+
function bundleRegionsScript(monorepoRoot, targetDir) {
|
|
735
|
+
if (!monorepoRoot) return;
|
|
736
|
+
const regionsScriptSrc = join8(
|
|
737
|
+
monorepoRoot,
|
|
738
|
+
"scripts",
|
|
739
|
+
"generate-regions-config.ts"
|
|
740
|
+
);
|
|
741
|
+
if (existsSync8(regionsScriptSrc)) {
|
|
742
|
+
const scriptsDir = join8(targetDir, "scripts");
|
|
743
|
+
mkdirSync2(scriptsDir, { recursive: true });
|
|
744
|
+
cpSync(regionsScriptSrc, join8(scriptsDir, "generate-regions-config.ts"));
|
|
745
|
+
log(" \u2713 Bundled generate-regions-config.ts", c.green);
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
async function createApp(opts) {
|
|
749
|
+
const {
|
|
750
|
+
name,
|
|
751
|
+
outputDir,
|
|
752
|
+
mode,
|
|
753
|
+
monorepoRoot,
|
|
754
|
+
skipGit,
|
|
755
|
+
skipInstall,
|
|
756
|
+
skipRemote,
|
|
757
|
+
skipVercel,
|
|
758
|
+
skipV0Context,
|
|
759
|
+
commerceProvider,
|
|
760
|
+
commerceApiUrl,
|
|
761
|
+
commerceApiKey,
|
|
762
|
+
preset,
|
|
763
|
+
siteUrl,
|
|
764
|
+
generateRegions
|
|
765
|
+
} = opts;
|
|
766
|
+
if (monorepoRoot) {
|
|
767
|
+
loadRootEnv(monorepoRoot);
|
|
768
|
+
}
|
|
769
|
+
const templatePath = resolveTemplatePath(mode, monorepoRoot);
|
|
770
|
+
if (!existsSync8(templatePath)) {
|
|
771
|
+
log(`
|
|
772
|
+
\u274C Template not found: ${templatePath}`, c.red);
|
|
773
|
+
if (mode === "standalone") {
|
|
774
|
+
log(
|
|
775
|
+
" The bundled template may be missing. Reinstall the package.",
|
|
776
|
+
c.yellow
|
|
777
|
+
);
|
|
778
|
+
}
|
|
779
|
+
process.exit(1);
|
|
780
|
+
}
|
|
781
|
+
log("\n" + bold("\u{1F528} create-thor-app"), c.bold);
|
|
782
|
+
log(` Mode : ${c.cyan}${mode}${c.reset}`);
|
|
783
|
+
log(` Template : ${c.cyan}${templatePath}${c.reset}`);
|
|
784
|
+
log(` Target : ${c.cyan}${outputDir}${c.reset}`);
|
|
785
|
+
log(` Package : ${c.cyan}@thorprovider/${name}${c.reset}
|
|
786
|
+
`);
|
|
787
|
+
if (existsSync8(outputDir)) {
|
|
788
|
+
log(`\u274C Directory already exists: ${outputDir}`, c.red);
|
|
789
|
+
log(
|
|
790
|
+
" Choose a different name or delete the existing directory.",
|
|
791
|
+
c.yellow
|
|
792
|
+
);
|
|
793
|
+
process.exit(1);
|
|
794
|
+
}
|
|
795
|
+
log("\u{1F4CB} Copying template...", c.cyan);
|
|
796
|
+
try {
|
|
797
|
+
mkdirSync2(outputDir, { recursive: true });
|
|
798
|
+
} catch (err) {
|
|
799
|
+
const code = err.code;
|
|
800
|
+
if (code === "EACCES") {
|
|
801
|
+
log(`
|
|
802
|
+
\u274C Permission denied: cannot create ${outputDir}`, c.red);
|
|
803
|
+
log(" Check directory permissions or choose a different output path.", c.yellow);
|
|
804
|
+
} else {
|
|
805
|
+
log(`
|
|
806
|
+
\u274C Failed to create directory: ${outputDir}`, c.red);
|
|
807
|
+
log(` ${err.message}`, c.yellow);
|
|
808
|
+
}
|
|
809
|
+
process.exit(1);
|
|
810
|
+
}
|
|
811
|
+
cpSync(templatePath, outputDir, {
|
|
812
|
+
recursive: true,
|
|
813
|
+
filter: createCopyFilter(templatePath)
|
|
814
|
+
});
|
|
815
|
+
log(" \u2713 Files copied", c.green);
|
|
816
|
+
log("\u{1F9F9} Removing build artifacts...", c.cyan);
|
|
817
|
+
for (const p of REMOVE_AFTER_COPY) {
|
|
818
|
+
const fullPath = join8(outputDir, p);
|
|
819
|
+
if (existsSync8(fullPath)) {
|
|
820
|
+
rmSync(fullPath, { recursive: true, force: true });
|
|
821
|
+
log(` \u2713 Removed ${p}`, c.gray);
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
log("\u{1F4E6} Updating package.json...", c.cyan);
|
|
825
|
+
updatePackageJson({
|
|
826
|
+
targetDir: outputDir,
|
|
827
|
+
name,
|
|
828
|
+
monorepoRoot,
|
|
829
|
+
mode
|
|
830
|
+
});
|
|
831
|
+
log(" \u2713 package.json updated", c.green);
|
|
832
|
+
bundleRegionsScript(monorepoRoot, outputDir);
|
|
833
|
+
createNpmrc(outputDir);
|
|
834
|
+
const envOverrides = {
|
|
835
|
+
...commerceProvider && { NEXT_PUBLIC_COMMERCE_PROVIDER: commerceProvider },
|
|
836
|
+
...commerceApiUrl && { NEXT_PUBLIC_COMMERCE_API_URL: commerceApiUrl },
|
|
837
|
+
...commerceApiKey && { NEXT_PUBLIC_COMMERCE_API_KEY: commerceApiKey }
|
|
838
|
+
};
|
|
839
|
+
createEnvFile(
|
|
840
|
+
outputDir,
|
|
841
|
+
Object.keys(envOverrides).length > 0 ? envOverrides : void 0
|
|
842
|
+
);
|
|
843
|
+
if (preset || siteUrl) {
|
|
844
|
+
log("\u2699\uFE0F Applying site configuration...", c.cyan);
|
|
845
|
+
patchSiteConfig(outputDir, { preset, siteUrl });
|
|
846
|
+
}
|
|
847
|
+
if (!skipV0Context) {
|
|
848
|
+
log("\u{1F4C4} Generating .v0-context.md...", c.cyan);
|
|
849
|
+
const contextSourceDir = mode === "monorepo" && monorepoRoot ? join8(monorepoRoot, "packages", "core") : outputDir;
|
|
850
|
+
generateV0Context(name, outputDir, contextSourceDir);
|
|
851
|
+
log(" \u2713 .v0-context.md generated", c.green);
|
|
852
|
+
}
|
|
853
|
+
if (!skipInstall) {
|
|
854
|
+
installDependencies(outputDir);
|
|
855
|
+
}
|
|
856
|
+
if (generateRegions) {
|
|
857
|
+
if (skipInstall) {
|
|
858
|
+
log(
|
|
859
|
+
"\n\u26A0\uFE0F --generate-regions skipped: requires dependencies to be installed.",
|
|
860
|
+
c.yellow
|
|
861
|
+
);
|
|
862
|
+
log(' Run "yarn generate:regions" manually after running "yarn install".', c.yellow);
|
|
863
|
+
} else {
|
|
864
|
+
log("\n\u{1F5FA}\uFE0F Generating regions configuration from backend...", c.cyan);
|
|
865
|
+
try {
|
|
866
|
+
const { execSync: execSync3 } = await import("child_process");
|
|
867
|
+
execSync3("yarn generate:regions", { cwd: outputDir, stdio: "inherit" });
|
|
868
|
+
} catch {
|
|
869
|
+
log(
|
|
870
|
+
'\n\u26A0\uFE0F Region generation failed. You can retry with "yarn generate:regions".',
|
|
871
|
+
c.yellow
|
|
872
|
+
);
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
let remoteCreated = false;
|
|
877
|
+
let githubRepo;
|
|
878
|
+
if (!skipGit) {
|
|
879
|
+
log("\u{1F527} Initializing git repository...", c.cyan);
|
|
880
|
+
const gitOk = initGitRepo(outputDir);
|
|
881
|
+
if (gitOk) {
|
|
882
|
+
log(" \u2713 Git initialized with initial commit", c.green);
|
|
883
|
+
} else {
|
|
884
|
+
log(" \u26A0 Git init failed (git may not be installed)", c.yellow);
|
|
885
|
+
}
|
|
886
|
+
if (gitOk && !skipRemote) {
|
|
887
|
+
const wantRemote = await promptConfirm(
|
|
888
|
+
"Create a GitHub remote repository?",
|
|
889
|
+
true
|
|
890
|
+
);
|
|
891
|
+
if (wantRemote) {
|
|
892
|
+
remoteCreated = await createRemoteRepo(name, outputDir, monorepoRoot);
|
|
893
|
+
if (remoteCreated) {
|
|
894
|
+
const org = process.env.GITHUB_ORG;
|
|
895
|
+
githubRepo = org ? `${org}/${name}` : name;
|
|
896
|
+
}
|
|
897
|
+
} else {
|
|
898
|
+
log("\u23ED\uFE0F Skipping remote repo creation", c.gray);
|
|
899
|
+
}
|
|
900
|
+
} else if (skipRemote) {
|
|
901
|
+
log("\u23ED\uFE0F Skipping remote repo creation (--skip-remote)", c.gray);
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
let vercelLinked = false;
|
|
905
|
+
if (!skipVercel) {
|
|
906
|
+
const wantVercel = await promptConfirm(
|
|
907
|
+
"Link project to Vercel for automatic deployments?",
|
|
908
|
+
false
|
|
909
|
+
);
|
|
910
|
+
if (wantVercel) {
|
|
911
|
+
const result = await linkVercel(name, outputDir, githubRepo);
|
|
912
|
+
vercelLinked = result !== null;
|
|
913
|
+
if (vercelLinked && !skipGit) {
|
|
914
|
+
try {
|
|
915
|
+
const { execSync: execSync3 } = await import("child_process");
|
|
916
|
+
execSync3('git add -A && git commit -m "chore: add Vercel integration"', {
|
|
917
|
+
cwd: outputDir,
|
|
918
|
+
stdio: "pipe"
|
|
919
|
+
});
|
|
920
|
+
log(" \u2713 Vercel files committed", c.green);
|
|
921
|
+
} catch {
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
} else {
|
|
925
|
+
log("\u23ED\uFE0F Skipping Vercel integration", c.gray);
|
|
926
|
+
}
|
|
927
|
+
} else {
|
|
928
|
+
log("\u23ED\uFE0F Skipping Vercel integration (--skip-vercel)", c.gray);
|
|
929
|
+
}
|
|
930
|
+
const isInsideMonorepo = monorepoRoot && !relative2(monorepoRoot, outputDir).startsWith("..");
|
|
931
|
+
const relPath = isInsideMonorepo ? relative2(monorepoRoot, outputDir) : name;
|
|
932
|
+
let step = 1;
|
|
933
|
+
log("\n" + bold("\u2705 Project created successfully!"), c.green);
|
|
934
|
+
log("\n" + bold("Next steps:"), c.bold);
|
|
935
|
+
const backendConfigured = !!(commerceApiUrl && commerceApiKey);
|
|
936
|
+
if (backendConfigured) {
|
|
937
|
+
log(`
|
|
938
|
+
${step++}. Backend already configured (via CLI flags) \u2713`, c.green);
|
|
939
|
+
} else {
|
|
940
|
+
log(`
|
|
941
|
+
${step++}. Configure your backend:`, c.cyan);
|
|
942
|
+
log(` Edit ${relPath}/.env with your commerce backend URL and API key`, c.yellow);
|
|
943
|
+
}
|
|
944
|
+
if (!remoteCreated && !skipRemote) {
|
|
945
|
+
const vis = process.env.GITHUB_REPO_VISIBILITY === "public" ? "--public" : "--private";
|
|
946
|
+
const org = process.env.GITHUB_ORG;
|
|
947
|
+
const repoName = org ? `${org}/${name}` : name;
|
|
948
|
+
const githubHint = monorepoRoot ? `once GITHUB_TOKEN is in ${relative2(monorepoRoot, join8(monorepoRoot, ".env"))}` : "once GITHUB_TOKEN is set";
|
|
949
|
+
log(
|
|
950
|
+
`
|
|
951
|
+
${step++}. Push to GitHub (${githubHint}):`,
|
|
952
|
+
c.cyan
|
|
953
|
+
);
|
|
954
|
+
log(` cd ${relPath}`, c.yellow);
|
|
955
|
+
log(
|
|
956
|
+
` gh repo create ${repoName} ${vis} --source=. --push`,
|
|
957
|
+
c.yellow
|
|
958
|
+
);
|
|
959
|
+
}
|
|
960
|
+
if (vercelLinked && githubRepo) {
|
|
961
|
+
log(
|
|
962
|
+
`
|
|
963
|
+
${step++}. Add GitHub Actions secrets:`,
|
|
964
|
+
c.cyan
|
|
965
|
+
);
|
|
966
|
+
log(
|
|
967
|
+
` https://github.com/${githubRepo}/settings/secrets/actions`,
|
|
968
|
+
c.yellow
|
|
969
|
+
);
|
|
970
|
+
} else if (!vercelLinked && !skipVercel) {
|
|
971
|
+
log(`
|
|
972
|
+
${step++}. Link to Vercel (when ready):`, c.cyan);
|
|
973
|
+
log(` cd ${relPath} && vercel link`, c.yellow);
|
|
974
|
+
}
|
|
975
|
+
log(`
|
|
976
|
+
${step++}. Connect to V0:`, c.cyan);
|
|
977
|
+
log(
|
|
978
|
+
" Open https://v0.dev \u2192 New project \u2192 Connect GitHub repo",
|
|
979
|
+
c.yellow
|
|
980
|
+
);
|
|
981
|
+
log(` Then paste .v0-context.md as the first message`, c.yellow);
|
|
982
|
+
log(`
|
|
983
|
+
${step++}. Start developing:`, c.cyan);
|
|
984
|
+
log(` cd ${relPath} && yarn dev`, c.yellow);
|
|
985
|
+
log("");
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
// src/index.ts
|
|
989
|
+
function normalizeProjectName(raw) {
|
|
990
|
+
return raw.toLowerCase().replace(/[^a-z0-9-]/g, "-").replace(/-+/g, "-").replace(/^-|-$/g, "");
|
|
991
|
+
}
|
|
992
|
+
async function promptForName() {
|
|
993
|
+
const rl = createInterface2({
|
|
994
|
+
input: process.stdin,
|
|
995
|
+
output: process.stdout
|
|
996
|
+
});
|
|
997
|
+
try {
|
|
998
|
+
const answer = await rl.question(
|
|
999
|
+
`${c.cyan}? Project name (kebab-case):${c.reset} `
|
|
1000
|
+
);
|
|
1001
|
+
return answer.trim();
|
|
1002
|
+
} finally {
|
|
1003
|
+
rl.close();
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
var NODE_MIN_MAJOR = 18;
|
|
1007
|
+
var NODE_MIN_MINOR = 17;
|
|
1008
|
+
var [nodeMajor, nodeMinor] = process.versions.node.split(".").map(Number);
|
|
1009
|
+
if (nodeMajor < NODE_MIN_MAJOR || nodeMajor === NODE_MIN_MAJOR && nodeMinor < NODE_MIN_MINOR) {
|
|
1010
|
+
log(
|
|
1011
|
+
`
|
|
1012
|
+
\u274C Node.js >= ${NODE_MIN_MAJOR}.${NODE_MIN_MINOR}.0 required (current: ${process.versions.node})`,
|
|
1013
|
+
c.red
|
|
1014
|
+
);
|
|
1015
|
+
process.exit(1);
|
|
1016
|
+
}
|
|
1017
|
+
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("--skip-v0-context", "Skip .v0-context.md generation").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").option("--preset <preset>", "Visual preset for site.config.ts (e.g., electro, minimal)").option("--site-url <url>", "Public site URL for site.config.ts").option("--generate-regions", "Run region generation from backend after install (requires --commerce-api-url and --commerce-api-key)").action(async (positionalName, opts) => {
|
|
1018
|
+
let rawName = positionalName || opts.name || "";
|
|
1019
|
+
if (!rawName) {
|
|
1020
|
+
rawName = await promptForName();
|
|
1021
|
+
}
|
|
1022
|
+
const name = normalizeProjectName(rawName);
|
|
1023
|
+
if (!name) {
|
|
1024
|
+
log("\n\u274C Missing project name.", c.red);
|
|
1025
|
+
log("\nUsage:", c.yellow);
|
|
1026
|
+
log(" npx @thorprovider/create-thor-app my-store", c.cyan);
|
|
1027
|
+
log(" yarn create:app --name my-store", c.cyan);
|
|
1028
|
+
process.exit(1);
|
|
1029
|
+
}
|
|
1030
|
+
const monorepoRoot = findMonorepoRoot();
|
|
1031
|
+
const mode = monorepoRoot ? "monorepo" : "standalone";
|
|
1032
|
+
if (mode === "standalone") {
|
|
1033
|
+
}
|
|
1034
|
+
const isInsideMonorepo = monorepoRoot != null && !relative3(monorepoRoot, process.cwd()).startsWith("..");
|
|
1035
|
+
const outputDir = opts.output ? resolve(opts.output) : isInsideMonorepo ? resolve(monorepoRoot, "projects", name) : resolve(process.cwd(), name);
|
|
1036
|
+
await createApp({
|
|
1037
|
+
name,
|
|
1038
|
+
outputDir,
|
|
1039
|
+
mode,
|
|
1040
|
+
monorepoRoot,
|
|
1041
|
+
skipGit: !!opts.skipGit,
|
|
1042
|
+
skipInstall: !!opts.skipInstall,
|
|
1043
|
+
skipRemote: !!opts.skipRemote,
|
|
1044
|
+
skipVercel: !!opts.skipVercel,
|
|
1045
|
+
skipV0Context: !!opts.skipV0Context,
|
|
1046
|
+
commerceProvider: opts.commerceProvider,
|
|
1047
|
+
commerceApiUrl: opts.commerceApiUrl,
|
|
1048
|
+
commerceApiKey: opts.commerceApiKey,
|
|
1049
|
+
preset: opts.preset,
|
|
1050
|
+
siteUrl: opts.siteUrl,
|
|
1051
|
+
generateRegions: !!opts.generateRegions
|
|
1052
|
+
});
|
|
1053
|
+
});
|
|
1054
|
+
program.parse();
|