@canonical/summon-application 0.29.0 → 0.31.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 (126) hide show
  1. package/README.md +75 -13
  2. package/dist/esm/application/react/index.js +347 -0
  3. package/dist/esm/application/react/index.js.map +1 -0
  4. package/dist/esm/application/react/templates/.browserslistrc +2 -0
  5. package/dist/esm/application/react/templates/.storybook/main.ts.ejs +10 -0
  6. package/{src → dist/esm}/application/react/templates/README.md.ejs +54 -0
  7. package/dist/esm/application/react/templates/biome.json.ejs +6 -0
  8. package/dist/esm/application/react/templates/gitignore +25 -0
  9. package/{src → dist/esm}/application/react/templates/package.json.ejs +36 -2
  10. package/dist/esm/application/react/templates/patches/react-relay@18.2.0.patch +52 -0
  11. package/dist/esm/application/react/templates/patches/relay-runtime-network@0.1.0.patch +46 -0
  12. package/dist/esm/application/react/templates/relay.config.json +8 -0
  13. package/{src/application/react/templates/src/client/entry.tsx → dist/esm/application/react/templates/src/client/entry.tsx.ejs} +23 -0
  14. package/dist/esm/application/react/templates/src/domains/catalog/CatalogPage.tsx +50 -0
  15. package/dist/esm/application/react/templates/src/domains/catalog/ErrorBoundary.tests.tsx +56 -0
  16. package/dist/esm/application/react/templates/src/domains/catalog/ErrorBoundary.tsx +37 -0
  17. package/dist/esm/application/react/templates/src/domains/catalog/ProductCard.tsx +49 -0
  18. package/dist/esm/application/react/templates/src/domains/catalog/ProductList.stories.tsx +103 -0
  19. package/dist/esm/application/react/templates/src/domains/catalog/ProductList.tests.tsx +80 -0
  20. package/dist/esm/application/react/templates/src/domains/catalog/ProductList.tsx +59 -0
  21. package/dist/esm/application/react/templates/src/domains/catalog/routes.ts +11 -0
  22. package/dist/esm/application/react/templates/src/lib/ClientOnly/ClientOnly.tests.tsx +35 -0
  23. package/dist/esm/application/react/templates/src/lib/ClientOnly/ClientOnly.tsx +31 -0
  24. package/dist/esm/application/react/templates/src/lib/ClientOnly/index.ts +1 -0
  25. package/{src → dist/esm}/application/react/templates/src/lib/Navigation/Navigation.tsx.ejs +3 -0
  26. package/{src/application/react/templates/src/lib/index.ts → dist/esm/application/react/templates/src/lib/index.ts.ejs} +3 -0
  27. package/dist/esm/application/react/templates/src/relay/__generated__/ProductCard_product.graphql.ts +82 -0
  28. package/dist/esm/application/react/templates/src/relay/__generated__/ProductListQuery.graphql.ts +272 -0
  29. package/dist/esm/application/react/templates/src/relay/environment.tests.ts +87 -0
  30. package/dist/esm/application/react/templates/src/relay/environment.ts +119 -0
  31. package/dist/esm/application/react/templates/src/relay/schema.graphql +111 -0
  32. package/dist/esm/application/react/templates/src/relay/schema.tests.ts +137 -0
  33. package/dist/esm/application/react/templates/src/relay/schema.ts +205 -0
  34. package/{src → dist/esm}/application/react/templates/src/routes.tsx.ejs +10 -0
  35. package/{src/application/react/templates/src/server/entry.tsx → dist/esm/application/react/templates/src/server/entry.tsx.ejs} +36 -0
  36. package/{src → dist/esm}/application/react/templates/src/sitemap/getSitemapItems.ts.ejs +3 -0
  37. package/{src/application/react/templates/tsconfig.json → dist/esm/application/react/templates/tsconfig.json.ejs} +3 -0
  38. package/dist/esm/application/react/templates/vite.config.ts.ejs +92 -0
  39. package/dist/esm/application/react/templates/vitest.setup.ts.ejs +22 -0
  40. package/dist/esm/domain/index.js +89 -0
  41. package/dist/esm/domain/index.js.map +1 -0
  42. package/{src/index.ts → dist/esm/index.js} +6 -8
  43. package/dist/esm/index.js.map +1 -0
  44. package/dist/esm/route/index.js +115 -0
  45. package/dist/esm/route/index.js.map +1 -0
  46. package/dist/esm/route/insertRoute.js +178 -0
  47. package/dist/esm/route/insertRoute.js.map +1 -0
  48. package/dist/esm/shared/casing.js +15 -0
  49. package/dist/esm/shared/casing.js.map +1 -0
  50. package/dist/esm/shared/packageManager.js +38 -0
  51. package/dist/esm/shared/packageManager.js.map +1 -0
  52. package/dist/esm/shared/versions.js +88 -0
  53. package/dist/esm/shared/versions.js.map +1 -0
  54. package/dist/esm/wrapper/index.js +72 -0
  55. package/dist/esm/wrapper/index.js.map +1 -0
  56. package/dist/types/application/react/index.d.ts +12 -0
  57. package/dist/types/application/react/index.d.ts.map +1 -0
  58. package/dist/types/domain/index.d.ts +7 -0
  59. package/dist/types/domain/index.d.ts.map +1 -0
  60. package/dist/types/index.d.ts +8 -0
  61. package/dist/types/index.d.ts.map +1 -0
  62. package/dist/types/route/index.d.ts +7 -0
  63. package/dist/types/route/index.d.ts.map +1 -0
  64. package/dist/types/route/insertRoute.d.ts +34 -0
  65. package/dist/types/route/insertRoute.d.ts.map +1 -0
  66. package/{src/shared/casing.ts → dist/types/shared/casing.d.ts} +2 -7
  67. package/dist/types/shared/casing.d.ts.map +1 -0
  68. package/dist/types/shared/packageManager.d.ts +19 -0
  69. package/dist/types/shared/packageManager.d.ts.map +1 -0
  70. package/dist/types/shared/versions.d.ts +15 -0
  71. package/dist/types/shared/versions.d.ts.map +1 -0
  72. package/dist/types/wrapper/index.d.ts +7 -0
  73. package/dist/types/wrapper/index.d.ts.map +1 -0
  74. package/package.json +16 -15
  75. package/src/application/react/index.ts +0 -294
  76. package/src/application/react/templates/.storybook/main.ts +0 -5
  77. package/src/application/react/templates/biome.json.ejs +0 -6
  78. package/src/application/react/templates/vite.config.ts +0 -45
  79. package/src/application/react/templates/vitest.setup.ts +0 -9
  80. package/src/domain/index.ts +0 -119
  81. package/src/index.test.ts +0 -398
  82. package/src/route/index.ts +0 -154
  83. package/src/route/insertRoute.test.ts +0 -98
  84. package/src/route/insertRoute.ts +0 -236
  85. package/src/shared/versions.ts +0 -48
  86. package/src/wrapper/index.ts +0 -100
  87. /package/{src → dist/esm}/application/react/templates/.storybook/decorators/index.ts +0 -0
  88. /package/{src → dist/esm}/application/react/templates/.storybook/decorators/withRouter.tsx +0 -0
  89. /package/{src → dist/esm}/application/react/templates/.storybook/preview.ts +0 -0
  90. /package/{src → dist/esm}/application/react/templates/index.html.ejs +0 -0
  91. /package/{src → dist/esm}/application/react/templates/public/.gitkeep +0 -0
  92. /package/{src → dist/esm}/application/react/templates/public/robots.txt +0 -0
  93. /package/{src → dist/esm}/application/react/templates/src/assets/.gitkeep +0 -0
  94. /package/{src → dist/esm}/application/react/templates/src/domains/account/AccountPage.tsx +0 -0
  95. /package/{src → dist/esm}/application/react/templates/src/domains/account/LoginPage.tsx +0 -0
  96. /package/{src → dist/esm}/application/react/templates/src/domains/account/routes.ts +0 -0
  97. /package/{src → dist/esm}/application/react/templates/src/domains/contact/ContactPage.tsx +0 -0
  98. /package/{src → dist/esm}/application/react/templates/src/domains/contact/routes.ts +0 -0
  99. /package/{src → dist/esm}/application/react/templates/src/domains/marketing/GuidePage.tsx +0 -0
  100. /package/{src → dist/esm}/application/react/templates/src/domains/marketing/HomePage.tsx +0 -0
  101. /package/{src → dist/esm}/application/react/templates/src/domains/marketing/routes.ts +0 -0
  102. /package/{src → dist/esm}/application/react/templates/src/lib/ExampleComponent/ExampleComponent.stories.tsx +0 -0
  103. /package/{src → dist/esm}/application/react/templates/src/lib/ExampleComponent/ExampleComponent.tests.tsx +0 -0
  104. /package/{src → dist/esm}/application/react/templates/src/lib/ExampleComponent/ExampleComponent.tsx +0 -0
  105. /package/{src → dist/esm}/application/react/templates/src/lib/ExampleComponent/index.ts +0 -0
  106. /package/{src → dist/esm}/application/react/templates/src/lib/ExampleComponent/styles.css +0 -0
  107. /package/{src → dist/esm}/application/react/templates/src/lib/ExampleComponent/types.ts +0 -0
  108. /package/{src → dist/esm}/application/react/templates/src/lib/LazyComponent/LazyComponent.stories.tsx +0 -0
  109. /package/{src → dist/esm}/application/react/templates/src/lib/LazyComponent/LazyComponent.tsx +0 -0
  110. /package/{src → dist/esm}/application/react/templates/src/lib/LazyComponent/index.ts +0 -0
  111. /package/{src → dist/esm}/application/react/templates/src/lib/Navigation/index.ts +0 -0
  112. /package/{src → dist/esm}/application/react/templates/src/lib/ThemeSelector/ThemeSelector.tsx +0 -0
  113. /package/{src → dist/esm}/application/react/templates/src/lib/ThemeSelector/index.ts +0 -0
  114. /package/{src → dist/esm}/application/react/templates/src/server/preview.bun.ts +0 -0
  115. /package/{src → dist/esm}/application/react/templates/src/server/preview.express.ts +0 -0
  116. /package/{src → dist/esm}/application/react/templates/src/server/renderer.tsx +0 -0
  117. /package/{src → dist/esm}/application/react/templates/src/server/server.bun.ts +0 -0
  118. /package/{src → dist/esm}/application/react/templates/src/server/server.express.ts +0 -0
  119. /package/{src → dist/esm}/application/react/templates/src/sitemap/renderer.ts +0 -0
  120. /package/{src → dist/esm}/application/react/templates/src/styles/app.css +0 -0
  121. /package/{src → dist/esm}/application/react/templates/src/styles/index.css.ejs +0 -0
  122. /package/{src → dist/esm}/application/react/templates/src/vite-env.d.ts +0 -0
  123. /package/{src → dist/esm}/application/react/templates/test/e2e/serverHarness.ts +0 -0
  124. /package/{src → dist/esm}/application/react/templates/test/e2e/servers.e2e.ts +0 -0
  125. /package/{src → dist/esm}/application/react/templates/vitest.config.ts +0 -0
  126. /package/{src → dist/esm}/application/react/templates/vitest.e2e.config.ts +0 -0
