@bitakit/core 0.1.1 → 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/README.md +47 -161
- package/assets/bitakit-logo-dark.svg +11 -0
- package/assets/bitakit-logo.svg +11 -0
- 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 +37 -9
- package/testing/index.d.ts +59 -0
- package/testing/index.mjs +150 -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
package/discovery/generate.mjs
DELETED
|
@@ -1,321 +0,0 @@
|
|
|
1
|
-
import { readFile, writeFile, mkdir, realpath } from 'node:fs/promises';
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
import { files, discoverEntries, contributionKindSpecs } from './entries.mjs';
|
|
4
|
-
import { createJiti } from 'jiti';
|
|
5
|
-
import { installedContributions } from './packages.mjs';
|
|
6
|
-
import { pluginManifests } from './modules.mjs';
|
|
7
|
-
const quote = JSON.stringify;
|
|
8
|
-
async function writeChanged(file, content) {
|
|
9
|
-
if ((await readFile(file, 'utf8').catch(() => null)) !== content) await writeFile(file, content);
|
|
10
|
-
}
|
|
11
|
-
/** Only static imports are generated. Services/actions are never executed by discovery. */
|
|
12
|
-
export async function generateFoundation(root) {
|
|
13
|
-
root = await realpath(root);
|
|
14
|
-
const jiti = createJiti(import.meta.url, {
|
|
15
|
-
moduleCache: false,
|
|
16
|
-
fsCache: false,
|
|
17
|
-
});
|
|
18
|
-
const configFile = path.join(root, 'foundation.config.ts');
|
|
19
|
-
// Missing customization is the default case; errors inside an existing config must propagate.
|
|
20
|
-
const config = await readFile(configFile, 'utf8')
|
|
21
|
-
.then(() => jiti.import(configFile, { default: true }))
|
|
22
|
-
.catch((error) => {
|
|
23
|
-
if (error.code === 'ENOENT' && error.path === configFile) return {};
|
|
24
|
-
throw error;
|
|
25
|
-
});
|
|
26
|
-
const directory = path.resolve(root, config.plugins?.directory ?? './src/plugins');
|
|
27
|
-
const installed = await installedContributions(root, config.packages, jiti);
|
|
28
|
-
const output = path.join(root, '.foundation');
|
|
29
|
-
await mkdir(output, { recursive: true });
|
|
30
|
-
const roots = pluginManifests(directory, await files(directory));
|
|
31
|
-
const imports = [];
|
|
32
|
-
const definitions = [];
|
|
33
|
-
const serviceTypes = [];
|
|
34
|
-
const publicServiceTypes = [];
|
|
35
|
-
const configTypes = [];
|
|
36
|
-
const allMessages = {};
|
|
37
|
-
const pluginIds = new Set();
|
|
38
|
-
let serial = 0;
|
|
39
|
-
const importFile = (file) => {
|
|
40
|
-
const name = `entry${serial++}`;
|
|
41
|
-
let relative = path
|
|
42
|
-
.relative(output, file)
|
|
43
|
-
.replaceAll(path.sep, '/')
|
|
44
|
-
.replace(/\.tsx?$/, '');
|
|
45
|
-
if (!relative.startsWith('.')) relative = `./${relative}`;
|
|
46
|
-
imports.push(`import ${name} from ${quote(relative)};`);
|
|
47
|
-
return name;
|
|
48
|
-
};
|
|
49
|
-
const conventions = config.conventions !== false;
|
|
50
|
-
// Explicit discovery configuration replaces package defaults by contribution kind.
|
|
51
|
-
const kinds = contributionKindSpecs([
|
|
52
|
-
...new Map(
|
|
53
|
-
[...contributionKindSpecs(installed.kinds), ...contributionKindSpecs(config.kinds)].map(
|
|
54
|
-
(kind) => [kind.name, kind],
|
|
55
|
-
),
|
|
56
|
-
).values(),
|
|
57
|
-
]);
|
|
58
|
-
const normalizers = new Map();
|
|
59
|
-
const contributionTypes = new Map();
|
|
60
|
-
/** Emits `{ kind: normalize([files], moduleId) }` for kinds with files; normalizers run at import time. */
|
|
61
|
-
const contributions = (entriesByKind, moduleId) => {
|
|
62
|
-
const parts = [];
|
|
63
|
-
kinds.forEach((kind, index) => {
|
|
64
|
-
const entries = entriesByKind[index];
|
|
65
|
-
if (!entries.length) return;
|
|
66
|
-
if (!normalizers.has(kind.name)) {
|
|
67
|
-
const alias = `normalize${index}`;
|
|
68
|
-
imports.push(`import { ${kind.normalize} as ${alias} } from ${quote(kind.from)};`);
|
|
69
|
-
normalizers.set(kind.name, alias);
|
|
70
|
-
}
|
|
71
|
-
const names = fileEntries(entries);
|
|
72
|
-
if (kind.types) {
|
|
73
|
-
if (!contributionTypes.has(kind.name)) {
|
|
74
|
-
imports.push(
|
|
75
|
-
`import type { ${kind.types.entry} as KindEntry${index} } from ${quote(kind.from)};`,
|
|
76
|
-
);
|
|
77
|
-
contributionTypes.set(kind.name, { kind, index, entries: [] });
|
|
78
|
-
}
|
|
79
|
-
contributionTypes
|
|
80
|
-
.get(kind.name)
|
|
81
|
-
.entries.push(
|
|
82
|
-
...names.map((name) => `KindEntry${index}<typeof ${name}, ${quote(moduleId)}>`),
|
|
83
|
-
);
|
|
84
|
-
}
|
|
85
|
-
parts.push(
|
|
86
|
-
`${quote(kind.name)}: ${normalizers.get(kind.name)}([${names}], ${quote(moduleId)})`,
|
|
87
|
-
);
|
|
88
|
-
});
|
|
89
|
-
return `{ ${parts.join(', ')} }`;
|
|
90
|
-
};
|
|
91
|
-
const actionTypes = [];
|
|
92
|
-
const moduleRoots = new Set(roots.map((file) => path.dirname(file)));
|
|
93
|
-
const appRoot = path.resolve(root, config.directory ?? './src');
|
|
94
|
-
const appManifestFile = path.join(appRoot, 'plugin.config.ts');
|
|
95
|
-
const appManifest = await readFile(appManifestFile, 'utf8')
|
|
96
|
-
.then(() => jiti.import(appManifestFile, { default: true }))
|
|
97
|
-
.catch((error) => {
|
|
98
|
-
if (error.code === 'ENOENT') return {};
|
|
99
|
-
throw error;
|
|
100
|
-
});
|
|
101
|
-
const appId = appManifest.id ?? '@foundation/app';
|
|
102
|
-
if (appId !== '@foundation/app' && !/^[a-z][a-z0-9-]*$/.test(appId))
|
|
103
|
-
throw new Error(`Invalid app module ID: ${appId}`);
|
|
104
|
-
pluginIds.add(appId);
|
|
105
|
-
const enabledModuleIds = new Set([appId]);
|
|
106
|
-
const appFiles = await files(appRoot, moduleRoots);
|
|
107
|
-
const fileEntries = (entries) =>
|
|
108
|
-
entries.map((entry) => {
|
|
109
|
-
const imported = importFile(entry.file);
|
|
110
|
-
const name = `file${serial++}`;
|
|
111
|
-
imports.push(
|
|
112
|
-
`const ${name} = discoveredFile(${imported}, ${quote(entry.path.join('.'))}, ${quote(entry.source)});`,
|
|
113
|
-
);
|
|
114
|
-
return name;
|
|
115
|
-
});
|
|
116
|
-
const emit = (kind, entries, moduleId = '') => {
|
|
117
|
-
const names = fileEntries(entries);
|
|
118
|
-
if (kind === 'actions')
|
|
119
|
-
for (const name of names)
|
|
120
|
-
actionTypes.push(
|
|
121
|
-
`DiscoveredActionEntry<typeof ${name}, ${quote(moduleId)}, ${conventions}>`,
|
|
122
|
-
);
|
|
123
|
-
if (kind === 'services') {
|
|
124
|
-
for (const name of names) {
|
|
125
|
-
serviceTypes.push(`DiscoveredServiceEntry<typeof ${name}, ${quote(moduleId)}>`);
|
|
126
|
-
publicServiceTypes.push(`DiscoveredPublicServiceEntry<typeof ${name}>`);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
if (kind === 'configs')
|
|
130
|
-
configTypes.push(
|
|
131
|
-
`${quote(moduleId)}: ${names.map((name) => `DiscoveredConfigEntry<typeof ${name}>`).join(' & ') || '{}'};`,
|
|
132
|
-
);
|
|
133
|
-
const fn = {
|
|
134
|
-
actions: 'discoverActions',
|
|
135
|
-
services: 'discoverServices',
|
|
136
|
-
interceptors: 'discoverInterceptors',
|
|
137
|
-
configs: 'discoverConfig',
|
|
138
|
-
}[kind];
|
|
139
|
-
return `${fn}([${names}]${kind === 'actions' ? `, ${quote(moduleId)}, ${conventions}` : kind === 'services' ? `, ${quote(moduleId)}` : ''})`;
|
|
140
|
-
};
|
|
141
|
-
const appEntries = async (kind) => {
|
|
142
|
-
const selected = config[kind];
|
|
143
|
-
if (selected === false) return [];
|
|
144
|
-
if (selected?.directory) {
|
|
145
|
-
const directory = path.resolve(root, selected.directory);
|
|
146
|
-
return discoverEntries(appRoot, await files(directory, moduleRoots), kind, { directory });
|
|
147
|
-
}
|
|
148
|
-
return discoverEntries(appRoot, appFiles, kind);
|
|
149
|
-
};
|
|
150
|
-
const appContributions = contributions(
|
|
151
|
-
await Promise.all(kinds.map((kind) => discoverEntries(appRoot, appFiles, kind))),
|
|
152
|
-
appId,
|
|
153
|
-
);
|
|
154
|
-
const appPages = (await appEntries('pages')).map((entry) => importFile(entry.file));
|
|
155
|
-
const appViews = (await appEntries('views')).map((entry) => importFile(entry.file));
|
|
156
|
-
const appSetupFile = appFiles.find((file) => file === path.join(appRoot, 'setup.ts'));
|
|
157
|
-
const appSetup = appSetupFile ? importFile(appSetupFile) : 'undefined';
|
|
158
|
-
const appActions = emit('actions', await appEntries('actions'), appManifest.id ?? '');
|
|
159
|
-
const appServices = emit('services', await appEntries('services'), appManifest.id ?? '');
|
|
160
|
-
const appConfig = emit('configs', await appEntries('configs'), appId);
|
|
161
|
-
let interceptorEntries = await appEntries('interceptors');
|
|
162
|
-
if (config.interceptors?.enabled !== undefined) {
|
|
163
|
-
const enabled = config.interceptors.enabled;
|
|
164
|
-
if (new Set(enabled).size !== enabled.length) throw new Error('Duplicate enabled interceptor.');
|
|
165
|
-
interceptorEntries = enabled.map((id) => {
|
|
166
|
-
const entry = interceptorEntries.find((entry) => entry.path.join('.') === id);
|
|
167
|
-
if (!entry) throw new Error(`Unknown interceptor "${id}".`);
|
|
168
|
-
return entry;
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
const interceptors = emit('interceptors', interceptorEntries);
|
|
172
|
-
const providerEntries = await appEntries('providers');
|
|
173
|
-
const enabledProviders = config.providers?.enabled ?? [];
|
|
174
|
-
if (new Set(enabledProviders).size !== enabledProviders.length)
|
|
175
|
-
throw new Error('Duplicate enabled provider.');
|
|
176
|
-
const providers = enabledProviders.map((id) => {
|
|
177
|
-
const entry = providerEntries.find((entry) => entry.path.join('.') === id);
|
|
178
|
-
if (!entry) throw new Error(`Unknown provider "${id}".`);
|
|
179
|
-
return importFile(entry.file);
|
|
180
|
-
});
|
|
181
|
-
for (const manifestFile of roots) {
|
|
182
|
-
const base = path.dirname(manifestFile);
|
|
183
|
-
const overrides = await readFile(manifestFile, 'utf8')
|
|
184
|
-
.then(() => jiti.import(manifestFile, { default: true }))
|
|
185
|
-
.catch((error) => {
|
|
186
|
-
if (error.code === 'ENOENT' && error.path === manifestFile) return {};
|
|
187
|
-
throw error;
|
|
188
|
-
});
|
|
189
|
-
const manifest = { id: path.basename(base), ...overrides };
|
|
190
|
-
if (!/^[a-z][a-z0-9-]*$/.test(manifest.id) || pluginIds.has(manifest.id))
|
|
191
|
-
throw new Error(`Invalid or duplicate plugin ID: ${manifest.id}`);
|
|
192
|
-
pluginIds.add(manifest.id);
|
|
193
|
-
const enabled =
|
|
194
|
-
config.plugins?.overrides?.[manifest.id]?.enabled ?? config.plugins?.autoEnable ?? true;
|
|
195
|
-
const packageInfo = await readFile(path.join(base, 'package.json'), 'utf8')
|
|
196
|
-
.then(JSON.parse)
|
|
197
|
-
.catch((error) => {
|
|
198
|
-
if (error.code === 'ENOENT') return {};
|
|
199
|
-
throw error;
|
|
200
|
-
});
|
|
201
|
-
const metadata = {};
|
|
202
|
-
for (const key of ['name', 'version', 'description', 'author', 'homepage', 'bugs', 'license']) {
|
|
203
|
-
const value = manifest.metadata?.[key] ?? packageInfo[key];
|
|
204
|
-
if (value !== undefined) metadata[key] = value;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
const resolveEntry = async (name, fallback) => {
|
|
208
|
-
const override = manifest.entries?.[name];
|
|
209
|
-
if (override === false) return [];
|
|
210
|
-
const target = path.resolve(base, override ?? fallback);
|
|
211
|
-
const relative = path.relative(base, target);
|
|
212
|
-
if (relative.startsWith('..') || path.isAbsolute(relative))
|
|
213
|
-
throw new Error(`Entry outside plugin: ${name}`);
|
|
214
|
-
const actual = await realpath(target).catch((error) => {
|
|
215
|
-
if (error.code === 'ENOENT') return null;
|
|
216
|
-
throw error;
|
|
217
|
-
});
|
|
218
|
-
if (!actual) {
|
|
219
|
-
if (override) throw new Error(`Missing explicit entry: ${target}`);
|
|
220
|
-
return [];
|
|
221
|
-
}
|
|
222
|
-
if (actual !== target) throw new Error(`Symlink entry: ${target}`);
|
|
223
|
-
return /\.tsx?$/.test(target) ? [target] : files(target);
|
|
224
|
-
};
|
|
225
|
-
const moduleFiles = await files(
|
|
226
|
-
base,
|
|
227
|
-
new Set([...moduleRoots].filter((item) => item !== base)),
|
|
228
|
-
);
|
|
229
|
-
const entriesFor = async (kind) => {
|
|
230
|
-
const override = manifest.entries?.[kind];
|
|
231
|
-
if (override === false) return [];
|
|
232
|
-
if (override) {
|
|
233
|
-
const paths = await resolveEntry(kind, kind);
|
|
234
|
-
const target = path.resolve(base, override);
|
|
235
|
-
return discoverEntries(
|
|
236
|
-
base,
|
|
237
|
-
paths,
|
|
238
|
-
kind,
|
|
239
|
-
/\.tsx?$/.test(target) ? { explicitFile: target } : { directory: target },
|
|
240
|
-
);
|
|
241
|
-
}
|
|
242
|
-
return discoverEntries(base, moduleFiles, kind);
|
|
243
|
-
};
|
|
244
|
-
const entriesForKind = async (kind) => {
|
|
245
|
-
const override = manifest.entries?.[kind.name];
|
|
246
|
-
if (override === false) return [];
|
|
247
|
-
if (override) {
|
|
248
|
-
const paths = await resolveEntry(kind.name, kind.directory);
|
|
249
|
-
const target = path.resolve(base, override);
|
|
250
|
-
return discoverEntries(
|
|
251
|
-
base,
|
|
252
|
-
paths,
|
|
253
|
-
kind,
|
|
254
|
-
/\.tsx?$/.test(target) ? { explicitFile: target } : { directory: target },
|
|
255
|
-
);
|
|
256
|
-
}
|
|
257
|
-
return discoverEntries(base, moduleFiles, kind);
|
|
258
|
-
};
|
|
259
|
-
const setupFiles = await resolveEntry('setup', 'setup.ts');
|
|
260
|
-
const viewFiles = (await entriesFor('views')).map((entry) => entry.file);
|
|
261
|
-
const pageFiles = (await entriesFor('pages')).map((entry) => entry.file);
|
|
262
|
-
if (!enabled) continue;
|
|
263
|
-
enabledModuleIds.add(manifest.id);
|
|
264
|
-
const actions = emit('actions', await entriesFor('actions'), manifest.id);
|
|
265
|
-
const services = emit('services', await entriesFor('services'), manifest.id);
|
|
266
|
-
const pluginConfig = emit('configs', await entriesFor('configs'), manifest.id);
|
|
267
|
-
const pluginInterceptors = emit('interceptors', await entriesFor('interceptors'));
|
|
268
|
-
const pages = pageFiles.map((file) => importFile(file));
|
|
269
|
-
const views = viewFiles.map((file) => importFile(file));
|
|
270
|
-
const setup = setupFiles[0] ? importFile(setupFiles[0]) : 'undefined';
|
|
271
|
-
const pluginContributions = contributions(
|
|
272
|
-
await Promise.all(kinds.map((kind) => entriesForKind(kind))),
|
|
273
|
-
manifest.id,
|
|
274
|
-
);
|
|
275
|
-
definitions.push(
|
|
276
|
-
`{ id: ${quote(manifest.id)}, metadata: ${quote(metadata)}, dependencies: ${quote(manifest.dependencies ?? [])}, actions: ${actions}, services: ${services}, config: ${pluginConfig}, interceptors: ${pluginInterceptors}, views: [${views}], pages: [${pages}], setup: ${setup}, contributions: ${pluginContributions} }`,
|
|
277
|
-
);
|
|
278
|
-
for (const file of await resolveEntry('locales', 'locales')) {
|
|
279
|
-
if (!file.endsWith('.json')) continue;
|
|
280
|
-
const locale = path.basename(file, '.json');
|
|
281
|
-
const messages = JSON.parse(await readFile(file, 'utf8'));
|
|
282
|
-
if (Object.keys(messages).some((key) => key !== manifest.id))
|
|
283
|
-
throw new Error(`Locale keys must use plugin namespace "${manifest.id}".`);
|
|
284
|
-
(allMessages[locale] ??= {})[manifest.id] = messages[manifest.id];
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
const header = '// Generated by SaaS Foundation. Do not edit.\n';
|
|
288
|
-
await writeChanged(
|
|
289
|
-
path.join(output, 'plugins.ts'),
|
|
290
|
-
`${header}${[...contributionTypes.values()].map(({ kind, index, entries }) => `type KindMap${index} = ${entries.join(' & ')}; declare module ${quote(kind.from)} { interface ${kind.types.map} extends KindMap${index} {} }`).join('\n')}
|
|
291
|
-
${imports.join('\n')}\nimport { ${imports.some(entry => entry.includes('= discoveredFile(')) ? 'discoveredFile, ' : ''}discoverActions, discoverServices, discoverInterceptors, discoverConfig, ${actionTypes.length ? 'type DiscoveredActionEntry, ' : ''}${serviceTypes.length ? 'type DiscoveredServiceEntry, ' : ''}${publicServiceTypes.length ? 'type DiscoveredPublicServiceEntry, ' : ''}${configTypes.some(entry => entry.includes('DiscoveredConfigEntry')) ? 'type DiscoveredConfigEntry, ' : ''}type FoundationConfig } from '@bitakit/core';\ntype DiscoveredServices = ${serviceTypes.join(' & ') || '{}'};\ntype DiscoveredPublicServices = ${publicServiceTypes.join(' & ') || '{}'};\ntype DiscoveredActions = ${actionTypes.join(' & ') || '{}'};\ndeclare module '@bitakit/core' { interface PublicServiceMap extends DiscoveredPublicServices {} interface ServiceMap extends DiscoveredServices {} interface ActionMap extends DiscoveredActions {} interface ConfigMap { ${configTypes.join(' \n')} } }\nexport const foundationConfig = { appId: ${quote(appId)}, metadata: ${quote(appManifest.metadata ?? {})}, dependencies: ${quote(appManifest.dependencies ?? [])}, pages: [${appPages}], views: [${appViews}], setup: ${appSetup}, groups: ${quote(config.groups ?? [])}, actionOverrides: ${quote((config.actions && config.actions.overrides) || {})}, config: ${appConfig}, configOverrides: ${quote(Object.fromEntries(Object.entries(config.configOverrides ?? {}).filter(([id]) => !pluginIds.has(id) || enabledModuleIds.has(id))))}, services: ${appServices}, interceptors: ${interceptors}, providers: [${providers}], actions: ${appActions}, contributions: ${appContributions}, plugins: [${definitions.join(',\n')}] } satisfies FoundationConfig;\n`,
|
|
292
|
-
);
|
|
293
|
-
await writeChanged(
|
|
294
|
-
path.join(output, 'messages.json'),
|
|
295
|
-
JSON.stringify(allMessages, null, 2) + '\n',
|
|
296
|
-
);
|
|
297
|
-
const configured = Object.keys(installed.configurations ?? {}).length > 0;
|
|
298
|
-
const pluginFactories = installed.plugins.map((specifier, index) => {
|
|
299
|
-
const configPath = installed.configurations?.[specifier];
|
|
300
|
-
return `packagePlugin${index}(${configPath ? `options.get(${quote(configPath)}, {})` : ''})`;
|
|
301
|
-
});
|
|
302
|
-
await writeChanged(
|
|
303
|
-
path.join(output, 'app.ts'),
|
|
304
|
-
`${header}
|
|
305
|
-
import { foundationConfig } from './plugins';
|
|
306
|
-
${installed.plugins.map((specifier, index) => `import packagePlugin${index} from ${quote(specifier)};`).join('\n')}
|
|
307
|
-
import { ${configured ? 'ConfigService, type FoundationConfig, ' : ''}type PublicServicesOf } from '@bitakit/core';
|
|
308
|
-
type InstalledPlugins = [${installed.plugins.map((_, index) => `ReturnType<typeof packagePlugin${index}>`).join(', ')}];
|
|
309
|
-
type InstalledPublicServices = PublicServicesOf<InstalledPlugins>;
|
|
310
|
-
declare module '@bitakit/core' { interface PublicServiceMap extends InstalledPublicServices {} }
|
|
311
|
-
/** Factories describe plugins per provider instance; runtime creation and cleanup stay in Core. */
|
|
312
|
-
export function createFoundationConfig() {
|
|
313
|
-
${configured ? `const { appId = '@foundation/app', config, configOverrides = {} }: FoundationConfig = foundationConfig;
|
|
314
|
-
const options = new ConfigService([{ id: appId, config }], {
|
|
315
|
-
[appId]: configOverrides[appId] ?? {},
|
|
316
|
-
}).plugin(appId);` : ''}
|
|
317
|
-
return { ...foundationConfig, plugins: [...foundationConfig.plugins, ${pluginFactories.join(', ')}] };
|
|
318
|
-
}
|
|
319
|
-
`,
|
|
320
|
-
);
|
|
321
|
-
}
|
package/discovery/host.mjs
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { generateFoundation as generate } from './generate.mjs';
|
|
2
|
-
import { readFile, writeFile } from 'node:fs/promises';
|
|
3
|
-
import path from 'node:path';
|
|
4
|
-
|
|
5
|
-
/** Connects Core's generated app types to the host compiler without manual imports. */
|
|
6
|
-
export async function generateFoundationHost(root) {
|
|
7
|
-
await generate(root);
|
|
8
|
-
const file = path.join(root, 'foundation-env.d.ts');
|
|
9
|
-
const content =
|
|
10
|
-
'// Generated by SaaS Foundation. Do not edit.\n/// <reference path="./.foundation/app.ts" />\n';
|
|
11
|
-
if ((await readFile(file, 'utf8').catch(() => null)) !== content) await writeFile(file, content);
|
|
12
|
-
}
|
package/discovery/modules.mjs
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import path from 'node:path';
|
|
2
|
-
|
|
3
|
-
/** Immediate folders are plugins; explicit nested manifests preserve existing module boundaries. */
|
|
4
|
-
export function pluginManifests(directory, files) {
|
|
5
|
-
const roots = new Set();
|
|
6
|
-
for (const file of files) {
|
|
7
|
-
const segments = path.relative(directory, file).split(path.sep);
|
|
8
|
-
if (segments.some(segment => segment.startsWith('-') || segment.startsWith('.'))) continue;
|
|
9
|
-
if (segments.length > 1) roots.add(path.join(directory, segments[0], 'plugin.config.ts'));
|
|
10
|
-
if (path.basename(file) === 'plugin.config.ts') roots.add(file);
|
|
11
|
-
}
|
|
12
|
-
return [...roots].sort();
|
|
13
|
-
}
|
package/discovery/packages.mjs
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { readFile } from 'node:fs/promises';
|
|
2
|
-
import { createRequire } from 'node:module';
|
|
3
|
-
import path from 'node:path';
|
|
4
|
-
|
|
5
|
-
/** Reads opt-in metadata from direct dependencies. Never imports client plugin code in Node. */
|
|
6
|
-
export async function installedContributions(root, selection, jiti) {
|
|
7
|
-
if (selection === false) return { plugins: [], kinds: [] };
|
|
8
|
-
const manifestFile = path.join(root, 'package.json');
|
|
9
|
-
const manifest = await readFile(manifestFile, 'utf8')
|
|
10
|
-
.then(JSON.parse)
|
|
11
|
-
.catch((error) => {
|
|
12
|
-
if (error.code === 'ENOENT') return {};
|
|
13
|
-
throw error;
|
|
14
|
-
});
|
|
15
|
-
const require = createRequire(manifestFile);
|
|
16
|
-
const plugins = [];
|
|
17
|
-
const kinds = [];
|
|
18
|
-
const configurations = {};
|
|
19
|
-
for (const name of Object.keys({
|
|
20
|
-
...manifest.dependencies,
|
|
21
|
-
...manifest.optionalDependencies,
|
|
22
|
-
}).sort()) {
|
|
23
|
-
if (selection?.[name] === false) continue;
|
|
24
|
-
let file;
|
|
25
|
-
try {
|
|
26
|
-
// Opt-in packages expose their manifest as a public subpath.
|
|
27
|
-
file = require.resolve(`${name}/package.json`);
|
|
28
|
-
} catch (error) {
|
|
29
|
-
if (error.code === 'MODULE_NOT_FOUND' || error.code === 'ERR_PACKAGE_PATH_NOT_EXPORTED')
|
|
30
|
-
continue;
|
|
31
|
-
throw error;
|
|
32
|
-
}
|
|
33
|
-
const metadata = JSON.parse(await readFile(file, 'utf8')).foundation;
|
|
34
|
-
if (!metadata) continue;
|
|
35
|
-
const specifier = (entry) => {
|
|
36
|
-
if (
|
|
37
|
-
typeof entry !== 'string' ||
|
|
38
|
-
!/^\.\/[\w./-]+$/.test(entry) ||
|
|
39
|
-
entry.split('/').includes('..')
|
|
40
|
-
)
|
|
41
|
-
throw new Error(`Invalid Foundation export in package "${name}".`);
|
|
42
|
-
return `${name}/${entry.slice(2)}`;
|
|
43
|
-
};
|
|
44
|
-
if (metadata.plugin) {
|
|
45
|
-
const plugin = specifier(metadata.plugin);
|
|
46
|
-
plugins.push(plugin);
|
|
47
|
-
if (metadata.configuration) configurations[plugin] = metadata.configuration;
|
|
48
|
-
}
|
|
49
|
-
if (metadata.discovery) {
|
|
50
|
-
const discover = await jiti.import(require.resolve(specifier(metadata.discovery)), {
|
|
51
|
-
default: true,
|
|
52
|
-
});
|
|
53
|
-
kinds.push(...discover());
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
return { plugins, kinds, configurations };
|
|
57
|
-
}
|
package/discovery/watch.mjs
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { watch } from 'node:fs';
|
|
2
|
-
import { generateFoundation } from './generate.mjs';
|
|
3
|
-
/** Framework-independent development discovery. The bundler observes generated imports. */
|
|
4
|
-
export function watchFoundation(root, onError = console.error) {
|
|
5
|
-
let timer;
|
|
6
|
-
let stopped = false;
|
|
7
|
-
let chain = Promise.resolve();
|
|
8
|
-
const watcher = watch(root, { recursive: true }, (event, file) => {
|
|
9
|
-
if (!file || /(^|\/)(node_modules|\.next|\.foundation|\.git)(?:\/|$)/.test(file)) return;
|
|
10
|
-
// Directory creation may be the only event before recursive watching attaches.
|
|
11
|
-
if (event !== 'rename' && !/\.(ts|tsx|json)$/.test(file)) return;
|
|
12
|
-
clearTimeout(timer);
|
|
13
|
-
timer = setTimeout(() => {
|
|
14
|
-
chain = chain.then(() => (stopped ? undefined : generateFoundation(root))).catch(onError);
|
|
15
|
-
}, 80);
|
|
16
|
-
});
|
|
17
|
-
watcher.unref();
|
|
18
|
-
return () => {
|
|
19
|
-
stopped = true;
|
|
20
|
-
clearTimeout(timer);
|
|
21
|
-
watcher.close();
|
|
22
|
-
};
|
|
23
|
-
}
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import type { Plugin } from '../definitions.js';
|
|
2
|
-
import type { PublicServiceKeys } from './types.js';
|
|
3
|
-
/** Resolves names onto existing container keys; this does not create or store service instances. */
|
|
4
|
-
export declare function serviceAliases(module: Pick<Plugin, 'id' | 'services' | 'publicServices'>): PublicServiceKeys;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { serviceKey } from '../service-key.js';
|
|
2
|
-
/** Resolves names onto existing container keys; this does not create or store service instances. */
|
|
3
|
-
export function serviceAliases(module) {
|
|
4
|
-
const aliases = Object.assign(Object.create(null), module.publicServices);
|
|
5
|
-
for (const service of module.services ?? []) {
|
|
6
|
-
if (service.exposeAs === undefined)
|
|
7
|
-
continue;
|
|
8
|
-
const name = service.exposeAs;
|
|
9
|
-
if (Object.hasOwn(aliases, name) && aliases[name] !== service.id)
|
|
10
|
-
throw new Error(`Duplicate public service "${name}" in plugin "${module.id}".`);
|
|
11
|
-
aliases[name] = serviceKey(service.id);
|
|
12
|
-
}
|
|
13
|
-
return aliases;
|
|
14
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { ConfigContext } from '../config-service.js';
|
|
2
|
-
import type { PlainDefinition } from '../definitions.js';
|
|
3
|
-
import type { ContributionHost } from './host.js';
|
|
4
|
-
import type { Adapted } from './index.js';
|
|
5
|
-
/** Plain commands: contextual presentation from services, explicit outcome feedback around execution. */
|
|
6
|
-
export declare function adaptCommand(resolved: PlainDefinition<any, any> & {
|
|
7
|
-
id: string;
|
|
8
|
-
}, host: ContributionHost, config: ConfigContext): Adapted;
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { ActionFailure } from '../actions/feedback.js';
|
|
2
|
-
import { applyOverride, cap, groupsOf, texts } from './shared.js';
|
|
3
|
-
/** Plain commands: contextual presentation from services, explicit outcome feedback around execution. */
|
|
4
|
-
export function adaptCommand(resolved, host, config) {
|
|
5
|
-
const { definition, override } = applyOverride(resolved, host);
|
|
6
|
-
const services = host.services;
|
|
7
|
-
const execute = definition.execute;
|
|
8
|
-
return {
|
|
9
|
-
definition,
|
|
10
|
-
setup: definition.setup || definition.onEvent
|
|
11
|
-
? (context) => {
|
|
12
|
-
if (definition.onEvent && definition.events?.length) {
|
|
13
|
-
const onEvent = definition.onEvent;
|
|
14
|
-
context.events.watch(definition.events, (event, { signal }) => onEvent(event, { ...context, signal }));
|
|
15
|
-
}
|
|
16
|
-
return definition.setup?.(context);
|
|
17
|
-
}
|
|
18
|
-
: undefined,
|
|
19
|
-
requires: definition.requires ?? [],
|
|
20
|
-
watch: definition.watch ?? [],
|
|
21
|
-
registrations: [
|
|
22
|
-
{
|
|
23
|
-
id: definition.id,
|
|
24
|
-
placement: {},
|
|
25
|
-
definition: {
|
|
26
|
-
...definition,
|
|
27
|
-
...texts(host, definition),
|
|
28
|
-
groups: groupsOf(host, definition),
|
|
29
|
-
resolve: (input) => cap(definition.resolve?.(input, { services, config }) ?? {}, override),
|
|
30
|
-
execute: execute
|
|
31
|
-
? async (context) => {
|
|
32
|
-
let result;
|
|
33
|
-
try {
|
|
34
|
-
result = await execute({ ...context, services, config, app: host.app });
|
|
35
|
-
}
|
|
36
|
-
catch (error) {
|
|
37
|
-
const feedback = context.get()
|
|
38
|
-
? host.feedback(definition, 'failure', definition.failure, {
|
|
39
|
-
error,
|
|
40
|
-
input: context.input,
|
|
41
|
-
services,
|
|
42
|
-
}).message
|
|
43
|
-
: false;
|
|
44
|
-
throw new ActionFailure(feedback, error);
|
|
45
|
-
}
|
|
46
|
-
if (context.get())
|
|
47
|
-
host.notify(host.feedback(definition, 'success', definition.success, {
|
|
48
|
-
result,
|
|
49
|
-
input: context.input,
|
|
50
|
-
services,
|
|
51
|
-
}).message);
|
|
52
|
-
return result;
|
|
53
|
-
}
|
|
54
|
-
: undefined,
|
|
55
|
-
},
|
|
56
|
-
},
|
|
57
|
-
],
|
|
58
|
-
};
|
|
59
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { ConfigContext } from '../config-service.js';
|
|
2
|
-
import type { DialogDefinition } from '../definitions.js';
|
|
3
|
-
import type { ContributionHost } from './host.js';
|
|
4
|
-
import type { Adapted } from './index.js';
|
|
5
|
-
/** Custom dialogs: the opener carries contextual guards; the body renders inside the dialog host. */
|
|
6
|
-
export declare function adaptDialog(resolved: DialogDefinition<any> & {
|
|
7
|
-
id: string;
|
|
8
|
-
}, host: ContributionHost, config: ConfigContext): Adapted;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { applyOverride, cap, opener } from './shared.js';
|
|
2
|
-
/** Custom dialogs: the opener carries contextual guards; the body renders inside the dialog host. */
|
|
3
|
-
export function adaptDialog(resolved, host, config) {
|
|
4
|
-
const { definition, override } = applyOverride(resolved, host);
|
|
5
|
-
const services = host.services;
|
|
6
|
-
return {
|
|
7
|
-
definition,
|
|
8
|
-
requires: definition.requires ?? [],
|
|
9
|
-
watch: definition.watch ?? [],
|
|
10
|
-
registrations: [
|
|
11
|
-
{
|
|
12
|
-
id: definition.id,
|
|
13
|
-
placement: {},
|
|
14
|
-
definition: opener(host, definition, (input) => cap(definition.resolve?.(input, { services, config }) ?? {}, override)),
|
|
15
|
-
},
|
|
16
|
-
],
|
|
17
|
-
};
|
|
18
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import type { ConfigContext } from '../config-service.js';
|
|
2
|
-
import type { ResolvedForm } from '../definitions.js';
|
|
3
|
-
import type { ContributionHost } from './host.js';
|
|
4
|
-
import type { Adapted } from './index.js';
|
|
5
|
-
/**
|
|
6
|
-
* Forms register the public opener plus an internal `<id>.submit` registration. Submission runs
|
|
7
|
-
* through the same executor so interceptors, pending state and execution events apply, but it is
|
|
8
|
-
* excluded from enumeration: `runtime.submitForm` is the supported programmatic path.
|
|
9
|
-
*/
|
|
10
|
-
export declare function adaptForm(resolved: ResolvedForm, host: ContributionHost, config: ConfigContext): Adapted;
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import { ActionFailure } from '../actions/feedback.js';
|
|
2
|
-
import { validateFields } from '../validation.js';
|
|
3
|
-
import { applyOverride, cap, opener, texts } from './shared.js';
|
|
4
|
-
/**
|
|
5
|
-
* Forms register the public opener plus an internal `<id>.submit` registration. Submission runs
|
|
6
|
-
* through the same executor so interceptors, pending state and execution events apply, but it is
|
|
7
|
-
* excluded from enumeration: `runtime.submitForm` is the supported programmatic path.
|
|
8
|
-
*/
|
|
9
|
-
export function adaptForm(resolved, host, config) {
|
|
10
|
-
const { definition, override } = applyOverride(resolved, host);
|
|
11
|
-
const services = host.services;
|
|
12
|
-
const shared = texts(host, definition);
|
|
13
|
-
const submit = {
|
|
14
|
-
id: `${definition.id}.submit`,
|
|
15
|
-
title: shared.title,
|
|
16
|
-
description: shared.description,
|
|
17
|
-
internal: true,
|
|
18
|
-
resolve: () => {
|
|
19
|
-
const state = host.dialog();
|
|
20
|
-
return {
|
|
21
|
-
enabled: state?.definition === definition &&
|
|
22
|
-
!!host.actions.resolve(definition.id, state?.input)?.enabled,
|
|
23
|
-
visible: !!host.actions.get(definition.id)?.visible,
|
|
24
|
-
};
|
|
25
|
-
},
|
|
26
|
-
execute: async (context) => {
|
|
27
|
-
const state = host.dialog();
|
|
28
|
-
const input = validateFields(definition.fields, context.input);
|
|
29
|
-
let result;
|
|
30
|
-
try {
|
|
31
|
-
result = await definition.submit(input, { services, config, input: state?.input });
|
|
32
|
-
}
|
|
33
|
-
catch (error) {
|
|
34
|
-
const feedback = context.get() && host.dialog() === state
|
|
35
|
-
? host.feedback(definition, 'failure', definition.failure, { error, input, services })
|
|
36
|
-
.message
|
|
37
|
-
: false;
|
|
38
|
-
throw new ActionFailure(feedback, error);
|
|
39
|
-
}
|
|
40
|
-
if (state && context.get() && host.dialog() === state) {
|
|
41
|
-
const feedback = host.feedback(definition, 'success', definition.success, {
|
|
42
|
-
result,
|
|
43
|
-
input,
|
|
44
|
-
services,
|
|
45
|
-
});
|
|
46
|
-
host.rememberFormFeedback(state, {
|
|
47
|
-
...feedback,
|
|
48
|
-
closeDialog: definition.closeOnSuccess ?? feedback.closeDialog,
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
return result;
|
|
52
|
-
},
|
|
53
|
-
};
|
|
54
|
-
return {
|
|
55
|
-
definition,
|
|
56
|
-
requires: definition.requires ?? [],
|
|
57
|
-
watch: [],
|
|
58
|
-
registrations: [
|
|
59
|
-
{
|
|
60
|
-
id: definition.id,
|
|
61
|
-
placement: {},
|
|
62
|
-
definition: opener(host, definition, () => cap({}, override)),
|
|
63
|
-
},
|
|
64
|
-
{ id: submit.id, placement: {}, definition: submit },
|
|
65
|
-
],
|
|
66
|
-
};
|
|
67
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { ConfigContext } from '../config-service.js';
|
|
2
|
-
import type { NativeDefinition } from '../definitions.js';
|
|
3
|
-
import type { ContributionHost } from './host.js';
|
|
4
|
-
import type { Adapted } from './index.js';
|
|
5
|
-
/**
|
|
6
|
-
* Class and function exports: one fresh instance per runtime. Metadata declared on the instance
|
|
7
|
-
* wins; translation, overrides and opt-in feedback are applied to that instance.
|
|
8
|
-
*/
|
|
9
|
-
export declare function adaptNative(resolved: NativeDefinition & {
|
|
10
|
-
id: string;
|
|
11
|
-
}, host: ContributionHost, config: ConfigContext): Adapted;
|