@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
@@ -60,17 +60,48 @@ async function start() {
60
60
  const template = fs.readFileSync("index.html", "utf-8");
61
61
  const html = await vite.transformIndexHtml(url, template);
62
62
 
63
- const { default: EntryServer } = await vite.ssrLoadModule(
64
- "/src/server/entry.tsx",
65
- );
63
+ const {
64
+ default: EntryServer,
65
+ prefetchRouteData,
66
+ resolveRouteDisposition,
67
+ } = await vite.ssrLoadModule("/src/server/entry.tsx");
68
+
69
+ const disposition = resolveRouteDisposition(url);
70
+
71
+ if (disposition.kind === "redirect") {
72
+ res.redirect(disposition.status, disposition.location);
73
+ return;
74
+ }
75
+
76
+ // Fetch-then-render: run the matched route's declared server query so
77
+ // its captured responses ride the bootstrap script (fixed at stream
78
+ // start); absent or failed, the client fetches after hydration.
79
+ const relayPayloads = await prefetchRouteData(disposition);
66
80
  const { JSXRenderer } = await vite.ssrLoadModule(
67
81
  "@canonical/react-ssr/renderer",
68
82
  );
69
83
  const { extractPreferences } = await vite.ssrLoadModule(
70
84
  "@canonical/react-hooks",
71
85
  );
86
+ <% if (intl) { -%>
87
+ const { negotiateLocale } = await vite.ssrLoadModule(
88
+ "@canonical/i18n-core",
89
+ );
90
+ const { i18nConfig } = await vite.ssrLoadModule("/src/i18n/config.ts");
91
+ <% } -%>
72
92
 
93
+ <% if (intl) { -%>
94
+ const cookie = req.headers.cookie ?? null;
95
+ const { theme } = extractPreferences(cookie);
96
+ const locale = negotiateLocale(i18nConfig, {
97
+ cookieHeader: cookie,
98
+ acceptLanguage: Array.isArray(req.headers["accept-language"])
99
+ ? req.headers["accept-language"].join(",")
100
+ : (req.headers["accept-language"] ?? null),
101
+ });
102
+ <% } else { -%>
73
103
  const { theme } = extractPreferences(req.headers.cookie ?? null);
104
+ <% } -%>
74
105
  const renderer = new JSXRenderer(
75
106
  EntryServer,
76
107
  // The cookie is client-controlled, so only the known theme values reach
@@ -79,8 +110,21 @@ async function start() {
79
110
  {
80
111
  url,
81
112
  theme: theme === "light" || theme === "dark" ? theme : undefined,
113
+ <% if (intl) { -%>
114
+ locale,
115
+ <% } -%>
116
+ ...(relayPayloads ? { relayPayloads } : {}),
117
+ ...(disposition.dehydratedState ?? {}),
118
+ },
119
+ <% if (intl) { -%>
120
+ {
121
+ htmlString: html,
122
+ defaultLocale: locale,
123
+ statusCode: disposition.status,
82
124
  },
83
- { htmlString: html },
125
+ <% } else { -%>
126
+ { htmlString: html, statusCode: disposition.status },
127
+ <% } -%>
84
128
  );
85
129
  const result = renderer.renderToPipeableStream();
86
130
 
@@ -0,0 +1,197 @@
1
+ // @vitest-environment node
2
+
3
+ /**
4
+ * End-to-end test of the build: boot each of the 2×3 server scripts and assert
5
+ * it serves correctly over HTTP. The defects this guards against live at the
6
+ * HTTP/content-type layer (assets served as text/html; a 500 page), so
7
+ * fetch-level assertions catch them — no browser needed.
8
+ *
9
+ * Each cell runs its real package.json script — including the build that
10
+ * `preview:*` performs — so the test mirrors the actual user experience. The
11
+ * `dev*` cells boot in a second or two (tight readiness budget); the
12
+ * `preview*` cells build the client + compile the renderer first, so they get a
13
+ * larger budget. Kept out of the default `test` run via the `*.e2e.ts` name;
14
+ * invoke with `bun run test:e2e`.
15
+ */
16
+ import { describe, expect, it } from "vitest";
17
+ import { startServer } from "./serverHarness.js";
18
+
19
+ const CWD = process.cwd();
20
+
21
+ // Readiness budgets: dev* boots straight away; preview* builds the client +
22
+ // renderer before serving, so it needs a larger budget.
23
+ const DEV_READY_MS = 20_000;
24
+ const PREVIEW_READY_MS = 90_000;
25
+ const TEST_TIMEOUT_MS = PREVIEW_READY_MS + 30_000;
26
+
27
+ interface Cell {
28
+ /** package.json script to run. */
29
+ script: string;
30
+ /** Readiness budget for this cell's boot (and build, for preview*). */
31
+ timeoutMs: number;
32
+ /**
33
+ * Whether this cell server-renders. The plain `dev`/`preview` cells are the
34
+ * Vite SPA path (no SSR), so they have no `/sitemap.xml` route; the four
35
+ * `*:bun`/`*:express` cells render it from the sitemap renderer.
36
+ */
37
+ ssr: boolean;
38
+ }
39
+
40
+ const MATRIX: Cell[] = [
41
+ { script: "dev", timeoutMs: DEV_READY_MS, ssr: false },
42
+ { script: "dev:bun", timeoutMs: DEV_READY_MS, ssr: true },
43
+ { script: "dev:express", timeoutMs: DEV_READY_MS, ssr: true },
44
+ { script: "preview", timeoutMs: PREVIEW_READY_MS, ssr: false },
45
+ { script: "preview:bun", timeoutMs: PREVIEW_READY_MS, ssr: true },
46
+ { script: "preview:express", timeoutMs: PREVIEW_READY_MS, ssr: true },
47
+ ];
48
+
49
+ /** A JS/TS module or CSS-as-JS asset must never come back as the HTML page. */
50
+ const JS_CONTENT_TYPE = /javascript/;
51
+
52
+ describe("server matrix (2×3) serves correctly", () => {
53
+ for (const cell of MATRIX) {
54
+ it(
55
+ `${cell.script} serves an HTML page and JS assets (not HTML)`,
56
+ async () => {
57
+ const server = await startServer(cell.script, CWD, {
58
+ timeoutMs: cell.timeoutMs,
59
+ });
60
+ try {
61
+ // 1. The document renders.
62
+ const page = await fetch(`${server.base}/`);
63
+ expect(page.status).toBe(200);
64
+ expect(page.headers.get("content-type")).toMatch(/text\/html/);
65
+ const html = await page.text();
66
+ expect(html).toContain('id="root"');
67
+
68
+ // 2. The HTML references at least one client script.
69
+ const scriptSrc = html.match(/<script[^>]+src="([^"]+)"/)?.[1];
70
+ expect(
71
+ scriptSrc,
72
+ "page should reference a client script",
73
+ ).toBeTruthy();
74
+
75
+ // 3. That script is served as JavaScript — never the HTML page.
76
+ // (This is the exact defect: assets returned with a text/html MIME.)
77
+ const asset = await fetch(new URL(scriptSrc as string, server.base));
78
+ expect(asset.status).toBe(200);
79
+ expect(asset.headers.get("content-type")).toMatch(JS_CONTENT_TYPE);
80
+
81
+ // 4. SSR cells render /sitemap.xml as XML from the sitemap renderer —
82
+ // the second renderer, picked by path, never the HTML app. (The SPA
83
+ // dev/preview cells have no SSR route, so they are exempt.)
84
+ if (cell.ssr) {
85
+ const sitemap = await fetch(`${server.base}/sitemap.xml`);
86
+ expect(sitemap.status).toBe(200);
87
+ expect(sitemap.headers.get("content-type")).toMatch(/xml/);
88
+ const xml = await sitemap.text();
89
+ expect(xml).toContain("<urlset");
90
+ expect(xml).toContain("<loc>");
91
+ }
92
+
93
+ <% if (intl) { -%>
94
+ // 5. SSR cells negotiate the locale per request (i18n-core:
95
+ // cookie > Accept-Language > default) and render the translated
96
+ // document server-side: `<html lang dir>` plus the localized
97
+ // navigation chrome, before any client JavaScript runs.
98
+ if (cell.ssr) {
99
+ const french = await fetch(`${server.base}/`, {
100
+ headers: { cookie: "locale=fr" },
101
+ });
102
+ const frenchHtml = await french.text();
103
+ expect(frenchHtml).toContain('lang="fr"');
104
+ expect(frenchHtml).toContain('dir="ltr"');
105
+ expect(frenchHtml).toContain("Accueil");
106
+
107
+ const arabic = await fetch(`${server.base}/`, {
108
+ headers: { "accept-language": "ar-EG,ar;q=0.9,en;q=0.5" },
109
+ });
110
+ const arabicHtml = await arabic.text();
111
+ expect(arabicHtml).toContain('lang="ar"');
112
+ expect(arabicHtml).toContain('dir="rtl"');
113
+ expect(arabicHtml).toContain("الرئيسية");
114
+ }
115
+
116
+ <% } -%>
117
+ // SSR cells answer with the router's disposition: a matched
118
+ // not-found page is a real 404, static redirect routes and the
119
+ // auth guard are real HTTP redirects, and an authorized request
120
+ // renders 200. (The SPA cells stay 200-only: Vite's static
121
+ // fallback serves index.html for every route and cannot express
122
+ // router statuses.)
123
+ if (cell.ssr) {
124
+ const notFound = await fetch(
125
+ `${server.base}/definitely-not-a-page`,
126
+ );
127
+ expect(notFound.status).toBe(404);
128
+ const notFoundHtml = await notFound.text();
129
+ expect(notFoundHtml).toContain('id="root"');
130
+
131
+ const legacy = await fetch(`${server.base}/home`, {
132
+ redirect: "manual",
133
+ });
134
+ expect(legacy.status).toBe(301);
135
+ const legacyLocation = legacy.headers.get("location");
136
+ expect(legacyLocation, "301 must carry a Location").toBeTruthy();
137
+ expect(
138
+ new URL(legacyLocation as string, server.base).pathname,
139
+ ).toBe("/");
140
+
141
+ const guarded = await fetch(`${server.base}/account`, {
142
+ redirect: "manual",
143
+ });
144
+ expect(guarded.status).toBe(302);
145
+ const guardedHeader = guarded.headers.get("location");
146
+ expect(guardedHeader, "302 must carry a Location").toBeTruthy();
147
+ const guardedLocation = new URL(
148
+ guardedHeader as string,
149
+ server.base,
150
+ );
151
+ expect(guardedLocation.pathname).toBe("/login");
152
+ expect(guardedLocation.searchParams.get("from")).toBe("/account");
153
+
154
+ // The auth decision comes from the router's own match, so URL
155
+ // shapes the router normalizes differently from a raw-URL check
156
+ // must still redirect: a trailing slash, and duplicate auth
157
+ // values where the router keeps the last one.
158
+ const trailing = await fetch(`${server.base}/account/`, {
159
+ redirect: "manual",
160
+ });
161
+ expect(trailing.status).toBe(302);
162
+
163
+ const duplicated = await fetch(
164
+ `${server.base}/account?auth=1&auth=0`,
165
+ { redirect: "manual" },
166
+ );
167
+ expect(duplicated.status).toBe(302);
168
+
169
+ const authorized = await fetch(`${server.base}/account?auth=1`);
170
+ expect(authorized.status).toBe(200);
171
+ }
172
+
173
+ // SSR cells serialize Relay data across the boundary (when the app
174
+ // was scaffolded with --relay): the catalog page arrives with
175
+ // server-rendered product markup and carries the captured operation
176
+ // payloads for the client to replay. Without --relay there is no
177
+ // /catalog route (404), so the assertions self-disable — this file
178
+ // is a plain copy shared by every flag combination.
179
+ if (cell.ssr) {
180
+ const catalog = await fetch(`${server.base}/catalog`);
181
+ if (catalog.status === 200) {
182
+ const catalogHtml = await catalog.text();
183
+ expect(catalogHtml).toContain("Vanguard Workstation");
184
+ expect(catalogHtml).toContain("relayPayloads");
185
+ }
186
+
187
+ const home = await fetch(`${server.base}/`);
188
+ expect(await home.text()).not.toContain("relayPayloads");
189
+ }
190
+ } finally {
191
+ await server.stop();
192
+ }
193
+ },
194
+ TEST_TIMEOUT_MS,
195
+ );
196
+ }
197
+ });
@@ -15,6 +15,9 @@
15
15
  "paths": {
16
16
  "#lib/*": ["./lib/*"],
17
17
  "#domains/*": ["./domains/*"],
18
+ <% if (intl) { -%>
19
+ "#i18n/*": ["./i18n/*"],
20
+ <% } -%>
18
21
  <% if (relay) { -%>
19
22
  "#relay/*": ["./relay/*"],
20
23
  <% } -%>
@@ -25,11 +25,16 @@ export default defineConfig(({ mode }) => ({
25
25
  // `@rolldown/plugin-babel` escape hatch; `vite-plugin-relay-lite` does the
26
26
  // same rewrite with its own parser and no Babel, and works on Vite 8 /
27
27
  // rolldown despite its peer range stopping at Vite 7 (bun installs it with
28
+ <% if (intl) { -%>
29
+ // only a peer-version warning). `codegen: false` because artifacts are
30
+ // committed and regenerated explicitly via `bun run relay` / `relay:watch`.
31
+ <% } else { -%>
28
32
  // only a peer-version warning). Footgun: its tag scanner matches the word
29
33
  // "graphql" followed by a backtick even inside comments, so never
30
34
  // backtick-quote a graphql file name in a comment. `codegen: false` because
31
35
  // artifacts are committed and regenerated explicitly via the `relay` /
32
36
  // `relay:watch` scripts.
37
+ <% } -%>
33
38
  plugins: [relay({ codegen: false }), react()],
34
39
  <% } else { -%>
35
40
  plugins: [react()],
@@ -2,6 +2,8 @@ import * as path from "node:path";
2
2
  import { exists, fail, flatMap, info, mkdir, sequence_, writeFile, } from "@canonical/task";
3
3
  import { toCamelCase, toTitleCase } from "@canonical/utils";
4
4
  import { normalizeCommandPath } from "../shared/casing.js";
5
+ import { packageVersion } from "../shared/packageVersion.js";
6
+ import { validateCommandPath } from "../shared/validators.js";
5
7
  import { printVersions } from "../shared/versions.js";
6
8
  const prompts = [
7
9
  {
@@ -10,6 +12,11 @@ const prompts = [
10
12
  message: "Domain name (for example billing):",
11
13
  default: "example",
12
14
  positional: true,
15
+ validate: validateCommandPath({
16
+ label: "Domain name",
17
+ maxSegments: 1,
18
+ example: "billing",
19
+ }),
13
20
  group: "Domain",
14
21
  },
15
22
  ];
@@ -50,7 +57,7 @@ export const generator = {
50
57
  name: "domain",
51
58
  displayName: "@canonical/summon-application:domain",
52
59
  description: "Create a domain folder with routes and a MainPage",
53
- version: "0.1.0",
60
+ version: packageVersion(),
54
61
  help: `Creates a domain directory under src/domains/ with:
55
62
  - MainPage.tsx — example page component with useHead()
56
63
  - routes.ts — route barrel exporting the domain's routes
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/domain/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAKlC,OAAO,EACL,MAAM,EACN,IAAI,EACJ,OAAO,EACP,IAAI,EACJ,KAAK,EACL,SAAS,EACT,SAAS,GACV,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC5D,OAAO,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAC3D,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAMtD,MAAM,OAAO,GAAuB;IAClC;QACE,IAAI,EAAE,YAAY;QAClB,IAAI,EAAE,MAAM;QACZ,OAAO,EAAE,oCAAoC;QAC7C,OAAO,EAAE,SAAS;QAClB,UAAU,EAAE,IAAI;QAChB,KAAK,EAAE,QAAQ;KAChB;CACF,CAAC;AAEF,SAAS,aAAa,CAAC,UAAkB;IACvC,MAAM,KAAK,GAAG,WAAW,CAAC,UAAU,CAAC,CAAC;IAEtC,OAAO;;;;sBAIa,KAAK;;;;4BAIC,KAAK;yCACQ,UAAU;;;;CAIlD,CAAC;AACF,CAAC;AAED,SAAS,eAAe,CAAC,UAAkB;IACzC,MAAM,QAAQ,GAAG,IAAI,oBAAoB,CAAC,UAAU,CAAC,EAAE,CAAC;IAExD,OAAO;;;;IAIL,WAAW,CAAC,UAAU,CAAC;YACf,QAA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equence_, transformFile, writeFile, } from "@canonical/task";
3
3
  import { toCamelCase, toPascalCase, toTitleCase } from "@canonical/utils";
4
4
  import { normalizeCommandPath } from "../shared/casing.js";
5
+ import { packageVersion } from "../shared/packageVersion.js";
6
+ import { validateCommandPath } from "../shared/validators.js";
5
7
  import { insertRoute, removeRoute } from "./insertRoute.js";
6
8
  const prompts = [
7
9
  {
@@ -10,6 +12,11 @@ const prompts = [
10
12
  message: "Route path (for example account/settings):",
11
13
  default: "example/page",
12
14
  positional: true,
15
+ validate: validateCommandPath({
16
+ label: "Route path",
17
+ minSegments: 2,
18
+ example: "account/settings",
19
+ }),
13
20
  group: "Route",
14
21
  },
15
22
  ];
@@ -36,7 +43,7 @@ export const generator = {
36
43
  name: "route",
37
44
  displayName: "@canonical/summon-application:route",
38
45
  description: "Add a route page to an existing domain",
39
- version: "0.1.0",
46
+ version: packageVersion(),
40
47
  help: `Creates a page component inside an existing domain directory.
41
48
 
42
49
  Given a path like "account/settings":
@@ -65,7 +72,10 @@ route key already existed) would remove a route you already had.`,
65
72
  const normalized = normalizeCommandPath(answers.routePath);
66
73
  const segments = normalized.split("/");
67
74
  if (segments.length < 2) {
68
- throw new Error(`Route path must include a domain and route name (e.g. "account/settings"), got "${normalized}"`);
75
+ return fail({
76
+ code: "ROUTE_PATH_INVALID",
77
+ message: `Route path must include a domain and route name (e.g. "account/settings"), got "${normalized}"`,
78
+ });
69
79
  }
70
80
  const domainName = segments[0];
71
81
  const routeName = segments[segments.length - 1];
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/route/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAKlC,OAAO,EACL,MAAM,EACN,IAAI,EACJ,OAAO,EACP,IAAI,EACJ,SAAS,EACT,aAAa,EACb,SAAS,GACV,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,kBAAkB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AM,CAAC,UAAU,CAAC,EAAE,CAAC,aAAa,EAAE,EAAE,CACnD,CAAC,aAAa;YACZ,CAAC,CAAC,IAAI,CAAC;gBACH,IAAI,EAAE,sBAAsB;gBAC5B,OAAO,EAAE,WAAW,UAAU,gBAAgB,UAAU,kDAAkD,UAAU,EAAE;aACvH,CAAC;YACJ,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,CAAC,WAAW,EAAE,EAAE,CACxC,WAAW;gBACT,CAAC,CAAC,IAAI,CAAC;oBACH,IAAI,EAAE,mBAAmB;oBACzB,OAAO,EAAE,SAAS,QAAQ,2EAA2E;iBACtG,CAAC;gBACJ,CAAC,CAAC,QAAQ,CACb,CACN,CAAC;IACJ,CAAC;CACF,CAAC;AAEF,eAAe,SAAS,CAAC"}
@@ -132,7 +132,7 @@ export function removeRoute(source, ins) {
132
132
  (ts.isIdentifier(p.name) || ts.isStringLiteral(p.name)) &&
133
133
  p.name.text === ins.routeKey);
134
134
  if (prop) {
135
- result = removeFullLines(result, prop.getStart(sf), prop.end);
135
+ result = removePropertySpan(result, prop.getStart(sf), prop.end);
136
136
  }
137
137
  }
138
138
  }
@@ -147,24 +147,52 @@ export function removeRoute(source, ins) {
147
147
  // it (`import Page, { x } from "..."`), leave it — removing the whole line
148
148
  // would discard their bindings.
149
149
  !stmt.importClause.namedBindings) {
150
- result = removeFullLines(result, stmt.getStart(sf2), stmt.end);
150
+ result = removeLinesOrSpan(result, stmt.getStart(sf2), stmt.end);
151
151
  break;
152
152
  }
153
153
  }
154
154
  return result;
155
155
  }
156
156
  /**
157
- * Remove the whole-line span covering [start, end), including the node's
158
- * leading indentation and its trailing newline, so no blank line is left behind.
157
+ * Remove a property node's span [start, end) plus its separator comma: the
158
+ * trailing comma when one follows, otherwise the preceding one (the
159
+ * last-property case), so the object literal stays syntactically valid.
159
160
  */
160
- function removeFullLines(source, start, end) {
161
+ function removePropertySpan(source, start, end) {
162
+ let spanStart = start;
163
+ let spanEnd = end;
164
+ let after = spanEnd;
165
+ while (after < source.length && /\s/.test(source.charAt(after)))
166
+ after++;
167
+ if (source.charAt(after) === ",") {
168
+ spanEnd = after + 1;
169
+ }
170
+ else {
171
+ let before = spanStart - 1;
172
+ while (before >= 0 && /\s/.test(source.charAt(before)))
173
+ before--;
174
+ if (source.charAt(before) === ",")
175
+ spanStart = before;
176
+ }
177
+ return removeLinesOrSpan(source, spanStart, spanEnd);
178
+ }
179
+ /**
180
+ * Remove [start, end): as whole lines (including leading indentation and the
181
+ * trailing newline, so no blank line is left behind) when the span owns its
182
+ * lines — but as an exact span when other code shares a line with it, so a
183
+ * single-line `const routes = { billing: route(...) } as const;` loses only
184
+ * the property, never the code around it.
185
+ */
186
+ function removeLinesOrSpan(source, start, end) {
161
187
  const lineStart = source.lastIndexOf("\n", start - 1) + 1;
162
- let lineEnd = source.indexOf("\n", end);
163
- if (lineEnd === -1)
164
- lineEnd = source.length;
165
- else
166
- lineEnd += 1; // include the newline
167
- return source.slice(0, lineStart) + source.slice(lineEnd);
188
+ const newlineAfter = source.indexOf("\n", end);
189
+ const lineEnd = newlineAfter === -1 ? source.length : newlineAfter + 1;
190
+ const leading = source.slice(lineStart, start);
191
+ const trailing = source.slice(end, newlineAfter === -1 ? source.length : newlineAfter);
192
+ if (/^\s*$/.test(leading) && /^\s*$/.test(trailing)) {
193
+ return source.slice(0, lineStart) + source.slice(lineEnd);
194
+ }
195
+ return source.slice(0, start) + source.slice(end);
168
196
  }
169
197
  /** Detect the indentation used by the first property of the object literal. */
170
198
  function detectIndent(source, obj) {
@@ -1 +1 @@
1
- {"version":3,"file":"insertRoute.js","sourceRoot":"","sources":["../../../src/route/insertRoute.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,YAAY,CAAC;AAE5B;;;GAGG;AACH,SAAS,mBAAmB,CAC1B,IAAmB;IAEnB,IAAI,IAAI,GAAkB,IAAI,CAAC;IAC/B,OACE,EAAE,CAAC,cAAc,CAAC,IAAI,CA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AC,EAAE,CAAC;QACpD,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,SAAS,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IAC5D,CAAC;IACD,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;AACpD,CAAC;AAED,+EAA+E;AAC/E,SAAS,YAAY,CACnB,MAAc,EACd,GAA+B;IAE/B,MAAM,KAAK,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;IAChC,IAAI,CAAC,KAAK;QAAE,OAAO,SAAS,CAAC;IAC7B,MAAM,SAAS,GAAG,MAAM,CAAC,WAAW,CAAC,IAAI,EAAE,KAAK,CAAC,QAAQ,EAAE,CAAC,GAAG,CAAC,CAAC;IACjE,MAAM,EAAE,GAAG,MAAM,CAAC,KAAK,CAAC,SAAS,EAAE,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC;IACrD,OAAO,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC;AAC3C,CAAC"}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * The published package name, used in generator `displayName` labels and the
3
+ * version walk-up. A constant rather than a `package.json` JSON import: the
4
+ * import path would resolve differently from `src/` and `dist/esm/`.
5
+ */
6
+ export const PACKAGE_NAME = "@canonical/summon-application";
7
+ //# sourceMappingURL=packageName.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"packageName.js","sourceRoot":"","sources":["../../../src/shared/packageName.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,+BAA+B,CAAC"}
@@ -0,0 +1,50 @@
1
+ import { readFileSync } from "node:fs";
2
+ import * as path from "node:path";
3
+ import { fileURLToPath } from "node:url";
4
+ import { PACKAGE_NAME } from "./packageName.js";
5
+ /**
6
+ * Walk up from `from` until a `package.json` names this package, and return
7
+ * its `version`. Exported for tests; production callers use
8
+ * {@link packageVersion}.
9
+ *
10
+ * A manifest that is unreadable, is not JSON, names another package, or
11
+ * carries no version is not a failure — it is an ancestor directory that
12
+ * happens to have a `package.json` (the workspace root, say), so the walk
13
+ * continues. Only running out of parents throws.
14
+ *
15
+ * @note Impure — reads the filesystem.
16
+ */
17
+ export function findOwnVersion(from) {
18
+ let dir = from;
19
+ for (;;) {
20
+ try {
21
+ const manifest = JSON.parse(readFileSync(path.join(dir, "package.json"), "utf-8"));
22
+ if (manifest.name === PACKAGE_NAME && manifest.version) {
23
+ return manifest.version;
24
+ }
25
+ }
26
+ catch {
27
+ // No readable manifest at this level — keep walking.
28
+ }
29
+ const parent = path.dirname(dir);
30
+ if (parent === dir) {
31
+ throw new Error(`packageVersion: no package.json naming ${PACKAGE_NAME} above ${from}`);
32
+ }
33
+ dir = parent;
34
+ }
35
+ }
36
+ /**
37
+ * This package's own published version, used as the generators' meta.version
38
+ * — which is also the stamp version core writes into every generated file.
39
+ * The metas previously froze "0.1.0", so stamps could never distinguish
40
+ * releases. Same shape as summon-package's packageVersion: not a JSON import
41
+ * (build-layout coupling), lazy and cached (no IO at module load).
42
+ *
43
+ * @note Impure on first call — reads the filesystem, then caches.
44
+ */
45
+ let cached;
46
+ export function packageVersion() {
47
+ cached ??= findOwnVersion(path.dirname(fileURLToPath(import.meta.url)));
48
+ return cached;
49
+ }
50
+ //# sourceMappingURL=packageVersion.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"packageVersion.js","sourceRoot":"","sources":["../../../src/shared/packageVersion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAEhD;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,cAAc,CAAC,IAAY;IACzC,IAAI,GAAG,GAAG,IAAI,CAAC;IACf,SAAS,CAAC;QACR,IAAI,CAAC;YACH,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,CACzB,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,cAAc,CAAC,EAAE,OAAO,CAAC,CACf,CAAC;YACzC,IAAI,QAAQ,CAAC,IAAI,KAAK,YAAY,IAAI,QAAQ,CAAC,OAAO,EAAE,CAAC;gBACvD,OAAO,QAAQ,CAAC,OAAO,CAAC;YAC1B,CAAC;QACH,CAAC;QAAC,MAAM,CAAC;YACP,qDAAqD;QACvD,CAAC;QACD,MAAM,MAAM,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QACjC,IAAI,MAAM,KAAK,GAAG,EAAE,CAAC;YACnB,MAAM,IAAI,KAAK,CACb,0CAA0C,YAAY,UAAU,IAAI,EAAE,CACvE,CAAC;QACJ,CAAC;QACD,GAAG,GAAG,MAAM,CAAC;IACf,CAAC;AACH,CAAC;AAED;;;;;;;;GAQG;AACH,IAAI,MAA0B,CAAC;AAC/B,MAAM,UAAU,cAAc;IAC5B,MAAM,KAAK,cAAc,CAAC,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;IACxE,OAAO,MAAM,CAAC;AAChB,CAAC"}
@@ -0,0 +1,61 @@
1
+ import { normalizeCommandPath } from "./casing.js";
2
+ /**
3
+ * A path segment that survives the casing helpers as a valid JS identifier:
4
+ * it must start with a letter (a leading digit would emit code like
5
+ * `function 2faPage()`), and stay within letters/digits/hyphens so
6
+ * toPascalCase/toCamelCase have something well-formed to work on.
7
+ */
8
+ const SEGMENT = /^[A-Za-z][A-Za-z0-9-]*$/;
9
+ /**
10
+ * Build a `PromptDefinition.validate` for a slash-separated command path
11
+ * (`billing`, `account/settings`). Returns `true | string` per the prompt
12
+ * contract; never throws.
13
+ */
14
+ export function validateCommandPath(options) {
15
+ const { label, minSegments = 1, maxSegments, example } = options;
16
+ return (value) => {
17
+ if (typeof value !== "string" || value.trim() === "") {
18
+ return `${label} is required (for example ${example})`;
19
+ }
20
+ const normalized = normalizeCommandPath(value);
21
+ if (normalized === "") {
22
+ return `${label} is required (for example ${example})`;
23
+ }
24
+ const segments = normalized.split("/");
25
+ if (segments.length < minSegments) {
26
+ return `${label} needs at least ${minSegments} segments (for example ${example})`;
27
+ }
28
+ if (maxSegments !== undefined && segments.length > maxSegments) {
29
+ return `${label} must be a single name without slashes (for example ${example})`;
30
+ }
31
+ for (const segment of segments) {
32
+ if (!SEGMENT.test(segment)) {
33
+ return (`${label} segment "${segment}" must start with a letter and use ` +
34
+ `only letters, digits, and hyphens (for example ${example})`);
35
+ }
36
+ }
37
+ return true;
38
+ };
39
+ }
40
+ /**
41
+ * Validate the application directory path: non-empty, relative, and free of
42
+ * `..` traversal — the generator writes an entire tree under it.
43
+ */
44
+ export function validateAppPath(value) {
45
+ if (typeof value !== "string" || value.trim() === "") {
46
+ return "Application directory is required (for example my-app)";
47
+ }
48
+ const trimmed = value.trim();
49
+ if (trimmed.startsWith("/") || /^[A-Za-z]:[\\/]/.test(trimmed)) {
50
+ return "Application directory must be a relative path, not absolute";
51
+ }
52
+ const segments = trimmed.replace(/\\/g, "/").split("/");
53
+ if (segments.some((segment) => segment === "..")) {
54
+ return 'Application directory must not contain ".."';
55
+ }
56
+ if (segments.some((segment) => segment === "" || segment === ".")) {
57
+ return "Application directory must not contain empty or '.' segments";
58
+ }
59
+ return true;
60
+ }
61
+ //# sourceMappingURL=validators.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validators.js","sourceRoot":"","sources":["../../../src/shared/validators.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAEnD;;;;;GAKG;AACH,MAAM,OAAO,GAAG,yBAAyB,CA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