@@ -1,45 +0,0 @@
1
- import react from "@vitejs/plugin-react";
2
- import { defineConfig } from "vite";
3
-
4
- // Path aliases (#lib, #domains, #styles) are declared as Node subpath imports
5
- // in package.json "imports" and resolved natively by Vite — no resolver plugin.
6
- const PORT = Number(process.env.PORT) || undefined;
7
-
8
- // `build:server` runs `vite build --mode server` to compile the two server
9
- // renderers in a single pass. Each is a separate, individually-instantiated
10
- // Lego brick, so they are named Rollup inputs — the build emits
11
- // `dist/server/renderer.js` (the JSX app) + `dist/server/sitemap.js` (the XML
12
- // sitemap), which the preview servers import directly. Every other command
13
- // (`dev`, `build:client`, `preview`) uses the default mode and the SPA client
14
- // build (`--ssrManifest --outDir dist/client`).
15
- export default defineConfig(({ mode }) => ({
16
- plugins: [react()],
17
- // Honour the PORT env var for `dev` (SPA) and `preview` so all server scripts
18
- // — including the SSR ones, which already read PORT — respond to it uniformly.
19
- server: { port: PORT },
20
- preview: { port: PORT },
21
- build:
22
- mode === "server"
23
- ? {
24
- ssr: true,
25
- outDir: "dist/server",
26
- rollupOptions: {
27
- input: {
28
- renderer: "src/server/renderer.tsx",
29
- sitemap: "src/sitemap/renderer.ts",
30
- },
31
- },
32
- }
33
- : undefined,
34
- ssr: {
35
- // Bundle @canonical/* for SSR rather than externalising them, for two
36
- // reasons: (1) some packages declare only a "module" entry (no
37
- // "main"/"exports"), which Vite's SSR (Node-style) resolver ignores —
38
- // externalising them fails with ERR_RESOLVE_PACKAGE_ENTRY_FAIL; (2) their
39
- // built output imports CSS as a side effect (e.g. `import "./x.css"`),
40
- // which Node cannot load (ERR_UNKNOWN_FILE_EXTENSION) but Vite's SSR
41
- // transform no-ops. The regex covers the whole scope so any current or
42
- // future @canonical dependency is handled.
43
- noExternal: [/^@canonical\//],
44
- },
45
- }));
@@ -1,9 +0,0 @@
1
- import * as matchers from "@testing-library/jest-dom/matchers";
2
- import { cleanup } from "@testing-library/react";
3
- import { afterEach, expect } from "vitest";
4
-
5
- expect.extend(matchers as unknown as Parameters<typeof expect.extend>[0]);
6
-
7
- afterEach(() => {
8
- cleanup();
9
- });
@@ -1,119 +0,0 @@
1
- import * as path from "node:path";
2
- import type {
3
- GeneratorDefinition,
4
- PromptDefinition,
5
- } from "@canonical/summon-core";
6
- import {
7
- exists,
8
- fail,
9
- flatMap,
10
- info,
11
- mkdir,
12
- sequence_,
13
- writeFile,
14
- } from "@canonical/task";
15
- import { toCamelCase, toTitleCase } from "@canonical/utils";
16
- import { normalizeCommandPath } from "../shared/casing.js";
17
- import { printVersions } from "../shared/versions.js";
18
-
19
- interface DomainAnswers {
20
- readonly domainName: string;
21
- }
22
-
23
- const prompts: PromptDefinition[] = [
24
- {
25
- name: "domainName",
26
- type: "text",
27
- message: "Domain name (for example billing):",
28
- default: "example",
29
- positional: true,
30
- group: "Domain",
31
- },
32
- ];
33
-
34
- function buildMainPage(domainName: string): string {
35
- const title = toTitleCase(domainName);
36
-
37
- return `import { useHead } from "@canonical/react-head";
38
- import type { ReactElement } from "react";
39
-
40
- export default function MainPage(): ReactElement {
41
- useHead({ title: "${title}" });
42
-
43
- return (
44
- <section aria-labelledby="main-title">
45
- <h1 id="main-title">${title}</h1>
46
- <p>This is the main page for the ${domainName} domain.</p>
47
- </section>
48
- );
49
- }
50
- `;
51
- }
52
-
53
- function buildRoutesFile(domainName: string): string {
54
- const routeUrl = `/${normalizeCommandPath(domainName)}`;
55
-
56
- return `import { route } from "@canonical/router-core";
57
- import MainPage from "./MainPage.js";
58
-
59
- const routes = {
60
- ${toCamelCase(domainName)}: route({
61
- url: "${routeUrl}",
62
- content: MainPage,
63
- }),
64
- } as const;
65
-
66
- export default routes;
67
- `;
68
- }
69
-
70
- export const generator: GeneratorDefinition<DomainAnswers> = {
71
- meta: {
72
- name: "domain",
73
- displayName: "@canonical/summon-application:domain",
74
- description: "Create a domain folder with routes and a MainPage",
75
- version: "0.1.0",
76
- help: `Creates a domain directory under src/domains/ with:
77
- - MainPage.tsx — example page component with useHead()
78
- - routes.ts — route barrel exporting the domain's routes
79
-
80
- Add more routes with: summon route <domain>/<route-name>`,
81
- examples: [
82
- "summon domain billing",
83
- "summon domain user-settings",
84
- "summon domain --dry-run billing",
85
- ],
86
- },
87
-
88
- prompts,
89
-
90
- generate: (answers) => {
91
- const name = normalizeCommandPath(answers.domainName);
92
- const domainDir = path.join("src", "domains", name);
93
-
94
- const scaffold = sequence_([
95
- printVersions("domain"),
96
- info(`Creating domain "${name}"...`),
97
- mkdir(domainDir),
98
- writeFile(path.join(domainDir, "MainPage.tsx"), buildMainPage(name)),
99
- writeFile(path.join(domainDir, "routes.ts"), buildRoutesFile(name)),
100
- info(
101
- `Domain "${name}" created. Import its routes in src/routes.tsx and wire them with group().`,
102
- ),
103
- ]);
104
-
105
- // Refuse to run when the domain already exists — mkdir/writeFile undos are
106
- // destructive (DeleteDirectory / DeleteFile), so overwriting then `--undo`
107
- // could delete a pre-existing domain.
108
- return flatMap(exists(domainDir), (present) =>
109
- present
110
- ? fail({
111
- code: "DOMAIN_EXISTS",
112
- message: `Domain "${domainDir}" already exists. Choose a different name or remove it first.`,
113
- })
114
- : scaffold,
115
- );
116
- },
117
- };
118
-
119
- export default generator;
package/src/index.test.ts DELETED
@@ -1,398 +0,0 @@
1
- import { readdirSync, statSync } from "node:fs";
2
- import path from "node:path";
3
- import { fileURLToPath } from "node:url";
4
- import { dryRun, sequence_ } from "@canonical/task";
5
- import { describe, expect, it } from "vitest";
6
- import { generators } from "./index.js";
7
-
8
- /**
9
- * The route generator requires an existing domain (it adds to one). In a
10
- * dry-run the virtual filesystem is empty, so we first run the domain generator
11
- * in the same sequence to "create" src/domains/<domain>/routes.ts, then add the
12
- * route — mirroring real usage (`summon domain` then `summon route`).
13
- */
14
- function dryRunRoute(domain: string, route: string) {
15
- return dryRun(
16
- sequence_([
17
- generators.domain.generate({ domainName: domain }),
18
- generators.route.generate({ routePath: `${domain}/${route}` }),
19
- ]),
20
- );
21
- }
22
-
23
- describe("application/react generator", () => {
24
- it("produces effects for all expected files", () => {
25
- const result = dryRun(
26
- generators["application/react"].generate({
27
- appPath: "my-app",
28
- ssr: true,
29
- router: true,
30
- forms: false,
31
- runInstall: false,
32
- }),
33
- );
34
-
35
- // template() produces WriteFile, copyFile() produces CopyFile
36
- const filePaths = result.effects
37
- .filter((e) => e._tag === "WriteFile" || e._tag === "CopyFile")
38
- .map(
39
- (e) =>
40
- (e as { path?: string; dest?: string }).path ??
41
- (e as { dest?: string }).dest,
42
- );
43
-
44
- // EJS templates (interpolated)
45
- expect(filePaths).toContain("my-app/package.json");
46
- expect(filePaths).toContain("my-app/README.md");
47
-
48
- // Static copies
49
- expect(filePaths).toContain("my-app/tsconfig.json");
50
- expect(filePaths).toContain("my-app/vite.config.ts");
51
- expect(filePaths).toContain("my-app/biome.json");
52
- expect(filePaths).toContain("my-app/index.html");
53
- expect(filePaths).toContain("my-app/.gitignore");
54
- expect(filePaths).toContain("my-app/src/client/entry.tsx");
55
- expect(filePaths).toContain("my-app/src/server/entry.tsx");
56
- expect(filePaths).toContain("my-app/src/server/renderer.tsx");
57
- expect(filePaths).toContain("my-app/src/server/server.express.ts");
58
- expect(filePaths).toContain("my-app/src/server/server.bun.ts");
59
- expect(filePaths).toContain("my-app/src/server/preview.express.ts");
60
- expect(filePaths).toContain("my-app/src/server/preview.bun.ts");
61
- expect(filePaths).toContain("my-app/public/robots.txt");
62
- expect(filePaths).toContain("my-app/src/sitemap/renderer.ts");
63
- expect(filePaths).toContain("my-app/src/sitemap/getSitemapItems.ts");
64
- expect(filePaths).toContain("my-app/vitest.e2e.config.ts");
65
- expect(filePaths).toContain("my-app/test/e2e/serverHarness.ts");
66
- expect(filePaths).toContain("my-app/test/e2e/servers.e2e.ts");
67
- expect(filePaths).toContain("my-app/src/domains/marketing/HomePage.tsx");
68
- expect(filePaths).toContain("my-app/src/domains/marketing/routes.ts");
69
- expect(filePaths).toContain("my-app/src/routes.tsx");
70
- expect(filePaths).toContain("my-app/src/lib/Navigation/Navigation.tsx");
71
- expect(filePaths).toContain("my-app/src/lib/Navigation/index.ts");
72
- expect(filePaths).toContain("my-app/src/lib/index.ts");
73
- expect(filePaths).toContain("my-app/src/vite-env.d.ts");
74
- expect(filePaths).toContain("my-app/src/styles/index.css");
75
- expect(filePaths).toContain("my-app/src/styles/app.css");
76
- expect(filePaths).toContain("my-app/.storybook/main.ts");
77
- expect(filePaths).toContain("my-app/.storybook/preview.ts");
78
- expect(filePaths).toContain("my-app/.storybook/decorators/withRouter.tsx");
79
- expect(filePaths).toContain("my-app/.storybook/decorators/index.ts");
80
-
81
- // Account domain
82
- expect(filePaths).toContain("my-app/src/domains/account/AccountPage.tsx");
83
- expect(filePaths).toContain("my-app/src/domains/account/LoginPage.tsx");
84
- expect(filePaths).toContain("my-app/src/domains/account/routes.ts");
85
-
86
- // Contact domain NOT included when forms=false
87
- expect(filePaths).not.toContain(
88
- "my-app/src/domains/contact/ContactPage.tsx",
89
- );
90
- expect(filePaths).not.toContain("my-app/src/domains/contact/routes.ts");
91
- });
92
-
93
- it("scaffolds every file present in the templates directory", () => {
94
- // Authoritative manifest-completeness guard: enumerate the templates dir on
95
- // disk and assert each file is emitted by the generator. The manifest is a
96
- // hand-maintained allow-list, so a new template file added without a matching
97
- // copy()/template() entry would silently never reach generated apps — this
98
- // test fails loudly when that happens.
99
- const templatesDir = fileURLToPath(
100
- new URL("./application/react/templates", import.meta.url),
101
- );
102
- const onDisk = readdirSync(templatesDir, { recursive: true })
103
- .map((entry) => String(entry).split(path.sep).join("/"))
104
- .filter((rel) => statSync(path.join(templatesDir, rel)).isFile())
105
- // `.ejs` templates are emitted at the interpolated dest (suffix stripped).
106
- .map((rel) =>
107
- rel.endsWith(".ejs") ? rel.slice(0, -".ejs".length) : rel,
108
- );
109
-
110
- // Generate with all features on so conditionally-included templates emit.
111
- const result = dryRun(
112
- generators["application/react"].generate({
113
- appPath: "my-app",
114
- ssr: true,
115
- router: true,
116
- forms: true,
117
- runInstall: false,
118
- }),
119
- );
120
- const emitted = new Set(
121
- result.effects
122
- .filter((e) => e._tag === "WriteFile" || e._tag === "CopyFile")
123
- .map(
124
- (e) =>
125
- (e as { path?: string; dest?: string }).path ??
126
- (e as { dest?: string }).dest,
127
- ),
128
- );
129
-
130
- const missing = onDisk.filter((rel) => !emitted.has(`my-app/${rel}`));
131
- expect(
132
- missing,
133
- `templates not wired into the manifest: ${missing}`,
134
- ).toEqual([]);
135
- });
136
-
137
- it("includes contact domain when forms=true", () => {
138
- const result = dryRun(
139
- generators["application/react"].generate({
140
- appPath: "my-app",
141
- ssr: true,
142
- router: true,
143
- forms: true,
144
- runInstall: false,
145
- }),
146
- );
147
-
148
- const filePaths = result.effects
149
- .filter((e) => e._tag === "WriteFile" || e._tag === "CopyFile")
150
- .map(
151
- (e) =>
152
- (e as { path?: string; dest?: string }).path ??
153
- (e as { dest?: string }).dest,
154
- );
155
-
156
- expect(filePaths).toContain("my-app/src/domains/contact/ContactPage.tsx");
157
- expect(filePaths).toContain("my-app/src/domains/contact/routes.ts");
158
-
159
- // routes.tsx is generated as an EJS template (WriteFile effect exists)
160
- // Note: readFile is mocked in dry-run so we can't inspect rendered content,
161
- // but we verify the template is wired up and the contact files are generated.
162
- const routesEffect = result.effects.find(
163
- (e) =>
164
- e._tag === "WriteFile" &&
165
- (e as { path: string }).path === "my-app/src/routes.tsx",
166
- );
167
- expect(routesEffect).toBeDefined();
168
- });
169
-
170
- it("excludes contact domain files when forms=false", () => {
171
- const result = dryRun(
172
- generators["application/react"].generate({
173
- appPath: "my-app",
174
- ssr: true,
175
- router: true,
176
- forms: false,
177
- runInstall: false,
178
- }),
179
- );
180
-
181
- const filePaths = result.effects
182
- .filter((e) => e._tag === "WriteFile" || e._tag === "CopyFile")
183
- .map(
184
- (e) =>
185
- (e as { path?: string; dest?: string }).path ??
186
- (e as { dest?: string }).dest,
187
- );
188
-
189
- expect(filePaths).not.toContain(
190
- "my-app/src/domains/contact/ContactPage.tsx",
191
- );
192
- expect(filePaths).not.toContain("my-app/src/domains/contact/routes.ts");
193
- });
194
-
195
- it("uses the appPath in file paths", () => {
196
- const result = dryRun(
197
- generators["application/react"].generate({
198
- appPath: "custom-app",
199
- ssr: true,
200
- router: true,
201
- forms: false,
202
- runInstall: false,
203
- }),
204
- );
205
-
206
- const filePaths = result.effects
207
- .filter((e) => e._tag === "WriteFile" || e._tag === "CopyFile")
208
- .map(
209
- (e) =>
210
- (e as { path?: string; dest?: string }).path ??
211
- (e as { dest?: string }).dest,
212
- );
213
-
214
- expect(filePaths).toContain("custom-app/package.json");
215
- expect(filePaths).toContain("custom-app/src/client/entry.tsx");
216
- });
217
-
218
- it("throws when --ssr is false", () => {
219
- expect(() =>
220
- dryRun(
221
- generators["application/react"].generate({
222
- appPath: "my-app",
223
- ssr: false,
224
- router: true,
225
- forms: false,
226
- runInstall: false,
227
- }),
228
- ),
229
- ).toThrow();
230
- });
231
-
232
- it("throws when --router is false", () => {
233
- expect(() =>
234
- dryRun(
235
- generators["application/react"].generate({
236
- appPath: "my-app",
237
- ssr: true,
238
- router: false,
239
- forms: false,
240
- runInstall: false,
241
- }),
242
- ),
243
- ).toThrow();
244
- });
245
- });
246
-
247
- describe("domain generator", () => {
248
- it("creates MainPage.tsx and routes.ts in src/domains/{name}/", () => {
249
- const result = dryRun(
250
- generators.domain.generate({ domainName: "billing" }),
251
- );
252
-
253
- const writeEffects = result.effects.filter((e) => e._tag === "WriteFile");
254
- const paths = writeEffects.map((e) => (e as { path: string }).path);
255
-
256
- expect(paths).toContain("src/domains/billing/MainPage.tsx");
257
- expect(paths).toContain("src/domains/billing/routes.ts");
258
- });
259
-
260
- it("creates a MakeDir effect for the domain directory", () => {
261
- const result = dryRun(
262
- generators.domain.generate({ domainName: "billing" }),
263
- );
264
-
265
- const mkdirEffects = result.effects.filter((e) => e._tag === "MakeDir");
266
- const paths = mkdirEffects.map((e) => (e as { path: string }).path);
267
-
268
- expect(paths).toContain("src/domains/billing");
269
- });
270
- });
271
-
272
- describe("route generator", () => {
273
- it("creates {Name}Page.tsx and transforms routes.ts", () => {
274
- const result = dryRunRoute("account", "settings");
275
-
276
- const writePaths = result.effects
277
- .filter((e) => e._tag === "WriteFile")
278
- .map((e) => (e as { path: string }).path);
279
- expect(writePaths).toContain("src/domains/account/SettingsPage.tsx");
280
-
281
- // The route is wired into routes.ts via a TransformFile (AST insert), not
282
- // an AppendFile of a TODO comment. The transform content is covered by
283
- // insertRoute.test.ts.
284
- const transformPaths = result.effects
285
- .filter((e) => e._tag === "TransformFile")
286
- .map((e) => (e as { path: string }).path);
287
- expect(transformPaths).toContain("src/domains/account/routes.ts");
288
- });
289
-
290
- it("includes correct content in the page component", () => {
291
- const result = dryRunRoute("account", "settings");
292
-
293
- const page = result.effects.find(
294
- (e) =>
295
- e._tag === "WriteFile" &&
296
- (e as { path: string }).path === "src/domains/account/SettingsPage.tsx",
297
- ) as { content: string } | undefined;
298
-
299
- expect(page).toBeDefined();
300
- expect(page?.content).toContain("export default function SettingsPage()");
301
- expect(page?.content).toContain('useHead({ title: "Settings" })');
302
- });
303
-
304
- it("wires the route via a reversible TransformFile (no TODO append)", () => {
305
- const result = dryRunRoute("account", "settings");
306
-
307
- // No AppendFile TODO stub any more.
308
- expect(result.effects.some((e) => e._tag === "AppendFile")).toBe(false);
309
-
310
- const transform = result.effects.find(
311
- (e) =>
312
- e._tag === "TransformFile" &&
313
- (e as { path: string }).path === "src/domains/account/routes.ts",
314
- ) as { transform: (s: string) => string; undo?: unknown } | undefined;
315
-
316
- expect(transform).toBeDefined();
317
- // It carries an undo (the inverse removeRoute transform).
318
- expect(transform?.undo).toBeDefined();
319
-
320
- // The forward transform actually inserts the import + route entry.
321
- const base = `import { route } from "@canonical/router-core";
322
- import MainPage from "./MainPage.js";
323
-
324
- const routes = {
325
- account: route({ url: "/account", content: MainPage }),
326
- } as const;
327
-
328
- export default routes;
329
- `;
330
- const out = transform?.transform(base) ?? "";
331
- expect(out).toContain('import SettingsPage from "./SettingsPage.js";');
332
- expect(out).toContain("settings: route({");
333
- expect(out).toContain('url: "/account/settings",');
334
- expect(out).toContain("content: SettingsPage,");
335
- });
336
-
337
- it("throws on single-segment path", () => {
338
- expect(() =>
339
- dryRun(generators.route.generate({ routePath: "settings" })),
340
- ).toThrow();
341
- });
342
-
343
- it("fails when the target domain does not exist", () => {
344
- // No domain created first → the guard rejects before writing anything.
345
- expect(() =>
346
- dryRun(generators.route.generate({ routePath: "missing/page" })),
347
- ).toThrow(/not found|Create it first/);
348
- });
349
- });
350
-
351
- describe("wrapper generator", () => {
352
- it("creates {Name}Layout.tsx and index.ts in src/lib/{Name}Layout/", () => {
353
- const result = dryRun(
354
- generators.wrapper.generate({ wrapperName: "settings" }),
355
- );
356
-
357
- const writeEffects = result.effects.filter((e) => e._tag === "WriteFile");
358
- const paths = writeEffects.map((e) => (e as { path: string }).path);
359
-
360
- expect(paths).toContain("src/lib/SettingsLayout/SettingsLayout.tsx");
361
- expect(paths).toContain("src/lib/SettingsLayout/index.ts");
362
- });
363
-
364
- it("generates correct layout content", () => {
365
- const result = dryRun(
366
- generators.wrapper.generate({ wrapperName: "settings" }),
367
- );
368
-
369
- const writeEffects = result.effects.filter((e) => e._tag === "WriteFile");
370
- const layout = writeEffects.find(
371
- (e) =>
372
- (e as { path: string }).path ===
373
- "src/lib/SettingsLayout/SettingsLayout.tsx",
374
- ) as { content: string } | undefined;
375
-
376
- expect(layout).toBeDefined();
377
- expect(layout?.content).toContain(
378
- "export default function SettingsLayout(",
379
- );
380
- expect(layout?.content).toContain('className="settings-layout"');
381
- });
382
-
383
- it("generates correct barrel export", () => {
384
- const result = dryRun(
385
- generators.wrapper.generate({ wrapperName: "settings" }),
386
- );
387
-
388
- const writeEffects = result.effects.filter((e) => e._tag === "WriteFile");
389
- const barrel = writeEffects.find(
390
- (e) => (e as { path: string }).path === "src/lib/SettingsLayout/index.ts",
391
- ) as { content: string } | undefined;
392
-
393
- expect(barrel).toBeDefined();
394
- expect(barrel?.content).toContain(
395
- 'export { default } from "./SettingsLayout.js"',
396
- );
397
- });
398
- });