@usethrottle/create-store 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/CLAUDE.md +48 -0
  2. package/README.md +29 -2
  3. package/dist/index.js +445 -0
  4. package/dist/index.js.map +1 -0
  5. package/package.json +49 -3
  6. package/template/.env.local.tmpl +11 -0
  7. package/template/.gitignore.tmpl +7 -0
  8. package/template/AGENTS.md +25 -0
  9. package/template/README.md +79 -0
  10. package/template/app/account/page.tsx +7 -0
  11. package/template/app/api/throttle/revalidate/route.ts +5 -0
  12. package/template/app/api/throttle/sync/route.ts +9 -0
  13. package/template/app/c/[handle]/page.tsx +7 -0
  14. package/template/app/collections/[handle]/page.tsx +7 -0
  15. package/template/app/layout.tsx +16 -0
  16. package/template/app/not-found.tsx +5 -0
  17. package/template/app/p/[handle]/page.tsx +11 -0
  18. package/template/app/page.tsx +2 -0
  19. package/template/app/providers.tsx +22 -0
  20. package/template/app/search/page.tsx +11 -0
  21. package/template/app/shop/page.tsx +7 -0
  22. package/template/app/tags/[tag]/page.tsx +7 -0
  23. package/template/lib/store.ts +19 -0
  24. package/template/next.config.ts +9 -0
  25. package/template/package.json.tmpl +22 -0
  26. package/template/proxy.ts +14 -0
  27. package/template/tsconfig.json +20 -0
  28. package/template-builder-build/.env.local.tmpl.append +11 -0
  29. package/template-builder-build/.gitignore.tmpl.append +3 -0
  30. package/template-builder-build/AGENTS.md.append +16 -0
  31. package/template-builder-build/README.md.append +49 -0
  32. package/template-builder-build/app/layout.tsx +36 -0
  33. package/template-builder-build/app/pages/[handle]/page.tsx +26 -0
  34. package/template-builder-build/app/providers.tsx +31 -0
  35. package/template-builder-build/app/sitemap.ts +10 -0
  36. package/template-builder-build/lib/builder.ts +75 -0
  37. package/template-builder-build/lib/packs.ts +9 -0
  38. package/template-builder-build/next.config.ts +61 -0
