@canonical/summon-application 0.34.0 → 0.36.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 (88) hide show
  1. package/README.md +9 -9
  2. package/dist/esm/application/react/index.js +403 -277
  3. package/dist/esm/application/react/index.js.map +1 -1
  4. package/dist/esm/application/react/templates/.storybook/decorators/index.ts.ejs +4 -0
  5. package/dist/esm/application/react/templates/.storybook/decorators/withI18n.tsx +35 -0
  6. package/dist/esm/application/react/templates/.storybook/decorators/withRouter.tsx +18 -20
  7. package/dist/esm/application/react/templates/.storybook/preview.ts.ejs +35 -0
  8. package/dist/esm/application/react/templates/index.html.ejs +2 -1
  9. package/dist/esm/application/react/templates/package.json.ejs +34 -3
  10. package/dist/esm/application/react/templates/src/client/entry.tsx.ejs +109 -10
  11. package/dist/esm/application/react/templates/src/domains/account/{AccountPage.tsx → AccountPage.tsx.ejs} +13 -0
  12. package/dist/esm/application/react/templates/src/domains/account/LoginPage.tsx.ejs +51 -0
  13. package/dist/esm/application/react/templates/src/domains/account/routes.ts +21 -8
  14. package/dist/esm/application/react/templates/src/domains/catalog/CatalogPage.tsx.ejs +97 -0
  15. package/dist/esm/application/react/templates/src/domains/catalog/{ErrorBoundary.tests.tsx → ErrorBoundary.tests.tsx.ejs} +24 -0
  16. package/dist/esm/application/react/templates/src/domains/catalog/{ProductCard.tsx → ProductCard.tsx.ejs} +31 -0
  17. package/dist/esm/application/react/templates/src/domains/catalog/{ProductList.stories.tsx → ProductList.stories.tsx.ejs} +63 -4
  18. package/dist/esm/application/react/templates/src/domains/catalog/{ProductList.tests.tsx → ProductList.tests.tsx.ejs} +44 -0
  19. package/dist/esm/application/react/templates/src/domains/catalog/{ProductList.tsx → ProductList.tsx.ejs} +30 -2
  20. package/dist/esm/application/react/templates/src/domains/catalog/routes.ts +25 -0
  21. package/dist/esm/application/react/templates/src/domains/contact/{ContactPage.tsx → ContactPage.tsx.ejs} +40 -0
  22. package/dist/esm/application/react/templates/src/domains/marketing/GuidePage.tsx.ejs +33 -0
  23. package/dist/esm/application/react/templates/src/domains/marketing/{HomePage.tsx → HomePage.tsx.ejs} +30 -0
  24. package/dist/esm/application/react/templates/src/domains/marketing/routes.ts +25 -0
  25. package/dist/esm/application/react/templates/src/i18n/ar.ts +74 -0
  26. package/dist/esm/application/react/templates/src/i18n/catalogs.ts +14 -0
  27. package/dist/esm/application/react/templates/src/i18n/config.ts +19 -0
  28. package/dist/esm/application/react/templates/src/i18n/en.ts +73 -0
  29. package/dist/esm/application/react/templates/src/i18n/fr.ts +70 -0
  30. package/dist/esm/application/react/templates/src/i18n/index.ts +5 -0
  31. package/dist/esm/application/react/templates/src/i18n/negotiation.tests.ts +92 -0
  32. package/dist/esm/application/react/templates/src/lib/LocaleSelector/LocaleSelector.tests.tsx +60 -0
  33. package/dist/esm/application/react/templates/src/lib/LocaleSelector/LocaleSelector.tsx +44 -0
  34. package/dist/esm/application/react/templates/src/lib/LocaleSelector/index.ts +1 -0
  35. package/dist/esm/application/react/templates/src/lib/Navigation/Navigation.tsx.ejs +34 -0
  36. package/dist/esm/application/react/templates/src/lib/ThemeSelector/{ThemeSelector.tsx → ThemeSelector.tsx.ejs} +16 -0
  37. package/dist/esm/application/react/templates/src/lib/index.ts.ejs +3 -0
  38. package/dist/esm/application/react/templates/src/relay/environment.ts +153 -5
  39. package/dist/esm/application/react/templates/src/routes.tsx.ejs +137 -33
  40. package/dist/esm/application/react/templates/src/server/entry.tsx.ejs +293 -11
  41. package/dist/esm/application/react/templates/src/server/preview.bun.ts +30 -7
  42. package/dist/esm/application/react/templates/src/server/preview.express.ts +19 -4
  43. package/dist/esm/application/react/templates/src/server/renderer.tsx.ejs +125 -0
  44. package/dist/esm/application/react/templates/src/server/{server.bun.ts → server.bun.ts.ejs} +56 -4
  45. package/dist/esm/application/react/templates/src/server/{server.express.ts → server.express.ts.ejs} +48 -4
  46. package/dist/esm/application/react/templates/test/e2e/servers.e2e.ts.ejs +197 -0
  47. package/dist/esm/application/react/templates/tsconfig.json.ejs +3 -0
  48. package/dist/esm/application/react/templates/vite.config.ts.ejs +5 -0
  49. package/dist/esm/domain/index.js +8 -1
  50. package/dist/esm/domain/index.js.map +1 -1
  51. package/dist/esm/route/index.js +12 -2
  52. package/dist/esm/route/index.js.map +1 -1
  53. package/dist/esm/route/insertRoute.js +39 -11
  54. package/dist/esm/route/insertRoute.js.map +1 -1
  55. package/dist/esm/shared/packageName.js +7 -0
  56. package/dist/esm/shared/packageName.js.map +1 -0
  57. package/dist/esm/shared/packageVersion.js +50 -0
  58. package/dist/esm/shared/packageVersion.js.map +1 -0
  59. package/dist/esm/shared/validators.js +61 -0
  60. package/dist/esm/shared/validators.js.map +1 -0
  61. package/dist/esm/shared/versions.js +87 -55
  62. package/dist/esm/shared/versions.js.map +1 -1
  63. package/dist/esm/shared/workspace.js +57 -0
  64. package/dist/esm/shared/workspace.js.map +1 -1
  65. package/dist/esm/wrapper/index.js +8 -1
  66. package/dist/esm/wrapper/index.js.map +1 -1
  67. package/dist/types/application/react/index.d.ts +1 -2
  68. package/dist/types/application/react/index.d.ts.map +1 -1
  69. package/dist/types/domain/index.d.ts.map +1 -1
  70. package/dist/types/route/index.d.ts.map +1 -1
  71. package/dist/types/shared/packageName.d.ts +7 -0
  72. package/dist/types/shared/packageName.d.ts.map +1 -0
  73. package/dist/types/shared/packageVersion.d.ts +15 -0
  74. package/dist/types/shared/packageVersion.d.ts.map +1 -0
  75. package/dist/types/shared/validators.d.ts +21 -0
  76. package/dist/types/shared/validators.d.ts.map +1 -0
  77. package/dist/types/shared/versions.d.ts +14 -5
  78. package/dist/types/shared/versions.d.ts.map +1 -1
  79. package/dist/types/shared/workspace.d.ts.map +1 -1
  80. package/dist/types/wrapper/index.d.ts.map +1 -1
  81. package/package.json +8 -8
  82. package/dist/esm/application/react/templates/.storybook/decorators/index.ts +0 -1
  83. package/dist/esm/application/react/templates/.storybook/preview.ts +0 -10
  84. package/dist/esm/application/react/templates/src/domains/account/LoginPage.tsx +0 -27
  85. package/dist/esm/application/react/templates/src/domains/catalog/CatalogPage.tsx +0 -50
  86. package/dist/esm/application/react/templates/src/domains/marketing/GuidePage.tsx +0 -17
  87. package/dist/esm/application/react/templates/src/server/renderer.tsx +0 -50
  88. package/dist/esm/application/react/templates/test/e2e/servers.e2e.ts +0 -99
