File size: 2,053 Bytes
46252cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Remote plugin catalog: the shape of an entry in the OpenWA-plugins `plugins.json`, and the pure
// logic that annotates each entry with install state relative to what is currently loaded.

/** One entry as published in the remote catalog (plugins.json). Extra fields are tolerated. */
export interface CatalogEntry {
  id: string;
  name: string;
  version: string;
  type?: string;
  status?: string;
  description?: string;
  author?: string;
  license?: string;
  keywords?: string[];
  minOpenWAVersion?: string;
  testedOpenWAVersion?: string;
  releasedAt?: string;
  repoUrl?: string;
  homepage?: string;
  download?: string;
  [key: string]: unknown;
}

/** A catalog entry annotated with this instance's install state. */
export interface CatalogPlugin extends CatalogEntry {
  installed: boolean;
  installedVersion: string | null;
  updateAvailable: boolean;
}

/** Compare two `MAJOR.MINOR.PATCH` strings (pre-release suffixes ignored). Returns -1 | 0 | 1. */
export function compareSemver(a: string, b: string): number {
  const parse = (v: string) =>
    String(v)
      .split('-')[0]
      .split('.')
      .map(n => Number.parseInt(n, 10) || 0);
  const pa = parse(a);
  const pb = parse(b);
  for (let i = 0; i < 3; i++) {
    const d = (pa[i] ?? 0) - (pb[i] ?? 0);
    if (d !== 0) return d > 0 ? 1 : -1;
  }
  return 0;
}

/**
 * Annotate catalog entries with install state, given the plugins currently loaded by this instance.
 * `updateAvailable` is true when an entry is installed and the catalog version is strictly newer.
 */
export function annotateCatalog(
  entries: CatalogEntry[],
  installed: { id: string; version: string }[],
): CatalogPlugin[] {
  const byId = new Map(installed.map(p => [p.id, p.version]));
  return entries.map(entry => {
    const installedVersion = byId.get(entry.id) ?? null;
    return {
      ...entry,
      installed: installedVersion !== null,
      installedVersion,
      updateAvailable: installedVersion !== null && compareSemver(entry.version, installedVersion) > 0,
    };
  });
}