@bitakit/cli 0.0.0-stage → 0.2.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.
- package/LICENSE +21 -0
- package/README.md +153 -2
- package/package.json +44 -4
- package/src/add.mjs +190 -0
- package/src/agent-rules.mjs +52 -0
- package/src/build.mjs +203 -0
- package/src/cli.mjs +266 -0
- package/src/doctor.mjs +908 -0
- package/src/index.mjs +30 -0
- package/src/init.mjs +254 -0
- package/src/inventory.mjs +126 -0
- package/src/project.mjs +97 -0
- package/src/routes.mjs +124 -0
- package/src/templates.mjs +530 -0
|
@@ -0,0 +1,530 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File templates for plugins and their contributions. Each function returns a map of
|
|
3
|
+
* module-relative paths to file contents; callers decide where the module root is
|
|
4
|
+
* (`src/plugins/<name>` in an app, `src` in a standalone plugin package).
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** Plugin and contribution names: lowercase, starting with a letter, words joined by hyphens. */
|
|
8
|
+
export const namePattern = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/;
|
|
9
|
+
|
|
10
|
+
export function validateName(name, what = 'name') {
|
|
11
|
+
if (!namePattern.test(name ?? ''))
|
|
12
|
+
throw new Error(
|
|
13
|
+
`Invalid ${what} "${name ?? ''}". Use lowercase letters and digits, optionally joined by hyphens (for example "notes" or "user-notes").`,
|
|
14
|
+
);
|
|
15
|
+
return name;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const words = (name) => name.split('-');
|
|
19
|
+
/** `user-notes` → `User notes` */
|
|
20
|
+
export const titleOf = (name) => {
|
|
21
|
+
const text = words(name).join(' ');
|
|
22
|
+
return text.charAt(0).toUpperCase() + text.slice(1);
|
|
23
|
+
};
|
|
24
|
+
/** `user-notes` → `UserNotes` */
|
|
25
|
+
export const pascalOf = (name) =>
|
|
26
|
+
words(name)
|
|
27
|
+
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
|
|
28
|
+
.join('');
|
|
29
|
+
/** Property access for a plugin namespace: `.notes` or `['user-notes']`. */
|
|
30
|
+
const member = (name) =>
|
|
31
|
+
/^[A-Za-z_$][\w$]*$/.test(name) ? `.${name}` : `[${JSON.stringify(name)}]`;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The smallest working feature: one page, plus its navigation link when the app has a navigation
|
|
35
|
+
* slot. More files are added one at a time with `bitakit add <kind>`.
|
|
36
|
+
*/
|
|
37
|
+
export function starterFiles(name, { navigationSlot } = {}) {
|
|
38
|
+
validateName(name, 'plugin name');
|
|
39
|
+
const title = titleOf(name);
|
|
40
|
+
const component = `${pascalOf(name)}Page`;
|
|
41
|
+
return {
|
|
42
|
+
...(navigationSlot
|
|
43
|
+
? {
|
|
44
|
+
'actions/open.ts': `import { defineAction } from '@bitakit/core';
|
|
45
|
+
|
|
46
|
+
// The menu entry for this feature; the page lives in ui/pages/index.tsx.
|
|
47
|
+
export default defineAction({ title: '${title}', href: '/${name}', groups: ['navigation'], slots: [${JSON.stringify(navigationSlot)}] });
|
|
48
|
+
`,
|
|
49
|
+
}
|
|
50
|
+
: {}),
|
|
51
|
+
'ui/pages/index.tsx': `'use client';
|
|
52
|
+
import { PageHeader } from '@bitakit/ui/components';
|
|
53
|
+
|
|
54
|
+
// Served at /${name}; the file's place under ui/pages is the URL.
|
|
55
|
+
// Grow the feature one file at a time:
|
|
56
|
+
// shared state -> npx bitakit add service ${name}/items
|
|
57
|
+
// a button -> npx bitakit add action ${name}/archive (bind it with <Action id="${name}.archive">)
|
|
58
|
+
export default function ${component}() {
|
|
59
|
+
return <PageHeader title="${title}" subtitle="Edit this page in src/plugins/${name}/ui/pages/index.tsx." />;
|
|
60
|
+
}
|
|
61
|
+
`,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* A complete feature: a service that owns the data, typed events, actions that change it, a
|
|
67
|
+
* navigation link and a page. `minimal` keeps only the service, the events and one action.
|
|
68
|
+
*/
|
|
69
|
+
export function pluginFiles(name, { minimal = false, navigationSlot } = {}) {
|
|
70
|
+
validateName(name, 'plugin name');
|
|
71
|
+
const title = titleOf(name);
|
|
72
|
+
const items = `services${member(name)}.items`;
|
|
73
|
+
const files = {
|
|
74
|
+
'services/items.ts': `import { defineService } from '@bitakit/core';
|
|
75
|
+
|
|
76
|
+
export type Item = { id: number; title: string };
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The ${title} plugin's data and behavior. Items live in memory, so a page reload starts
|
|
80
|
+
* empty. Replace the body with calls to your API or database when you are ready.
|
|
81
|
+
*/
|
|
82
|
+
export default defineService(({ store, events }) => {
|
|
83
|
+
// Observable state: actions and pages follow it without any wiring.
|
|
84
|
+
// Resources belong in onStart/onStop; Core awaits cleanup before replacement.
|
|
85
|
+
const state = store<{ items: Item[] }>({ items: [] });
|
|
86
|
+
return {
|
|
87
|
+
state,
|
|
88
|
+
add(title: string) {
|
|
89
|
+
state.set((s) => ({ items: [...s.items, { id: Date.now(), title: title.trim() }] }));
|
|
90
|
+
events.emit('${name}.changed', { kind: 'added' });
|
|
91
|
+
},
|
|
92
|
+
remove(id: number) {
|
|
93
|
+
state.set((s) => ({ items: s.items.filter((item) => item.id !== id) }));
|
|
94
|
+
events.emit('${name}.changed', { kind: 'removed' });
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
});
|
|
98
|
+
`,
|
|
99
|
+
'events.ts': `import { defineEvents, event } from '@bitakit/core';
|
|
100
|
+
|
|
101
|
+
// Other plugins listen with events.on('${name}.changed', ...). Only this plugin emits it.
|
|
102
|
+
export default defineEvents({
|
|
103
|
+
changed: event<{ kind: 'added' | 'removed' }>(),
|
|
104
|
+
});
|
|
105
|
+
`,
|
|
106
|
+
'actions/add.ts': `import { defineAction } from '@bitakit/core';
|
|
107
|
+
|
|
108
|
+
export default defineAction<string>({
|
|
109
|
+
title: 'Add item',
|
|
110
|
+
// Buttons bound to this action are disabled while their input is empty.
|
|
111
|
+
enabled: ({ input }) => typeof input === 'string' && input.trim().length > 0,
|
|
112
|
+
// Runs when a bound button or your code executes "${name}.add".
|
|
113
|
+
execute: ({ services, input }) => ${items}.add(input),
|
|
114
|
+
});
|
|
115
|
+
`,
|
|
116
|
+
};
|
|
117
|
+
if (minimal) return files;
|
|
118
|
+
return {
|
|
119
|
+
...files,
|
|
120
|
+
'actions/remove.ts': `import { defineAction } from '@bitakit/core';
|
|
121
|
+
|
|
122
|
+
export default defineAction<number>({
|
|
123
|
+
title: 'Remove',
|
|
124
|
+
// Each row tracks its own pending state.
|
|
125
|
+
concurrency: 'per-input',
|
|
126
|
+
execute: ({ services, input }) => ${items}.remove(input),
|
|
127
|
+
});
|
|
128
|
+
`,
|
|
129
|
+
'actions/clear.ts': `import { defineAction } from '@bitakit/core';
|
|
130
|
+
|
|
131
|
+
export default defineAction({
|
|
132
|
+
title: 'Clear all',
|
|
133
|
+
// Re-evaluated whenever the items store changes.
|
|
134
|
+
enabled: ({ services }) => ${items}.state.get().items.length > 0,
|
|
135
|
+
confirm: 'Remove all items?',
|
|
136
|
+
execute: ({ services }) => {
|
|
137
|
+
for (const item of ${items}.state.get().items) ${items}.remove(item.id);
|
|
138
|
+
},
|
|
139
|
+
success: 'All items removed',
|
|
140
|
+
});
|
|
141
|
+
`,
|
|
142
|
+
'actions/open.ts': `import { defineAction } from '@bitakit/core';
|
|
143
|
+
|
|
144
|
+
// The page lives in ui/pages/index.tsx. Placement is explicit; groups are logical queries.
|
|
145
|
+
export default defineAction({ title: '${title}', href: '/${name}', groups: ['navigation']${navigationSlot ? `, slots: [${JSON.stringify(navigationSlot)}]` : ''} });
|
|
146
|
+
`,
|
|
147
|
+
'ui/pages/index.tsx': pageFile(name, `/${name}`, title, { list: true }),
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* The Todo sample (`create-app --sample`), written relative to the plugins folder. It teaches
|
|
153
|
+
* by level: `todos` starts with a page, Actions and a service, adds a setting, then announces
|
|
154
|
+
* changes (`events.ts`) and offers an extension point (`slots/footer.ts`). `activity` is a
|
|
155
|
+
* second plugin that listens to those events and fills that slot without changing `todos`.
|
|
156
|
+
*/
|
|
157
|
+
export function sampleFiles({ navigationSlot } = {}) {
|
|
158
|
+
return {
|
|
159
|
+
'todos/services/items.ts': `import { defineService } from '@bitakit/core';
|
|
160
|
+
|
|
161
|
+
export type Todo = { id: number; title: string };
|
|
162
|
+
|
|
163
|
+
// The todo list. It lives in memory, so a reload starts empty; call your API here later.
|
|
164
|
+
export default defineService(({ store, events }) => {
|
|
165
|
+
const state = store<{ items: Todo[] }>({ items: [] });
|
|
166
|
+
let next = 1;
|
|
167
|
+
return {
|
|
168
|
+
state,
|
|
169
|
+
add(title: string) {
|
|
170
|
+
const item = { id: next++, title: title.trim() };
|
|
171
|
+
state.set((current) => ({ items: [...current.items, item] }));
|
|
172
|
+
events.emit('todos.changed', { kind: 'added', title: item.title });
|
|
173
|
+
},
|
|
174
|
+
remove(id: number) {
|
|
175
|
+
const item = state.get().items.find((entry) => entry.id === id);
|
|
176
|
+
state.set((current) => ({ items: current.items.filter((entry) => entry.id !== id) }));
|
|
177
|
+
if (item) events.emit('todos.changed', { kind: 'removed', title: item.title });
|
|
178
|
+
},
|
|
179
|
+
clear() {
|
|
180
|
+
state.set({ items: [] });
|
|
181
|
+
events.emit('todos.changed', { kind: 'cleared' });
|
|
182
|
+
},
|
|
183
|
+
};
|
|
184
|
+
});
|
|
185
|
+
`,
|
|
186
|
+
'todos/actions/add.ts': `import { defineAction } from '@bitakit/core';
|
|
187
|
+
|
|
188
|
+
export default defineAction<string>({
|
|
189
|
+
title: 'Add',
|
|
190
|
+
// Bound buttons stay disabled while the input is empty or the list is full.
|
|
191
|
+
enabled: ({ input, services, settings }) =>
|
|
192
|
+
!!input?.trim() && services.todos.items.state.get().items.length < settings.todos.limit.get(),
|
|
193
|
+
execute: ({ services, input }) => services.todos.items.add(input),
|
|
194
|
+
});
|
|
195
|
+
`,
|
|
196
|
+
'todos/actions/remove.ts': `import { defineAction } from '@bitakit/core';
|
|
197
|
+
|
|
198
|
+
export default defineAction<number>({
|
|
199
|
+
title: 'Remove',
|
|
200
|
+
// Each row tracks its own pending state.
|
|
201
|
+
concurrency: 'per-input',
|
|
202
|
+
execute: ({ services, input }) => services.todos.items.remove(input),
|
|
203
|
+
});
|
|
204
|
+
`,
|
|
205
|
+
'todos/actions/clear.ts': `import { defineAction } from '@bitakit/core';
|
|
206
|
+
|
|
207
|
+
export default defineAction({
|
|
208
|
+
title: 'Clear all',
|
|
209
|
+
enabled: ({ services }) => services.todos.items.state.get().items.length > 0,
|
|
210
|
+
confirm: 'Delete every todo?',
|
|
211
|
+
execute: ({ services }) => services.todos.items.clear(),
|
|
212
|
+
success: 'All todos deleted',
|
|
213
|
+
});
|
|
214
|
+
`,
|
|
215
|
+
'todos/actions/open.ts': `import { defineAction } from '@bitakit/core';
|
|
216
|
+
|
|
217
|
+
// The menu entry for the page in ui/pages/index.tsx.
|
|
218
|
+
export default defineAction({ title: 'Todos', href: '/todos', groups: ['navigation']${navigationSlot ? `, slots: [${JSON.stringify(navigationSlot)}]` : ''} });
|
|
219
|
+
`,
|
|
220
|
+
'todos/settings.ts': `import { defineSettings, setting } from '@bitakit/core';
|
|
221
|
+
|
|
222
|
+
// A user setting with a default. Actions read it as settings.todos.limit.get().
|
|
223
|
+
export default defineSettings({
|
|
224
|
+
limit: setting({ default: 20, control: 'number', label: 'Maximum todos' }),
|
|
225
|
+
});
|
|
226
|
+
`,
|
|
227
|
+
'todos/events.ts': `import { defineEvents, event } from '@bitakit/core';
|
|
228
|
+
|
|
229
|
+
// Tells other plugins what changed; they listen with events.on('todos.changed', ...).
|
|
230
|
+
export default defineEvents({
|
|
231
|
+
changed: event<{ kind: 'added' | 'removed' | 'cleared'; title?: string }>(),
|
|
232
|
+
});
|
|
233
|
+
`,
|
|
234
|
+
'todos/slots/footer.ts': `import { defineSlot } from '@bitakit/core';
|
|
235
|
+
|
|
236
|
+
// An extension point below the list, todos.footer. Other plugins fill it from ui/slots/todos.footer.tsx.
|
|
237
|
+
export default defineSlot({ accepts: 'component' });
|
|
238
|
+
`,
|
|
239
|
+
'todos/ui/pages/index.tsx': `'use client';
|
|
240
|
+
import { useState } from 'react';
|
|
241
|
+
import { Action, PluginSlot, useServiceState } from '@bitakit/ui';
|
|
242
|
+
import { Button, Input, PageHeader } from '@bitakit/ui/components';
|
|
243
|
+
|
|
244
|
+
// Served at /todos. Each button takes its label, disabled and pending state from its Action.
|
|
245
|
+
export default function TodosPage() {
|
|
246
|
+
const items = useServiceState('todos.items', (state) => state.items, []);
|
|
247
|
+
const [title, setTitle] = useState('');
|
|
248
|
+
return (
|
|
249
|
+
<div className="flex flex-col gap-6">
|
|
250
|
+
<PageHeader title="Todos" subtitle="Add a todo and press Enter. Everything here lives in src/plugins/todos." />
|
|
251
|
+
{/* Enter submits the form, which clicks the bound Add button. */}
|
|
252
|
+
<form className="flex gap-2" onSubmit={(event) => event.preventDefault()}>
|
|
253
|
+
<Input
|
|
254
|
+
value={title}
|
|
255
|
+
placeholder="What needs doing?"
|
|
256
|
+
aria-label="New todo"
|
|
257
|
+
onChange={(event) => setTitle(event.target.value)}
|
|
258
|
+
/>
|
|
259
|
+
<Action id="todos.add" input={title} onExecuted={() => setTitle('')}>
|
|
260
|
+
<Button type="submit" />
|
|
261
|
+
</Action>
|
|
262
|
+
</form>
|
|
263
|
+
<ul className="flex flex-col gap-2">
|
|
264
|
+
{items.map((item) => (
|
|
265
|
+
<li key={item.id} className="flex items-center justify-between gap-2">
|
|
266
|
+
<span className="min-w-0 truncate">{item.title}</span>
|
|
267
|
+
<Action id="todos.remove" input={item.id}>
|
|
268
|
+
<Button variant="ghost" size="sm" />
|
|
269
|
+
</Action>
|
|
270
|
+
</li>
|
|
271
|
+
))}
|
|
272
|
+
</ul>
|
|
273
|
+
<Action id="todos.clear">
|
|
274
|
+
<Button variant="outline" className="self-start" />
|
|
275
|
+
</Action>
|
|
276
|
+
<PluginSlot name="todos.footer" />
|
|
277
|
+
</div>
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
`,
|
|
281
|
+
'activity/services/feed.ts': `import { defineService } from '@bitakit/core';
|
|
282
|
+
|
|
283
|
+
// Listens to the todos plugin without changing it; the listener ends when this plugin stops.
|
|
284
|
+
export default defineService(({ store, events }) => {
|
|
285
|
+
const state = store<{ entries: string[] }>({ entries: [] });
|
|
286
|
+
events.on('todos.changed', ({ data }) => {
|
|
287
|
+
const entry =
|
|
288
|
+
data.kind === 'cleared' ? 'Cleared the list' : \`\${data.kind === 'added' ? 'Added' : 'Removed'} "\${data.title}"\`;
|
|
289
|
+
state.set((current) => ({ entries: [entry, ...current.entries].slice(0, 5) }));
|
|
290
|
+
});
|
|
291
|
+
return { state };
|
|
292
|
+
});
|
|
293
|
+
`,
|
|
294
|
+
'activity/ui/slots/todos.footer.tsx': `'use client';
|
|
295
|
+
import { useServiceState } from '@bitakit/ui';
|
|
296
|
+
|
|
297
|
+
// Fills the todos.footer slot that the todos plugin declares; todos itself is unchanged.
|
|
298
|
+
export default function RecentActivity() {
|
|
299
|
+
const entries = useServiceState('activity.feed', (state) => state.entries, []);
|
|
300
|
+
if (!entries.length) return null;
|
|
301
|
+
return (
|
|
302
|
+
<section aria-label="Recent activity" className="flex flex-col gap-2">
|
|
303
|
+
<p>Recent activity</p>
|
|
304
|
+
<ul className="flex flex-col gap-1">
|
|
305
|
+
{entries.map((entry, index) => (
|
|
306
|
+
<li key={index}>{entry}</li>
|
|
307
|
+
))}
|
|
308
|
+
</ul>
|
|
309
|
+
</section>
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
`,
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
export function pageFile(plugin, path, title, { list = false } = {}) {
|
|
317
|
+
const component = `${pascalOf(path.split('/').filter(Boolean).join('-') || plugin)}Page`;
|
|
318
|
+
if (!list)
|
|
319
|
+
return `'use client';
|
|
320
|
+
import { PageHeader } from '@bitakit/ui/components';
|
|
321
|
+
|
|
322
|
+
// Served at ${path}; the path comes from this file's location under ui/pages.
|
|
323
|
+
export default function ${component}() {
|
|
324
|
+
return <PageHeader title="${title}" subtitle="Edit this page in the ${plugin} plugin." />;
|
|
325
|
+
}
|
|
326
|
+
`;
|
|
327
|
+
return `'use client';
|
|
328
|
+
import { useState } from 'react';
|
|
329
|
+
import { Action, useServiceState } from '@bitakit/ui';
|
|
330
|
+
import { Button, Input, PageHeader } from '@bitakit/ui/components';
|
|
331
|
+
|
|
332
|
+
// Served at ${path}; the path comes from this file's location under ui/pages.
|
|
333
|
+
export default function ${component}() {
|
|
334
|
+
// Re-renders whenever the service's store changes.
|
|
335
|
+
const items = useServiceState('${plugin}.items', (state) => state.items, []);
|
|
336
|
+
const [title, setTitle] = useState('');
|
|
337
|
+
return (
|
|
338
|
+
<div className="flex flex-col gap-6">
|
|
339
|
+
<PageHeader title="${title}" subtitle="Add and remove items. Everything here lives in the ${plugin} plugin." />
|
|
340
|
+
<div className="flex gap-2">
|
|
341
|
+
<Input
|
|
342
|
+
value={title}
|
|
343
|
+
placeholder="New item"
|
|
344
|
+
aria-label="New item"
|
|
345
|
+
onChange={(event) => setTitle(event.target.value)}
|
|
346
|
+
/>
|
|
347
|
+
<Action id="${plugin}.add" input={title} onExecuted={() => setTitle('')}>
|
|
348
|
+
<Button />
|
|
349
|
+
</Action>
|
|
350
|
+
</div>
|
|
351
|
+
<ul className="flex flex-col gap-2">
|
|
352
|
+
{items.map((item) => (
|
|
353
|
+
<li key={item.id} className="flex items-center justify-between gap-2">
|
|
354
|
+
<span className="min-w-0 truncate">{item.title}</span>
|
|
355
|
+
<Action id="${plugin}.remove" input={item.id}>
|
|
356
|
+
<Button variant="ghost" size="sm" />
|
|
357
|
+
</Action>
|
|
358
|
+
</li>
|
|
359
|
+
))}
|
|
360
|
+
</ul>
|
|
361
|
+
<Action id="${plugin}.clear">
|
|
362
|
+
<Button variant="outline" className="self-start" />
|
|
363
|
+
</Action>
|
|
364
|
+
</div>
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
`;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
export function actionFile(name) {
|
|
371
|
+
const segments = name.split('/');
|
|
372
|
+
segments.forEach((segment) => validateName(segment, 'action name'));
|
|
373
|
+
return `import { defineAction } from '@bitakit/core';
|
|
374
|
+
|
|
375
|
+
export default defineAction({
|
|
376
|
+
title: '${titleOf(segments.at(-1))}',
|
|
377
|
+
// Services are services.<plugin>.<name>; reading an unknown one fails with a named error.
|
|
378
|
+
execute: ({ services }) => {
|
|
379
|
+
// Call your services here, for example services.notes.items.add('…').
|
|
380
|
+
},
|
|
381
|
+
});
|
|
382
|
+
`;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/** Retains the generated fields and store; navigation and body placement are automatic. */
|
|
386
|
+
export function settingsPageFile() {
|
|
387
|
+
return `'use client';
|
|
388
|
+
import type { ReactNode } from 'react';
|
|
389
|
+
import type { SettingsCatalogSection, SettingsCatalogField } from '@bitakit/app-preferences';
|
|
390
|
+
|
|
391
|
+
// Requires @bitakit/app-preferences. Add custom content alongside the canonical fields.
|
|
392
|
+
// No Action, navigation entry or slot registration is needed for this page.
|
|
393
|
+
export default function SettingsPage({ section, renderField }: {
|
|
394
|
+
section: SettingsCatalogSection;
|
|
395
|
+
renderField: (field: SettingsCatalogField) => ReactNode;
|
|
396
|
+
}) {
|
|
397
|
+
return <>{section.fields.map(renderField)}</>;
|
|
398
|
+
}
|
|
399
|
+
`;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
export function serviceFile(name, plugin = '<plugin>') {
|
|
403
|
+
const segments = name.split('/');
|
|
404
|
+
segments.forEach((segment) => validateName(segment, 'service name'));
|
|
405
|
+
const id = `${plugin}.${segments.join('.')}`;
|
|
406
|
+
return `import { defineService } from '@bitakit/core';
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Shared state and logic: services.${id} in services and Actions,
|
|
410
|
+
* useServiceState('${id}', (state) => state.count, 0) in React.
|
|
411
|
+
* Async work goes in onStart, cleanup in onStop.
|
|
412
|
+
*/
|
|
413
|
+
export default defineService(({ store }) => {
|
|
414
|
+
const state = store({ count: 0 });
|
|
415
|
+
return {
|
|
416
|
+
state,
|
|
417
|
+
increment: () => state.set((s) => ({ count: s.count + 1 })),
|
|
418
|
+
};
|
|
419
|
+
});
|
|
420
|
+
`;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
export function eventsFile(plugin) {
|
|
424
|
+
return `import { defineEvents, event } from '@bitakit/core';
|
|
425
|
+
|
|
426
|
+
// Names are prefixed with the plugin ID: emit and listen as '${plugin}.<name>'.
|
|
427
|
+
export default defineEvents({
|
|
428
|
+
changed: event<{ id: number }>(),
|
|
429
|
+
});
|
|
430
|
+
`;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
export function settingsFile(plugin) {
|
|
434
|
+
return `import { defineSettings, setting } from '@bitakit/core';
|
|
435
|
+
|
|
436
|
+
// Saved preferences of this feature. Read them as settings.${plugin}.<name>.get() in services and
|
|
437
|
+
// Actions, or useSetting('${plugin}.<name>').value in React. Each gets a field on /settings/${plugin}.
|
|
438
|
+
export default defineSettings({
|
|
439
|
+
pageSize: setting({ default: 20, control: 'number', label: 'Items per page' }),
|
|
440
|
+
});
|
|
441
|
+
`;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
export function layoutFile(name) {
|
|
445
|
+
const component = `${pascalOf(name)}Layout`;
|
|
446
|
+
return `import type { ReactNode } from 'react';
|
|
447
|
+
|
|
448
|
+
// Pages choose this layout with: export const layout = '${name}';
|
|
449
|
+
export default function ${component}({ children }: { children: ReactNode }) {
|
|
450
|
+
return <div className="mx-auto flex w-full max-w-6xl flex-1 flex-col gap-6 p-4 md:p-6">{children}</div>;
|
|
451
|
+
}
|
|
452
|
+
`;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
export function slotFile(plugin, slot) {
|
|
456
|
+
const component = `${pascalOf(slot.replace(/\./g, '-'))}Slot`;
|
|
457
|
+
return `'use client';
|
|
458
|
+
|
|
459
|
+
// A widget for the "${slot}" extension point; its owner renders it with <PluginSlot name="${slot}" />.
|
|
460
|
+
// It appears once both plugins are ready. Optional: export const order = 1 to sort widgets.
|
|
461
|
+
export default function ${component}() {
|
|
462
|
+
return <span className="text-sm text-muted-foreground">${plugin}</span>;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
`;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/** An extension point this plugin owns; `slotFile` contributes a widget to one. */
|
|
469
|
+
export function slotDefinitionFile(id = '<plugin>.<name>') {
|
|
470
|
+
return `import { defineSlot } from '@bitakit/core';
|
|
471
|
+
|
|
472
|
+
// An extension point other plugins can fill. Render it where it belongs: <PluginSlot name="${id}" />.
|
|
473
|
+
// Another plugin adds a widget with: npx bitakit add slot <their-plugin>/${id}
|
|
474
|
+
export default defineSlot({ accepts: 'component', policy: 'list' });
|
|
475
|
+
`;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
export function dialogFile(plugin, name) {
|
|
479
|
+
const component = `${pascalOf(name)}Dialog`;
|
|
480
|
+
return `'use client';
|
|
481
|
+
import { Button } from '@bitakit/ui/components';
|
|
482
|
+
|
|
483
|
+
// Opened by an action with dialog: ${component}; the host decides the frame (dialog, sheet, drawer).
|
|
484
|
+
export default function ${component}({ close }: { input?: unknown; close: () => void }) {
|
|
485
|
+
return (
|
|
486
|
+
<div className="flex flex-col gap-4">
|
|
487
|
+
<p className="text-sm text-muted-foreground">Content of ${plugin}.${name}.</p>
|
|
488
|
+
<Button type="button" onClick={close}>
|
|
489
|
+
Done
|
|
490
|
+
</Button>
|
|
491
|
+
</div>
|
|
492
|
+
);
|
|
493
|
+
}
|
|
494
|
+
`;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
export function providerFile(plugin, name) {
|
|
498
|
+
const component = `${pascalOf(name)}Provider`;
|
|
499
|
+
return `'use client';
|
|
500
|
+
import type { ReactNode } from 'react';
|
|
501
|
+
|
|
502
|
+
// Wrapped around the app by BitaKitProvider; export const requires = ['other-plugin.provider'] to order providers.
|
|
503
|
+
export default function ${component}({ children }: { children: ReactNode }) {
|
|
504
|
+
return children;
|
|
505
|
+
}
|
|
506
|
+
`;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
export function interceptorFile(plugin, name) {
|
|
510
|
+
return `import { defineInterceptor } from '@bitakit/core';
|
|
511
|
+
|
|
512
|
+
// Observes every action of this plugin; use action: '${plugin}.<name>' for one of them.
|
|
513
|
+
export default defineInterceptor({
|
|
514
|
+
action: '${plugin}.*',
|
|
515
|
+
before: ({ id, input }) => {
|
|
516
|
+
console.debug('[${plugin}] ${name}', id, input);
|
|
517
|
+
},
|
|
518
|
+
});
|
|
519
|
+
`;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
export function configFile(name) {
|
|
523
|
+
return `import { defineConfig } from '@bitakit/core';
|
|
524
|
+
|
|
525
|
+
// Read as config.plugin.get('${name}.enabled', true) in contributions, or app.config.<plugin>.${name}.
|
|
526
|
+
export default defineConfig({
|
|
527
|
+
defaults: { enabled: true },
|
|
528
|
+
});
|
|
529
|
+
`;
|
|
530
|
+
}
|