@@ -1,8 +1,10 @@
1
1
  import * as path from "node:path";
2
2
  import { fileURLToPath } from "node:url";
3
- import { template, withHelpers } from "@canonical/summon-core";
4
- import { copyFile, exec, exists, flatMap, info, pure, sequence_, warn, when, } from "@canonical/task";
3
+ import { loadTemplateSync, rawFile, template, withHelpers, } from "@canonical/summon-core";
4
+ import { exec, exists, fail, flatMap, info, pure, sequence_, warn, when, } from "@canonical/task";
5
5
  import { pickPackageManager } from "../../shared/packageManager.js";
6
+ import { packageVersion } from "../../shared/packageVersion.js";
7
+ import { validateAppPath } from "../../shared/validators.js";
6
8
  import { resolvePragmaVersion } from "../../shared/versions.js";
7
9
  import { findEnclosingWorkspaceRoot } from "../../shared/workspace.js";
8
10
  const prompts = [
@@ -12,27 +14,28 @@ const prompts = [
12
14
  message: "Application directory name:",
13
15
  default: "my-app",
14
16
  positional: true,
17
+ validate: validateAppPath,
15
18
  group: "Application",
16
19
  },
20
+ // SSR and the router are NOT prompts: they are always on (the scaffold has no
21
+ // SPA arm — every template assumes both), so a question that only ever
22
+ // accepted its default was two dead wizard steps and, worse, an unanswerable
23
+ // refusal: a default-`true` confirm can be made explicit ONLY by negating it
24
+ // (`--no-ssr`/`--no-router`), which the old cross-answer guard then rejected —
25
+ // so `create application react` had no reachable all-flags completion. The
26
+ // help text states the facts; the flags simply do not exist.
17
27
  {
18
- name: "ssr",
19
- type: "confirm",
20
- message: "Include SSR?",
21
- default: true,
22
- group: "Application",
23
- },
24
- {
25
- name: "router",
28
+ name: "forms",
26
29
  type: "confirm",
27
- message: "Include router?",
30
+ message: "Include form components?",
28
31
  default: true,
29
32
  group: "Application",
30
33
  },
31
34
  {
32
- name: "forms",
35
+ name: "intl",
33
36
  type: "confirm",
34
- message: "Include form components?",
35
- default: true,
37
+ message: "Include internationalisation (locale negotiation, translated UI, locale switcher)?",
38
+ default: false,
36
39
  group: "Application",
37
40
  },
38
41
  {
@@ -54,39 +57,48 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
54
57
  const templatesDir = path.join(__dirname, "templates");
55
58
  /** Resolve a path inside the templates directory. */
56
59
  const src = (templatePath) => path.join(templatesDir, templatePath);
60
+ /**
61
+ * Carried templates, read on first `generate()` and cached by path — never at
62
+ * module eval, so importing the generator costs no filesystem work.
63
+ */
64
+ const templateCache = new Map();
65
+ function load(templatePath) {
66
+ let loaded = templateCache.get(templatePath);
67
+ if (!loaded) {
68
+ loaded = loadTemplateSync(src(templatePath));
69
+ templateCache.set(templatePath, loaded);
70
+ }
71
+ return loaded;
72
+ }
57
73
  export const generator = {
58
74
  meta: {
59
75
  name: "application/react",
60
76
  displayName: "@canonical/summon-application:application/react",
61
77
  description: "Scaffold a complete React application with SSR and routing",
62
- version: "0.1.0",
78
+ version: packageVersion(),
63
79
  help: `Creates a full React application with:
64
80
  - Vite build + dev server
65
81
  - Server-side rendering (Express + Bun dev servers)
66
82
  - Routing with @canonical/router-core
67
83
  - Head management with @canonical/react-head
68
84
  - Two domains (marketing + account) with pages
69
- - Contact domain with form components (when --forms is enabled)
85
+ - Contact domain with form components (on by default; omit with --no-forms)
70
86
  - Relay (GraphQL) data layer with a local mock schema, catalog example
71
87
  domain, and Storybook mocking (when --relay is enabled)
72
88
  - Navigation, ThemeSelector, ExampleComponent
73
89
  - Storybook with router decorator
74
90
  - Biome + TypeScript configuration
75
91
 
76
- Requires both --ssr and --router flags.`,
92
+ SSR and routing are always included; standalone SPA mode is not supported.`,
77
93
  examples: [
78
94
  "summon application/react my-app",
79
- "summon application/react --forms my-app",
95
+ "summon application/react --no-forms my-app",
80
96
  "summon application/react --relay my-app",
81
- "summon application/react --ssr --router --forms --relay my-app",
97
+ "summon application/react --no-forms --relay my-app",
82
98
  ],
83
99
  },
84
100
  prompts,
85
101
  generate: (answers) => {
86
- if (!answers.ssr || !answers.router) {
87
- throw new Error("The application/react generator requires both --ssr and --router. " +
88
- "Standalone SPA mode is not supported.");
89
- }
90
102
  // The app path is a directory path, not a route path — keep it as given
91
103
  // (absolute or relative), only trimming surrounding whitespace and any
92
104
  // trailing slash.
@@ -101,11 +113,18 @@ Requires both --ssr and --router flags.`,
101
113
  .replace(/[^a-z0-9._-]+/g, "-")
102
114
  .replace(/^[._-]+|[._-]+$/g, "");
103
115
  if (!name) {
104
- throw new Error(`Could not derive a valid application name from path "${appPath}". ` +
105
- "Pass an explicit directory name (lowercase letters/digits).");
116
+ return fail({
117
+ code: "APP_NAME_INVALID",
118
+ message: `Could not derive a valid application name from path "${appPath}". ` +
119
+ "Pass an explicit directory name (lowercase letters/digits).",
120
+ });
106
121
  }
107
122
  const dest = (...segments) => path.join(appPath, ...segments);
108
- const copy = (filePath) => copyFile(src(filePath), dest(filePath));
123
+ const copy = (filePath) => rawFile({
124
+ source: src(filePath),
125
+ content: load(filePath).content,
126
+ dest: dest(filePath),
127
+ });
109
128
  // Whether the app lands inside a bun workspace decides who owns dependency
110
129
  // patching: bun resolves `patchedDependencies` paths from the WORKSPACE
111
130
  // ROOT, so an app-local block inside a workspace aborts `bun install`
@@ -114,258 +133,365 @@ Requires both --ssr and --router flags.`,
114
133
  // standalone apps get their own patches/ + patchedDependencies.
115
134
  const workspaceRoot = findEnclosingWorkspaceRoot(path.resolve(appPath));
116
135
  const standalone = workspaceRoot === null;
117
- // Resolve the @canonical/* version range first (latest from npm, with an
118
- // offline fallback), then build the rest of the pipeline with it baked into
119
- // the template vars.
120
- return flatMap(resolvePragmaVersion(), (pragmaVersion) => {
121
- const vars = withHelpers({
122
- name,
123
- forms: answers.forms,
124
- relay: answers.relay,
125
- standalone,
126
- pragmaVersion,
136
+ // Guard BEFORE any network: refusing an existing destination is locally
137
+ // decidable, so it must not wait on (or block behind) the npm lookup.
138
+ // Every write's default undo is a delete, so overwrite-then-`--undo`
139
+ // would destroy files the user owned before the run (the same guard
140
+ // domain/wrapper apply).
141
+ return flatMap(exists(appPath), (present) => {
142
+ if (present) {
143
+ return fail({
144
+ code: "APP_DEST_EXISTS",
145
+ message: `"${appPath}" already exists. Scaffolding over it would let ` +
146
+ "--undo delete pre-existing files. Choose a different " +
147
+ "directory or remove it first.",
148
+ });
149
+ }
150
+ // Resolve the @canonical/* version range (latest from npm, with an
151
+ // offline fallback), then build the rest of the pipeline with it baked
152
+ // into the template vars.
153
+ return flatMap(resolvePragmaVersion(), (pragmaVersion) => {
154
+ const vars = withHelpers({
155
+ name,
156
+ forms: answers.forms,
157
+ intl: answers.intl,
158
+ relay: answers.relay,
159
+ standalone,
160
+ pragmaVersion,
161
+ });
162
+ // Detect an available package manager for both the (optional) install
163
+ // step and the closing message, so the suggested commands reflect what's
164
+ // actually on the machine.
165
+ const pm = pickPackageManager();
166
+ // Build the install task only when a package manager was actually found —
167
+ // this narrows `pm` to non-null, so the install command never invents a
168
+ // manager. `null` when we won't (or can't) install.
169
+ const installTask = answers.runInstall && pm !== null
170
+ ? sequence_([
171
+ info(`Installing dependencies with ${pm}...`),
172
+ exec(pm, ["install"], appPath),
173
+ ])
174
+ : null;
175
+ // The closing command only names a package manager we actually found; if
176
+ // none was detected we don't invent one (previously this suggested `bun`
177
+ // even on a machine without bun), and instead point the user at the
178
+ // package-manager step.
179
+ const finalMessage = installTask
180
+ ? `Application "${appPath}" created. Run \`cd ${appPath} && ${pm} run dev\` to start.`
181
+ : pm
182
+ ? `Application "${appPath}" created. Run \`cd ${appPath} && ${pm} install && ${pm} run dev\` to start.`
183
+ : `Application "${appPath}" created. Install a package manager (bun, pnpm, npm, or yarn), then run \`cd ${appPath} && <pm> install && <pm> run dev\` to start.`;
184
+ return sequence_([
185
+ info(`Scaffolding React application in "${appPath}"...`),
186
+ // The scaffolded package.json composes its scripts with `bun run`; on
187
+ // a machine where another manager was detected, say so up front.
188
+ when(pm !== null && pm !== "bun", warn(`Detected ${pm}, but the generated scripts use \`bun run\` — ` +
189
+ "install bun to run the app's composite scripts.")),
190
+ // EJS templates (files needing interpolation)
191
+ template({
192
+ source: src("package.json.ejs"),
193
+ dest: dest("package.json"),
194
+ vars,
195
+ }),
196
+ template({
197
+ source: src("README.md.ejs"),
198
+ dest: dest("README.md"),
199
+ vars,
200
+ }),
201
+ template({
202
+ source: src("biome.json.ejs"),
203
+ dest: dest("biome.json"),
204
+ vars,
205
+ }),
206
+ // Root config
207
+ // tsconfig (EJS — #relay path alias only when --relay)
208
+ template({
209
+ source: src("tsconfig.json.ejs"),
210
+ dest: dest("tsconfig.json"),
211
+ vars,
212
+ }),
213
+ // vite config (EJS — vite-plugin-relay-lite only when --relay)
214
+ template({
215
+ source: src("vite.config.ts.ejs"),
216
+ dest: dest("vite.config.ts"),
217
+ vars,
218
+ }),
219
+ copy("vitest.config.ts"),
220
+ // vitest setup (EJS — relay-test-utils' jest→vi alias only when --relay)
221
+ template({
222
+ source: src("vitest.setup.ts.ejs"),
223
+ dest: dest("vitest.setup.ts"),
224
+ vars,
225
+ }),
226
+ copy("vitest.e2e.config.ts"),
227
+ // Relay compiler config (validates queries against the mock SDL)
228
+ when(answers.relay, copy("relay.config.json")),
229
+ // index.html (EJS — <title> uses the app name)
230
+ template({
231
+ source: src("index.html.ejs"),
232
+ dest: dest("index.html"),
233
+ vars,
234
+ }),
235
+ // The template is stored as `gitignore` (no leading dot): npm strips a
236
+ // literal `.gitignore` from published tarballs, so we ship it dotless and
237
+ // restore the dot at write time.
238
+ rawFile({
239
+ source: src("gitignore"),
240
+ content: load("gitignore").content,
241
+ dest: dest(".gitignore"),
242
+ }),
243
+ // The app's browser floor, read by vite.config.ts to derive Lightning
244
+ // CSS targets. Unlike `.gitignore` above, npm does not strip
245
+ // `.browserslistrc` from tarballs, so the template keeps its dot.
246
+ copy(".browserslistrc"),
247
+ // E2e tests (the 2×3 server matrix + its spawn/teardown harness)
248
+ copy("test/e2e/serverHarness.ts"),
249
+ template({
250
+ source: src("test/e2e/servers.e2e.ts.ejs"),
251
+ dest: dest("test/e2e/servers.e2e.ts"),
252
+ vars,
253
+ }),
254
+ // Styles
255
+ // styles (EJS — form stylesheet imported only when forms is on)
256
+ template({
257
+ source: src("src/styles/index.css.ejs"),
258
+ dest: dest("src/styles/index.css"),
259
+ vars,
260
+ }),
261
+ copy("src/styles/app.css"),
262
+ // Client (EJS — RelayEnvironmentProvider only when --relay)
263
+ template({
264
+ source: src("src/client/entry.tsx.ejs"),
265
+ dest: dest("src/client/entry.tsx"),
266
+ vars,
267
+ }),
268
+ // Server — dev (Vite + HMR) and preview (compiled) servers each route
269
+ // between the app + sitemap renderers; the renderers stay routing-agnostic.
270
+ // Server entry (EJS — a per-request RelayEnvironmentProvider only when --relay)
271
+ template({
272
+ source: src("src/server/entry.tsx.ejs"),
273
+ dest: dest("src/server/entry.tsx"),
274
+ vars,
275
+ }),
276
+ template({
277
+ source: src("src/server/renderer.tsx.ejs"),
278
+ dest: dest("src/server/renderer.tsx"),
279
+ vars,
280
+ }),
281
+ template({
282
+ source: src("src/server/server.express.ts.ejs"),
283
+ dest: dest("src/server/server.express.ts"),
284
+ vars,
285
+ }),
286
+ template({
287
+ source: src("src/server/server.bun.ts.ejs"),
288
+ dest: dest("src/server/server.bun.ts"),
289
+ vars,
290
+ }),
291
+ copy("src/server/preview.express.ts"),
292
+ copy("src/server/preview.bun.ts"),
293
+ // Sitemap (rendered route at /sitemap.xml)
294
+ copy("src/sitemap/renderer.ts"),
295
+ // sitemap getters (EJS — /contact entry only when forms is on, /catalog
296
+ // entry only when relay is on)
297
+ template({
298
+ source: src("src/sitemap/getSitemapItems.ts.ejs"),
299
+ dest: dest("src/sitemap/getSitemapItems.ts"),
300
+ vars,
301
+ }),
302
+ // Domain: marketing
303
+ template({
304
+ source: src("src/domains/marketing/HomePage.tsx.ejs"),
305
+ dest: dest("src/domains/marketing/HomePage.tsx"),
306
+ vars,
307
+ }),
308
+ template({
309
+ source: src("src/domains/marketing/GuidePage.tsx.ejs"),
310
+ dest: dest("src/domains/marketing/GuidePage.tsx"),
311
+ vars,
312
+ }),
313
+ copy("src/domains/marketing/routes.ts"),
314
+ // Domain: account
315
+ template({
316
+ source: src("src/domains/account/AccountPage.tsx.ejs"),
317
+ dest: dest("src/domains/account/AccountPage.tsx"),
318
+ vars,
319
+ }),
320
+ template({
321
+ source: src("src/domains/account/LoginPage.tsx.ejs"),
322
+ dest: dest("src/domains/account/LoginPage.tsx"),
323
+ vars,
324
+ }),
325
+ copy("src/domains/account/routes.ts"),
326
+ // Domain: contact (when forms is on)
327
+ when(answers.forms, template({
328
+ source: src("src/domains/contact/ContactPage.tsx.ejs"),
329
+ dest: dest("src/domains/contact/ContactPage.tsx"),
330
+ vars,
331
+ })),
332
+ when(answers.forms, copy("src/domains/contact/routes.ts")),
333
+ // Relay data layer (when --relay is enabled): environment factory +
334
+ // executable mock schema, the catalog example domain, and the
335
+ // ClientOnly SSR guard (whose only consumer today is the catalog page).
336
+ when(answers.relay, copy("src/relay/schema.graphql")),
337
+ when(answers.relay, copy("src/relay/schema.ts")),
338
+ when(answers.relay, copy("src/relay/schema.tests.ts")),
339
+ when(answers.relay, copy("src/relay/environment.ts")),
340
+ when(answers.relay, copy("src/relay/environment.tests.ts")),
341
+ // Committed relay-compiler artifacts — deterministic outputs of the
342
+ // committed schema + the catalog queries; `bun run relay` regenerates
343
+ // them in the scaffolded app after any schema or graphql-tag edit.
344
+ when(answers.relay, copy("src/relay/__generated__/ProductCard_product.graphql.ts")),
345
+ when(answers.relay, copy("src/relay/__generated__/ProductListQuery.graphql.ts")),
346
+ // Domain: catalog (when --relay is enabled)
347
+ when(answers.relay, template({
348
+ source: src("src/domains/catalog/CatalogPage.tsx.ejs"),
349
+ dest: dest("src/domains/catalog/CatalogPage.tsx"),
350
+ vars,
351
+ })),
352
+ when(answers.relay, template({
353
+ source: src("src/domains/catalog/ProductList.tsx.ejs"),
354
+ dest: dest("src/domains/catalog/ProductList.tsx"),
355
+ vars,
356
+ })),
357
+ when(answers.relay, template({
358
+ source: src("src/domains/catalog/ProductList.stories.tsx.ejs"),
359
+ dest: dest("src/domains/catalog/ProductList.stories.tsx"),
360
+ vars,
361
+ })),
362
+ when(answers.relay, template({
363
+ source: src("src/domains/catalog/ProductList.tests.tsx.ejs"),
364
+ dest: dest("src/domains/catalog/ProductList.tests.tsx"),
365
+ vars,
366
+ })),
367
+ when(answers.relay, template({
368
+ source: src("src/domains/catalog/ProductCard.tsx.ejs"),
369
+ dest: dest("src/domains/catalog/ProductCard.tsx"),
370
+ vars,
371
+ })),
372
+ when(answers.relay, copy("src/domains/catalog/ErrorBoundary.tsx")),
373
+ when(answers.relay, template({
374
+ source: src("src/domains/catalog/ErrorBoundary.tests.tsx.ejs"),
375
+ dest: dest("src/domains/catalog/ErrorBoundary.tests.tsx"),
376
+ vars,
377
+ })),
378
+ when(answers.relay, copy("src/domains/catalog/routes.ts")),
379
+ // Standalone dependency patches (when --relay is enabled): a
380
+ // standalone app cannot inherit a workspace's patches/, so they ship
381
+ // with the scaffold and are applied via "patchedDependencies" in
382
+ // package.json. Inside a bun workspace the WORKSPACE ROOT owns
383
+ // patching — bun resolves patch paths from the root, so an app-local
384
+ // block would abort `bun install` ("Couldn't find patch file") —
385
+ // hence neither patches/ nor patchedDependencies are emitted there.
386
+ when(answers.relay && !standalone, info(`Scaffolding into the bun workspace at "${workspaceRoot}" — ` +
387
+ "dependency patches are owned by the workspace root, so no " +
388
+ "app-local patches/ or patchedDependencies were emitted. " +
389
+ "Ensure the workspace root patches react-relay, relay-runtime " +
390
+ "and relay-runtime-network (see the app README).")),
391
+ // react-relay: cjs-module-lexer export hints so named imports survive
392
+ // Node SSR externalisation.
393
+ when(answers.relay && standalone, copy("patches/react-relay@21.0.1.patch")),
394
+ // relay-runtime: real `module.exports.X = undefined;` assignments for
395
+ // type-only names (ConcreteRequest, ReaderFragment, FragmentRefs…)
396
+ // that relay-compiler's TypeScript artifacts import as values —
397
+ // strict ESM-CJS interop (Node ESM, Vite's SSR module runner, Bun)
398
+ // rejects them otherwise. Real assignments, not a dead-branch lexer
399
+ // hint: Bun's interop reflects the actual runtime exports object.
400
+ when(answers.relay && standalone, copy("patches/relay-runtime@21.0.1.patch")),
401
+ // relay-runtime-network: fixes the broken package `imports` map.
402
+ // Temporary until the fixed upstream release lands (advl/lit-relay#32);
403
+ // then this patch and its patchedDependencies entry can be dropped.
404
+ when(answers.relay && standalone, copy("patches/relay-runtime-network@0.1.0.patch")),
405
+ // Routes (EJS — conditionally includes contact + catalog domains)
406
+ template({
407
+ source: src("src/routes.tsx.ejs"),
408
+ dest: dest("src/routes.tsx"),
409
+ vars,
410
+ }),
411
+ // Lib: Navigation (EJS — contact link only when forms is on, catalog
412
+ // link only when relay is on)
413
+ template({
414
+ source: src("src/lib/Navigation/Navigation.tsx.ejs"),
415
+ dest: dest("src/lib/Navigation/Navigation.tsx"),
416
+ vars,
417
+ }),
418
+ copy("src/lib/Navigation/index.ts"),
419
+ // Lib: ThemeSelector
420
+ template({
421
+ source: src("src/lib/ThemeSelector/ThemeSelector.tsx.ejs"),
422
+ dest: dest("src/lib/ThemeSelector/ThemeSelector.tsx"),
423
+ vars,
424
+ }),
425
+ copy("src/lib/ThemeSelector/index.ts"),
426
+ // Lib: LocaleSelector (when --intl is enabled)
427
+ when(answers.intl, copy("src/lib/LocaleSelector/LocaleSelector.tsx")),
428
+ when(answers.intl, copy("src/lib/LocaleSelector/LocaleSelector.tests.tsx")),
429
+ when(answers.intl, copy("src/lib/LocaleSelector/index.ts")),
430
+ // i18n (when --intl is enabled): locale config, one catalog per
431
+ // locale, negotiation tests
432
+ when(answers.intl, copy("src/i18n/config.ts")),
433
+ when(answers.intl, copy("src/i18n/catalogs.ts")),
434
+ when(answers.intl, copy("src/i18n/en.ts")),
435
+ when(answers.intl, copy("src/i18n/fr.ts")),
436
+ when(answers.intl, copy("src/i18n/ar.ts")),
437
+ when(answers.intl, copy("src/i18n/index.ts")),
438
+ when(answers.intl, copy("src/i18n/negotiation.tests.ts")),
439
+ // Lib: ExampleComponent
440
+ copy("src/lib/ExampleComponent/ExampleComponent.tsx"),
441
+ copy("src/lib/ExampleComponent/ExampleComponent.stories.tsx"),
442
+ copy("src/lib/ExampleComponent/ExampleComponent.tests.tsx"),
443
+ copy("src/lib/ExampleComponent/index.ts"),
444
+ copy("src/lib/ExampleComponent/types.ts"),
445
+ copy("src/lib/ExampleComponent/styles.css"),
446
+ // Lib: LazyComponent
447
+ copy("src/lib/LazyComponent/LazyComponent.tsx"),
448
+ copy("src/lib/LazyComponent/LazyComponent.stories.tsx"),
449
+ copy("src/lib/LazyComponent/index.ts"),
450
+ // Lib: ClientOnly (when --relay is enabled — the catalog page is its
451
+ // only consumer, keeping Relay queries off the server render until
452
+ // SSR data serialization/hydration is supported)
453
+ when(answers.relay, copy("src/lib/ClientOnly/ClientOnly.tsx")),
454
+ when(answers.relay, copy("src/lib/ClientOnly/ClientOnly.tests.tsx")),
455
+ when(answers.relay, copy("src/lib/ClientOnly/index.ts")),
456
+ // Lib barrel (EJS — ClientOnly export only when --relay)
457
+ template({
458
+ source: src("src/lib/index.ts.ejs"),
459
+ dest: dest("src/lib/index.ts"),
460
+ vars,
461
+ }),
462
+ // Vite types
463
+ copy("src/vite-env.d.ts"),
464
+ // Storybook
465
+ // main config (EJS — the relay mocking addon only when --relay)
466
+ template({
467
+ source: src(".storybook/main.ts.ejs"),
468
+ dest: dest(".storybook/main.ts"),
469
+ vars,
470
+ }),
471
+ template({
472
+ source: src(".storybook/preview.ts.ejs"),
473
+ dest: dest(".storybook/preview.ts"),
474
+ vars,
475
+ }),
476
+ copy(".storybook/decorators/withRouter.tsx"),
477
+ when(answers.intl, copy(".storybook/decorators/withI18n.tsx")),
478
+ template({
479
+ source: src(".storybook/decorators/index.ts.ejs"),
480
+ dest: dest(".storybook/decorators/index.ts"),
481
+ vars,
482
+ }),
483
+ // Static asset dirs (kept by placeholder; both wired into Storybook staticDirs)
484
+ copy("src/assets/.gitkeep"),
485
+ copy("public/.gitkeep"),
486
+ copy("public/robots.txt"),
487
+ // Install dependencies with the detected package manager, or a no-op
488
+ // when none was found / the user declined. Built above where `pm` is
489
+ // narrowed to non-null, so the install command never invents a manager;
490
+ // the scaffold completes and the closing message says what to run.
491
+ installTask ?? pure(undefined),
492
+ info(finalMessage),
493
+ ]);
127
494
  });
128
- // Detect an available package manager for both the (optional) install
129
- // step and the closing message, so the suggested commands reflect what's
130
- // actually on the machine.
131
- const pm = pickPackageManager();
132
- // Build the install task only when a package manager was actually found —
133
- // this narrows `pm` to non-null, so the install command never invents a
134
- // manager. `null` when we won't (or can't) install.
135
- const installTask = answers.runInstall && pm !== null
136
- ? sequence_([
137
- info(`Installing dependencies with ${pm}...`),
138
- exec(pm, ["install"], appPath),
139
- ])
140
- : null;
141
- // The closing command only names a package manager we actually found; if
142
- // none was detected we don't invent one (previously this suggested `bun`
143
- // even on a machine without bun), and instead point the user at the
144
- // package-manager step.
145
- const finalMessage = installTask
146
- ? `Application "${appPath}" created. Run \`cd ${appPath} && ${pm} run dev\` to start.`
147
- : pm
148
- ? `Application "${appPath}" created. Run \`cd ${appPath} && ${pm} install && ${pm} run dev\` to start.`
149
- : `Application "${appPath}" created. Install a package manager (bun, pnpm, npm, or yarn), then run \`cd ${appPath} && <pm> install && <pm> run dev\` to start.`;
150
- return sequence_([
151
- // Warn (don't block) if the destination already exists — scaffolding
152
- // will overwrite files in place.
153
- flatMap(exists(appPath), (present) => when(present, warn(`"${appPath}" already exists — existing files may be overwritten.`))),
154
- info(`Scaffolding React application in "${appPath}"...`),
155
- // EJS templates (files needing interpolation)
156
- template({
157
- source: src("package.json.ejs"),
158
- dest: dest("package.json"),
159
- vars,
160
- }),
161
- template({
162
- source: src("README.md.ejs"),
163
- dest: dest("README.md"),
164
- vars,
165
- }),
166
- template({
167
- source: src("biome.json.ejs"),
168
- dest: dest("biome.json"),
169
- vars,
170
- }),
171
- // Root config
172
- // tsconfig (EJS — #relay path alias only when --relay)
173
- template({
174
- source: src("tsconfig.json.ejs"),
175
- dest: dest("tsconfig.json"),
176
- vars,
177
- }),
178
- // vite config (EJS — vite-plugin-relay-lite only when --relay)
179
- template({
180
- source: src("vite.config.ts.ejs"),
181
- dest: dest("vite.config.ts"),
182
- vars,
183
- }),
184
- copy("vitest.config.ts"),
185
- // vitest setup (EJS — relay-test-utils' jest→vi alias only when --relay)
186
- template({
187
- source: src("vitest.setup.ts.ejs"),
188
- dest: dest("vitest.setup.ts"),
189
- vars,
190
- }),
191
- copy("vitest.e2e.config.ts"),
192
- // Relay compiler config (validates queries against the mock SDL)
193
- when(answers.relay, copy("relay.config.json")),
194
- // index.html (EJS — <title> uses the app name)
195
- template({
196
- source: src("index.html.ejs"),
197
- dest: dest("index.html"),
198
- vars,
199
- }),
200
- // The template is stored as `gitignore` (no leading dot): npm strips a
201
- // literal `.gitignore` from published tarballs, so we ship it dotless and
202
- // restore the dot at write time.
203
- copyFile(src("gitignore"), dest(".gitignore")),
204
- // The app's browser floor, read by vite.config.ts to derive Lightning
205
- // CSS targets. Unlike `.gitignore` above, npm does not strip
206
- // `.browserslistrc` from tarballs, so the template keeps its dot.
207
- copy(".browserslistrc"),
208
- // E2e tests (the 2×3 server matrix + its spawn/teardown harness)
209
- copy("test/e2e/serverHarness.ts"),
210
- copy("test/e2e/servers.e2e.ts"),
211
- // Styles
212
- // styles (EJS — form stylesheet imported only when --forms)
213
- template({
214
- source: src("src/styles/index.css.ejs"),
215
- dest: dest("src/styles/index.css"),
216
- vars,
217
- }),
218
- copy("src/styles/app.css"),
219
- // Client (EJS — RelayEnvironmentProvider only when --relay)
220
- template({
221
- source: src("src/client/entry.tsx.ejs"),
222
- dest: dest("src/client/entry.tsx"),
223
- vars,
224
- }),
225
- // Server — dev (Vite + HMR) and preview (compiled) servers each route
226
- // between the app + sitemap renderers; the renderers stay routing-agnostic.
227
- // Server entry (EJS — a per-request RelayEnvironmentProvider only when --relay)
228
- template({
229
- source: src("src/server/entry.tsx.ejs"),
230
- dest: dest("src/server/entry.tsx"),
231
- vars,
232
- }),
233
- copy("src/server/renderer.tsx"),
234
- copy("src/server/server.express.ts"),
235
- copy("src/server/server.bun.ts"),
236
- copy("src/server/preview.express.ts"),
237
- copy("src/server/preview.bun.ts"),
238
- // Sitemap (rendered route at /sitemap.xml)
239
- copy("src/sitemap/renderer.ts"),
240
- // sitemap getters (EJS — /contact entry only when --forms, /catalog
241
- // entry only when --relay)
242
- template({
243
- source: src("src/sitemap/getSitemapItems.ts.ejs"),
244
- dest: dest("src/sitemap/getSitemapItems.ts"),
245
- vars,
246
- }),
247
- // Domain: marketing
248
- copy("src/domains/marketing/HomePage.tsx"),
249
- copy("src/domains/marketing/GuidePage.tsx"),
250
- copy("src/domains/marketing/routes.ts"),
251
- // Domain: account
252
- copy("src/domains/account/AccountPage.tsx"),
253
- copy("src/domains/account/LoginPage.tsx"),
254
- copy("src/domains/account/routes.ts"),
255
- // Domain: contact (when --forms is enabled)
256
- when(answers.forms, copy("src/domains/contact/ContactPage.tsx")),
257
- when(answers.forms, copy("src/domains/contact/routes.ts")),
258
- // Relay data layer (when --relay is enabled): environment factory +
259
- // executable mock schema, the catalog example domain, and the
260
- // ClientOnly SSR guard (whose only consumer today is the catalog page).
261
- when(answers.relay, copy("src/relay/schema.graphql")),
262
- when(answers.relay, copy("src/relay/schema.ts")),
263
- when(answers.relay, copy("src/relay/schema.tests.ts")),
264
- when(answers.relay, copy("src/relay/environment.ts")),
265
- when(answers.relay, copy("src/relay/environment.tests.ts")),
266
- // Committed relay-compiler artifacts — deterministic outputs of the
267
- // committed schema + the catalog queries; `bun run relay` regenerates
268
- // them in the scaffolded app after any schema or graphql-tag edit.
269
- when(answers.relay, copy("src/relay/__generated__/ProductCard_product.graphql.ts")),
270
- when(answers.relay, copy("src/relay/__generated__/ProductListQuery.graphql.ts")),
271
- // Domain: catalog (when --relay is enabled)
272
- when(answers.relay, copy("src/domains/catalog/CatalogPage.tsx")),
273
- when(answers.relay, copy("src/domains/catalog/ProductList.tsx")),
274
- when(answers.relay, copy("src/domains/catalog/ProductList.stories.tsx")),
275
- when(answers.relay, copy("src/domains/catalog/ProductList.tests.tsx")),
276
- when(answers.relay, copy("src/domains/catalog/ProductCard.tsx")),
277
- when(answers.relay, copy("src/domains/catalog/ErrorBoundary.tsx")),
278
- when(answers.relay, copy("src/domains/catalog/ErrorBoundary.tests.tsx")),
279
- when(answers.relay, copy("src/domains/catalog/routes.ts")),
280
- // Standalone dependency patches (when --relay is enabled): a
281
- // standalone app cannot inherit a workspace's patches/, so they ship
282
- // with the scaffold and are applied via "patchedDependencies" in
283
- // package.json. Inside a bun workspace the WORKSPACE ROOT owns
284
- // patching — bun resolves patch paths from the root, so an app-local
285
- // block would abort `bun install` ("Couldn't find patch file") —
286
- // hence neither patches/ nor patchedDependencies are emitted there.
287
- when(answers.relay && !standalone, info(`Scaffolding into the bun workspace at "${workspaceRoot}" — ` +
288
- "dependency patches are owned by the workspace root, so no " +
289
- "app-local patches/ or patchedDependencies were emitted. " +
290
- "Ensure the workspace root patches react-relay, relay-runtime " +
291
- "and relay-runtime-network (see the app README).")),
292
- // react-relay: cjs-module-lexer export hints so named imports survive
293
- // Node SSR externalisation.
294
- when(answers.relay && standalone, copy("patches/react-relay@21.0.1.patch")),
295
- // relay-runtime: real `module.exports.X = undefined;` assignments for
296
- // type-only names (ConcreteRequest, ReaderFragment, FragmentRefs…)
297
- // that relay-compiler's TypeScript artifacts import as values —
298
- // strict ESM-CJS interop (Node ESM, Vite's SSR module runner, Bun)
299
- // rejects them otherwise. Real assignments, not a dead-branch lexer
300
- // hint: Bun's interop reflects the actual runtime exports object.
301
- when(answers.relay && standalone, copy("patches/relay-runtime@21.0.1.patch")),
302
- // relay-runtime-network: fixes the broken package `imports` map.
303
- // Temporary until the fixed upstream release lands (advl/lit-relay#32);
304
- // then this patch and its patchedDependencies entry can be dropped.
305
- when(answers.relay && standalone, copy("patches/relay-runtime-network@0.1.0.patch")),
306
- // Routes (EJS — conditionally includes contact + catalog domains)
307
- template({
308
- source: src("src/routes.tsx.ejs"),
309
- dest: dest("src/routes.tsx"),
310
- vars,
311
- }),
312
- // Lib: Navigation (EJS — contact link only when --forms, catalog
313
- // link only when --relay)
314
- template({
315
- source: src("src/lib/Navigation/Navigation.tsx.ejs"),
316
- dest: dest("src/lib/Navigation/Navigation.tsx"),
317
- vars,
318
- }),
319
- copy("src/lib/Navigation/index.ts"),
320
- // Lib: ThemeSelector
321
- copy("src/lib/ThemeSelector/ThemeSelector.tsx"),
322
- copy("src/lib/ThemeSelector/index.ts"),
323
- // Lib: ExampleComponent
324
- copy("src/lib/ExampleComponent/ExampleComponent.tsx"),
325
- copy("src/lib/ExampleComponent/ExampleComponent.stories.tsx"),
326
- copy("src/lib/ExampleComponent/ExampleComponent.tests.tsx"),
327
- copy("src/lib/ExampleComponent/index.ts"),
328
- copy("src/lib/ExampleComponent/types.ts"),
329
- copy("src/lib/ExampleComponent/styles.css"),
330
- // Lib: LazyComponent
331
- copy("src/lib/LazyComponent/LazyComponent.tsx"),
332
- copy("src/lib/LazyComponent/LazyComponent.stories.tsx"),
333
- copy("src/lib/LazyComponent/index.ts"),
334
- // Lib: ClientOnly (when --relay is enabled — the catalog page is its
335
- // only consumer, keeping Relay queries off the server render until
336
- // SSR data serialization/hydration is supported)
337
- when(answers.relay, copy("src/lib/ClientOnly/ClientOnly.tsx")),
338
- when(answers.relay, copy("src/lib/ClientOnly/ClientOnly.tests.tsx")),
339
- when(answers.relay, copy("src/lib/ClientOnly/index.ts")),
340
- // Lib barrel (EJS — ClientOnly export only when --relay)
341
- template({
342
- source: src("src/lib/index.ts.ejs"),
343
- dest: dest("src/lib/index.ts"),
344
- vars,
345
- }),
346
- // Vite types
347
- copy("src/vite-env.d.ts"),
348
- // Storybook
349
- // main config (EJS — the relay mocking addon only when --relay)
350
- template({
351
- source: src(".storybook/main.ts.ejs"),
352
- dest: dest(".storybook/main.ts"),
353
- vars,
354
- }),
355
- copy(".storybook/preview.ts"),
356
- copy(".storybook/decorators/withRouter.tsx"),
357
- copy(".storybook/decorators/index.ts"),
358
- // Static asset dirs (kept by placeholder; both wired into Storybook staticDirs)
359
- copy("src/assets/.gitkeep"),
360
- copy("public/.gitkeep"),
361
- copy("public/robots.txt"),
362
- // Install dependencies with the detected package manager, or a no-op
363
- // when none was found / the user declined. Built above where `pm` is
364
- // narrowed to non-null, so the install command never invents a manager;
365
- // the scaffold completes and the closing message says what to run.
366
- installTask ?? pure(undefined),
367
- info(finalMessage),
368
- ]);
369
495
  });
370
496
  },
371
497
  };