@kosdev-code/kos-ddk-cli 0.0.0-stage → 0.1.0-dev.105

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 (124) hide show
  1. package/README.md +181 -2
  2. package/agent-plugin/.claude-plugin/marketplace.json +13 -0
  3. package/agent-plugin/kos-ddk-tools/.claude-plugin/plugin.json +8 -0
  4. package/agent-plugin/kos-ddk-tools/.mcp.json +11 -0
  5. package/agent-plugin/kos-ddk-tools/agents/kos-ddk-plugin-planner.md +45 -0
  6. package/agent-plugin/kos-ddk-tools/agents/kos-ddk-plugin-reviewer.md +43 -0
  7. package/agent-plugin/kos-ddk-tools/catalog/contributions.json +921 -0
  8. package/agent-plugin/kos-ddk-tools/mcp.json +12 -0
  9. package/agent-plugin/kos-ddk-tools/plugin.json +7 -0
  10. package/agent-plugin/kos-ddk-tools/reference/reference.json +21141 -0
  11. package/agent-plugin/kos-ddk-tools/servers/kos-ddk.mjs +48656 -0
  12. package/agent-plugin/kos-ddk-tools/skills/build-ddk-plugin/SKILL.md +110 -0
  13. package/agent-plugin/kos-ddk-tools/skills/ddk-config-page/SKILL.md +130 -0
  14. package/agent-plugin/kos-ddk-tools/skills/ddk-content-component/SKILL.md +59 -0
  15. package/agent-plugin/kos-ddk-tools/skills/ddk-storybook/SKILL.md +45 -0
  16. package/agent-plugin/kos-ddk-tools/templates/cui/__nameDashCase__.stories.tsx__template__ +32 -0
  17. package/agent-plugin/kos-ddk-tools/templates/cui/__nameDashCase__.tsx__template__ +35 -0
  18. package/agent-plugin/kos-ddk-tools/templates/cui/index.ts__template__ +1 -0
  19. package/agent-plugin/kos-ddk-tools/templates/dashboard-detail/detail/__nameDashCase__.stories.tsx__template__ +34 -0
  20. package/agent-plugin/kos-ddk-tools/templates/dashboard-detail/detail/__nameDashCase__.tsx__template__ +52 -0
  21. package/agent-plugin/kos-ddk-tools/templates/dashboard-detail/detail/index.ts__template__ +1 -0
  22. package/agent-plugin/kos-ddk-tools/templates/dashboard-region/__nameDashCase__.stories.tsx__template__ +38 -0
  23. package/agent-plugin/kos-ddk-tools/templates/dashboard-region/__nameDashCase__.tsx__template__ +28 -0
  24. package/agent-plugin/kos-ddk-tools/templates/dashboard-region/index.ts__template__ +1 -0
  25. package/agent-plugin/kos-ddk-tools/templates/dashboard-summary/summary/__nameDashCase__.stories.tsx__template__ +34 -0
  26. package/agent-plugin/kos-ddk-tools/templates/dashboard-summary/summary/__nameDashCase__.tsx__template__ +41 -0
  27. package/agent-plugin/kos-ddk-tools/templates/dashboard-summary/summary/index.ts__template__ +1 -0
  28. package/agent-plugin/kos-ddk-tools/templates/macro-summary/__nameDashCase__.stories.tsx__template__ +34 -0
  29. package/agent-plugin/kos-ddk-tools/templates/macro-summary/__nameDashCase__.tsx__template__ +24 -0
  30. package/agent-plugin/kos-ddk-tools/templates/macro-summary/index.ts__template__ +1 -0
  31. package/agent-plugin/kos-ddk-tools/templates/nav/__nameDashCase__.stories.tsx__template__ +32 -0
  32. package/agent-plugin/kos-ddk-tools/templates/nav/__nameDashCase__.tsx__template__ +27 -0
  33. package/agent-plugin/kos-ddk-tools/templates/nav/index.ts__template__ +1 -0
  34. package/agent-plugin/kos-ddk-tools/templates/plugin-project/.storybook/preview.tsx.template +113 -0
  35. package/agent-plugin/kos-ddk-tools/templates/plugin-project/.storybook/registration.ts.template +6 -0
  36. package/agent-plugin/kos-ddk-tools/templates/pump-detail/__nameDashCase__.stories.tsx__template__ +34 -0
  37. package/agent-plugin/kos-ddk-tools/templates/pump-detail/__nameDashCase__.tsx__template__ +38 -0
  38. package/agent-plugin/kos-ddk-tools/templates/pump-detail/index.ts__template__ +1 -0
  39. package/agent-plugin/kos-ddk-tools/templates/pump-summary/__nameDashCase__.stories.tsx__template__ +34 -0
  40. package/agent-plugin/kos-ddk-tools/templates/pump-summary/__nameDashCase__.tsx__template__ +25 -0
  41. package/agent-plugin/kos-ddk-tools/templates/pump-summary/index.ts__template__ +1 -0
  42. package/agent-plugin/kos-ddk-tools/templates/setup/__nameDashCase__.stories.tsx__template__ +32 -0
  43. package/agent-plugin/kos-ddk-tools/templates/setup/__nameDashCase__.tsx__template__ +33 -0
  44. package/agent-plugin/kos-ddk-tools/templates/setup/index.ts__template__ +1 -0
  45. package/agent-plugin/kos-ddk-tools/templates/theme-project/theme/assets/component.css.template +2 -0
  46. package/agent-plugin/kos-ddk-tools/templates/theme-project/theme/assets/generic.css.template +2 -0
  47. package/agent-plugin/kos-ddk-tools/templates/theme-project/theme/assets/semantic.css.template +2 -0
  48. package/agent-plugin/kos-ddk-tools/templates/theme-project/theme/assets/theme-overlay.css.template +7 -0
  49. package/agent-plugin/kos-ddk-tools/templates/trouble-action/__nameDashCase__.stories.tsx__template__ +31 -0
  50. package/agent-plugin/kos-ddk-tools/templates/trouble-action/__nameDashCase__.tsx__template__ +57 -0
  51. package/agent-plugin/kos-ddk-tools/templates/trouble-action/index.ts__template__ +1 -0
  52. package/agent-plugin/kos-ddk-tools/templates/utility/__nameDashCase__.stories.tsx__template__ +60 -0
  53. package/agent-plugin/kos-ddk-tools/templates/utility/__nameDashCase__.tsx__template__ +88 -0
  54. package/agent-plugin/kos-ddk-tools/templates/utility/index.ts__template__ +1 -0
  55. package/catalog/contributions.json +921 -0
  56. package/guides/config-property-cards.md +77 -0
  57. package/guides/ddk-dev.md +59 -0
  58. package/guides/getting-started.md +126 -0
  59. package/guides/storybook.md +127 -0
  60. package/package.json +33 -3
  61. package/reference/reference.json +21141 -0
  62. package/src/build-paths.mjs +20 -0
  63. package/src/codegen/contribution.mjs +530 -0
  64. package/src/codegen/descriptor.mjs +422 -0
  65. package/src/codegen/json-preserve.mjs +137 -0
  66. package/src/codegen/ncui-descriptor.mjs +167 -0
  67. package/src/codegen/overrides.mjs +263 -0
  68. package/src/codegen/project.mjs +271 -0
  69. package/src/extension.mjs +32 -0
  70. package/src/generators/agent-install.mjs +80 -0
  71. package/src/generators/catalog.mjs +81 -0
  72. package/src/generators/contributions.mjs +164 -0
  73. package/src/generators/dashboard.mjs +282 -0
  74. package/src/generators/descriptor-entries.mjs +53 -0
  75. package/src/generators/descriptor-project.mjs +84 -0
  76. package/src/generators/dev.mjs +231 -0
  77. package/src/generators/override-prompts.mjs +27 -0
  78. package/src/generators/plugin-project.mjs +71 -0
  79. package/src/generators/theme-project.mjs +53 -0
  80. package/src/handlers.mjs +335 -0
  81. package/src/mcp/pkg-info.mjs +7 -0
  82. package/src/mcp/plugin-inspect.mjs +328 -0
  83. package/src/mcp/reference.mjs +315 -0
  84. package/src/mcp/server.mjs +12 -0
  85. package/src/mcp/tools.mjs +619 -0
  86. package/templates/cui/__nameDashCase__.stories.tsx__template__ +32 -0
  87. package/templates/cui/__nameDashCase__.tsx__template__ +35 -0
  88. package/templates/cui/index.ts__template__ +1 -0
  89. package/templates/dashboard-detail/detail/__nameDashCase__.stories.tsx__template__ +34 -0
  90. package/templates/dashboard-detail/detail/__nameDashCase__.tsx__template__ +52 -0
  91. package/templates/dashboard-detail/detail/index.ts__template__ +1 -0
  92. package/templates/dashboard-region/__nameDashCase__.stories.tsx__template__ +38 -0
  93. package/templates/dashboard-region/__nameDashCase__.tsx__template__ +28 -0
  94. package/templates/dashboard-region/index.ts__template__ +1 -0
  95. package/templates/dashboard-summary/summary/__nameDashCase__.stories.tsx__template__ +34 -0
  96. package/templates/dashboard-summary/summary/__nameDashCase__.tsx__template__ +41 -0
  97. package/templates/dashboard-summary/summary/index.ts__template__ +1 -0
  98. package/templates/macro-summary/__nameDashCase__.stories.tsx__template__ +34 -0
  99. package/templates/macro-summary/__nameDashCase__.tsx__template__ +24 -0
  100. package/templates/macro-summary/index.ts__template__ +1 -0
  101. package/templates/nav/__nameDashCase__.stories.tsx__template__ +32 -0
  102. package/templates/nav/__nameDashCase__.tsx__template__ +27 -0
  103. package/templates/nav/index.ts__template__ +1 -0
  104. package/templates/plugin-project/.storybook/preview.tsx.template +113 -0
  105. package/templates/plugin-project/.storybook/registration.ts.template +6 -0
  106. package/templates/pump-detail/__nameDashCase__.stories.tsx__template__ +34 -0
  107. package/templates/pump-detail/__nameDashCase__.tsx__template__ +38 -0
  108. package/templates/pump-detail/index.ts__template__ +1 -0
  109. package/templates/pump-summary/__nameDashCase__.stories.tsx__template__ +34 -0
  110. package/templates/pump-summary/__nameDashCase__.tsx__template__ +25 -0
  111. package/templates/pump-summary/index.ts__template__ +1 -0
  112. package/templates/setup/__nameDashCase__.stories.tsx__template__ +32 -0
  113. package/templates/setup/__nameDashCase__.tsx__template__ +33 -0
  114. package/templates/setup/index.ts__template__ +1 -0
  115. package/templates/theme-project/theme/assets/component.css.template +2 -0
  116. package/templates/theme-project/theme/assets/generic.css.template +2 -0
  117. package/templates/theme-project/theme/assets/semantic.css.template +2 -0
  118. package/templates/theme-project/theme/assets/theme-overlay.css.template +7 -0
  119. package/templates/trouble-action/__nameDashCase__.stories.tsx__template__ +31 -0
  120. package/templates/trouble-action/__nameDashCase__.tsx__template__ +57 -0
  121. package/templates/trouble-action/index.ts__template__ +1 -0
  122. package/templates/utility/__nameDashCase__.stories.tsx__template__ +60 -0
  123. package/templates/utility/__nameDashCase__.tsx__template__ +88 -0
  124. package/templates/utility/index.ts__template__ +1 -0
