@bridgebrew/plugin-manager 0.0.1 → 0.0.2

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.
@@ -0,0 +1,356 @@
1
+ // The marketplace's model (research-043 M1), what `marketplace.tsx` shows of `GET /_bridge/workspaces/<slug>/marketplace`:
2
+ // the search field's words and filters, as VS Code's Extensions view reads them (`@installed`, `@updates`,
3
+ // `@official`, `@source:<id or label>`, `@domain:<domain>`), and the sections the entries found fall in: Updates first,
4
+ // then one per source in the host's order (registries, folders, Built-in) or one per domain, each sorted by title.
5
+ import { domainOf, domains } from './plugins-model.ts';
6
+
7
+ export type SourceType = 'built-in' | 'folder' | 'npm';
8
+ export interface CatalogSource {
9
+ id: string;
10
+ type: SourceType;
11
+ label: string;
12
+ location: string;
13
+ count: number;
14
+ error?: string;
15
+ }
16
+ export interface CatalogEntry {
17
+ name: string;
18
+ key: string;
19
+ title: string;
20
+ description: string;
21
+ version: string;
22
+ icon?: string;
23
+ kind?: string;
24
+ domain?: string;
25
+ keywords: string[];
26
+ /** The contracts it requires and the ones it implements, sorted. */
27
+ requires?: string[];
28
+ implements?: string[];
29
+ source: string;
30
+ install: string;
31
+ registry?: string;
32
+ official?: true;
33
+ author?: string;
34
+ homepage?: string;
35
+ repository?: string;
36
+ license?: string;
37
+ updated?: string;
38
+ installed?: { id: string; version: string; linked: boolean };
39
+ update?: string;
40
+ /** What that update asks before it runs: a plugin Bridge does not test, or one that may change its data. */
41
+ updateAsks?: 'external' | 'migrates' | 'data';
42
+ /** A newer version the user's hold keeps back (#472): no `update` until they lift it. */
43
+ held?: true;
44
+ /** No version this Bridge meets: the Bridge build the plugin asks for. */
45
+ asks?: string;
46
+ }
47
+ export interface Catalog {
48
+ sources: CatalogSource[];
49
+ entries: CatalogEntry[];
50
+ read: string;
51
+ }
52
+ export interface Section {
53
+ id: string;
54
+ label: string;
55
+ entries: CatalogEntry[];
56
+ }
57
+
58
+ /** An entry's key in the list: one package may be offered by several sources. */
59
+ export const entryId = (entry: Pick<CatalogEntry, 'source' | 'name'>) =>
60
+ `${entry.source}/${entry.name}`;
61
+
62
+ /** Case and accents ignored. */
63
+ const fold = (text: string) => text.normalize('NFD').replace(/\p{M}/gu, '').toLowerCase();
64
+
65
+ export interface Query {
66
+ words: string[];
67
+ installed: boolean;
68
+ updates: boolean;
69
+ official: boolean;
70
+ source?: string;
71
+ domain?: string;
72
+ }
73
+ /** The filters VS Code-style, typed in the search field. */
74
+ export const filterHints = [
75
+ { token: '@installed', label: 'Installed in this workspace' },
76
+ { token: '@updates', label: 'With an update' },
77
+ { token: '@official', label: 'Published by Bridge' },
78
+ { token: '@source:', label: 'From one source' },
79
+ { token: '@domain:', label: 'Of one domain' },
80
+ ] as const;
81
+
82
+ export function parseQuery(text: string): Query {
83
+ const query: Query = { words: [], installed: false, updates: false, official: false };
84
+ for (const token of text.trim().split(/\s+/).filter(Boolean)) {
85
+ const lower = fold(token);
86
+ if (lower === '@installed') query.installed = true;
87
+ else if (lower === '@updates' || lower === '@update') query.updates = true;
88
+ else if (lower === '@official') query.official = true;
89
+ else if (lower.startsWith('@source:') && lower.length > 8) query.source = lower.slice(8);
90
+ else if (lower.startsWith('@domain:') && lower.length > 8) query.domain = lower.slice(8);
91
+ else query.words.push(lower);
92
+ }
93
+ return query;
94
+ }
95
+
96
+ export const searching = (query: Query) =>
97
+ query.words.length > 0 ||
98
+ query.installed ||
99
+ query.updates ||
100
+ query.official ||
101
+ query.source !== undefined ||
102
+ query.domain !== undefined;
103
+
104
+ export function matches(entry: CatalogEntry, query: Query, sources: CatalogSource[]) {
105
+ if (query.installed && !entry.installed) return false;
106
+ if (query.updates && entry.update === undefined) return false;
107
+ if (query.official && !entry.official) return false;
108
+ if (query.source !== undefined) {
109
+ const source = sources.find(({ id }) => id === entry.source);
110
+ if (
111
+ !source ||
112
+ (fold(source.id) !== query.source &&
113
+ !fold(source.label).includes(query.source) &&
114
+ source.type !== query.source)
115
+ )
116
+ return false;
117
+ }
118
+ if (query.domain !== undefined && fold(entry.domain ?? '') !== query.domain) return false;
119
+ const text = fold(
120
+ `${entry.title} ${entry.name} ${entry.description} ${entry.keywords.join(' ')} ${entry.author ?? ''}`,
121
+ );
122
+ return query.words.every((word) => text.includes(word));
123
+ }
124
+
125
+ const byTitle = (a: CatalogEntry, b: CatalogEntry) =>
126
+ a.title.localeCompare(b.title) || a.name.localeCompare(b.name);
127
+
128
+ /** How the marketplace lists: by source (Name), by domain, or what is installed only. */
129
+ export const marketModes = [
130
+ { value: 'name', label: 'Name' },
131
+ { value: 'domain', label: 'Domain' },
132
+ { value: 'installed', label: 'Installed' },
133
+ ] as const;
134
+ export type MarketMode = (typeof marketModes)[number]['value'];
135
+
136
+ /** The sections shown: Updates first, then each source's matching entries (Name, Installed: installed ones only),
137
+ * or each domain's in Bridge's order (Domain); empty sections left out. */
138
+ export function sections(catalog: Catalog, text: string, mode: MarketMode = 'name'): Section[] {
139
+ const query = parseQuery(text);
140
+ if (mode === 'installed') query.installed = true;
141
+ const found = catalog.entries.filter((entry) => matches(entry, query, catalog.sources));
142
+ const out: Section[] = [];
143
+ const updates = found.filter((entry) => entry.update !== undefined).sort(byTitle);
144
+ if (updates.length > 0 && !query.updates)
145
+ out.push({ id: 'updates', label: 'Updates', entries: updates });
146
+ if (mode === 'domain') {
147
+ for (const domain of domains) {
148
+ const entries = found.filter((entry) => domainOf(entry) === domain.id).sort(byTitle);
149
+ if (entries.length > 0) out.push({ id: `domain:${domain.id}`, label: domain.title, entries });
150
+ }
151
+ return out;
152
+ }
153
+ for (const source of catalog.sources) {
154
+ const entries = found.filter((entry) => entry.source === source.id).sort(byTitle);
155
+ if (entries.length > 0) out.push({ id: source.id, label: source.label, entries });
156
+ }
157
+ return out;
158
+ }
159
+
160
+ /** What the entry's action is: install it, update it, or nothing (installed, linked to a folder, or no version this
161
+ * Bridge meets). */
162
+ export type Action = 'install' | 'update' | 'installed' | 'linked' | 'other' | 'asks';
163
+ export function actionOf(entry: CatalogEntry): Action {
164
+ if (!entry.installed) return entry.asks === undefined ? 'install' : 'asks';
165
+ if (entry.update !== undefined) return 'update';
166
+ if (entry.installed.linked) return entry.installed.id === entry.name ? 'linked' : 'other';
167
+ return entry.installed.id === entry.name ? 'installed' : 'other';
168
+ }
169
+
170
+ /** The count of entries with an update: the rail's and the switch's badge. */
171
+ export const updateCount = (catalog: Catalog | undefined) =>
172
+ new Set(
173
+ (catalog?.entries ?? []).filter((entry) => entry.update !== undefined).map(({ key }) => key),
174
+ ).size;
175
+ /** The Plugins screen's sidebar mark (#491): how many plugins have an update, a red counter (`alert`); none, no
176
+ * mark. */
177
+ export const updatesMark = (catalog: Catalog | undefined) => {
178
+ const count = updateCount(catalog);
179
+ return count > 0 ? { count, alert: 'error' as const } : undefined;
180
+ };
181
+
182
+ /** Where an entry comes from, in words. */
183
+ export const sourceText = (entry: CatalogEntry, sources: CatalogSource[]) => {
184
+ const source = sources.find(({ id }) => id === entry.source);
185
+ return source?.label ?? entry.source;
186
+ };
187
+
188
+ /** The tabs of a plugin's page, the same from the Installed and the Marketplace tab (research-043 U2). */
189
+ export type PageTab =
190
+ | 'details'
191
+ | 'info'
192
+ | 'dependencies'
193
+ | 'docs'
194
+ | 'settings'
195
+ | 'logs'
196
+ | 'versions';
197
+ /** Details (the README) and Versions come from the catalog, so only when the plugin is in it; a plugin that is not
198
+ * installed has nothing else. The first tab is the one shown unless the caller chose another that exists. */
199
+ export function pageTabs({ installed, inCatalog }: { installed: boolean; inCatalog: boolean }): {
200
+ tabs: PageTab[];
201
+ initial: PageTab;
202
+ } {
203
+ if (!installed) return { tabs: ['details', 'dependencies', 'versions'], initial: 'details' };
204
+ const own: PageTab[] = ['info', 'dependencies', 'docs', 'settings', 'logs'];
205
+ return inCatalog
206
+ ? { tabs: ['details', ...own, 'versions'], initial: 'details' }
207
+ : { tabs: own, initial: 'info' };
208
+ }
209
+
210
+ /** The catalog entry of the installed plugin `id`: the one named as it (the same package), one with an update first. */
211
+ export function entryOfPlugin(catalog: Catalog | undefined, id: string): CatalogEntry | undefined {
212
+ const same = (catalog?.entries ?? []).filter(
213
+ (entry) => entry.name === id && entry.installed?.id === id,
214
+ );
215
+ return same.find((entry) => entry.update !== undefined) ?? same[0];
216
+ }
217
+
218
+ /** What provides a contract a plugin requires: an installed plugin, else entries of the catalog, else nothing. */
219
+ export interface DependencyRow {
220
+ contract: string;
221
+ status: 'provided' | 'available' | 'missing';
222
+ /** The plugins (`provided`: installed ids) or the catalog entries (`available`: entry ids, see `entryId`) offering it. */
223
+ by: { id: string; title: string }[];
224
+ }
225
+ /** One row per required contract, in the order given. An installed plugin wins over the catalog's entries. */
226
+ export function dependencyRows(
227
+ requires: string[],
228
+ installed: { id: string; title: string; implements: string[] }[],
229
+ catalog: CatalogEntry[],
230
+ ): DependencyRow[] {
231
+ return requires.map((contract): DependencyRow => {
232
+ const provided = installed.filter((each) => each.implements.includes(contract));
233
+ if (provided.length > 0)
234
+ return {
235
+ contract,
236
+ status: 'provided',
237
+ by: provided.map(({ id, title }) => ({ id, title })),
238
+ };
239
+ const seen = new Set<string>();
240
+ const available = catalog
241
+ .filter((entry) => entry.implements?.includes(contract) === true)
242
+ .filter((entry) => !seen.has(entry.name) && seen.add(entry.name))
243
+ .map((entry) => ({ id: entryId(entry), title: entry.title }));
244
+ return available.length > 0
245
+ ? { contract, status: 'available', by: available }
246
+ : { contract, status: 'missing', by: [] };
247
+ });
248
+ }
249
+
250
+ /** A step of installing a plugin with its dependencies: install a catalog entry, or enable an installed plugin. */
251
+ export type PlanStep =
252
+ | { kind: 'install'; entry: CatalogEntry }
253
+ | { kind: 'enable'; id: string; key: string; title: string };
254
+ /** Why no entry of `catalog` that implements `contract` can be installed by this Bridge, when some implement it but
255
+ * each asks for a later build: `<name> asks for Bridge <build>` of the first. */
256
+ const askingFor = (catalog: CatalogEntry[], contract: string) => {
257
+ const blocked = catalog.find(
258
+ (each) => each.asks !== undefined && each.implements?.includes(contract) === true,
259
+ );
260
+ return blocked && `${blocked.name} asks for Bridge ${blocked.asks}`;
261
+ };
262
+ /** The first entry of `catalog` implementing `contract` this Bridge can install (none asking for a later build). */
263
+ const providerOf = (catalog: CatalogEntry[], contract: string) =>
264
+ catalog.find((each) => each.asks === undefined && each.implements?.includes(contract) === true);
265
+
266
+ /** The `host<Name>` proxies a plugin requires: `_host` exposes them; the host checks them at install. */
267
+ const hostProvided = (contract: string) => /^host[A-Z]/.test(contract);
268
+
269
+ /** What installing `entry` with its dependencies means, as a Linux package manager shows it: the steps before it,
270
+ * dependencies before what needs them (an installed provider disabled is enabled, else the catalog's first entry
271
+ * offering the contract this Bridge can install is installed, with its own dependencies), the contracts nothing
272
+ * provides, the providers asking for a later Bridge (`asks`, when no other provides the contract), and the entries
273
+ * needing each other (a cycle cannot be installed one after the other). */
274
+ export function installPlan(
275
+ entry: CatalogEntry,
276
+ installed: { id: string; key?: string; title: string; implements: string[]; state?: string }[],
277
+ catalog: CatalogEntry[],
278
+ ): { before: PlanStep[]; missing: string[]; cycle: string[]; asks: string[] } {
279
+ const before: PlanStep[] = [];
280
+ const missing: string[] = [];
281
+ const asks: string[] = [];
282
+ const cycle: string[] = [];
283
+ const active = installed.filter((each) => each.state !== 'disabled');
284
+ const done = new Set<string>();
285
+ const visiting = new Set<string>([entry.name]);
286
+ const provides = (step: PlanStep, contract: string) =>
287
+ step.kind === 'install'
288
+ ? step.entry.implements?.includes(contract) === true
289
+ : installed.find(({ id }) => id === step.id)?.implements.includes(contract) === true;
290
+ const visit = (requires: string[]) => {
291
+ for (const contract of requires) {
292
+ if (hostProvided(contract)) continue;
293
+ if (active.some((each) => each.implements.includes(contract))) continue;
294
+ if (before.some((step) => provides(step, contract))) continue;
295
+ const off = installed.find(
296
+ (each) => each.state === 'disabled' && each.implements.includes(contract),
297
+ );
298
+ if (off) {
299
+ before.push({ kind: 'enable', id: off.id, key: off.key ?? off.id, title: off.title });
300
+ continue;
301
+ }
302
+ const chosen = providerOf(catalog, contract);
303
+ if (!chosen) {
304
+ const asked = askingFor(catalog, contract);
305
+ if (asked !== undefined) {
306
+ if (!asks.includes(asked)) asks.push(asked);
307
+ } else if (!missing.includes(contract)) missing.push(contract);
308
+ continue;
309
+ }
310
+ if (visiting.has(chosen.name)) {
311
+ if (!cycle.includes(chosen.name)) cycle.push(chosen.name);
312
+ continue;
313
+ }
314
+ if (done.has(chosen.name)) continue;
315
+ visiting.add(chosen.name);
316
+ visit(chosen.requires ?? []);
317
+ visiting.delete(chosen.name);
318
+ done.add(chosen.name);
319
+ before.push({ kind: 'install', entry: chosen });
320
+ }
321
+ };
322
+ visit(entry.requires ?? []);
323
+ return { before, missing, cycle, asks };
324
+ }
325
+
326
+ /** Whether Bridge does not publish `entry` (not Official: neither Built-in nor a verified package): installing it asks
327
+ * once more. */
328
+ export const thirdParty = (entry: CatalogEntry) => !entry.official;
329
+
330
+ /** A one-click way out for a plugin waiting for contracts (`unresolved`), the same rule for every plugin: enable the
331
+ * installed plugin that provides it, else install the catalog's, else none (the contract named). */
332
+ export type UnresolvedFix =
333
+ | { contract: string; kind: 'enable'; id: string; title: string }
334
+ | { contract: string; kind: 'install'; entry: CatalogEntry }
335
+ /** Installed and on, yet not giving it (unresolved or failed itself): its own page says why. */
336
+ | { contract: string; kind: 'open'; id: string; title: string }
337
+ /** Nothing to do here; `asks`: the catalog's provider asks for a later Bridge. */
338
+ | { contract: string; kind: 'none'; asks?: string };
339
+ export function unresolvedFixes(
340
+ unresolved: string[],
341
+ plugins: { id: string; title: string; state: string; implements: string[] }[],
342
+ catalog: CatalogEntry[],
343
+ ): UnresolvedFix[] {
344
+ return unresolved.map((contract): UnresolvedFix => {
345
+ const off = plugins.find(
346
+ (each) => each.state === 'disabled' && each.implements.includes(contract),
347
+ );
348
+ if (off) return { contract, kind: 'enable', id: off.id, title: off.title };
349
+ const stuck = plugins.find((each) => each.implements.includes(contract));
350
+ if (stuck) return { contract, kind: 'open', id: stuck.id, title: stuck.title };
351
+ const entry = providerOf(catalog, contract);
352
+ const asks = askingFor(catalog, contract);
353
+ if (entry) return { contract, kind: 'install', entry };
354
+ return asks === undefined ? { contract, kind: 'none' } : { contract, kind: 'none', asks };
355
+ });
356
+ }