@bitakit/core 0.1.2 → 0.2.1
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 +44 -207
- package/discovery/index.d.ts +1 -41
- package/discovery/index.mjs +1 -4
- package/dist/actions/action-list.d.ts +4 -0
- package/dist/actions/action-list.js +58 -16
- package/dist/actions/action.d.ts +2 -0
- package/dist/actions/action.js +4 -0
- package/dist/actions/brand.d.ts +21 -0
- package/dist/actions/brand.js +36 -0
- package/dist/actions/core.d.ts +4 -3
- package/dist/actions/core.js +4 -2
- package/dist/actions/execution.d.ts +2 -2
- package/dist/actions/execution.js +61 -9
- package/dist/actions/feedback.d.ts +2 -0
- package/dist/actions/feedback.js +8 -0
- package/dist/actions/groups.js +5 -1
- package/dist/actions/instance-bindings.js +3 -2
- package/dist/actions/interceptors.d.ts +9 -3
- package/dist/actions/interceptors.js +31 -3
- package/dist/actions/navigation.d.ts +2 -1
- package/dist/actions/navigation.js +10 -5
- package/dist/actions/reference.js +10 -5
- package/dist/actions/types.d.ts +9 -1
- package/dist/app/access.d.ts +4 -4
- package/dist/app/access.js +33 -71
- package/dist/app/action-access.d.ts +2 -2
- package/dist/app/action-access.js +6 -4
- package/dist/app/service-error.d.ts +2 -2
- package/dist/app/service-error.js +10 -6
- package/dist/app/types.d.ts +76 -33
- package/dist/config-service.d.ts +30 -6
- package/dist/config-service.js +25 -9
- package/dist/configuration.d.ts +6 -3
- package/dist/configuration.js +8 -6
- package/dist/contracts.d.ts +4 -2
- package/dist/contracts.js +3 -3
- package/dist/contribution-origin.d.ts +4 -1
- package/dist/contribution-origin.js +14 -1
- package/dist/contributions/action.d.ts +26 -0
- package/dist/contributions/action.js +171 -0
- package/dist/contributions/host.d.ts +14 -11
- package/dist/contributions/index.d.ts +1 -26
- package/dist/contributions/index.js +1 -18
- package/dist/conventions.d.ts +8 -4
- package/dist/conventions.js +12 -31
- package/dist/core.d.ts +41 -18
- package/dist/core.js +25 -13
- package/dist/definitions.d.ts +272 -306
- package/dist/definitions.js +28 -56
- package/dist/destinations.d.ts +25 -0
- package/dist/destinations.js +21 -0
- package/dist/discovered.d.ts +52 -21
- package/dist/discovered.js +89 -35
- package/dist/events/define.d.ts +43 -0
- package/dist/events/define.js +17 -0
- package/dist/events/service.d.ts +35 -8
- package/dist/events/service.js +184 -42
- package/dist/events/types.d.ts +29 -9
- package/dist/inspection/inventory.d.ts +86 -0
- package/dist/inspection/inventory.js +132 -0
- package/dist/interceptors/define.d.ts +76 -0
- package/dist/interceptors/define.js +23 -0
- package/dist/interceptors/service-observers.d.ts +6 -3
- package/dist/interceptors/service-observers.js +13 -10
- package/dist/lifecycle/cleanup.d.ts +5 -0
- package/dist/lifecycle/cleanup.js +41 -0
- package/dist/lifecycle/controller.d.ts +82 -0
- package/dist/lifecycle/controller.js +276 -0
- package/dist/lifecycle/reconcile.d.ts +19 -0
- package/dist/lifecycle/reconcile.js +68 -0
- package/dist/lifecycle/state.d.ts +53 -0
- package/dist/lifecycle/state.js +86 -0
- package/dist/names.d.ts +10 -0
- package/dist/names.js +1 -0
- package/dist/pages.d.ts +39 -6
- package/dist/pages.js +93 -11
- package/dist/presentation.d.ts +9 -0
- package/dist/runtime.d.ts +166 -28
- package/dist/runtime.js +1485 -248
- package/dist/schema.d.ts +13 -0
- package/dist/schema.js +38 -0
- package/dist/scope.d.ts +59 -4
- package/dist/scope.js +133 -11
- package/dist/service-key.d.ts +33 -14
- package/dist/service-key.js +21 -5
- package/dist/service-names.d.ts +39 -0
- package/dist/service-names.js +175 -0
- package/dist/services.d.ts +24 -5
- package/dist/services.js +230 -37
- package/dist/settings/access.d.ts +19 -0
- package/dist/settings/access.js +78 -0
- package/dist/settings/define.d.ts +97 -0
- package/dist/settings/define.js +83 -0
- package/dist/settings/storage.d.ts +45 -0
- package/dist/settings/storage.js +102 -0
- package/dist/settings/store.d.ts +74 -0
- package/dist/settings/store.js +345 -0
- package/dist/slots/action-placement.d.ts +47 -0
- package/dist/slots/action-placement.js +1 -0
- package/dist/slots/define.d.ts +52 -0
- package/dist/slots/define.js +12 -0
- package/dist/slots/normalize.d.ts +7 -0
- package/dist/slots/normalize.js +36 -0
- package/dist/slots/runtime.d.ts +43 -0
- package/dist/slots/runtime.js +348 -0
- package/dist/slots/types.d.ts +113 -0
- package/dist/slots/types.js +1 -0
- package/dist/startup-error.d.ts +21 -0
- package/dist/startup-error.js +29 -0
- package/dist/store.d.ts +24 -0
- package/dist/store.js +40 -0
- package/dist/translation.d.ts +9 -4
- package/dist/translation.js +2 -3
- package/examples/todos/actions/create.mjs +1 -3
- package/examples/todos/actions/delete.mjs +3 -10
- package/examples/todos/actions/list.mjs +1 -3
- package/examples/todos/events.mjs +5 -0
- package/examples/todos/plugin.mjs +4 -2
- package/examples/todos/services/tasks.mjs +14 -20
- package/examples/todos.mjs +8 -7
- package/package.json +36 -9
- package/testing/index.d.ts +59 -0
- package/testing/index.mjs +150 -0
- package/assets/bitakit-banner.png +0 -0
- package/discovery/entries.mjs +0 -124
- package/discovery/generate.mjs +0 -321
- package/discovery/host.mjs +0 -12
- package/discovery/modules.mjs +0 -13
- package/discovery/packages.mjs +0 -57
- package/discovery/watch.mjs +0 -23
- package/dist/app/service-aliases.d.ts +0 -4
- package/dist/app/service-aliases.js +0 -14
- package/dist/contributions/command.d.ts +0 -8
- package/dist/contributions/command.js +0 -59
- package/dist/contributions/dialog.d.ts +0 -8
- package/dist/contributions/dialog.js +0 -18
- package/dist/contributions/form.d.ts +0 -10
- package/dist/contributions/form.js +0 -67
- package/dist/contributions/native.d.ts +0 -11
- package/dist/contributions/native.js +0 -87
- package/dist/contributions/shared.d.ts +0 -19
- package/dist/contributions/shared.js +0 -48
- package/dist/contributions/stateful-action.d.ts +0 -29
- package/dist/contributions/stateful-action.js +0 -33
- package/dist/events/action-setup.d.ts +0 -19
- package/dist/events/action-setup.js +0 -24
- package/dist/interceptors/service.d.ts +0 -37
- package/dist/interceptors/service.js +0 -7
- package/dist/replacements.d.ts +0 -22
- package/dist/replacements.js +0 -54
- package/dist/validation.d.ts +0 -2
- package/dist/validation.js +0 -16
- package/docs/auth.md +0 -106
- package/docs/events.md +0 -157
- package/docs/file-contributions.md +0 -174
- package/docs/reference.md +0 -404
- package/docs/todo-tutorial.md +0 -88
|
@@ -1,14 +1,7 @@
|
|
|
1
1
|
import { defineAction } from '@bitakit/core';
|
|
2
2
|
|
|
3
3
|
export default defineAction({
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
setup({ services, action }) {
|
|
8
|
-
action.enabled = services.todos.list().length > 0;
|
|
9
|
-
},
|
|
10
|
-
onEvent(_event, { services, action }) {
|
|
11
|
-
action.enabled = services.todos.list().length > 0;
|
|
12
|
-
},
|
|
13
|
-
execute: ({ services, input }) => services.todos.delete(input),
|
|
4
|
+
// Re-evaluated whenever the tasks store changes: disabled while the list is empty.
|
|
5
|
+
enabled: ({ services }) => services.todos.tasks.state.get().items.length > 0,
|
|
6
|
+
execute: ({ services, input }) => services.todos.tasks.delete(input),
|
|
14
7
|
});
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { definePlugin } from '@bitakit/core';
|
|
2
2
|
import tasks from './services/tasks.mjs';
|
|
3
|
+
import events from './events.mjs';
|
|
3
4
|
import create from './actions/create.mjs';
|
|
4
5
|
import list from './actions/list.mjs';
|
|
5
6
|
import remove from './actions/delete.mjs';
|
|
6
7
|
|
|
7
8
|
export default definePlugin({
|
|
8
9
|
id: 'todos',
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
events,
|
|
11
|
+
services: { tasks },
|
|
12
|
+
actions: { create, list, delete: remove },
|
|
11
13
|
});
|
|
@@ -1,30 +1,24 @@
|
|
|
1
1
|
import { defineService } from '@bitakit/core';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
function create(title) {
|
|
8
|
-
const task = { id: nextId++, title, done: false };
|
|
9
|
-
tasks.push(task);
|
|
10
|
-
events.emit('todos.changed', { kind: 'added', id: task.id });
|
|
11
|
-
return task;
|
|
12
|
-
}
|
|
3
|
+
/** The Todos data and behavior: an observable store plus the methods that change it. */
|
|
4
|
+
export default defineService(({ store, events }) => {
|
|
5
|
+
const state = store({ items: [], nextId: 1 });
|
|
13
6
|
|
|
14
7
|
return {
|
|
15
|
-
|
|
16
|
-
|
|
8
|
+
state,
|
|
9
|
+
create(title) {
|
|
10
|
+
const task = { id: state.get().nextId, title, done: false };
|
|
11
|
+
state.set((s) => ({ items: [...s.items, task], nextId: s.nextId + 1 }));
|
|
12
|
+
events.emit('todos.changed', { kind: 'added', id: task.id });
|
|
13
|
+
return task;
|
|
14
|
+
},
|
|
15
|
+
list: () => [...state.get().items],
|
|
17
16
|
delete(id) {
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
const removed =
|
|
17
|
+
const before = state.get().items.length;
|
|
18
|
+
state.set((s) => ({ ...s, items: s.items.filter((task) => task.id !== id) }));
|
|
19
|
+
const removed = state.get().items.length < before;
|
|
21
20
|
if (removed) events.emit('todos.changed', { kind: 'removed', id });
|
|
22
21
|
return removed;
|
|
23
22
|
},
|
|
24
23
|
};
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export default defineService({
|
|
28
|
-
id: 'todos',
|
|
29
|
-
create: createTodoService,
|
|
30
24
|
});
|
package/examples/todos.mjs
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createRuntime } from '@bitakit/core';
|
|
2
2
|
import todos from './todos/plugin.mjs';
|
|
3
3
|
|
|
4
|
-
const
|
|
5
|
-
const
|
|
4
|
+
const runtime = createRuntime({ plugins: [todos] });
|
|
5
|
+
const { actions } = runtime;
|
|
6
6
|
const stop = runtime.mount();
|
|
7
7
|
|
|
8
8
|
try {
|
|
9
|
+
console.log('Delete enabled before:', actions.get('todos.delete').enabled);
|
|
9
10
|
await actions.execute('todos.create', 'Learn plugins');
|
|
10
11
|
await actions.execute('todos.create', 'Connect a service');
|
|
11
|
-
|
|
12
|
-
console.log('Before:', await actions.execute('todos.list'));
|
|
12
|
+
console.log('Delete enabled after:', actions.get('todos.delete').enabled);
|
|
13
|
+
console.log('Before:', (await actions.execute('todos.list')).value);
|
|
13
14
|
await actions.execute('todos.delete', 2);
|
|
14
|
-
console.log('After:', await actions.execute('todos.list'));
|
|
15
|
+
console.log('After:', (await actions.execute('todos.list')).value);
|
|
15
16
|
} finally {
|
|
16
|
-
stop();
|
|
17
|
+
await stop();
|
|
17
18
|
}
|
package/package.json
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bitakit/core",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"files": [
|
|
7
7
|
"dist",
|
|
8
8
|
"README.md",
|
|
9
|
+
"assets",
|
|
9
10
|
"discovery",
|
|
10
|
-
"docs",
|
|
11
11
|
"examples",
|
|
12
|
-
"
|
|
12
|
+
"testing"
|
|
13
13
|
],
|
|
14
14
|
"exports": {
|
|
15
15
|
".": {
|
|
@@ -23,6 +23,10 @@
|
|
|
23
23
|
"./discovery": {
|
|
24
24
|
"types": "./discovery/index.d.ts",
|
|
25
25
|
"default": "./discovery/index.mjs"
|
|
26
|
+
},
|
|
27
|
+
"./testing": {
|
|
28
|
+
"types": "./testing/index.d.ts",
|
|
29
|
+
"default": "./testing/index.mjs"
|
|
26
30
|
}
|
|
27
31
|
},
|
|
28
32
|
"scripts": {
|
|
@@ -32,13 +36,26 @@
|
|
|
32
36
|
"dev": "tsc -p tsconfig.json --watch"
|
|
33
37
|
},
|
|
34
38
|
"dependencies": {
|
|
35
|
-
"jiti": "^2.7.0",
|
|
36
|
-
"mitt": "^3.0.1",
|
|
37
|
-
"typescript": "^5.9.3",
|
|
38
39
|
"zustand": "^5.0.0"
|
|
39
40
|
},
|
|
40
|
-
"
|
|
41
|
-
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"@bitakit/discovery": "^0.2.1",
|
|
43
|
+
"jiti": "^2.7.0"
|
|
44
|
+
},
|
|
45
|
+
"peerDependenciesMeta": {
|
|
46
|
+
"@bitakit/discovery": {
|
|
47
|
+
"optional": true
|
|
48
|
+
},
|
|
49
|
+
"jiti": {
|
|
50
|
+
"optional": true
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@bitakit/discovery": "^0.2.1",
|
|
55
|
+
"jiti": "^2.7.0",
|
|
56
|
+
"typescript": "^5.9.3"
|
|
57
|
+
},
|
|
58
|
+
"description": "BitaKit Core: a framework-independent runtime for plugins, services, Actions, events, slots and settings.",
|
|
42
59
|
"license": "MIT",
|
|
43
60
|
"repository": {
|
|
44
61
|
"type": "git",
|
|
@@ -47,5 +64,15 @@
|
|
|
47
64
|
},
|
|
48
65
|
"publishConfig": {
|
|
49
66
|
"access": "public"
|
|
50
|
-
}
|
|
67
|
+
},
|
|
68
|
+
"keywords": [
|
|
69
|
+
"bitakit",
|
|
70
|
+
"ai-agents",
|
|
71
|
+
"composition",
|
|
72
|
+
"runtime",
|
|
73
|
+
"plugins",
|
|
74
|
+
"actions",
|
|
75
|
+
"services",
|
|
76
|
+
"events"
|
|
77
|
+
]
|
|
51
78
|
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
EventService,
|
|
3
|
+
BitaKitApp,
|
|
4
|
+
BitaKitEvent,
|
|
5
|
+
BitaKitRuntime,
|
|
6
|
+
Plugin,
|
|
7
|
+
Services,
|
|
8
|
+
} from '@bitakit/core';
|
|
9
|
+
import type {
|
|
10
|
+
ActionId,
|
|
11
|
+
ActionInput,
|
|
12
|
+
ActionList,
|
|
13
|
+
ActionResult,
|
|
14
|
+
ExecutionResult,
|
|
15
|
+
} from '@bitakit/core/actions';
|
|
16
|
+
|
|
17
|
+
export type MountedPlugin = {
|
|
18
|
+
id: string;
|
|
19
|
+
runtime: BitaKitRuntime;
|
|
20
|
+
app: BitaKitApp;
|
|
21
|
+
/** The runtime's ActionList; IDs known from `ActionMap` type the input and result of `execute`. */
|
|
22
|
+
actions: ActionList;
|
|
23
|
+
/** Services as the plugin's contributions see them: `services.notes.items`. */
|
|
24
|
+
services: Services;
|
|
25
|
+
/** The runtime bus plus every event emitted while mounted. */
|
|
26
|
+
events: EventService & { recorded: Pick<BitaKitEvent, 'type' | 'data'>[] };
|
|
27
|
+
/**
|
|
28
|
+
* Executes `<plugin>.<name>` or a full action ID. IDs known from `ActionMap` type the input and
|
|
29
|
+
* the result through `ActionId` of `@bitakit/core/actions`, so no generated file is imported;
|
|
30
|
+
* short names and unknown IDs stay untyped.
|
|
31
|
+
*/
|
|
32
|
+
execute<Id extends ActionId | (string & {})>(
|
|
33
|
+
name: Id,
|
|
34
|
+
input?: ActionInput<Id>,
|
|
35
|
+
): Promise<ExecutionResult<ActionResult<Id>>>;
|
|
36
|
+
stop(): Promise<void>;
|
|
37
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Mounts one plugin folder headlessly for tests, discovered with the same conventions as apps,
|
|
42
|
+
* including `events.ts` and `setup.ts`. Files under `ui/` are skipped. Resolves after every
|
|
43
|
+
* `onStart`; a failing start rejects with the attributed startup error.
|
|
44
|
+
*/
|
|
45
|
+
export function mountPlugin(
|
|
46
|
+
directory?: string,
|
|
47
|
+
options?: {
|
|
48
|
+
/** Defaults to `id` in the folder's `plugin.config.ts`, the folder under `plugins/`, or the package name. */
|
|
49
|
+
id?: string;
|
|
50
|
+
/**
|
|
51
|
+
* Further plugins mounted alongside: `Plugin` objects (stubs such as a Shell slot owner) or
|
|
52
|
+
* plugin folder paths, discovered like the tested folder.
|
|
53
|
+
*/
|
|
54
|
+
plugins?: readonly (Plugin | string)[];
|
|
55
|
+
/** Host-provided services, such as a fake authentication service. */
|
|
56
|
+
services?: Readonly<Record<string, unknown>>;
|
|
57
|
+
configOverrides?: Readonly<Record<string, Record<string, unknown>>>;
|
|
58
|
+
},
|
|
59
|
+
): Promise<MountedPlugin>;
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { pathToFileURL } from 'node:url';
|
|
4
|
+
import { createJiti } from 'jiti';
|
|
5
|
+
import { describeModule } from '@bitakit/discovery';
|
|
6
|
+
import {
|
|
7
|
+
createRuntime,
|
|
8
|
+
discoverActions,
|
|
9
|
+
discoverConfig,
|
|
10
|
+
discoverEvents,
|
|
11
|
+
discoverSettings,
|
|
12
|
+
discoverInterceptors,
|
|
13
|
+
discoverServices,
|
|
14
|
+
discoverSlots,
|
|
15
|
+
discoveredFile,
|
|
16
|
+
} from '../dist/core.js';
|
|
17
|
+
|
|
18
|
+
/** The plugin ID a package declares (`bitakit.id`), or one derived from its name. */
|
|
19
|
+
async function packageId(directory) {
|
|
20
|
+
for (let folder = directory; ; folder = path.dirname(folder)) {
|
|
21
|
+
const manifest = await readFile(path.join(folder, 'package.json'), 'utf8')
|
|
22
|
+
.then(JSON.parse)
|
|
23
|
+
.catch(() => undefined);
|
|
24
|
+
if (manifest)
|
|
25
|
+
return (
|
|
26
|
+
manifest.bitakit?.id ??
|
|
27
|
+
manifest.foundation?.id ??
|
|
28
|
+
(manifest.name ?? '')
|
|
29
|
+
.replace(/^@[^/]+\//, '')
|
|
30
|
+
.replace(/^bitakit-(plugin-)?/, '')
|
|
31
|
+
.replace(/-plugin$/, '')
|
|
32
|
+
);
|
|
33
|
+
if (path.dirname(folder) === folder) return undefined;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The folder's `plugin.config.ts` (id, dependencies, metadata), loaded the way discovery loads it. */
|
|
38
|
+
async function manifestOf(directory) {
|
|
39
|
+
const file = path.join(directory, 'plugin.config.ts');
|
|
40
|
+
const jiti = createJiti(import.meta.url, { moduleCache: false, fsCache: false });
|
|
41
|
+
return readFile(file, 'utf8')
|
|
42
|
+
.then(() => jiti.import(file, { default: true }))
|
|
43
|
+
.catch((error) => {
|
|
44
|
+
if (error.code === 'ENOENT' && error.path === file) return {};
|
|
45
|
+
throw error;
|
|
46
|
+
})
|
|
47
|
+
.then((manifest) => manifest ?? {});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Builds the headless plugin of one folder: services, actions, events, settings, configs,
|
|
52
|
+
* interceptors, slot definitions and `setup.ts`, discovered with the same conventions as apps.
|
|
53
|
+
* Files under `ui/` are skipped. The ID is `id`, then `plugin.config.ts`, the folder under
|
|
54
|
+
* `plugins/`, or the package name.
|
|
55
|
+
*/
|
|
56
|
+
async function loadPlugin(directory, id) {
|
|
57
|
+
const module = await describeModule(path.resolve(directory));
|
|
58
|
+
const manifest = await manifestOf(module.directory);
|
|
59
|
+
id =
|
|
60
|
+
id ??
|
|
61
|
+
manifest.id ??
|
|
62
|
+
(path.basename(path.dirname(module.directory)) === 'plugins'
|
|
63
|
+
? path.basename(module.directory)
|
|
64
|
+
: await packageId(module.directory));
|
|
65
|
+
if (!id)
|
|
66
|
+
throw new Error(
|
|
67
|
+
`No plugin ID for ${directory}: pass { id }, set one in plugin.config.ts, or run inside a package with a name.`,
|
|
68
|
+
);
|
|
69
|
+
const importDefault = async (file) => (await import(pathToFileURL(file).href)).default;
|
|
70
|
+
const load = (entries) =>
|
|
71
|
+
Promise.all(
|
|
72
|
+
entries.map(async (entry) =>
|
|
73
|
+
discoveredFile(
|
|
74
|
+
await importDefault(entry.file),
|
|
75
|
+
entry.path,
|
|
76
|
+
path.relative(process.cwd(), entry.file),
|
|
77
|
+
),
|
|
78
|
+
),
|
|
79
|
+
);
|
|
80
|
+
const plugin = {
|
|
81
|
+
id,
|
|
82
|
+
dependencies: manifest.dependencies,
|
|
83
|
+
metadata: manifest.metadata,
|
|
84
|
+
services: discoverServices(await load(module.services), id),
|
|
85
|
+
slots: discoverSlots(await load(module.slotDefinitions), id),
|
|
86
|
+
actions: discoverActions(await load(module.actions), id),
|
|
87
|
+
events: discoverEvents(await load(module.events)),
|
|
88
|
+
settings: discoverSettings(await load(module.settings)),
|
|
89
|
+
config: discoverConfig(await load(module.configs)),
|
|
90
|
+
interceptors: discoverInterceptors(await load(module.interceptors)),
|
|
91
|
+
setup: module.setup ? await importDefault(module.setup) : undefined,
|
|
92
|
+
};
|
|
93
|
+
return plugin;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Mounts one plugin folder headlessly for tests (see `loadPlugin` for what is discovered).
|
|
98
|
+
* Files load through Node's TypeScript type stripping, so keep them free of JSX.
|
|
99
|
+
* `options.plugins` adds further plugins: `Plugin` objects or other plugin folders, so an
|
|
100
|
+
* integration test mounts two real folders without a hand-built `definePlugin`.
|
|
101
|
+
* The returned `events.recorded` lists every event emitted while mounted.
|
|
102
|
+
*/
|
|
103
|
+
export async function mountPlugin(directory = 'src', options = {}) {
|
|
104
|
+
const plugin = await loadPlugin(directory, options.id);
|
|
105
|
+
const { id } = plugin;
|
|
106
|
+
const others = await Promise.all(
|
|
107
|
+
(options.plugins ?? []).map((other) =>
|
|
108
|
+
typeof other === 'string' ? loadPlugin(other) : other,
|
|
109
|
+
),
|
|
110
|
+
);
|
|
111
|
+
const runtime = createRuntime(
|
|
112
|
+
{ plugins: [plugin, ...others], configOverrides: options.configOverrides },
|
|
113
|
+
{ services: options.services },
|
|
114
|
+
);
|
|
115
|
+
const recorded = [];
|
|
116
|
+
const unobserve = runtime.events.observe((event) =>
|
|
117
|
+
recorded.push({ type: event.type, data: event.data }),
|
|
118
|
+
);
|
|
119
|
+
const { actions } = runtime;
|
|
120
|
+
let completion;
|
|
121
|
+
const release = () => {
|
|
122
|
+
if (!completion) {
|
|
123
|
+
unobserve();
|
|
124
|
+
completion = runtime.stop();
|
|
125
|
+
}
|
|
126
|
+
return completion;
|
|
127
|
+
};
|
|
128
|
+
try {
|
|
129
|
+
runtime.mount();
|
|
130
|
+
await runtime.ready;
|
|
131
|
+
} catch (error) {
|
|
132
|
+
try { await release(); } catch (cleanupError) {
|
|
133
|
+
throw new AggregateError([error, cleanupError], 'Plugin startup and cleanup failed.');
|
|
134
|
+
}
|
|
135
|
+
throw error;
|
|
136
|
+
}
|
|
137
|
+
return {
|
|
138
|
+
id,
|
|
139
|
+
runtime,
|
|
140
|
+
app: runtime.app,
|
|
141
|
+
actions,
|
|
142
|
+
/** Services as the plugin's contributions see them: `services.notes.items`. */
|
|
143
|
+
services: runtime.services,
|
|
144
|
+
events: Object.assign(runtime.events, { recorded }),
|
|
145
|
+
/** Executes `<plugin>.<name>` (or a full action ID) and returns the execution outcome. */
|
|
146
|
+
execute: (name, input) => actions.execute(name.includes('.') ? name : `${id}.${name}`, input),
|
|
147
|
+
stop: release,
|
|
148
|
+
[Symbol.asyncDispose]: release,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
Binary file
|
package/discovery/entries.mjs
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
import { readdir, readFile } from 'node:fs/promises';
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
import ts from 'typescript';
|
|
4
|
-
|
|
5
|
-
/** Shared traversal for every contribution kind. Nested modules are separate boundaries. */
|
|
6
|
-
export async function files(directory, boundaries = new Set()) {
|
|
7
|
-
let entries;
|
|
8
|
-
try {
|
|
9
|
-
entries = await readdir(directory, { withFileTypes: true });
|
|
10
|
-
} catch (error) {
|
|
11
|
-
if (error.code === 'ENOENT') return [];
|
|
12
|
-
throw error;
|
|
13
|
-
}
|
|
14
|
-
const result = [];
|
|
15
|
-
for (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) {
|
|
16
|
-
const file = path.join(directory, entry.name);
|
|
17
|
-
if (entry.isSymbolicLink()) throw new Error(`Symlink contribution: ${file}`);
|
|
18
|
-
if (entry.isDirectory()) {
|
|
19
|
-
if (['node_modules', '.git', '.foundation', '.next', 'dist'].includes(entry.name)) continue;
|
|
20
|
-
if (!boundaries.has(file)) result.push(...(await files(file, boundaries)));
|
|
21
|
-
} else result.push(file);
|
|
22
|
-
}
|
|
23
|
-
return result;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export const contributionKinds = {
|
|
27
|
-
actions: ['form-action', 'dialog-action', 'action'],
|
|
28
|
-
services: ['service'],
|
|
29
|
-
interceptors: ['interceptor'],
|
|
30
|
-
configs: [],
|
|
31
|
-
providers: ['provider'],
|
|
32
|
-
views: ['view'],
|
|
33
|
-
pages: ['page'],
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Validates integration kinds from `foundation.config.ts`. Each names a contribution kind, the
|
|
38
|
-
* folder that identifies it inside every module root, and the runtime normalizer to import.
|
|
39
|
-
*/
|
|
40
|
-
export function contributionKindSpecs(kinds = []) {
|
|
41
|
-
if (!Array.isArray(kinds)) throw new Error('Discovery "kinds" must be an array.');
|
|
42
|
-
const names = new Set();
|
|
43
|
-
const folders = new Map(
|
|
44
|
-
Object.keys(contributionKinds).map((kind) => [kind, `built-in kind "${kind}"`]),
|
|
45
|
-
);
|
|
46
|
-
return kinds.map((kind) => {
|
|
47
|
-
const { name, directory, from, normalize } = kind ?? {};
|
|
48
|
-
if (typeof name !== 'string' || !/^[a-z][a-z0-9-]*(?:\.[a-z][a-z0-9-]*)*$/.test(name))
|
|
49
|
-
throw new Error(`Invalid contribution kind name: ${JSON.stringify(name)}`);
|
|
50
|
-
if (names.has(name)) throw new Error(`Duplicate contribution kind "${name}".`);
|
|
51
|
-
names.add(name);
|
|
52
|
-
if (Object.hasOwn(contributionKinds, name))
|
|
53
|
-
throw new Error(`Contribution kind "${name}" is built in.`);
|
|
54
|
-
const folder =
|
|
55
|
-
typeof directory === 'string' ? directory.replace(/^\.\//, '').replace(/\/+$/, '') : '';
|
|
56
|
-
if (!folder || folder.split('/').some((part) => !/^[a-z0-9][a-z0-9_-]*$/i.test(part)))
|
|
57
|
-
throw new Error(
|
|
58
|
-
`Contribution kind "${name}" needs a relative folder name, got ${JSON.stringify(directory)}.`,
|
|
59
|
-
);
|
|
60
|
-
const first = folder.split('/')[0];
|
|
61
|
-
if (folders.has(first))
|
|
62
|
-
throw new Error(
|
|
63
|
-
`Contribution kind "${name}" uses folder "${first}", already used by ${folders.get(first)}.`,
|
|
64
|
-
);
|
|
65
|
-
folders.set(first, `kind "${name}"`);
|
|
66
|
-
if (typeof from !== 'string' || !from.trim())
|
|
67
|
-
throw new Error(`Contribution kind "${name}" needs the module that exports its normalizer.`);
|
|
68
|
-
if (typeof normalize !== 'string' || !/^[A-Za-z_$][\w$]*$/.test(normalize))
|
|
69
|
-
throw new Error(`Contribution kind "${name}" needs the normalizer export name.`);
|
|
70
|
-
return Object.freeze({ name, directory: folder, from, normalize, types: kind.types });
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/** Folders and dots have identical meaning. Legacy suffixes are migration aliases. */
|
|
75
|
-
export async function discoverEntries(base, paths, kind, { directory, explicitFile } = {}) {
|
|
76
|
-
const extension = typeof kind === 'object' && kind !== null;
|
|
77
|
-
const suffixes = extension ? [] : contributionKinds[kind];
|
|
78
|
-
if (!suffixes) throw new Error(`Unknown contribution kind: ${kind}`);
|
|
79
|
-
const folder = extension ? kind.directory.split('/') : [kind];
|
|
80
|
-
const label = extension ? kind.name : kind;
|
|
81
|
-
const result = [];
|
|
82
|
-
const seen = new Map();
|
|
83
|
-
for (const file of paths) {
|
|
84
|
-
if (!/\.tsx?$/.test(file) || /\.d\.ts$/.test(file)) continue;
|
|
85
|
-
const relative = path.relative(base, file);
|
|
86
|
-
if (!directory && !explicitFile && (relative.startsWith('..') || path.isAbsolute(relative)))
|
|
87
|
-
continue;
|
|
88
|
-
let segments = relative.replace(/\.tsx?$/, '').split(/[\\/.]/);
|
|
89
|
-
if (segments.some((segment) => segment.startsWith('-'))) continue;
|
|
90
|
-
// This discovery entry emits client-importable contributions only.
|
|
91
|
-
if (segments.includes('server')) continue;
|
|
92
|
-
if (explicitFile === file) segments = [path.basename(file).replace(/\.tsx?$/, '')];
|
|
93
|
-
else if (directory) {
|
|
94
|
-
const local = path.relative(directory, file);
|
|
95
|
-
if (local.startsWith('..') || path.isAbsolute(local)) continue;
|
|
96
|
-
segments = local.replace(/\.tsx?$/, '').split(/[\\/.]/);
|
|
97
|
-
} else {
|
|
98
|
-
if (folder.some((part, index) => segments[index] !== part)) continue;
|
|
99
|
-
segments = segments.slice(folder.length);
|
|
100
|
-
}
|
|
101
|
-
const legacy = suffixes.includes(segments.at(-1));
|
|
102
|
-
if (legacy) segments.pop();
|
|
103
|
-
if (!legacy && segments.at(-1) === 'index') segments.pop();
|
|
104
|
-
if (segments.some((segment) => !segment)) throw new Error(`Empty contribution path: ${file}`);
|
|
105
|
-
const sourceText = await readFile(file, 'utf8');
|
|
106
|
-
const syntax = ts.createSourceFile(file, sourceText, ts.ScriptTarget.Latest, false);
|
|
107
|
-
const hasDefault = syntax.statements.some(
|
|
108
|
-
(statement) =>
|
|
109
|
-
(ts.isExportAssignment(statement) && !statement.isExportEquals) ||
|
|
110
|
-
statement.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.DefaultKeyword) ||
|
|
111
|
-
(ts.isExportDeclaration(statement) &&
|
|
112
|
-
statement.exportClause &&
|
|
113
|
-
ts.isNamedExports(statement.exportClause) &&
|
|
114
|
-
statement.exportClause.elements.some((element) => element.name.text === 'default')),
|
|
115
|
-
);
|
|
116
|
-
if (!hasDefault) continue;
|
|
117
|
-
const key = segments.join('.');
|
|
118
|
-
if (seen.has(key))
|
|
119
|
-
throw new Error(`Duplicate ${label} path "${key}": ${seen.get(key)} and ${file}`);
|
|
120
|
-
seen.set(key, file);
|
|
121
|
-
result.push({ file, path: segments, source: relative });
|
|
122
|
-
}
|
|
123
|
-
return result;
|
|
124
|
-
}
|