@@ -0,0 +1,422 @@
1
+ // NCUI descriptor projects: a ddk.ncui.descriptor KAB whose descriptor.json (its
2
+ // .kos.json) carries `kondra.ncui`. The DDK app uses an installed one in place of the
3
+ // system app's descriptor, so a plugin's new utility, setup step or navigation slot
4
+ // is listed there.
5
+ import path from "node:path";
6
+ import {
7
+ discoverProjects,
8
+ findProjectByName,
9
+ } from "@kosdev-code/kos-codegen-core";
10
+ import {
11
+ NCUI_LAYOUTS,
12
+ NCUI_OPTIONS,
13
+ buildNcuiDescriptor,
14
+ } from "./ncui-descriptor.mjs";
15
+ import { stringifyPreserving } from "./json-preserve.mjs";
16
+
17
+ export const DESCRIPTOR_KAB_TYPE = "ddk.ncui.descriptor";
18
+
19
+ /** The workspace's descriptor projects: {name, kosJsonPath} relative to the root. */
20
+ export function findDescriptorProjects(codegenFs) {
21
+ return [...discoverProjects(codegenFs.root).values()]
22
+ .map((project) => ({
23
+ name: project.name,
24
+ kosJsonPath: path.join(project.root, ".kos.json"),
25
+ }))
26
+ .filter(({ kosJsonPath }) => {
27
+ const raw = codegenFs.read(kosJsonPath);
28
+ return raw && JSON.parse(raw).type === DESCRIPTOR_KAB_TYPE;
29
+ });
30
+ }
31
+
32
+ function descriptorProject(codegenFs, name) {
33
+ const projects = findDescriptorProjects(codegenFs);
34
+ const project = projects.find((p) => p.name === name);
35
+ if (!project) {
36
+ throw new Error(
37
+ `No descriptor project "${name}". ${
38
+ projects.length
39
+ ? `Descriptor projects: ${projects.map((p) => p.name).join(", ")}.`
40
+ : "Create one with kosui ddk:descriptor-project."
41
+ }`
42
+ );
43
+ }
44
+ return project;
45
+ }
46
+
47
+ /**
48
+ * The KosMock setup module that serves a descriptor project without deploying it:
49
+ * `fixtures/setups/<name>.mjs`, relative to the workspace root, for
50
+ * `kosui fixtures serve`. Regenerated whenever the descriptor project changes.
51
+ */
52
+ export function descriptorMockSetup(kosJson) {
53
+ const { name, kondra } = kosJson;
54
+ return {
55
+ path: path.join("fixtures", "setups", `${name}.mjs`),
56
+ content: `// Generated from the descriptor project ${name}; regenerated whenever a ddk
57
+ // command lists an entry there, so put other mocking in a setup of its own.
58
+ // Serves ${name} as the installed NCUI descriptor without deploying it: run
59
+ // \`kosui fixtures serve\` and open the DDK app with
60
+ // ?kosMock=on&kosFixtureServer&kosSetup=${name}
61
+ const descriptor = ${JSON.stringify({ kondra }, null, 2)};
62
+
63
+ export default (kosMock) => {
64
+ if (!kosMock.ddk) {
65
+ throw new Error("kosMock.ddk is missing: it needs a DDK and kos-ui-sdk that provide it");
66
+ }
67
+ return kosMock.ddk.useDescriptor(descriptor);
68
+ };
69
+ `,
70
+ };
71
+ }
72
+
73
+ const parseRoles = (roles) =>
74
+ (Array.isArray(roles) ? roles : String(roles ?? "").split(","))
75
+ .map((role) => role.trim())
76
+ .filter(Boolean);
77
+
78
+ /**
79
+ * Lists a slot in a descriptor project's `kondra.ncui`, unless it already is.
80
+ * Returns the note for the command's report.
81
+ *
82
+ * @param {"utility" | "setup" | "nav"} type
83
+ * @param {{project: string, slot: string, roles?: string | string[], column?: number | string}} options
84
+ * `column` (1-based, default the last) is for utilities; `roles` default to everyone
85
+ * for a utility and to the parent's roles for a navigation view.
86
+ */
87
+ export function addDescriptorEntry(
88
+ codegenFs,
89
+ type,
90
+ { project, slot, roles, column }
91
+ ) {
92
+ const { name, kosJsonPath } = descriptorProject(codegenFs, project);
93
+ const original = codegenFs.read(kosJsonPath);
94
+ const kosJson = JSON.parse(original);
95
+ const ncui = ((kosJson.kondra ??= {}).ncui ??= {});
96
+ const roleList = parseRoles(roles);
97
+ const withRoles = roleList.length ? { roles: roleList } : {};
98
+ let note;
99
+
100
+ if (type === "utility") {
101
+ const columns = (ncui.utilities ??= [[]]);
102
+ if (columns.flat().some((entry) => entry.name === slot)) {
103
+ return `${name} already lists utility "${slot}".`;
104
+ }
105
+ const index =
106
+ column === undefined || column === ""
107
+ ? columns.length - 1
108
+ : Number(column) - 1;
109
+ if (!Number.isInteger(index) || index < 0 || index > columns.length) {
110
+ throw new Error(
111
+ `column must be 1 to ${columns.length + 1}; got "${column}"`
112
+ );
113
+ }
114
+ (columns[index] ??= []).push({ name: slot, options: {}, ...withRoles });
115
+ note = `Listed utility "${slot}" in ${name} (column ${index + 1}).`;
116
+ } else if (type === "setup") {
117
+ const steps = ((ncui.setup ??= {}).steps ??= []);
118
+ if (steps.some((step) => step.name === slot)) {
119
+ return `${name} already lists setup step "${slot}".`;
120
+ }
121
+ steps.push({ name: slot });
122
+ note = `Listed setup step "${slot}" last in ${name}; the setup flow runs steps in that order.`;
123
+ } else {
124
+ const parts = slot.split(".");
125
+ let level = (ncui.navigations ??= []);
126
+ let parent;
127
+ for (const part of parts.slice(0, -1)) {
128
+ parent = level.find((item) => item.name === part);
129
+ if (!parent) {
130
+ throw new Error(
131
+ `${name} has no navigation "${parts
132
+ .slice(0, parts.indexOf(part) + 1)
133
+ .join(".")}" to add "${slot}" under`
134
+ );
135
+ }
136
+ level = parent.children ??= [];
137
+ }
138
+ const leaf = parts[parts.length - 1];
139
+ if (level.some((item) => item.name === leaf)) {
140
+ return `${name} already lists navigation "${slot}".`;
141
+ }
142
+ const navRoles = roleList.length ? roleList : parent?.roles;
143
+ level.push({ name: leaf, ...(navRoles ? { roles: navRoles } : {}) });
144
+ note = `Listed navigation "${slot}" in ${name}.`;
145
+ }
146
+
147
+ codegenFs.write(kosJsonPath, stringifyPreserving(original, kosJson));
148
+ const setup = descriptorMockSetup(kosJson);
149
+ codegenFs.write(setup.path, setup.content);
150
+ return note;
151
+ }
152
+
153
+ function ncuiLists(ncui, type, slot) {
154
+ if (type === "utility") {
155
+ return (ncui.utilities ?? []).flat().some((entry) => entry.name === slot);
156
+ }
157
+ if (type === "setup") {
158
+ return (ncui.setup?.steps ?? []).some((step) => step.name === slot);
159
+ }
160
+ let level = ncui.navigations ?? [];
161
+ for (const part of slot.split(".")) {
162
+ const found = level.find((item) => item.name === part);
163
+ if (!found) return false;
164
+ level = found.children ?? [];
165
+ }
166
+ return true;
167
+ }
168
+
169
+ /**
170
+ * Whether the workspace's descriptor projects list the slot, or with none, whether
171
+ * a generated NCUI descriptor could (the system app's descriptor is one of them).
172
+ */
173
+ export function descriptorListsSlot(codegenFs, type, slot) {
174
+ const projects = findDescriptorProjects(codegenFs);
175
+ if (!projects.length) {
176
+ return NCUI_LAYOUTS.some((layout) =>
177
+ [false, true].some((on) =>
178
+ ncuiLists(
179
+ buildNcuiDescriptor({
180
+ layout,
181
+ ...Object.fromEntries(
182
+ Object.keys(NCUI_OPTIONS).map((option) => [option, on])
183
+ ),
184
+ }).ncui,
185
+ type,
186
+ slot
187
+ )
188
+ )
189
+ );
190
+ }
191
+ return projects.some(({ kosJsonPath }) =>
192
+ ncuiLists(
193
+ JSON.parse(codegenFs.read(kosJsonPath)).kondra?.ncui ?? {},
194
+ type,
195
+ slot
196
+ )
197
+ );
198
+ }
199
+
200
+ // `*` is the only wildcard in a dashboard region's holder and summary patterns.
201
+ const matchesPattern = (pattern, value) =>
202
+ new RegExp(
203
+ `^${pattern
204
+ .split("*")
205
+ .map((part) => part.replace(/[.+?^${}()|[\]\\]/g, "\\$&"))
206
+ .join(".*")}$`
207
+ ).test(value);
208
+
209
+ const SHARE = /^\d+(\.\d+)?%$/;
210
+ const GRID_ALIGN = ["left", "right", "center"];
211
+ const REGION_ALIGN = [...GRID_ALIGN, "full"];
212
+
213
+ const SIZED_COLUMN = /^(fixed|max) \d+(\.\d+)?(%|px)$/;
214
+
215
+ function trackProblems(orientation, kind, sizes) {
216
+ if (!sizes) return [];
217
+ const known = (size) =>
218
+ size === "fit" ||
219
+ size === "fill" ||
220
+ SHARE.test(size) ||
221
+ (kind === "column" && SIZED_COLUMN.test(size));
222
+ const forms =
223
+ kind === "column"
224
+ ? 'a share like "50%", fill, fit, fixed like "fixed 20%" or max like "max 300px"'
225
+ : 'a share like "50%", fill or fit';
226
+ const problems = sizes
227
+ .filter((size) => !known(size))
228
+ .map((size) => `${orientation} ${kind} size "${size}" is not ${forms}`);
229
+ const shares = sizes.filter((size) => SHARE.test(size));
230
+ const total = shares.reduce((sum, size) => sum + parseFloat(size), 0);
231
+ if (sizes.includes("fill")) {
232
+ if (total > 99.5) {
233
+ problems.push(
234
+ `${orientation} ${kind} shares add up to ${total}%, leaving nothing for fill`
235
+ );
236
+ }
237
+ } else if (shares.length > 0 && Math.abs(total - 100) > 0.5) {
238
+ problems.push(`${orientation} ${kind} shares add up to ${total}%, not 100%`);
239
+ }
240
+ return problems;
241
+ }
242
+
243
+ /**
244
+ * Problems with a descriptor project's dashboard layout for a plugin's summary
245
+ * cards: area names that are no region, summary keys no placed region claims, sizes
246
+ * and alignment it does not know, and region components no workspace plugin contributes.
247
+ */
248
+ function dashboardProblems(dashboard, summaryKeys, regionKeys) {
249
+ if (!dashboard) return [];
250
+ const regions = dashboard.regions ?? [];
251
+ const names = new Set(regions.map((region) => region.name));
252
+ const problems = [];
253
+ if (!dashboard.layouts?.portrait && !dashboard.layouts?.landscape) {
254
+ problems.push(
255
+ "dashboard has no portrait or landscape layout, so the built-in one shows"
256
+ );
257
+ }
258
+ for (const region of regions) {
259
+ if (regionKeys && region.component && !regionKeys.includes(region.component)) {
260
+ problems.push(
261
+ `region "${region.name}" component "${region.component}" is contributed by no plugin in the workspace, so the region shows its built-in cards`
262
+ );
263
+ }
264
+ if (region.justify !== undefined && !REGION_ALIGN.includes(region.justify)) {
265
+ problems.push(
266
+ `region "${region.name}" justify "${region.justify}" is not ${REGION_ALIGN.join(", ")}`
267
+ );
268
+ }
269
+ }
270
+ for (const [orientation, layout] of Object.entries(dashboard.layouts ?? {})) {
271
+ if (layout?.justify !== undefined && !GRID_ALIGN.includes(layout.justify)) {
272
+ problems.push(
273
+ `${orientation} justify "${layout.justify}" is not ${GRID_ALIGN.join(", ")}`
274
+ );
275
+ }
276
+ problems.push(
277
+ ...trackProblems(orientation, "column", layout?.columns),
278
+ ...trackProblems(orientation, "row", layout?.rows)
279
+ );
280
+ const placed = new Set(
281
+ (layout?.areas ?? [])
282
+ .flatMap((row) => row.trim().split(/\s+/))
283
+ .filter((cell) => !/^\.+$/.test(cell))
284
+ );
285
+ for (const cell of placed) {
286
+ if (!names.has(cell)) {
287
+ problems.push(
288
+ `${orientation} area "${cell}" is not a dashboard region`
289
+ );
290
+ }
291
+ }
292
+ const claiming = regions.filter((region) => placed.has(region.name));
293
+ for (const key of summaryKeys) {
294
+ const claimed = claiming.some((region) =>
295
+ (region.summaries ?? []).some((pattern) => matchesPattern(pattern, key))
296
+ );
297
+ if (!claimed) {
298
+ problems.push(
299
+ `dashboard summary "${key}" is in no ${orientation} region, so it does not show`
300
+ );
301
+ }
302
+ }
303
+ }
304
+ return problems;
305
+ }
306
+
307
+ /**
308
+ * Dashboard layout problems in each of the workspace's descriptor projects, for
309
+ * the given summary dashboardKeys and the region keys the workspace's plugins
310
+ * contribute: [{project, problem}].
311
+ */
312
+ export function descriptorDashboardProblems(codegenFs, summaryKeys, regionKeys) {
313
+ return findDescriptorProjects(codegenFs).flatMap(({ name, kosJsonPath }) =>
314
+ dashboardProblems(
315
+ JSON.parse(codegenFs.read(kosJsonPath)).kondra?.ncui?.dashboard,
316
+ summaryKeys,
317
+ regionKeys
318
+ ).map((problem) => ({ project: name, problem }))
319
+ );
320
+ }
321
+
322
+ const regionKey = (region) => region.component ?? region.name;
323
+
324
+ /**
325
+ * Whether a descriptor project's dashboard has a region rendered by `key`, or with no
326
+ * descriptor project, whether the built-in layout has one.
327
+ */
328
+ export function dashboardHasRegion(codegenFs, key) {
329
+ const projects = findDescriptorProjects(codegenFs);
330
+ const dashboards = projects.length
331
+ ? projects.map(
332
+ ({ kosJsonPath }) =>
333
+ JSON.parse(codegenFs.read(kosJsonPath)).kondra?.ncui?.dashboard
334
+ )
335
+ : NCUI_LAYOUTS.map((layout) => buildNcuiDescriptor({ layout }).ncui.dashboard);
336
+ return dashboards.some((dashboard) =>
337
+ (dashboard?.regions ?? []).some((region) => regionKey(region) === key)
338
+ );
339
+ }
340
+
341
+ /**
342
+ * Adds a region rendered by `key` to a descriptor project's dashboard, claiming no
343
+ * holders or summaries, as a full-width last row of each layout. Returns the note.
344
+ */
345
+ export function addDashboardRegion(codegenFs, { project, key }) {
346
+ const { name, kosJsonPath } = descriptorProject(codegenFs, project);
347
+ const original = codegenFs.read(kosJsonPath);
348
+ const kosJson = JSON.parse(original);
349
+ const ncui = ((kosJson.kondra ??= {}).ncui ??= {});
350
+ const notes = [];
351
+ if (!ncui.dashboard) {
352
+ ncui.dashboard = buildNcuiDescriptor({ layout: "standard" }).ncui.dashboard;
353
+ notes.push(`${name} had no dashboard; it now has the built-in standard one.`);
354
+ }
355
+ const regions = (ncui.dashboard.regions ??= []);
356
+ if (regions.some((region) => regionKey(region) === key)) {
357
+ return `${name} already has a dashboard region rendered by "${key}".`;
358
+ }
359
+ if (regions.some((region) => region.name === key)) {
360
+ throw new Error(
361
+ `${name} has a dashboard region named "${key}" with another component; choose another regionKey`
362
+ );
363
+ }
364
+ regions.push({ name: key });
365
+ for (const layout of Object.values(ncui.dashboard.layouts ?? {})) {
366
+ const width = Math.max(
367
+ 1,
368
+ ...(layout.areas ?? []).map((row) => row.trim().split(/\s+/).length)
369
+ );
370
+ (layout.areas ??= []).push(Array(width).fill(key).join(" "));
371
+ if (layout.rows) layout.rows.push("fit");
372
+ }
373
+ codegenFs.write(kosJsonPath, stringifyPreserving(original, kosJson));
374
+ const setup = descriptorMockSetup(kosJson);
375
+ codegenFs.write(setup.path, setup.content);
376
+ return [
377
+ ...notes,
378
+ `Added dashboard region "${key}" to ${name} as the full-width last row of each layout; it claims no holders or summaries, so the component reads the assembly. Move it in kondra.ncui.dashboard.layouts, or give it holders and summaries patterns.`,
379
+ ].join(" ");
380
+ }
381
+
382
+ const SLOT_FIELDS = [
383
+ ["utilities", "utility", "utilDescriptor"],
384
+ ["setupStep", "setup", "setupDescriptor"],
385
+ ["navViews", "nav", "navDescriptor"],
386
+ ];
387
+
388
+ /**
389
+ * Lists every utility, setup step and navigation slot a plugin already contributes in
390
+ * a descriptor project, keeping the entries it has; for a plugin that predates it.
391
+ */
392
+ export function addPluginDescriptorEntries(
393
+ codegenFs,
394
+ { project, descriptorProject }
395
+ ) {
396
+ const root = findProjectByName(codegenFs.root, project)?.root;
397
+ const raw = root && codegenFs.read(path.join(root, ".kos.json"));
398
+ if (!raw) throw new Error(`No plugin project "${project}" with a .kos.json`);
399
+ const contributes = JSON.parse(raw).kos?.ui?.plugin?.contributes ?? {};
400
+ const notes = [];
401
+ for (const [key, type, field] of SLOT_FIELDS) {
402
+ for (const entry of contributes[key] ?? []) {
403
+ if (!entry[field]) continue;
404
+ try {
405
+ notes.push(
406
+ addDescriptorEntry(codegenFs, type, {
407
+ project: descriptorProject,
408
+ slot: entry[field],
409
+ })
410
+ );
411
+ } catch (error) {
412
+ notes.push(`${entry.id}: ${error.message}`);
413
+ }
414
+ }
415
+ }
416
+ return {
417
+ label: "DDK descriptor entries",
418
+ notes: notes.length
419
+ ? notes
420
+ : [`${project} contributes no utility, setup step or navigation slot.`],
421
+ };
422
+ }
@@ -0,0 +1,137 @@
1
+ // JSON serialization that keeps the original text of unchanged nodes. New nodes
2
+ // follow Prettier's JSON layout: objects expanded, short primitive arrays inline.
3
+
4
+ const PRINT_WIDTH = 80;
5
+ const INDENT = " ";
6
+
7
+ /** Parses strict JSON into `{ value, start, end, kind, children }` nodes. */
8
+ function parse(text) {
9
+ let i = 0;
10
+ const ws = () => {
11
+ while (i < text.length && /\s/.test(text[i])) i++;
12
+ };
13
+ const fail = () => {
14
+ throw new Error(`Invalid JSON at offset ${i}`);
15
+ };
16
+ const node = () => {
17
+ ws();
18
+ const start = i;
19
+ const ch = text[i];
20
+ if (ch === "{") {
21
+ i++;
22
+ const children = new Map();
23
+ const value = {};
24
+ ws();
25
+ if (text[i] === "}") {
26
+ i++;
27
+ return { kind: "object", value, children, start, end: i };
28
+ }
29
+ for (;;) {
30
+ ws();
31
+ const key = node();
32
+ if (typeof key.value !== "string") fail();
33
+ ws();
34
+ if (text[i++] !== ":") fail();
35
+ const child = node();
36
+ children.set(key.value, child);
37
+ value[key.value] = child.value;
38
+ ws();
39
+ if (text[i] === ",") {
40
+ i++;
41
+ continue;
42
+ }
43
+ if (text[i++] !== "}") fail();
44
+ return { kind: "object", value, children, start, end: i };
45
+ }
46
+ }
47
+ if (ch === "[") {
48
+ i++;
49
+ const children = [];
50
+ ws();
51
+ if (text[i] === "]") {
52
+ i++;
53
+ return { kind: "array", value: [], children, start, end: i };
54
+ }
55
+ for (;;) {
56
+ children.push(node());
57
+ ws();
58
+ if (text[i] === ",") {
59
+ i++;
60
+ continue;
61
+ }
62
+ if (text[i++] !== "]") fail();
63
+ return {
64
+ kind: "array",
65
+ value: children.map((c) => c.value),
66
+ children,
67
+ start,
68
+ end: i,
69
+ };
70
+ }
71
+ }
72
+ if (ch === '"') {
73
+ i++;
74
+ while (text[i] !== '"') {
75
+ if (i >= text.length) fail();
76
+ i += text[i] === "\\" ? 2 : 1;
77
+ }
78
+ i++;
79
+ return { kind: "primitive", value: JSON.parse(text.slice(start, i)), start, end: i };
80
+ }
81
+ const m = /^(?:-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?|true|false|null)/.exec(
82
+ text.slice(i)
83
+ );
84
+ if (!m) fail();
85
+ i += m[0].length;
86
+ return { kind: "primitive", value: JSON.parse(m[0]), start, end: i };
87
+ };
88
+ const root = node();
89
+ ws();
90
+ if (i !== text.length) fail();
91
+ return root;
92
+ }
93
+
94
+ const equal = (a, b) => JSON.stringify(a) === JSON.stringify(b);
95
+ const isPlainObject = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
96
+
97
+ function emit(text, original, value, depth, column) {
98
+ if (original && equal(original.value, value)) {
99
+ return text.slice(original.start, original.end);
100
+ }
101
+ const pad = INDENT.repeat(depth + 1);
102
+ const close = INDENT.repeat(depth);
103
+ if (Array.isArray(value)) {
104
+ if (!value.length) return "[]";
105
+ const children = original?.kind === "array" ? original.children : [];
106
+ if (value.every((v) => v === null || typeof v !== "object")) {
107
+ const inline = `[${value.map((v) => JSON.stringify(v)).join(", ")}]`;
108
+ if (column + inline.length <= PRINT_WIDTH) return inline;
109
+ }
110
+ const items = value.map(
111
+ (v, n) => pad + emit(text, children[n], v, depth + 1, pad.length)
112
+ );
113
+ return `[\n${items.join(",\n")}\n${close}]`;
114
+ }
115
+ if (isPlainObject(value)) {
116
+ const keys = Object.keys(value);
117
+ if (!keys.length) return "{}";
118
+ const children = original?.kind === "object" ? original.children : new Map();
119
+ const entries = keys.map((key) => {
120
+ const head = `${pad}${JSON.stringify(key)}: `;
121
+ return head + emit(text, children.get(key), value[key], depth + 1, head.length);
122
+ });
123
+ return `{\n${entries.join(",\n")}\n${close}}`;
124
+ }
125
+ return JSON.stringify(value);
126
+ }
127
+
128
+ /** `value` as JSON, reusing `originalText` verbatim for every unchanged node. */
129
+ export function stringifyPreserving(originalText, value) {
130
+ let root;
131
+ try {
132
+ root = originalText ? parse(originalText) : undefined;
133
+ } catch {
134
+ root = undefined;
135
+ }
136
+ return `${emit(originalText ?? "", root, value, 0, 0)}\n`;
137
+ }