@bitakit/cli 0.2.1 → 0.2.3
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/README.md +128 -11
- package/package.json +4 -3
- package/skills/bitakit-app-development/README.md +121 -0
- package/skills/bitakit-app-development/SKILL.md +147 -0
- package/skills/bitakit-app-development/references/actions.md +198 -0
- package/skills/bitakit-app-development/references/interceptors.md +164 -0
- package/skills/bitakit-app-development/references/plugins-and-pages.md +188 -0
- package/skills/bitakit-app-development/references/services-and-events.md +173 -0
- package/skills/bitakit-app-development/references/settings.md +183 -0
- package/skills/bitakit-app-development/references/slots.md +165 -0
- package/skills/bitakit-app-development/references/testing-and-preview.md +171 -0
- package/src/add.mjs +96 -4
- package/src/agent-rules.mjs +42 -3
- package/src/agent-skill.mjs +152 -0
- package/src/check-page.mjs +385 -0
- package/src/cli.mjs +109 -18
- package/src/doctor-agent-files.mjs +66 -0
- package/src/doctor.mjs +17 -2
- package/src/index.mjs +9 -0
- package/src/init.mjs +65 -21
- package/src/slots.mjs +227 -0
- package/src/templates.mjs +86 -45
package/src/templates.mjs
CHANGED
|
@@ -53,7 +53,7 @@ import { PageHeader } from '@bitakit/ui/components';
|
|
|
53
53
|
|
|
54
54
|
// Served at /${name}; the file's place under ui/pages is the URL.
|
|
55
55
|
// Grow the feature one file at a time:
|
|
56
|
-
// shared state -> npx bitakit add service ${name}
|
|
56
|
+
// shared state -> npx bitakit add service ${name}/<name> (starts as a counter store)
|
|
57
57
|
// a button -> npx bitakit add action ${name}/archive (bind it with <Action id="${name}.archive">)
|
|
58
58
|
export default function ${component}() {
|
|
59
59
|
return <PageHeader title="${title}" subtitle="Edit this page in src/plugins/${name}/ui/pages/index.tsx." />;
|
|
@@ -69,28 +69,28 @@ export default function ${component}() {
|
|
|
69
69
|
export function pluginFiles(name, { minimal = false, navigationSlot } = {}) {
|
|
70
70
|
validateName(name, 'plugin name');
|
|
71
71
|
const title = titleOf(name);
|
|
72
|
-
const
|
|
72
|
+
const list = `services${member(name)}.list`;
|
|
73
73
|
const files = {
|
|
74
|
-
'services/
|
|
74
|
+
'services/list.ts': `import { defineService } from '@bitakit/core';
|
|
75
75
|
|
|
76
|
-
export type
|
|
76
|
+
export type Entry = { id: number; title: string };
|
|
77
77
|
|
|
78
78
|
/**
|
|
79
|
-
* The ${title} plugin's data and behavior.
|
|
79
|
+
* The ${title} plugin's data and behavior. Entries live in memory, so a page reload starts
|
|
80
80
|
* empty. Replace the body with calls to your API or database when you are ready.
|
|
81
81
|
*/
|
|
82
82
|
export default defineService(({ store, events }) => {
|
|
83
83
|
// Observable state: actions and pages follow it without any wiring.
|
|
84
84
|
// Resources belong in onStart/onStop; Core awaits cleanup before replacement.
|
|
85
|
-
const state = store<{
|
|
85
|
+
const state = store<{ entries: Entry[] }>({ entries: [] });
|
|
86
86
|
return {
|
|
87
87
|
state,
|
|
88
88
|
add(title: string) {
|
|
89
|
-
state.set((s) => ({
|
|
89
|
+
state.set((s) => ({ entries: [...s.entries, { id: Date.now(), title: title.trim() }] }));
|
|
90
90
|
events.emit('${name}.changed', { kind: 'added' });
|
|
91
91
|
},
|
|
92
92
|
remove(id: number) {
|
|
93
|
-
state.set((s) => ({
|
|
93
|
+
state.set((s) => ({ entries: s.entries.filter((entry) => entry.id !== id) }));
|
|
94
94
|
events.emit('${name}.changed', { kind: 'removed' });
|
|
95
95
|
},
|
|
96
96
|
};
|
|
@@ -106,11 +106,11 @@ export default defineEvents({
|
|
|
106
106
|
'actions/add.ts': `import { defineAction } from '@bitakit/core';
|
|
107
107
|
|
|
108
108
|
export default defineAction<string>({
|
|
109
|
-
title: 'Add
|
|
109
|
+
title: 'Add entry',
|
|
110
110
|
// Buttons bound to this action are disabled while their input is empty.
|
|
111
111
|
enabled: ({ input }) => typeof input === 'string' && input.trim().length > 0,
|
|
112
112
|
// Runs when a bound button or your code executes "${name}.add".
|
|
113
|
-
execute: ({ services, input }) => ${
|
|
113
|
+
execute: ({ services, input }) => ${list}.add(input),
|
|
114
114
|
});
|
|
115
115
|
`,
|
|
116
116
|
};
|
|
@@ -123,20 +123,20 @@ export default defineAction<number>({
|
|
|
123
123
|
title: 'Remove',
|
|
124
124
|
// Each row tracks its own pending state.
|
|
125
125
|
concurrency: 'per-input',
|
|
126
|
-
execute: ({ services, input }) => ${
|
|
126
|
+
execute: ({ services, input }) => ${list}.remove(input),
|
|
127
127
|
});
|
|
128
128
|
`,
|
|
129
129
|
'actions/clear.ts': `import { defineAction } from '@bitakit/core';
|
|
130
130
|
|
|
131
131
|
export default defineAction({
|
|
132
132
|
title: 'Clear all',
|
|
133
|
-
// Re-evaluated whenever the
|
|
134
|
-
enabled: ({ services }) => ${
|
|
135
|
-
confirm: 'Remove all
|
|
133
|
+
// Re-evaluated whenever the list's store changes.
|
|
134
|
+
enabled: ({ services }) => ${list}.state.get().entries.length > 0,
|
|
135
|
+
confirm: 'Remove all entries?',
|
|
136
136
|
execute: ({ services }) => {
|
|
137
|
-
for (const
|
|
137
|
+
for (const entry of ${list}.state.get().entries) ${list}.remove(entry.id);
|
|
138
138
|
},
|
|
139
|
-
success: 'All
|
|
139
|
+
success: 'All entries removed',
|
|
140
140
|
});
|
|
141
141
|
`,
|
|
142
142
|
'actions/open.ts': `import { defineAction } from '@bitakit/core';
|
|
@@ -156,28 +156,28 @@ export default defineAction({ title: '${title}', href: '/${name}', groups: ['nav
|
|
|
156
156
|
*/
|
|
157
157
|
export function sampleFiles({ navigationSlot } = {}) {
|
|
158
158
|
return {
|
|
159
|
-
'todos/services/
|
|
159
|
+
'todos/services/list.ts': `import { defineService } from '@bitakit/core';
|
|
160
160
|
|
|
161
161
|
export type Todo = { id: number; title: string };
|
|
162
162
|
|
|
163
163
|
// The todo list. It lives in memory, so a reload starts empty; call your API here later.
|
|
164
164
|
export default defineService(({ store, events }) => {
|
|
165
|
-
const state = store<{
|
|
165
|
+
const state = store<{ todos: Todo[] }>({ todos: [] });
|
|
166
166
|
let next = 1;
|
|
167
167
|
return {
|
|
168
168
|
state,
|
|
169
169
|
add(title: string) {
|
|
170
|
-
const
|
|
171
|
-
state.set((current) => ({
|
|
172
|
-
events.emit('todos.changed', { kind: 'added', title:
|
|
170
|
+
const todo = { id: next++, title: title.trim() };
|
|
171
|
+
state.set((current) => ({ todos: [...current.todos, todo] }));
|
|
172
|
+
events.emit('todos.changed', { kind: 'added', title: todo.title });
|
|
173
173
|
},
|
|
174
174
|
remove(id: number) {
|
|
175
|
-
const
|
|
176
|
-
state.set((current) => ({
|
|
177
|
-
if (
|
|
175
|
+
const removed = state.get().todos.find((todo) => todo.id === id);
|
|
176
|
+
state.set((current) => ({ todos: current.todos.filter((todo) => todo.id !== id) }));
|
|
177
|
+
if (removed) events.emit('todos.changed', { kind: 'removed', title: removed.title });
|
|
178
178
|
},
|
|
179
179
|
clear() {
|
|
180
|
-
state.set({
|
|
180
|
+
state.set({ todos: [] });
|
|
181
181
|
events.emit('todos.changed', { kind: 'cleared' });
|
|
182
182
|
},
|
|
183
183
|
};
|
|
@@ -189,8 +189,8 @@ export default defineAction<string>({
|
|
|
189
189
|
title: 'Add',
|
|
190
190
|
// Bound buttons stay disabled while the input is empty or the list is full.
|
|
191
191
|
enabled: ({ input, services, settings }) =>
|
|
192
|
-
!!input?.trim() && services.todos.
|
|
193
|
-
execute: ({ services, input }) => services.todos.
|
|
192
|
+
!!input?.trim() && services.todos.list.state.get().todos.length < settings.todos.limit.get(),
|
|
193
|
+
execute: ({ services, input }) => services.todos.list.add(input),
|
|
194
194
|
});
|
|
195
195
|
`,
|
|
196
196
|
'todos/actions/remove.ts': `import { defineAction } from '@bitakit/core';
|
|
@@ -199,16 +199,16 @@ export default defineAction<number>({
|
|
|
199
199
|
title: 'Remove',
|
|
200
200
|
// Each row tracks its own pending state.
|
|
201
201
|
concurrency: 'per-input',
|
|
202
|
-
execute: ({ services, input }) => services.todos.
|
|
202
|
+
execute: ({ services, input }) => services.todos.list.remove(input),
|
|
203
203
|
});
|
|
204
204
|
`,
|
|
205
205
|
'todos/actions/clear.ts': `import { defineAction } from '@bitakit/core';
|
|
206
206
|
|
|
207
207
|
export default defineAction({
|
|
208
208
|
title: 'Clear all',
|
|
209
|
-
enabled: ({ services }) => services.todos.
|
|
209
|
+
enabled: ({ services }) => services.todos.list.state.get().todos.length > 0,
|
|
210
210
|
confirm: 'Delete every todo?',
|
|
211
|
-
execute: ({ services }) => services.todos.
|
|
211
|
+
execute: ({ services }) => services.todos.list.clear(),
|
|
212
212
|
success: 'All todos deleted',
|
|
213
213
|
});
|
|
214
214
|
`,
|
|
@@ -243,7 +243,7 @@ import { Button, Input, PageHeader } from '@bitakit/ui/components';
|
|
|
243
243
|
|
|
244
244
|
// Served at /todos. Each button takes its label, disabled and pending state from its Action.
|
|
245
245
|
export default function TodosPage() {
|
|
246
|
-
const
|
|
246
|
+
const todos = useServiceState('todos.list', (state) => state.todos, []);
|
|
247
247
|
const [title, setTitle] = useState('');
|
|
248
248
|
return (
|
|
249
249
|
<div className="flex flex-col gap-6">
|
|
@@ -261,10 +261,10 @@ export default function TodosPage() {
|
|
|
261
261
|
</Action>
|
|
262
262
|
</form>
|
|
263
263
|
<ul className="flex flex-col gap-2">
|
|
264
|
-
{
|
|
265
|
-
<li key={
|
|
266
|
-
<span className="min-w-0 truncate">{
|
|
267
|
-
<Action id="todos.remove" input={
|
|
264
|
+
{todos.map((todo) => (
|
|
265
|
+
<li key={todo.id} className="flex items-center justify-between gap-2">
|
|
266
|
+
<span className="min-w-0 truncate">{todo.title}</span>
|
|
267
|
+
<Action id="todos.remove" input={todo.id}>
|
|
268
268
|
<Button variant="ghost" size="sm" />
|
|
269
269
|
</Action>
|
|
270
270
|
</li>
|
|
@@ -332,16 +332,16 @@ import { Button, Input, PageHeader } from '@bitakit/ui/components';
|
|
|
332
332
|
// Served at ${path}; the path comes from this file's location under ui/pages.
|
|
333
333
|
export default function ${component}() {
|
|
334
334
|
// Re-renders whenever the service's store changes.
|
|
335
|
-
const
|
|
335
|
+
const entries = useServiceState('${plugin}.list', (state) => state.entries, []);
|
|
336
336
|
const [title, setTitle] = useState('');
|
|
337
337
|
return (
|
|
338
338
|
<div className="flex flex-col gap-6">
|
|
339
|
-
<PageHeader title="${title}" subtitle="Add and remove
|
|
339
|
+
<PageHeader title="${title}" subtitle="Add and remove entries. Everything here lives in the ${plugin} plugin." />
|
|
340
340
|
<div className="flex gap-2">
|
|
341
341
|
<Input
|
|
342
342
|
value={title}
|
|
343
|
-
placeholder="New
|
|
344
|
-
aria-label="New
|
|
343
|
+
placeholder="New entry"
|
|
344
|
+
aria-label="New entry"
|
|
345
345
|
onChange={(event) => setTitle(event.target.value)}
|
|
346
346
|
/>
|
|
347
347
|
<Action id="${plugin}.add" input={title} onExecuted={() => setTitle('')}>
|
|
@@ -349,10 +349,10 @@ export default function ${component}() {
|
|
|
349
349
|
</Action>
|
|
350
350
|
</div>
|
|
351
351
|
<ul className="flex flex-col gap-2">
|
|
352
|
-
{
|
|
353
|
-
<li key={
|
|
354
|
-
<span className="min-w-0 truncate">{
|
|
355
|
-
<Action id="${plugin}.remove" input={
|
|
352
|
+
{entries.map((entry) => (
|
|
353
|
+
<li key={entry.id} className="flex items-center justify-between gap-2">
|
|
354
|
+
<span className="min-w-0 truncate">{entry.title}</span>
|
|
355
|
+
<Action id="${plugin}.remove" input={entry.id}>
|
|
356
356
|
<Button variant="ghost" size="sm" />
|
|
357
357
|
</Action>
|
|
358
358
|
</li>
|
|
@@ -367,16 +367,18 @@ export default function ${component}() {
|
|
|
367
367
|
`;
|
|
368
368
|
}
|
|
369
369
|
|
|
370
|
-
|
|
370
|
+
/** `plugin` names the owning plugin in the hint when the caller knows it. */
|
|
371
|
+
export function actionFile(name, plugin) {
|
|
371
372
|
const segments = name.split('/');
|
|
372
373
|
segments.forEach((segment) => validateName(segment, 'action name'));
|
|
374
|
+
const services = plugin ? `services${member(plugin)}` : 'services.<plugin>';
|
|
373
375
|
return `import { defineAction } from '@bitakit/core';
|
|
374
376
|
|
|
375
377
|
export default defineAction({
|
|
376
378
|
title: '${titleOf(segments.at(-1))}',
|
|
377
379
|
// Services are services.<plugin>.<name>; reading an unknown one fails with a named error.
|
|
378
380
|
execute: ({ services }) => {
|
|
379
|
-
// Call your services here, for example services
|
|
381
|
+
// Call your services here, for example ${services}.<name>.<method>().
|
|
380
382
|
},
|
|
381
383
|
});
|
|
382
384
|
`;
|
|
@@ -519,6 +521,45 @@ export default defineInterceptor({
|
|
|
519
521
|
`;
|
|
520
522
|
}
|
|
521
523
|
|
|
524
|
+
/**
|
|
525
|
+
* A headless test of one app plugin (`bitakit add test`), run from the app root with `node --test`.
|
|
526
|
+
* `directory` is the plugin folder relative to that root; `id` is only needed when the folder is
|
|
527
|
+
* not under a `plugins` directory. `shellSlots` are the Shell slots its Actions are placed in:
|
|
528
|
+
* without Shell in the test, a stand-in has to own them or startup fails.
|
|
529
|
+
*/
|
|
530
|
+
export function testFile(plugin, { directory, id, shellSlots = [] }) {
|
|
531
|
+
const options = [
|
|
532
|
+
...(id ? [`id: '${id}'`] : []),
|
|
533
|
+
...(shellSlots.length ? ['plugins: [shell]'] : []),
|
|
534
|
+
];
|
|
535
|
+
const slots = shellSlots.map(
|
|
536
|
+
(slot) =>
|
|
537
|
+
`${/^[A-Za-z_$][\w$]*$/.test(slot) ? slot : `'${slot}'`}: defineSlot({ accepts: 'actions' })`,
|
|
538
|
+
);
|
|
539
|
+
const shell = shellSlots.length
|
|
540
|
+
? `
|
|
541
|
+
// ${plugin} places Actions in Shell slots; this stand-in owns them so the plugin starts without Shell.
|
|
542
|
+
const shell = definePlugin({
|
|
543
|
+
id: 'shell',
|
|
544
|
+
slots: ${slots.length === 1 ? `{ ${slots[0]} }` : `{\n ${slots.join(',\n ')},\n }`},
|
|
545
|
+
});
|
|
546
|
+
`
|
|
547
|
+
: '';
|
|
548
|
+
return `import assert from 'node:assert/strict';
|
|
549
|
+
import test from 'node:test';
|
|
550
|
+
${shellSlots.length ? "import { definePlugin, defineSlot } from '@bitakit/core';\n" : ''}import { mountPlugin } from '@bitakit/core/testing';
|
|
551
|
+
${shell}
|
|
552
|
+
test('the ${plugin} plugin starts headlessly', async () => {
|
|
553
|
+
const plugin = await mountPlugin('${directory}'${options.length ? `, { ${options.join(', ')} }` : ''});
|
|
554
|
+
try {
|
|
555
|
+
assert.equal(plugin.runtime.isReady(plugin.id), true);
|
|
556
|
+
} finally {
|
|
557
|
+
await plugin.stop();
|
|
558
|
+
}
|
|
559
|
+
});
|
|
560
|
+
`;
|
|
561
|
+
}
|
|
562
|
+
|
|
522
563
|
export function configFile(name) {
|
|
523
564
|
return `import { defineConfig } from '@bitakit/core';
|
|
524
565
|
|