package/CLAUDE.md ADDED
@@ -0,0 +1,48 @@
1
+ # @usethrottle/create-store
2
+
3
+ `npx @usethrottle/create-store [dir] --catalog-key pc_pk_...` scaffolds a Next.js store on
4
+ `@usethrottle/storefront-react`. Build: `pnpm --filter @usethrottle/create-store build`; test:
5
+ `pnpm --filter @usethrottle/create-store test` (the CLI test runs `dist/`, so build first; turbo does).
6
+
7
+ ## Layout
8
+
9
+ - `src/`: the CLI. `args.ts` (flags), `pm.ts` (package manager), `catalog-key.ts` (key format and the
10
+ `GET /v1/storefront/config` check), `prompt.ts` (the one question), `scaffold.ts` (copy the template),
11
+ `versions.ts` (`TEMPLATE_VERSIONS`), `index.ts` (the binary).
12
+ - `template/`: what a new store contains. `*.tmpl` files lose the suffix (npm drops `.gitignore` and
13
+ rewrites `package.json`) and have `{{name}}` and `{{catalogKey}}` filled in. `package.json.tmpl` gets
14
+ `dependencies` from `TEMPLATE_VERSIONS`.
15
+ - `e2e/smoke.spec.ts`: a Playwright run against a store this CLI scaffolded, built and started by hand on
16
+ port 3048. Recipe: `pnpm pack` ui, auth, storefront-react and create-store; scaffold with
17
+ `--skip-install` and both keys (`--catalog-key` browser, `--server-key` server); add `pnpm.overrides`
18
+ pointing at the tarballs; `pnpm install --ignore-workspace`; build; `next start -p 3048` with
19
+ `THROTTLE_WEBHOOK_SECRET` set. Stop the server by
20
+ port, delete the scratch store by its literal path.
21
+
22
+ - `template-builder-build/`: `--builder-mode build` overlays it on `template/`: a file replaces the template's file of the
23
+ same path, `*.append` is added to the end of it, anything else is new. A live store never reads it, and a live scaffold
24
+ is byte-identical to before (`src/builder-mode.test.ts` compares it with a golden hash taken from `origin/main`). Its
25
+ `next.config.ts` reads the release once into `.builder/release.json` (traced into the output, `lib/builder.ts` renders
26
+ from it: no Builder request or key at run time, and the layout never downloads the bundle), and serves a renamed
27
+ page's redirect itself. Its layout reads the catalog settings with a one-year cache policy so the release pages are not
28
+ rendered again every hour; do not shorten it.
29
+
30
+ ## Invariants an agent must not break
31
+
32
+ - **ISR cost rules.** Every new store copies the template, and the owner was billed about $1k for ISR
33
+ writes. The template must keep: the cache policy of `createStorefrontServerClient`
34
+ (`STOREFRONT_CACHE_POLICY`), `createRevalidateHandler` and `createGenerationSyncHandler` (one coarse
35
+ tag, coalesced, signature-checked), `staticParams: 0`, a dynamic uncached search, a `generationWatch`
36
+ of 60 s or more, and no `revalidate` under 3600, no `revalidatePath`, no `next: { revalidate }`.
37
+ `src/isr-cost.test.ts` greps a fresh scaffold for these; do not weaken it. The template README has the
38
+ short explanation for merchants.
39
+ - **Keys.** A catalog has a browser key (`NEXT_PUBLIC_THROTTLE_CATALOG_KEY`) and a server key
40
+ (`THROTTLE_CATALOG_SERVER_KEY`); `src/keys.ts` detects which is which with `classifyCatalogKey` and never
41
+ writes a server key to a `NEXT_PUBLIC_` variable. Never a secret (`pc_sk_`) key.
42
+ - **Published** since 0.1.0: `private` is gone and the package is in the root `release` filter.
43
+ - **`TEMPLATE_VERSIONS`** takes the workspace versions of storefront-react, ui and auth at build (the
44
+ `package.json` files are imported and bundled). Do not hard-code them.
45
+ - `template-typecheck.test.ts` compiles the template against the workspace packages; the template's
46
+ imports must resolve from this package's devDependencies.
47
+ - **Docs.** `apps/web/src/pages/docs/product-catalog/storefront-components/create-store` documents the prompts and the options; a
48
+ change to a flag, a prompt or a template file updates it in the same PR.
package/README.md CHANGED
@@ -1,3 +1,30 @@
1
- # Temporary Holding Version
1
+ # @usethrottle/create-store
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Scaffold a complete Next.js storefront on [`@usethrottle/storefront-react`](https://www.npmjs.com/package/@usethrottle/storefront-react).
4
+
5
+ ```bash
6
+ npx @usethrottle/create-store my-store
7
+ # or: pnpm create @usethrottle/store my-store
8
+ ```
9
+
10
+ It asks for one thing, your catalog's publishable key (`pc_pk_...`), writes it to `.env.local`, and installs
11
+ dependencies. Pass it as a flag to skip the question:
12
+
13
+ ```bash
14
+ npx @usethrottle/create-store my-store --catalog-key pc_pk_... --yes
15
+ ```
16
+
17
+ | Option | |
18
+ |---|---|
19
+ | `[dir]` | Where to create the store (default `my-store`). Must be empty or missing. |
20
+ | `--catalog-key <key>` | The catalog's publishable key. |
21
+ | `--yes`, `-y` | Ask nothing (needs `--catalog-key`). |
22
+ | `--pm <pnpm\|npm\|yarn\|bun>` | Package manager (default: the one running the command). |
23
+ | `--skip-install` | Write the files and stop. |
24
+ | `--server-key <key>` | The catalog's server key, for your store's server. The CLI tells browser and server keys apart and asks for the other one. |
25
+ | `--offline` | Do not call the catalog to tell the keys apart; then pass both `--catalog-key` and `--server-key`. |
26
+
27
+ The store has the shop, collections, categories, tags, product, search and account pages, the cart drawer,
28
+ and the cache-safe webhook and sync routes. Its `README.md` explains how it keeps Vercel ISR costs down.
29
+
30
+ Documentation: <https://usethrottle.dev/docs/product-catalog/storefront-components/create-store/>.
package/dist/index.js ADDED
@@ -0,0 +1,445 @@
1
+ #!/usr/bin/env node
2
+
3
+ // src/index.ts
4
+ import { spawn } from "child_process";
5
+ import { relative as relative2, resolve as resolve2 } from "path";
6
+
7
+ // src/pm.ts
8
+ var MANAGERS = ["pnpm", "npm", "yarn", "bun"];
9
+ function detectPackageManager(userAgent) {
10
+ const name = userAgent?.trim().split(/[\s/]/u)[0];
11
+ return MANAGERS.find((pm) => pm === name) ?? "npm";
12
+ }
13
+ function isPackageManager(value) {
14
+ return MANAGERS.includes(value);
15
+ }
16
+ function installCommand(pm) {
17
+ return { command: pm, args: ["install"] };
18
+ }
19
+ function runScript(pm, script) {
20
+ return pm === "npm" ? `npm run ${script}` : `${pm} ${script}`;
21
+ }
22
+
23
+ // src/args.ts
24
+ var BUILDER_MODES = ["live", "build"];
25
+ var DEFAULT_DIR = "my-store";
26
+ var USAGE = `Usage: create-store [dir] [options]
27
+
28
+ dir Where to create the store (default: ${DEFAULT_DIR})
29
+ --catalog-key <key> The catalog's browser key (pc_pk_...); asked for when missing. A server key is detected
30
+ --server-key <key> The catalog's server key, for reads made by your store's server (asked for after the browser key)
31
+ --yes, -y Take every default and ask nothing
32
+ --builder-mode <m> live (default) or build. build adds a Throttle Builder site that is generated at build time from a frozen release, with no Builder reads while the store runs
33
+ --pm <manager> pnpm, npm, yarn or bun (default: the one running this command)
34
+ --skip-install Write the files and stop before installing dependencies
35
+ --offline Do not call the catalog to tell the keys apart (then --catalog-key and --server-key are both needed)
36
+ --help, -h Show this text`;
37
+ function parseArgs(argv) {
38
+ const args = { dir: DEFAULT_DIR, builderMode: "live", yes: false, skipInstall: false, offline: false, help: false };
39
+ const positional = [];
40
+ const valueOf = (flag, inline, index) => {
41
+ if (inline !== void 0) return { value: inline, next: index };
42
+ const next = argv[index + 1];
43
+ if (next === void 0 || next.startsWith("-")) throw new Error(`${flag} needs a value.`);
44
+ return { value: next, next: index + 1 };
45
+ };
46
+ for (let i = 0; i < argv.length; i++) {
47
+ const raw = argv[i] ?? "";
48
+ if (raw === "--") {
49
+ positional.push(...argv.slice(i + 1));
50
+ break;
51
+ }
52
+ if (!raw.startsWith("-")) {
53
+ positional.push(raw);
54
+ continue;
55
+ }
56
+ const eq = raw.startsWith("--") ? raw.indexOf("=") : -1;
57
+ const flag = eq === -1 ? raw : raw.slice(0, eq);
58
+ const inline = eq === -1 ? void 0 : raw.slice(eq + 1);
59
+ switch (flag) {
60
+ case "--catalog-key": {
61
+ const { value, next } = valueOf(flag, inline, i);
62
+ args.catalogKey = value;
63
+ i = next;
64
+ break;
65
+ }
66
+ case "--server-key": {
67
+ const { value, next } = valueOf(flag, inline, i);
68
+ args.serverKey = value;
69
+ i = next;
70
+ break;
71
+ }
72
+ case "--builder-mode": {
73
+ const { value, next } = valueOf(flag, inline, i);
74
+ if (!BUILDER_MODES.includes(value)) throw new Error(`--builder-mode must be live or build, not "${value}".`);
75
+ args.builderMode = value;
76
+ i = next;
77
+ break;
78
+ }
79
+ case "--pm": {
80
+ const { value, next } = valueOf(flag, inline, i);
81
+ if (!isPackageManager(value)) throw new Error(`--pm must be pnpm, npm, yarn or bun, not "${value}".`);
82
+ args.pm = value;
83
+ i = next;
84
+ break;
85
+ }
86
+ case "--yes":
87
+ case "-y":
88
+ args.yes = true;
89
+ break;
90
+ case "--skip-install":
91
+ args.skipInstall = true;
92
+ break;
93
+ case "--offline":
94
+ args.offline = true;
95
+ break;
96
+ case "--help":
97
+ case "-h":
98
+ args.help = true;
99
+ break;
100
+ default:
101
+ throw new Error(`Unknown option ${flag}.`);
102
+ }
103
+ }
104
+ if (positional.length > 1) throw new Error(`Give one directory, not ${positional.length} (${positional.join(", ")}).`);
105
+ if (positional[0]) args.dir = positional[0];
106
+ return args;
107
+ }
108
+
109
+ // src/catalog-key.ts
110
+ var DEFAULT_CATALOG_URL = "https://catalog.usethrottle.dev";
111
+ function validateCatalogKey(key) {
112
+ return /^pc_pk_[\x21-\x7e]{1,94}$/u.test(key);
113
+ }
114
+ function catalogKeyProblem(key) {
115
+ if (validateCatalogKey(key)) return void 0;
116
+ if (key.startsWith("pc_sk_")) return "That is a secret key (pc_sk_). A storefront takes the publishable key (pc_pk_).";
117
+ return "A catalog publishable key starts with pc_pk_ and has no spaces.";
118
+ }
119
+ var PROBE_ORIGIN = "https://create-store.invalid";
120
+ async function classifyCatalogKey(key, options = {}) {
121
+ const base = (options.baseUrl ?? DEFAULT_CATALOG_URL).replace(/\/+$/u, "");
122
+ const doFetch = options.fetch ?? fetch;
123
+ const ask = async (origin) => {
124
+ try {
125
+ const response = await doFetch(`${base}/v1/storefront/config`, {
126
+ headers: { "x-catalog-key": key, ...origin ? { origin } : {} },
127
+ signal: AbortSignal.timeout(options.timeoutMs ?? 8e3)
128
+ });
129
+ const text = response.ok ? "" : await response.text().catch(() => "");
130
+ const code = /"code"\s*:\s*"([A-Z_]+)"/u.exec(text)?.[1] ?? "";
131
+ return { status: response.status, code };
132
+ } catch {
133
+ return void 0;
134
+ }
135
+ };
136
+ const [server, browser] = [await ask(void 0), await ask(PROBE_ORIGIN)];
137
+ if (server?.status === 401 || browser?.status === 401) {
138
+ return { kind: "rejected", detail: "The catalog does not know this key (401)." };
139
+ }
140
+ if (server?.status === 200 || browser?.code === "SERVER_KEY_IN_BROWSER") {
141
+ return { kind: "server", detail: "This is a server key." };
142
+ }
143
+ if (server?.code === "ORIGIN_NOT_ALLOWED" || browser?.status === 200 || browser?.code === "ORIGIN_NOT_ALLOWED") {
144
+ return { kind: "browser", detail: "This is a browser key." };
145
+ }
146
+ return { kind: "unknown", detail: server || browser ? `The catalog answered ${(server ?? browser).status}.` : "The catalog did not answer." };
147
+ }
148
+
149
+ // src/keys.ts
150
+ var NEEDS_BOTH = "Could not tell whether the key is a browser key or a server key, and a server key must never be written as the browser key. Pass both: --catalog-key <browser key> --server-key <server key>.";
151
+ var SERVER_NEEDS_BROWSER = "That is a server key, and a store also needs a browser key for the open tab. Pass it with --catalog-key and the server key with --server-key.";
152
+ var ON_YOUR_WORD = "The catalog did not check which key is which, so the roles are taken on your word: --catalog-key as the browser key and --server-key as the server key. If they are the wrong way round, the server key is published to every visitor in NEXT_PUBLIC_THROTTLE_CATALOG_KEY. Check .env.local.";
153
+ function assertBrowserKeyPlacement(p) {
154
+ if (p.browserKind === "browser") return;
155
+ if (p.browserKind !== "server" && p.named && p.hasServerKey) return;
156
+ throw new Error(NEEDS_BOTH);
157
+ }
158
+ async function resolveKeys(o, deps) {
159
+ const notes = [];
160
+ const classify = async (key) => {
161
+ const problem = catalogKeyProblem(key);
162
+ if (problem) throw new Error(problem);
163
+ if (o.offline) return "unchecked";
164
+ const result = await deps.classify(key);
165
+ if (result.kind === "rejected") throw new Error(`${result.detail} Check the key, or pass --offline to skip this check.`);
166
+ if (result.kind === "unknown") notes.push(`Could not tell what kind of key this is (${result.detail}).`);
167
+ return result.kind;
168
+ };
169
+ const askFor = async (role) => (await deps.ask(role))?.trim() || void 0;
170
+ let browser = o.catalogKey?.trim() || void 0;
171
+ let server = o.serverKey?.trim() || void 0;
172
+ let named = false;
173
+ let browserKind;
174
+ let serverChecked = false;
175
+ if (!browser && !server) {
176
+ if (o.yes) throw new Error("--yes needs --catalog-key <pc_pk_...>.");
177
+ browser = await askFor("browser");
178
+ if (!browser) throw new Error("Cancelled.");
179
+ named = true;
180
+ }
181
+ if (browser) {
182
+ browserKind = await classify(browser);
183
+ if (browserKind === "server") {
184
+ if (server) {
185
+ const other = await classify(server);
186
+ if (other === "server") throw new Error("--catalog-key and --server-key are both server keys. One of them has to be the browser key.");
187
+ if (other !== "browser") {
188
+ throw new Error(
189
+ `The catalog says --catalog-key is a server key, but could not confirm that --server-key is a browser key (${other === "unchecked" ? "not checked" : "no clear answer"}). The flags are not swapped on a guess: pass them the right way round, or check the keys in the catalog.`
190
+ );
191
+ }
192
+ [browser, server] = [server, browser];
193
+ browserKind = "browser";
194
+ } else {
195
+ server = browser;
196
+ browser = void 0;
197
+ browserKind = void 0;
198
+ }
199
+ serverChecked = true;
200
+ } else if (server) {
201
+ named = true;
202
+ }
203
+ }
204
+ if (server && !serverChecked) {
205
+ if (await classify(server) === "browser") throw new Error("The key given as the server key is a browser key. A server key is made in the catalog under server keys.");
206
+ }
207
+ if (!browser) {
208
+ if (o.yes) throw new Error(SERVER_NEEDS_BROWSER);
209
+ browser = await askFor("browser");
210
+ if (!browser) throw new Error("A browser key is needed. Cancelled.");
211
+ named = true;
212
+ browserKind = await classify(browser);
213
+ if (browserKind === "server") throw new Error("That is a server key too. The browser key is the one the catalog allows your website to use.");
214
+ }
215
+ if (!server) {
216
+ if (browserKind !== "browser") {
217
+ if (o.yes) throw new Error(NEEDS_BOTH);
218
+ if (!named) {
219
+ notes.push("Could not tell what kind of key --catalog-key is, so both keys are asked for.");
220
+ browser = await askFor("browser");
221
+ if (!browser) throw new Error("A browser key is needed. Cancelled.");
222
+ named = true;
223
+ browserKind = await classify(browser);
224
+ if (browserKind === "server") throw new Error("That is a server key too. The browser key is the one the catalog allows your website to use.");
225
+ }
226
+ server = await askFor("server");
227
+ if (!server) throw new Error("A server key is needed when the keys cannot be told apart. Cancelled.");
228
+ if (await classify(server) === "browser") throw new Error("That is a browser key, not a server key.");
229
+ } else if (!o.yes) {
230
+ const given = await askFor("server");
231
+ if (given) {
232
+ if (await classify(given) === "browser") throw new Error("That is a browser key, not a server key.");
233
+ server = given;
234
+ }
235
+ }
236
+ }
237
+ assertBrowserKeyPlacement({ browserKind, named, hasServerKey: server !== void 0 });
238
+ if (browserKind !== "browser") notes.push(ON_YOUR_WORD);
239
+ if (!server) {
240
+ notes.push(
241
+ "No server key. The browser key is only answered from an allowed website, so your store's server cannot read with it: add the catalog's server key as THROTTLE_CATALOG_SERVER_KEY in .env.local (see README.md)."
242
+ );
243
+ }
244
+ return { browserKey: browser, ...server ? { serverKey: server } : {}, notes };
245
+ }
246
+
247
+ // src/prompt.ts
248
+ import prompts from "prompts";
249
+ var MESSAGES = {
250
+ browser: "Catalog publishable key (pc_pk_...)",
251
+ server: "Catalog server key, for your store's server (pc_pk_..., press enter to skip)"
252
+ };
253
+ async function askCatalogKey(role) {
254
+ const answer = await prompts(
255
+ {
256
+ type: "text",
257
+ name: "catalogKey",
258
+ message: MESSAGES[role],
259
+ validate: (value) => role === "server" && value.trim() === "" ? true : catalogKeyProblem(value.trim()) ?? true
260
+ },
261
+ { onCancel: () => false }
262
+ );
263
+ const key = answer.catalogKey;
264
+ return typeof key === "string" ? key.trim() : void 0;
265
+ }
266
+
267
+ // src/scaffold.ts
268
+ import { mkdir, readdir, readFile, stat, writeFile } from "fs/promises";
269
+ import { basename, dirname, join, relative, resolve } from "path";
270
+ import { fileURLToPath } from "url";
271
+ var TEMPLATE_DIR = resolve(dirname(fileURLToPath(import.meta.url)), "../template");
272
+ var BUILD_TEMPLATE_DIR = resolve(dirname(fileURLToPath(import.meta.url)), "../template-builder-build");
273
+ async function filesUnder(root, dir = root) {
274
+ const found = [];
275
+ for (const entry of await readdir(dir, { withFileTypes: true })) {
276
+ const path = join(dir, entry.name);
277
+ if (entry.isDirectory()) found.push(...await filesUnder(root, path));
278
+ else found.push(relative(root, path));
279
+ }
280
+ return found.sort();
281
+ }
282
+ function packageNameOf(dir) {
283
+ const cleaned = basename(resolve(dir)).toLowerCase().replace(/[^a-z0-9._-]+/gu, "-").replace(/^[._-]+|[-]+$/gu, "");
284
+ return cleaned || "throttle-store";
285
+ }
286
+ function render(text, values) {
287
+ return text.replace(/\{\{(\w+)\}\}/gu, (_all, name) => {
288
+ const value = values[name];
289
+ if (value === void 0) throw new Error(`The template uses {{${name}}}, which scaffold does not set.`);
290
+ return value;
291
+ });
292
+ }
293
+ async function scaffold(o) {
294
+ const problem = catalogKeyProblem(o.catalogKey);
295
+ if (problem) throw new Error(problem);
296
+ const serverProblem = o.serverKey === void 0 ? void 0 : catalogKeyProblem(o.serverKey);
297
+ if (serverProblem) throw new Error(serverProblem);
298
+ const target = resolve(o.dir);
299
+ const existing = await stat(target).then(
300
+ () => readdir(target),
301
+ () => null
302
+ );
303
+ if (existing && existing.length > 0) {
304
+ throw new Error(`${target} is not empty. Pick a new directory, or empty this one first.`);
305
+ }
306
+ const values = {
307
+ name: packageNameOf(target),
308
+ catalogKey: o.catalogKey,
309
+ serverKeyLine: o.serverKey ? `THROTTLE_CATALOG_SERVER_KEY=${o.serverKey}` : "# THROTTLE_CATALOG_SERVER_KEY= (the catalog's server key; see README.md)"
310
+ };
311
+ const files = /* @__PURE__ */ new Map();
312
+ const read = async (root, overlay) => {
313
+ for (const file of await filesUnder(root)) {
314
+ const append = overlay && file.endsWith(".append");
315
+ const name = append ? file.slice(0, -".append".length) : file;
316
+ const isTemplate = name.endsWith(".tmpl");
317
+ const out = isTemplate ? name.slice(0, -".tmpl".length) : name;
318
+ let text = await readFile(join(root, file), "utf8");
319
+ if (isTemplate) text = render(text, values);
320
+ files.set(out, append ? (files.get(out) ?? "") + text : text);
321
+ }
322
+ };
323
+ await read(TEMPLATE_DIR, false);
324
+ if (o.builderMode === "build") await read(BUILD_TEMPLATE_DIR, true);
325
+ const written = [];
326
+ for (const [file, content] of files) {
327
+ const out = join(target, file);
328
+ let text = content;
329
+ if (basename(out) === "package.json") {
330
+ const manifest = JSON.parse(text);
331
+ const dependencies = { ...manifest.dependencies, ...o.versions };
332
+ manifest.dependencies = Object.fromEntries(Object.entries(dependencies).sort(([a], [b]) => a.localeCompare(b)));
333
+ text = `${JSON.stringify(manifest, null, 2)}
334
+ `;
335
+ }
336
+ await mkdir(dirname(out), { recursive: true });
337
+ await writeFile(out, text, "utf8");
338
+ written.push(out);
339
+ }
340
+ return written;
341
+ }
342
+
343
+ // ../auth-sdk/package.json
344
+ var version = "0.15.0";
345
+
346
+ // ../builder/package.json
347
+ var version2 = "0.2.0";
348
+
349
+ // ../builder-react/package.json
350
+ var version3 = "0.2.0";
351
+
352
+ // ../builder-schema/package.json
353
+ var version4 = "0.1.2";
354
+
355
+ // ../storefront-react/package.json
356
+ var version5 = "0.2.2";
357
+
358
+ // ../ui/package.json
359
+ var version6 = "0.1.2";
360
+
361
+ // src/versions.ts
362
+ var TEMPLATE_VERSIONS = {
363
+ "@usethrottle/storefront-react": `^${version5}`,
364
+ "@usethrottle/ui": `^${version6}`,
365
+ "@usethrottle/auth": `^${version}`,
366
+ "@usethrottle/catalog": "^1.4.1",
367
+ "@usethrottle/money": "^1.1.0"
368
+ };
369
+ var BUILD_MODE_VERSIONS = {
370
+ "@usethrottle/builder": `^${version2}`,
371
+ "@usethrottle/builder-react": `^${version3}`,
372
+ "@usethrottle/builder-schema": `^${version4}`
373
+ };
374
+
375
+ // src/index.ts
376
+ var fail = (message) => {
377
+ console.error(`create-store: ${message}`);
378
+ process.exit(1);
379
+ };
380
+ function install(pm, cwd) {
381
+ const { command, args } = installCommand(pm);
382
+ return new Promise((done) => {
383
+ const child = spawn(command, args, { cwd, stdio: "inherit", shell: process.platform === "win32" });
384
+ child.on("error", () => done(false));
385
+ child.on("exit", (code) => done(code === 0));
386
+ });
387
+ }
388
+ async function main() {
389
+ let args;
390
+ try {
391
+ args = parseArgs(process.argv.slice(2));
392
+ } catch (error) {
393
+ console.error(`create-store: ${error instanceof Error ? error.message : "bad arguments"}
394
+
395
+ ${USAGE}`);
396
+ process.exit(1);
397
+ }
398
+ if (args.help) {
399
+ console.log(USAGE);
400
+ return;
401
+ }
402
+ let keys;
403
+ try {
404
+ keys = await resolveKeys(args, {
405
+ classify: (key) => classifyCatalogKey(key),
406
+ ask: askCatalogKey
407
+ });
408
+ } catch (error) {
409
+ return fail(error instanceof Error ? error.message : "Could not settle the catalog keys.");
410
+ }
411
+ for (const note of keys.notes) console.warn(`create-store: ${note}`);
412
+ let written;
413
+ try {
414
+ written = await scaffold({
415
+ dir: args.dir,
416
+ catalogKey: keys.browserKey,
417
+ ...keys.serverKey ? { serverKey: keys.serverKey } : {},
418
+ builderMode: args.builderMode,
419
+ versions: args.builderMode === "build" ? { ...TEMPLATE_VERSIONS, ...BUILD_MODE_VERSIONS } : TEMPLATE_VERSIONS
420
+ });
421
+ } catch (error) {
422
+ return fail(error instanceof Error ? error.message : "Could not write the store.");
423
+ }
424
+ const target = resolve2(args.dir);
425
+ console.log(`Created ${written.length} files in ${target}`);
426
+ const pm = args.pm ?? detectPackageManager(process.env.npm_config_user_agent);
427
+ if (!args.skipInstall) {
428
+ console.log(`Installing with ${pm}...`);
429
+ if (!await install(pm, target)) console.warn(`create-store: ${pm} install did not finish. Run it yourself in the folder.`);
430
+ }
431
+ const rel = relative2(process.cwd(), target) || ".";
432
+ const cd = rel.startsWith("..") ? target : rel;
433
+ console.log(`
434
+ Next:
435
+ cd ${cd}
436
+ ${args.skipInstall ? ` ${pm} install
437
+ ` : ""} ${runScript(pm, "dev")}
438
+
439
+ Read README.md for how the store stays cheap on Vercel before you deploy.`);
440
+ }
441
+ main().catch((error) => {
442
+ console.error(`create-store: ${error instanceof Error ? error.message : String(error)}`);
443
+ process.exit(1);
444
+ });
445
+ //# sourceMappingURL=index.js.map