@bitakit/cli 0.2.4 → 0.2.6

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.
package/src/slots.mjs CHANGED
@@ -1,4 +1,5 @@
1
1
  import path from 'node:path';
2
+ import { candidateNames, selectSingle } from '@bitakit/core';
2
3
  import { describeDefinitions } from '@bitakit/discovery';
3
4
 
4
5
  const objectHelpers = new Set(['object', 'strictObject', 'looseObject']);
@@ -102,6 +103,15 @@ export async function describeSlots(description) {
102
103
  plugin: module.id,
103
104
  source: entry.file,
104
105
  });
106
+ // Layout files are candidates for layout.<family>, keyed by their variant.
107
+ for (const entry of module.layouts ?? [])
108
+ contribute(`layout.${entry.family}`, {
109
+ kind: 'component',
110
+ id: null,
111
+ plugin: module.id,
112
+ key: entry.variant,
113
+ source: entry.file,
114
+ });
105
115
  const actions = await describeDefinitions(absolute(module.actions), {
106
116
  helper: 'defineAction',
107
117
  moduleId: moduleId(module),
@@ -144,6 +154,24 @@ export async function describeSlots(description) {
144
154
  };
145
155
  });
146
156
  const declared = new Set(slots.map((slot) => slot.id));
157
+ // Discovery declares layout families nobody else declares as app-owned single slots.
158
+ for (const [id, entries] of contributions)
159
+ if (id.startsWith('layout.') && !declared.has(id) && entries.some((entry) => entry.key)) {
160
+ declared.add(id);
161
+ slots.push({
162
+ id,
163
+ owner: description.app.id,
164
+ plugin: description.app.id,
165
+ contract: true,
166
+ accepts: 'component',
167
+ policy: 'single',
168
+ payload: false,
169
+ payloadKeys: [],
170
+ source: entries[0].source,
171
+ generated: true,
172
+ contributions: entries,
173
+ });
174
+ }
147
175
  for (const id of description.slots ?? [])
148
176
  if (!declared.has(id)) {
149
177
  declared.add(id);
@@ -171,6 +199,23 @@ export async function describeSlots(description) {
171
199
  const index = slot.contract ? order.indexOf(slot.owner) : -1;
172
200
  return index >= 0 ? index : order.length + (slot.contract ? 0 : 1);
173
201
  };
202
+ // A single slot renders one candidate: report which, by the runtime's own rules, from files.
203
+ for (const slot of slots)
204
+ if (slot.policy === 'single') {
205
+ const candidates = slot.contributions.filter((entry) => entry.kind === 'component');
206
+ const selection = selectSingle(candidates, {
207
+ choice: description.choose?.[slot.id],
208
+ appId: description.app.id,
209
+ });
210
+ slot.selection = {
211
+ candidates: candidates.map((entry) => candidateNames(entry)[1] ?? entry.plugin),
212
+ renders: selection.selected
213
+ ? (candidateNames(selection.selected)[1] ?? selection.selected.plugin)
214
+ : null,
215
+ reason: selection.reason ?? null,
216
+ choice: description.choose?.[slot.id] ?? null,
217
+ };
218
+ }
174
219
  return {
175
220
  version: 1,
176
221
  slots: slots
@@ -182,6 +227,15 @@ export async function describeSlots(description) {
182
227
  };
183
228
  }
184
229
 
230
+ /** One replaceable part: which candidate renders, or why the owner's default does. */
231
+ function selectionText({ renders, reason, candidates }) {
232
+ if (renders) return `renders ${renders}`;
233
+ if (reason === 'ambiguous')
234
+ return `renders the default: choose one of ${candidates.join(', ')} in bitakit.config.ts`;
235
+ if (reason === 'choice-unavailable') return 'renders the default: choose names no candidate';
236
+ return 'renders the default';
237
+ }
238
+
185
239
  /** The short human listing: one line per slot under its owner. */
186
240
  export function formatSlots(result, { dim = (text) => text } = {}) {
187
241
  const lines = result.slots.length ? [] : ['No slots are declared.'];
@@ -197,7 +251,7 @@ export function formatSlots(result, { dim = (text) => text } = {}) {
197
251
  if (title !== group) lines.push('', title);
198
252
  group = title;
199
253
  lines.push(
200
- ` ${slot.id.padEnd(width)} ${(slot.accepts ?? 'unknown').padEnd(9)} ${plural(slot.contributions.length, 'contribution')}`,
254
+ ` ${slot.id.padEnd(width)} ${(slot.accepts ?? 'unknown').padEnd(9)} ${plural(slot.contributions.length, 'contribution')}${slot.selection ? ` ${selectionText(slot.selection)}` : ''}`,
201
255
  );
202
256
  }
203
257
  if (result.slots.length)
package/src/templates.mjs CHANGED
@@ -443,12 +443,14 @@ export default defineSettings({
443
443
  `;
444
444
  }
445
445
 
446
- export function layoutFile(name) {
447
- const component = `${pascalOf(name)}Layout`;
446
+ export function layoutFile(family, variant) {
447
+ const component = `${pascalOf(variant === undefined ? family : `${family}-${variant}`)}Layout`;
448
448
  return `import type { ReactNode } from 'react';
449
449
 
450
- // Pages choose this layout with: export const layout = '${name}';
451
- export default function ${component}({ children }: { children: ReactNode }) {
450
+ // A candidate for the layout.${family} slot. Pages choose the family with:
451
+ // export const layout = '${family}';
452
+ // With several candidates, pick one in bitakit.config.ts: choose: { 'layout.${family}': '${variant ?? 'default'}' }
453
+ export default function ${component}({ children }: { children?: ReactNode }) {
452
454
  return <div className="mx-auto flex w-full max-w-6xl flex-1 flex-col gap-6 p-4 md:p-6">{children}</div>;
453
455
  }
454
456
  `;
@@ -458,8 +460,9 @@ export function slotFile(plugin, slot) {
458
460
  const component = `${pascalOf(slot.replace(/\./g, '-'))}Slot`;
459
461
  return `'use client';
460
462
 
461
- // A widget for the "${slot}" extension point; its owner renders it with <PluginSlot name="${slot}" />.
462
- // It appears once both plugins are ready. Optional: export const order = 1 to sort widgets.
463
+ // A contribution to the "${slot}" slot; its owner renders it with <PluginSlot name="${slot}" />.
464
+ // In a list slot it appears next to the others (export const order = 1 sorts them). In a single
465
+ // slot, a replaceable part such as screen.not-found, it replaces the owner's default.
463
466
  export default function ${component}() {
464
467
  return <span className="text-sm text-muted-foreground">${plugin}</span>;
465
468
  }