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

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 +965 -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 +21161 -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 +965 -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 +21161 -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,335 @@
1
+ // DDK extension-point handlers for codegen-core's generateComponent. Register them
2
+ // in the same module graph that calls generateComponent: the registry is per copy.
3
+ import path from "node:path";
4
+ import { fileURLToPath } from "node:url";
5
+ import {
6
+ BasePluginHandler,
7
+ PluginHandlerFactory,
8
+ } from "@kosdev-code/kos-codegen-core";
9
+
10
+ export const TEMPLATES_DIR = path.resolve(
11
+ path.dirname(fileURLToPath(import.meta.url)),
12
+ "../templates"
13
+ );
14
+
15
+ class DdkHandler extends BasePluginHandler {
16
+ requiresI18n = true;
17
+ /** Whether the extension point keeps a contribution's rank. */
18
+ rankable = false;
19
+
20
+ getTargetDirectory() {
21
+ return this.contributionKey;
22
+ }
23
+
24
+ contribute(options, { suffix, experienceSuffix, key, fields }) {
25
+ const configPrefix = this.getConfigPrefix(options);
26
+ const experienceId = `${configPrefix}.${experienceSuffix}.experience`;
27
+ return {
28
+ contributions: {
29
+ [key]: [
30
+ {
31
+ id: suffix ? `${configPrefix}.${suffix}` : configPrefix,
32
+ title: `${configPrefix}.${fields.titleSuffix}.title`,
33
+ namespace: options.appProject,
34
+ ...fields.extra,
35
+ ...(this.rankable && Number.isInteger(options.rank)
36
+ ? { rank: options.rank }
37
+ : {}),
38
+ experienceId,
39
+ },
40
+ ],
41
+ },
42
+ experiences: {
43
+ [experienceId]: this.createExperience(options, experienceId),
44
+ },
45
+ };
46
+ }
47
+ }
48
+
49
+ export class DdkCuiHandler extends DdkHandler {
50
+ pluginType = "ddk.cui";
51
+ contributionKey = "cui";
52
+
53
+ createConfiguration(options) {
54
+ if (!options.descriptor) {
55
+ throw new Error(
56
+ "A CUI configuration needs its cuiDescriptor (descriptor)"
57
+ );
58
+ }
59
+ return this.contribute(options, {
60
+ suffix: "",
61
+ experienceSuffix: "cui",
62
+ key: "cui",
63
+ fields: {
64
+ titleSuffix: "cui",
65
+ extra: { cuiDescriptor: options.descriptor },
66
+ },
67
+ });
68
+ }
69
+ }
70
+
71
+ export class DdkSetupHandler extends DdkHandler {
72
+ pluginType = "ddk.setup";
73
+ contributionKey = "setup";
74
+ rankable = true;
75
+
76
+ createConfiguration(options) {
77
+ return this.contribute(options, {
78
+ suffix: "setup",
79
+ experienceSuffix: "setup",
80
+ key: "setupStep",
81
+ fields: {
82
+ titleSuffix: "setup",
83
+ extra: {
84
+ setupDescriptor: options.descriptor || options.nameCamelCase,
85
+ },
86
+ },
87
+ });
88
+ }
89
+ }
90
+
91
+ export class DdkUtilityHandler extends DdkHandler {
92
+ pluginType = "ddk.utility";
93
+ contributionKey = "utility";
94
+ rankable = true;
95
+
96
+ createConfiguration(options) {
97
+ return this.contribute(options, {
98
+ suffix: "util",
99
+ experienceSuffix: "util",
100
+ key: "utilities",
101
+ fields: {
102
+ titleSuffix: "utility",
103
+ extra: {
104
+ utilDescriptor: options.descriptor || options.nameCamelCase,
105
+ },
106
+ },
107
+ });
108
+ }
109
+ }
110
+
111
+ export class DdkNavHandler extends DdkHandler {
112
+ pluginType = "ddk.nav";
113
+ contributionKey = "nav";
114
+ rankable = true;
115
+
116
+ createConfiguration(options) {
117
+ return this.contribute(options, {
118
+ suffix: "nav",
119
+ experienceSuffix: "nav",
120
+ key: "navViews",
121
+ fields: {
122
+ titleSuffix: "nav",
123
+ extra: {
124
+ navDescriptor: options.descriptor || options.nameLowerCase,
125
+ },
126
+ },
127
+ });
128
+ }
129
+ }
130
+
131
+ // Contributions that carry their extension point's `type` literal. Ids, title key
132
+ // and i18n entry all hang off `${project}.${name}.${contributionKey}`.
133
+ class TypedDdkHandler extends DdkHandler {
134
+ rankable = true;
135
+
136
+ /** Exposed component name, which must be unique within the plugin. */
137
+ componentName(options) {
138
+ return options.namePascalCase;
139
+ }
140
+
141
+ contributionFields() {
142
+ return {};
143
+ }
144
+
145
+ createConfiguration(options) {
146
+ const config = this.contribute(options, {
147
+ suffix: this.contributionKey,
148
+ experienceSuffix: this.contributionKey,
149
+ key: this.kosJsonKey,
150
+ fields: {
151
+ titleSuffix: this.contributionKey,
152
+ extra: {
153
+ type: this.extensionType,
154
+ ...this.contributionFields(options),
155
+ },
156
+ },
157
+ });
158
+ for (const experience of Object.values(config.experiences)) {
159
+ experience.component = this.componentName(options);
160
+ }
161
+ return config;
162
+ }
163
+ }
164
+
165
+ class DashboardCardHandler extends TypedDdkHandler {
166
+ getTargetDirectory() {
167
+ return "dashboard";
168
+ }
169
+
170
+ getComponentPath(options) {
171
+ return `${options.appDirectory}/dashboard/${options.nameDashCase}/${this.part}/${options.nameDashCase}.tsx`;
172
+ }
173
+
174
+ componentName(options) {
175
+ return `${options.namePascalCase}${
176
+ this.part === "summary" ? "Summary" : "Detail"
177
+ }`;
178
+ }
179
+
180
+ contributionFields(options) {
181
+ return { dashboardKey: options.dashboardKey };
182
+ }
183
+ }
184
+
185
+ export class DdkDashboardSummaryHandler extends DashboardCardHandler {
186
+ pluginType = "ddk.dashboardSummary";
187
+ contributionKey = "dashboardSummary";
188
+ kosJsonKey = "dashboardSummary";
189
+ extensionType = "dashboard.summary";
190
+ part = "summary";
191
+
192
+ getTemplatePath() {
193
+ return "dashboard-summary";
194
+ }
195
+ }
196
+
197
+ export class DdkDashboardDetailHandler extends DashboardCardHandler {
198
+ pluginType = "ddk.dashboardDetail";
199
+ contributionKey = "dashboardDetail";
200
+ kosJsonKey = "dashboardDetail";
201
+ extensionType = "dashboard.detail";
202
+ part = "detail";
203
+
204
+ getTemplatePath() {
205
+ return "dashboard-detail";
206
+ }
207
+ }
208
+
209
+ export class DdkPumpDetailHandler extends TypedDdkHandler {
210
+ pluginType = "ddk.pumpDetail";
211
+ contributionKey = "dashboardPumpDetail";
212
+ kosJsonKey = "dashboardPumpDetail";
213
+ extensionType = "dashboard.detail.pump";
214
+
215
+ getTargetDirectory() {
216
+ return "dashboard/pumpDetail";
217
+ }
218
+
219
+ getTemplatePath() {
220
+ return "pump-detail";
221
+ }
222
+
223
+ contributionFields(options) {
224
+ return { dashboardKey: options.dashboardKey };
225
+ }
226
+ }
227
+
228
+ export class DdkPumpSummaryHandler extends TypedDdkHandler {
229
+ pluginType = "ddk.pumpSummary";
230
+ contributionKey = "dashboardPumpSummary";
231
+ kosJsonKey = "dashboardPumpSummary";
232
+ extensionType = "dashboard.summary.pump";
233
+
234
+ getTargetDirectory() {
235
+ return "dashboard/pumpSummary";
236
+ }
237
+
238
+ getTemplatePath() {
239
+ return "pump-summary";
240
+ }
241
+
242
+ // Distinct from the pump detail of the same name, which is exposed as its PascalCase name.
243
+ componentName(options) {
244
+ return `${options.namePascalCase}Summary`;
245
+ }
246
+
247
+ contributionFields(options) {
248
+ return { dashboardKey: options.dashboardKey };
249
+ }
250
+ }
251
+
252
+ export class DdkMacroSummaryHandler extends TypedDdkHandler {
253
+ pluginType = "ddk.macroSummary";
254
+ contributionKey = "dashboardMacroSummary";
255
+ kosJsonKey = "dashboardMacroSummary";
256
+ extensionType = "dashboard.macro.summary";
257
+
258
+ getTargetDirectory() {
259
+ return "dashboard/macroSummary";
260
+ }
261
+
262
+ getTemplatePath() {
263
+ return "macro-summary";
264
+ }
265
+
266
+ contributionFields(options) {
267
+ return { dashboardKey: options.dashboardKey };
268
+ }
269
+ }
270
+
271
+ export class DdkDashboardRegionHandler extends TypedDdkHandler {
272
+ pluginType = "ddk.dashboardRegion";
273
+ contributionKey = "dashboardRegion";
274
+ kosJsonKey = "dashboardRegion";
275
+ extensionType = "dashboard.region";
276
+
277
+ getTargetDirectory() {
278
+ return "dashboard/region";
279
+ }
280
+
281
+ getTemplatePath() {
282
+ return "dashboard-region";
283
+ }
284
+
285
+ contributionFields(options) {
286
+ return { regionKey: options.regionKey };
287
+ }
288
+ }
289
+
290
+ export class DdkTroubleActionHandler extends TypedDdkHandler {
291
+ pluginType = "ddk.troubleAction";
292
+ contributionKey = "troubleAction";
293
+ kosJsonKey = "troubleActions";
294
+ extensionType = "trouble.action";
295
+
296
+ getTargetDirectory() {
297
+ return "trouble-action";
298
+ }
299
+
300
+ getTemplatePath() {
301
+ return "trouble-action";
302
+ }
303
+
304
+ contributionFields(options) {
305
+ return {
306
+ troubleType: options.troubleType,
307
+ stateKey: options.stateKey || options.troubleType,
308
+ stateValue: options.stateValue || options.troubleType,
309
+ };
310
+ }
311
+ }
312
+
313
+ export const DDK_HANDLERS = [
314
+ DdkCuiHandler,
315
+ DdkSetupHandler,
316
+ DdkUtilityHandler,
317
+ DdkNavHandler,
318
+ DdkDashboardSummaryHandler,
319
+ DdkDashboardDetailHandler,
320
+ DdkPumpDetailHandler,
321
+ DdkPumpSummaryHandler,
322
+ DdkMacroSummaryHandler,
323
+ DdkDashboardRegionHandler,
324
+ DdkTroubleActionHandler,
325
+ ];
326
+
327
+ /** Register every DDK handler once with this copy of codegen-core. */
328
+ export function registerDdkHandlers() {
329
+ for (const Handler of DDK_HANDLERS) {
330
+ const type = new Handler().pluginType;
331
+ if (!PluginHandlerFactory.isValidPluginType(type)) {
332
+ PluginHandlerFactory.register(type, Handler);
333
+ }
334
+ }
335
+ }
@@ -0,0 +1,7 @@
1
+ // The name and version the kos-ddk server reports; the agent-plugin bundle swaps this
2
+ // module for one reading its plugin.json.
3
+ import { readFileSync } from "node:fs";
4
+
5
+ export const pkg = JSON.parse(
6
+ readFileSync(new URL("../../package.json", import.meta.url), "utf-8")
7
+ );
@@ -0,0 +1,328 @@
1
+ // A DDK plugin project's contributions, read from its .kos.json, and their checks
2
+ // against the extension points' schemas, the standard contributions they override
3
+ // and the rule that plugins compose DDK components without setting tokens.
4
+ import { existsSync, readFileSync, readdirSync } from "node:fs";
5
+ import path from "node:path";
6
+ import {
7
+ MAX_RANK,
8
+ OVERRIDABLE_POINTS,
9
+ listOverridableContributions,
10
+ loadCatalog,
11
+ uniqueContributions,
12
+ } from "../codegen/overrides.mjs";
13
+ import {
14
+ dashboardHasRegion,
15
+ descriptorDashboardProblems,
16
+ descriptorListsSlot,
17
+ findDescriptorProjects,
18
+ } from "../codegen/descriptor.mjs";
19
+ import { findDdkPlugins } from "../generators/dev.mjs";
20
+ import { loadReference } from "./reference.mjs";
21
+
22
+ const readJson = (file) => JSON.parse(readFileSync(file, "utf-8"));
23
+
24
+ const SLOT_KEYS = Object.fromEntries(
25
+ Object.values(OVERRIDABLE_POINTS).flatMap(({ contributionKeys, keys }) =>
26
+ contributionKeys.map((key) => [key, keys])
27
+ )
28
+ );
29
+
30
+ /** The plugin project, its directory and its kos.ui.plugin block. */
31
+ function loadPlugin(root, project) {
32
+ const plugins = findDdkPlugins(root);
33
+ const plugin = plugins.find(
34
+ (p) => p.project === project || p.alias === project
35
+ );
36
+ if (!plugin) {
37
+ throw new Error(
38
+ `No DDK plugin project "${project}". DDK plugin projects: ${
39
+ plugins.map((p) => p.project).join(", ") || "none"
40
+ }.`
41
+ );
42
+ }
43
+ const dir = path.join(root, plugin.dir);
44
+ const kos = readJson(path.join(dir, ".kos.json"));
45
+ return { ...plugin, absDir: dir, block: kos.kos?.ui?.plugin ?? {} };
46
+ }
47
+
48
+ const slotOf = (key, entry) =>
49
+ Object.fromEntries(
50
+ (SLOT_KEYS[key] ?? [])
51
+ .filter((field) => entry[field] !== undefined)
52
+ .map((field) => [field, entry[field]])
53
+ );
54
+ const sameSlot = (a, b) => {
55
+ const keys = Object.keys(a);
56
+ return (
57
+ keys.length > 0 &&
58
+ keys.length === Object.keys(b).length &&
59
+ keys.every((k) => a[k] === b[k])
60
+ );
61
+ };
62
+ // Lower rank wins; an unranked contribution loses to any ranked one.
63
+ const beats = (rank, other) =>
64
+ typeof rank === "number" && (typeof other !== "number" || rank < other);
65
+
66
+ /** Each contribution with its point, slot, experience and the standard ones it shares a slot with. */
67
+ export function describePlugin(root, project) {
68
+ const plugin = loadPlugin(root, project);
69
+ const { extensionPoints } = loadReference();
70
+ const standard = uniqueContributions(listOverridableContributions());
71
+ const contributes = plugin.block.contributes ?? {};
72
+ const experiences = contributes.experiences ?? {};
73
+ const contributions = [];
74
+ for (const [key, entries] of Object.entries(contributes)) {
75
+ if (!Array.isArray(entries)) continue;
76
+ const point = extensionPoints.find((p) => p.contributionKey === key);
77
+ for (const entry of entries) {
78
+ const slot = slotOf(key, entry);
79
+ const shared = standard.filter(
80
+ (s) =>
81
+ s.plugin !== plugin.project &&
82
+ s.contributionKey === key &&
83
+ sameSlot(slot, s.keys)
84
+ );
85
+ const experience = experiences[entry.experienceId];
86
+ contributions.push({
87
+ contributionKey: key,
88
+ ...(point ? { extensionPoint: point.id } : {}),
89
+ id: entry.id,
90
+ ...(Object.keys(slot).length ? { slot } : {}),
91
+ ...(typeof entry.rank === "number" ? { rank: entry.rank } : {}),
92
+ ...(experience
93
+ ? { component: experience.component, location: experience.location }
94
+ : {}),
95
+ ...(shared.length
96
+ ? {
97
+ sharesSlotWith: shared.map((s) => ({
98
+ id: s.id,
99
+ ...(typeof s.rank === "number" ? { rank: s.rank } : {}),
100
+ wins: beats(entry.rank, s.rank),
101
+ })),
102
+ }
103
+ : {}),
104
+ });
105
+ }
106
+ }
107
+ return {
108
+ project: plugin.project,
109
+ dir: plugin.dir,
110
+ pluginId: plugin.block.id,
111
+ contributions,
112
+ };
113
+ }
114
+
115
+ function sourceFiles(dir) {
116
+ if (!existsSync(dir)) return [];
117
+ return readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
118
+ const full = path.join(dir, entry.name);
119
+ if (entry.isDirectory())
120
+ return entry.name === "node_modules" ? [] : sourceFiles(full);
121
+ return /\.(tsx?|css)$/.test(entry.name) ? [full] : [];
122
+ });
123
+ }
124
+
125
+ // The experience loads the module's default export, or the export named by component.
126
+ const exportsComponent = (text, name) =>
127
+ /export\s+default\b/.test(text) ||
128
+ new RegExp(`export\\s+(const|let|function|class)\\s+${name}\\b`).test(text) ||
129
+ new RegExp(`export\\s*\\{[^}]*\\b${name}\\b[^}]*\\}`).test(text);
130
+
131
+ function checkField(field, value) {
132
+ if (value === undefined || value === "") {
133
+ return field.required ? `missing required ${field.name}` : null;
134
+ }
135
+ if (field.type.startsWith('"')) {
136
+ const allowed = field.type.split(" | ").map((t) => JSON.parse(t));
137
+ return allowed.includes(value)
138
+ ? null
139
+ : `${field.name} must be ${field.type}`;
140
+ }
141
+ if (
142
+ ["string", "number", "boolean"].includes(field.type) &&
143
+ typeof value !== field.type
144
+ ) {
145
+ return `${field.name} must be a ${field.type}`;
146
+ }
147
+ return null;
148
+ }
149
+
150
+ /** Errors and warnings for a plugin project; ok when there are no errors. */
151
+ export function validatePlugin(root, project) {
152
+ const plugin = loadPlugin(root, project);
153
+ const { extensionPoints } = loadReference();
154
+ const described = describePlugin(root, project);
155
+ const contributes = plugin.block.contributes ?? {};
156
+ const experiences = contributes.experiences ?? {};
157
+ const errors = [];
158
+ const warnings = [];
159
+ const ids = new Map();
160
+
161
+ for (const [key, entries] of Object.entries(contributes)) {
162
+ if (!Array.isArray(entries)) continue;
163
+ const point = extensionPoints.find((p) => p.contributionKey === key);
164
+ for (const entry of entries) {
165
+ const where = `contributes.${key} ${entry.id ?? "(no id)"}`;
166
+ if (!entry.id) errors.push(`${where}: missing id`);
167
+ else if (ids.has(entry.id))
168
+ errors.push(
169
+ `${where}: id also used in contributes.${ids.get(entry.id)}`
170
+ );
171
+ else ids.set(entry.id, key);
172
+
173
+ for (const field of point?.fields ?? []) {
174
+ if (field.name === "rank") continue;
175
+ const problem = checkField(field, entry[field.name]);
176
+ if (problem) errors.push(`${where}: ${problem} (${point.id})`);
177
+ }
178
+ if (entry.rank !== undefined) {
179
+ if (
180
+ !Number.isInteger(entry.rank) ||
181
+ entry.rank < 0 ||
182
+ entry.rank > MAX_RANK
183
+ ) {
184
+ errors.push(
185
+ `${where}: rank must be an integer from 0 to ${MAX_RANK}`
186
+ );
187
+ } else if (point && !point.isRankable) {
188
+ warnings.push(
189
+ `${where}: ${point.id} is not ranked, so rank has no effect`
190
+ );
191
+ }
192
+ }
193
+
194
+ if (entry.experienceId !== undefined || point?.hasView) {
195
+ const experience = experiences[entry.experienceId];
196
+ if (!experience) {
197
+ errors.push(
198
+ `${where}: experience ${
199
+ entry.experienceId ?? "(none)"
200
+ } is not in contributes.experiences`
201
+ );
202
+ } else {
203
+ const file = path.join(plugin.absDir, experience.location ?? "");
204
+ if (!experience.location || !existsSync(file)) {
205
+ errors.push(
206
+ `${where}: component file ${experience.location} does not exist`
207
+ );
208
+ } else if (
209
+ experience.component &&
210
+ !exportsComponent(readFileSync(file, "utf-8"), experience.component)
211
+ ) {
212
+ errors.push(
213
+ `${where}: ${experience.location} has no default export and does not export ${experience.component}`
214
+ );
215
+ }
216
+ }
217
+ }
218
+ }
219
+ }
220
+
221
+ // A utility, setup step or navigation view shows only if the NCUI descriptor lists it.
222
+ const readFs = {
223
+ root,
224
+ read: (file) => {
225
+ const abs = path.join(root, file);
226
+ return existsSync(abs) ? readFileSync(abs, "utf-8") : null;
227
+ },
228
+ };
229
+ const descriptorProjects = findDescriptorProjects(readFs).map((p) => p.name);
230
+ for (const [key, type, field] of [
231
+ ["utilities", "utility", "utilDescriptor"],
232
+ ["setupStep", "setup", "setupDescriptor"],
233
+ ["navViews", "nav", "navDescriptor"],
234
+ ]) {
235
+ for (const entry of contributes[key] ?? []) {
236
+ const slot = entry[field];
237
+ if (!slot || descriptorListsSlot(readFs, type, slot)) continue;
238
+ warnings.push(
239
+ `contributes.${key} ${entry.id}: ${field} "${slot}" is not in ${
240
+ descriptorProjects.length
241
+ ? `descriptor project ${descriptorProjects.join(", ")}`
242
+ : "the standard NCUI descriptor (no descriptor project in the workspace)"
243
+ }, so the NCUI does not show it${
244
+ descriptorProjects.length
245
+ ? `; list it with kosui ddk:descriptor-entries --project ${project} --descriptorProject ${descriptorProjects[0]}`
246
+ : ""
247
+ }`
248
+ );
249
+ }
250
+ }
251
+
252
+ // A region component renders only a descriptor region it is the component (or name) of.
253
+ for (const entry of contributes.dashboardRegion ?? []) {
254
+ if (!entry.regionKey || dashboardHasRegion(readFs, entry.regionKey)) continue;
255
+ warnings.push(
256
+ `contributes.dashboardRegion ${entry.id}: regionKey "${entry.regionKey}" is no region's component or name in ${
257
+ descriptorProjects.length
258
+ ? `descriptor project ${descriptorProjects.join(", ")}`
259
+ : "the built-in dashboard layout (no descriptor project in the workspace)"
260
+ }, so the dashboard does not show it${
261
+ descriptorProjects.length
262
+ ? `; add it with kosui ddk:dashboard-region --descriptorProject ${descriptorProjects[0]}`
263
+ : ""
264
+ }`
265
+ );
266
+ }
267
+
268
+ // A dashboard summary shows only where a descriptor's dashboard layout places it.
269
+ const summaryKeys = (contributes.dashboardSummary ?? [])
270
+ .map((entry) => entry.dashboardKey)
271
+ .filter(Boolean);
272
+ const regionKeys = findDdkPlugins(root).flatMap(({ dir }) =>
273
+ (
274
+ readJson(path.join(root, dir, ".kos.json")).kos?.ui?.plugin?.contributes
275
+ ?.dashboardRegion ?? []
276
+ ).map((entry) => entry.regionKey)
277
+ );
278
+ for (const { project: descriptor, problem } of descriptorDashboardProblems(
279
+ readFs,
280
+ summaryKeys,
281
+ regionKeys
282
+ )) {
283
+ warnings.push(`descriptor project ${descriptor}: ${problem}`);
284
+ }
285
+
286
+ const bySlot = new Map();
287
+ for (const c of described.contributions.filter((c) => c.slot)) {
288
+ const slotKey = `${c.contributionKey} ${JSON.stringify(c.slot)}`;
289
+ const other = bySlot.get(slotKey);
290
+ if (other && other.rank === c.rank) {
291
+ warnings.push(
292
+ `contributes.${c.contributionKey} ${c.id}: same slot and rank as ${other.id}; which renders is not defined`
293
+ );
294
+ }
295
+ bySlot.set(slotKey, c);
296
+ // The standard plugins are alternatives, so sharing slots with each other is expected.
297
+ if (loadCatalog().sources.includes(plugin.project)) continue;
298
+ for (const s of c.sharesSlotWith ?? []) {
299
+ if (!s.wins) {
300
+ warnings.push(
301
+ `contributes.${c.contributionKey} ${c.id}: shares a slot with ${
302
+ s.id
303
+ } (${
304
+ typeof s.rank === "number" ? `rank ${s.rank}` : "unranked"
305
+ }) without a winning rank, so it does not replace it`
306
+ );
307
+ }
308
+ }
309
+ }
310
+
311
+ const srcDir = path.join(plugin.absDir, "src");
312
+ for (const file of sourceFiles(srcDir)) {
313
+ const text = readFileSync(file, "utf-8");
314
+ const rel = path.relative(plugin.absDir, file).split(path.sep).join("/");
315
+ const set = [
316
+ ...new Set([...text.matchAll(/(--ddk-[\w-]+)\s*:/g)].map((m) => m[1])),
317
+ ];
318
+ if (set.length) {
319
+ errors.push(
320
+ `${rel}: sets DDK tokens (${set.join(
321
+ ", "
322
+ )}); plugins compose DDK components and do not set tokens`
323
+ );
324
+ }
325
+ }
326
+
327
+ return { project: plugin.project, ok: errors.length === 0, errors, warnings };
328
+ }