@mostra/protocol 0.14.0 → 0.15.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.
Files changed (125) hide show
  1. package/dist/artifact-path.d.ts +22 -0
  2. package/dist/artifact-path.d.ts.map +1 -0
  3. package/dist/artifact-path.js +39 -0
  4. package/dist/artifact-path.js.map +1 -0
  5. package/dist/diagnostics/codes/index.d.ts +5 -2
  6. package/dist/diagnostics/codes/index.d.ts.map +1 -1
  7. package/dist/diagnostics/codes/index.js +7 -1
  8. package/dist/diagnostics/codes/index.js.map +1 -1
  9. package/dist/diagnostics/codes/license.d.ts +8 -0
  10. package/dist/diagnostics/codes/license.d.ts.map +1 -0
  11. package/dist/diagnostics/codes/license.js +55 -0
  12. package/dist/diagnostics/codes/license.js.map +1 -0
  13. package/dist/diagnostics/codes/pack.d.ts +8 -0
  14. package/dist/diagnostics/codes/pack.d.ts.map +1 -0
  15. package/dist/diagnostics/codes/pack.js +110 -0
  16. package/dist/diagnostics/codes/pack.js.map +1 -0
  17. package/dist/diagnostics/codes/plugin.d.ts +8 -0
  18. package/dist/diagnostics/codes/plugin.d.ts.map +1 -0
  19. package/dist/diagnostics/codes/plugin.js +154 -0
  20. package/dist/diagnostics/codes/plugin.js.map +1 -0
  21. package/dist/diagnostics/diagnostic.d.ts +1 -1
  22. package/dist/diagnostics/diagnostic.d.ts.map +1 -1
  23. package/dist/diagnostics/diagnostic.js +4 -1
  24. package/dist/diagnostics/diagnostic.js.map +1 -1
  25. package/dist/index.d.ts +13 -1
  26. package/dist/index.js +13 -1
  27. package/dist/methods/deps/add.d.ts +6 -0
  28. package/dist/methods/deps/add.d.ts.map +1 -1
  29. package/dist/methods/deps/add.js +1 -0
  30. package/dist/methods/deps/add.js.map +1 -1
  31. package/dist/methods/index.d.ts.map +1 -1
  32. package/dist/methods/index.js +15 -1
  33. package/dist/methods/index.js.map +1 -1
  34. package/dist/methods/mostra/initialize.d.ts +7 -0
  35. package/dist/methods/mostra/initialize.d.ts.map +1 -1
  36. package/dist/methods/mostra/initialize.js +4 -2
  37. package/dist/methods/mostra/initialize.js.map +1 -1
  38. package/dist/methods/pack/add.d.ts +80 -0
  39. package/dist/methods/pack/add.d.ts.map +1 -0
  40. package/dist/methods/pack/add.js +59 -0
  41. package/dist/methods/pack/add.js.map +1 -0
  42. package/dist/methods/plugin/add.d.ts +43 -0
  43. package/dist/methods/plugin/add.d.ts.map +1 -0
  44. package/dist/methods/plugin/add.js +39 -0
  45. package/dist/methods/plugin/add.js.map +1 -0
  46. package/dist/methods/plugin/commands.d.ts +38 -0
  47. package/dist/methods/plugin/commands.d.ts.map +1 -0
  48. package/dist/methods/plugin/commands.js +33 -0
  49. package/dist/methods/plugin/commands.js.map +1 -0
  50. package/dist/methods/plugin/export.d.ts +58 -0
  51. package/dist/methods/plugin/export.d.ts.map +1 -0
  52. package/dist/methods/plugin/export.js +57 -0
  53. package/dist/methods/plugin/export.js.map +1 -0
  54. package/dist/methods/plugin/list.d.ts +26 -0
  55. package/dist/methods/plugin/list.d.ts.map +1 -0
  56. package/dist/methods/plugin/list.js +30 -0
  57. package/dist/methods/plugin/list.js.map +1 -0
  58. package/dist/methods/plugin/remove.d.ts +32 -0
  59. package/dist/methods/plugin/remove.d.ts.map +1 -0
  60. package/dist/methods/plugin/remove.js +33 -0
  61. package/dist/methods/plugin/remove.js.map +1 -0
  62. package/dist/methods/plugin/validate.d.ts +35 -0
  63. package/dist/methods/plugin/validate.d.ts.map +1 -0
  64. package/dist/methods/plugin/validate.js +33 -0
  65. package/dist/methods/plugin/validate.js.map +1 -0
  66. package/dist/plugin/contract-versions.generated.js +14 -0
  67. package/dist/plugin/contract-versions.generated.js.map +1 -0
  68. package/dist/plugin/contracts.d.ts +51 -0
  69. package/dist/plugin/contracts.d.ts.map +1 -0
  70. package/dist/plugin/contracts.js +129 -0
  71. package/dist/plugin/contracts.js.map +1 -0
  72. package/dist/plugin/license.d.ts +87 -0
  73. package/dist/plugin/license.d.ts.map +1 -0
  74. package/dist/plugin/license.js +243 -0
  75. package/dist/plugin/license.js.map +1 -0
  76. package/dist/plugin/manifest.d.ts +141 -0
  77. package/dist/plugin/manifest.d.ts.map +1 -0
  78. package/dist/plugin/manifest.js +281 -0
  79. package/dist/plugin/manifest.js.map +1 -0
  80. package/dist/plugin/registry-item.d.ts +171 -0
  81. package/dist/plugin/registry-item.d.ts.map +1 -0
  82. package/dist/plugin/registry-item.js +162 -0
  83. package/dist/plugin/registry-item.js.map +1 -0
  84. package/dist/plugin/spdx.generated.d.ts +9 -0
  85. package/dist/plugin/spdx.generated.d.ts.map +1 -0
  86. package/dist/plugin/spdx.generated.js +846 -0
  87. package/dist/plugin/spdx.generated.js.map +1 -0
  88. package/dist/plugin.d.ts +7 -0
  89. package/dist/plugin.js +6 -0
  90. package/dist/plugins.d.ts +118 -0
  91. package/dist/plugins.d.ts.map +1 -0
  92. package/dist/plugins.js +109 -0
  93. package/dist/plugins.js.map +1 -0
  94. package/package.json +19 -4
  95. package/schemas/plugin-manifest.draft-07.json +611 -0
  96. package/schemas/plugin-manifest.schema.json +607 -0
  97. package/schemas/protocol.draft-07.json +899 -1
  98. package/schemas/protocol.schema.json +875 -1
  99. package/src/artifact-path.ts +46 -0
  100. package/src/diagnostics/codes/index.ts +14 -2
  101. package/src/diagnostics/codes/license.ts +65 -0
  102. package/src/diagnostics/codes/pack.ts +125 -0
  103. package/src/diagnostics/codes/plugin.ts +173 -0
  104. package/src/diagnostics/diagnostic.ts +7 -1
  105. package/src/index.ts +57 -0
  106. package/src/methods/deps/add.ts +7 -0
  107. package/src/methods/index.ts +14 -0
  108. package/src/methods/mostra/initialize.ts +9 -1
  109. package/src/methods/pack/add.ts +133 -0
  110. package/src/methods/plugin/add.ts +70 -0
  111. package/src/methods/plugin/commands.ts +67 -0
  112. package/src/methods/plugin/export.ts +100 -0
  113. package/src/methods/plugin/list.ts +45 -0
  114. package/src/methods/plugin/remove.ts +58 -0
  115. package/src/methods/plugin/validate.ts +60 -0
  116. package/src/plugin/contract-versions.generated.ts +15 -0
  117. package/src/plugin/contracts.ts +171 -0
  118. package/src/plugin/license.ts +320 -0
  119. package/src/plugin/manifest.ts +546 -0
  120. package/src/plugin/registry-item.ts +330 -0
  121. package/src/plugin/spdx-tables.ts +74 -0
  122. package/src/plugin/spdx.generated.ts +844 -0
  123. package/src/plugin.ts +77 -0
  124. package/src/plugins.ts +239 -0
  125. package/LICENSE +0 -202
@@ -0,0 +1,330 @@
1
+ /**
2
+ * Source packs (D30, ROUND-3 §6.3, EX-03): copy-source items in a superset of the shadcn registry
3
+ * format. An item is a shadcn `registry-item.json` (verified against shadcn 4.23.0, VERIFIED_FACTS
4
+ * WP-16) with an optional `mostra` block, so any shadcn item installs in Mostra unchanged, and a
5
+ * Mostra pack installs through shadcn too: shadcn's own schema drops the `mostra` key it doesn't
6
+ * know. Every object here is open, so an item a later shadcn release writes still reads.
7
+ */
8
+ import * as z from "zod";
9
+ import { LicenseClass } from "../plugins.ts";
10
+ import { pluginManifestRegistry } from "./manifest.ts";
11
+
12
+ /** One file of an item. */
13
+ export interface RegistryFile {
14
+ /** Its path in the registry (where `content` came from, or where to read it). */
15
+ readonly path: string;
16
+ /** Its text; a GitHub registry's item and a local item may leave it to `path`. */
17
+ readonly content?: string;
18
+ /** `registry:ui`, `registry:lib`, `registry:hook`, `registry:component`, `registry:page`, `registry:file` … */
19
+ readonly type: string;
20
+ /** Where it goes in the project; required for `registry:page` and `registry:file`. */
21
+ readonly target?: string;
22
+ }
23
+
24
+ /** CSS variables an item adds, by where they go. */
25
+ export interface RegistryCssVars {
26
+ /** Into Tailwind 4's `@theme`. */
27
+ readonly theme?: Readonly<Record<string, string>>;
28
+ /** Variables for the light theme. */
29
+ readonly light?: Readonly<Record<string, string>>;
30
+ /** Variables for the dark theme. */
31
+ readonly dark?: Readonly<Record<string, string>>;
32
+ }
33
+
34
+ /** A CSS value: a declaration's value, or a nested block (selectors, at-rules). */
35
+ export type RegistryCssValue =
36
+ | string
37
+ | readonly (string | Readonly<Record<string, string>>)[]
38
+ | { readonly [key: string]: RegistryCssValue };
39
+
40
+ /** A font a `registry:font` item installs. */
41
+ export interface RegistryFont {
42
+ /** The font family. */
43
+ readonly family: string;
44
+ /** Where the font comes from: `google` for Google Fonts. */
45
+ readonly provider: string;
46
+ /** The name its provider's import takes: `Inter`. */
47
+ readonly import: string;
48
+ /** The CSS variable that names it: `--font-sans`. */
49
+ readonly variable: string;
50
+ /** The weights it installs. */
51
+ readonly weight?: readonly string[];
52
+ /** The character subsets it installs. */
53
+ readonly subsets?: readonly string[];
54
+ /** The CSS selector it applies to; `html` when absent. */
55
+ readonly selector?: string;
56
+ /** The npm package that carries it outside Next.js: `@fontsource-variable/inter`. */
57
+ readonly dependency?: string;
58
+ }
59
+
60
+ /** The `mostra` block a Mostra pack adds to its item (ROUND-3 §6.3). */
61
+ export interface PackMostraBlock {
62
+ /** The kit base its components are written for (D27): `base` (Base UI) or `radix`. */
63
+ readonly base?: "base" | "radix";
64
+ /** The same item for the other base, by base: an address `mostra add` takes instead. */
65
+ readonly variants?: Readonly<Partial<Record<"base" | "radix", string>>>;
66
+ /** The field IDs its components introduce (D52), shown at install. */
67
+ readonly fields?: readonly string[];
68
+ /** Its verify baselines, a path in its registry. */
69
+ readonly baselines?: string;
70
+ /** The pack's own license, an SPDX expression; shadcn items carry none. */
71
+ readonly license?: string;
72
+ /** For a `LicenseRef-` license: `source-available` or `proprietary`. */
73
+ readonly licenseClass?: LicenseClass;
74
+ }
75
+
76
+ /** A registry item: shadcn's `registry-item.json`, plus `mostra`. */
77
+ export interface RegistryItem {
78
+ /** The JSON Schema it follows. */
79
+ readonly $schema?: string;
80
+ /** Its name, unique in its registry. */
81
+ readonly name: string;
82
+ /** Its shadcn item type: `registry:ui`, `registry:block`, `registry:lib`, `registry:file` and the rest. */
83
+ readonly type: string;
84
+ /** A short name for people. */
85
+ readonly title?: string;
86
+ /** One sentence on what it does, for people and agents. */
87
+ readonly description?: string;
88
+ /** Who wrote it. */
89
+ readonly author?: string;
90
+ /** npm packages it needs: `name` or `name@version`. */
91
+ readonly dependencies?: readonly string[];
92
+ /** npm packages it needs while developing. */
93
+ readonly devDependencies?: readonly string[];
94
+ /** Other items it needs, by address. */
95
+ readonly registryDependencies?: readonly string[];
96
+ /** Its files. */
97
+ readonly files?: readonly RegistryFile[];
98
+ /** Tailwind 3's configuration; Tailwind 4 projects use `cssVars.theme`. */
99
+ readonly tailwind?: Readonly<Record<string, unknown>>;
100
+ /** CSS variables it adds: to `@theme`, and for the light and dark themes. */
101
+ readonly cssVars?: RegistryCssVars;
102
+ /** CSS rules it adds, by selector or at-rule. */
103
+ readonly css?: { readonly [key: string]: RegistryCssValue };
104
+ /** Example environment variables; Mostra's server code reads secrets instead (D44). */
105
+ readonly envVars?: Readonly<Record<string, string>>;
106
+ /** Anything else the item's author records. */
107
+ readonly meta?: Readonly<Record<string, unknown>>;
108
+ /** A note shown after it installs. */
109
+ readonly docs?: string;
110
+ /** Categories for listings. */
111
+ readonly categories?: readonly string[];
112
+ /** The item it builds on (`none` for a base that starts afresh). */
113
+ readonly extends?: string;
114
+ /** The font a `registry:font` item installs. */
115
+ readonly font?: RegistryFont;
116
+ /** What a Mostra pack adds: its base, variants, fields, baselines and license. */
117
+ readonly mostra?: PackMostraBlock;
118
+ }
119
+
120
+ const RegistryFileSchema: z.ZodType<RegistryFile> = z
121
+ .looseObject({
122
+ path: z.string().min(1),
123
+ content: z.string().exactOptional(),
124
+ type: z.string().min(1),
125
+ target: z.string().exactOptional(),
126
+ })
127
+ .register(pluginManifestRegistry, { id: "RegistryFile" });
128
+
129
+ const stringRecord = z.record(z.string(), z.string());
130
+
131
+ const RegistryCssValueSchema: z.ZodType<RegistryCssValue> = z.lazy(() =>
132
+ z.union([
133
+ z.string(),
134
+ z.array(z.union([z.string(), stringRecord])).readonly(),
135
+ z.record(z.string(), RegistryCssValueSchema),
136
+ ]),
137
+ );
138
+
139
+ const PackMostraBlockSchema: z.ZodType<PackMostraBlock> = z
140
+ .looseObject({
141
+ base: z.enum(["base", "radix"]).exactOptional(),
142
+ variants: z.partialRecord(z.enum(["base", "radix"]), z.string().min(1)).exactOptional(),
143
+ fields: z.array(z.string().min(1)).readonly().exactOptional(),
144
+ baselines: z.string().min(1).exactOptional(),
145
+ license: z.string().min(1).max(512).exactOptional(),
146
+ licenseClass: LicenseClass.exactOptional(),
147
+ })
148
+ .register(pluginManifestRegistry, {
149
+ id: "PackMostraBlock",
150
+ description:
151
+ "What a Mostra pack adds to a shadcn registry item: its base, variants, fields, baselines and license.",
152
+ });
153
+
154
+ /** The Zod schema of the `RegistryItem` type: shadcn's item, open, with the `mostra` block. */
155
+ export const RegistryItemSchema: z.ZodType<RegistryItem> = z
156
+ .looseObject({
157
+ $schema: z.string().exactOptional(),
158
+ name: z.string().min(1),
159
+ type: z.string().min(1),
160
+ title: z.string().exactOptional(),
161
+ description: z.string().exactOptional(),
162
+ author: z.string().exactOptional(),
163
+ dependencies: z.array(z.string().min(1)).readonly().exactOptional(),
164
+ devDependencies: z.array(z.string().min(1)).readonly().exactOptional(),
165
+ registryDependencies: z.array(z.string().min(1)).readonly().exactOptional(),
166
+ files: z.array(RegistryFileSchema).readonly().exactOptional(),
167
+ tailwind: z.record(z.string(), z.unknown()).exactOptional(),
168
+ cssVars: z
169
+ .looseObject({
170
+ theme: stringRecord.exactOptional(),
171
+ light: stringRecord.exactOptional(),
172
+ dark: stringRecord.exactOptional(),
173
+ })
174
+ .exactOptional(),
175
+ css: z.record(z.string(), RegistryCssValueSchema).exactOptional(),
176
+ envVars: stringRecord.exactOptional(),
177
+ meta: z.record(z.string(), z.unknown()).exactOptional(),
178
+ docs: z.string().exactOptional(),
179
+ categories: z.array(z.string()).readonly().exactOptional(),
180
+ extends: z.string().exactOptional(),
181
+ font: z
182
+ .looseObject({
183
+ family: z.string(),
184
+ provider: z.string(),
185
+ import: z.string(),
186
+ variable: z.string(),
187
+ weight: z.array(z.string()).readonly().exactOptional(),
188
+ subsets: z.array(z.string()).readonly().exactOptional(),
189
+ selector: z.string().exactOptional(),
190
+ dependency: z.string().exactOptional(),
191
+ })
192
+ .exactOptional(),
193
+ mostra: PackMostraBlockSchema.exactOptional(),
194
+ })
195
+ .register(pluginManifestRegistry, {
196
+ id: "RegistryItem",
197
+ description:
198
+ "A source pack item (D30): shadcn's registry-item.json, which installs unchanged, plus an optional mostra block.",
199
+ });
200
+
201
+ /** A registry (`registry.json`): its items, or files that hold them (`include`). */
202
+ export interface Registry {
203
+ /** Its name, unique in its registry. */
204
+ readonly name?: string;
205
+ /** Where people read about it. */
206
+ readonly homepage?: string;
207
+ /** Other `registry.json` files whose items it includes. */
208
+ readonly include?: readonly string[];
209
+ /** Its items. */
210
+ readonly items?: readonly RegistryItem[];
211
+ }
212
+
213
+ /** The Zod schema of the `Registry` type. */
214
+ export const RegistrySchema: z.ZodType<Registry> = z
215
+ .looseObject({
216
+ name: z.string().exactOptional(),
217
+ homepage: z.string().exactOptional(),
218
+ include: z.array(z.string().min(1)).readonly().exactOptional(),
219
+ items: z.array(RegistryItemSchema).readonly().exactOptional(),
220
+ })
221
+ .register(pluginManifestRegistry, { id: "Registry" });
222
+
223
+ /**
224
+ * Where an item comes from, as `mostra add` reads an address (shadcn's forms, D30):
225
+ * - `url`: an `https:` (or loopback `http:`) URL of an item's JSON;
226
+ * - `file`: a local item, a path ending in `.json` (or starting with `./`, `../`, `/`);
227
+ * - `github`: `owner/repo/item[#ref]`, read from the repository's root `registry.json`;
228
+ * - `namespace`: `@namespace/item`, through the artifact's `components.json` `registries`;
229
+ * - `name`: a bare name, from the default registry (shadcn's own);
230
+ * - `plugin`: `plugin:<package>/<item>`, a pack an installed plugin carries (`contributes.packs`,
231
+ * by the item's `name`), Mostra's own form.
232
+ */
233
+ export type PackAddress =
234
+ | { readonly kind: "url"; readonly url: string }
235
+ | { readonly kind: "file"; readonly path: string }
236
+ | {
237
+ readonly kind: "github";
238
+ readonly owner: string;
239
+ readonly repo: string;
240
+ readonly item: string;
241
+ readonly ref?: string;
242
+ }
243
+ | { readonly kind: "namespace"; readonly namespace: string; readonly item: string }
244
+ | { readonly kind: "name"; readonly name: string }
245
+ | { readonly kind: "plugin"; readonly package: string; readonly item: string };
246
+
247
+ const GITHUB_PART = /^[A-Za-z0-9](?:[A-Za-z0-9._-]{0,99})$/;
248
+
249
+ /**
250
+ * Reads an item address, or answers `undefined` for text that is none of shadcn's forms (an npm
251
+ * package spec, for `mostra add` to install as a package).
252
+ */
253
+ export function parsePackAddress(text: string): PackAddress | undefined {
254
+ const plugin = /^plugin:((?:@[a-z0-9][a-z0-9._-]*\/)?[a-z0-9][a-z0-9._-]*)\/([^\s/#]+)$/.exec(
255
+ text,
256
+ );
257
+ if (plugin) return { kind: "plugin", package: plugin[1] as string, item: plugin[2] as string };
258
+ if (/^https?:\/\//i.test(text)) return { kind: "url", url: text };
259
+ if (/^(?:\.{1,2}[\\/]|[\\/]|[A-Za-z]:[\\/])/.test(text) || /\.json$/i.test(text)) {
260
+ return { kind: "file", path: text };
261
+ }
262
+ const namespaced = /^(@[a-z0-9][a-z0-9-_.]*)\/([^\s#]+)$/i.exec(text);
263
+ if (namespaced)
264
+ return { kind: "namespace", namespace: namespaced[1] as string, item: namespaced[2] as string };
265
+ const [address, ref] = text.split("#", 2) as [string, string | undefined];
266
+ const parts = address.split("/");
267
+ if (
268
+ parts.length >= 3 &&
269
+ GITHUB_PART.test(parts[0] as string) &&
270
+ GITHUB_PART.test(parts[1] as string) &&
271
+ parts.slice(2).every((part) => /^[A-Za-z0-9._-]+$/.test(part))
272
+ ) {
273
+ return {
274
+ kind: "github",
275
+ owner: parts[0] as string,
276
+ repo: parts[1] as string,
277
+ item: parts.slice(2).join("/"),
278
+ ...(ref !== undefined && ref !== "" ? { ref } : {}),
279
+ };
280
+ }
281
+ return undefined;
282
+ }
283
+
284
+ /** The variables every pack read may use: GitHub's tokens, shadcn's registry and the proxies. */
285
+ export const PACK_VARIABLES: readonly string[] = [
286
+ "GH_TOKEN",
287
+ "GITHUB_TOKEN",
288
+ "REGISTRY_URL",
289
+ "HTTPS_PROXY",
290
+ "https_proxy",
291
+ "HTTP_PROXY",
292
+ "http_proxy",
293
+ "NO_PROXY",
294
+ "no_proxy",
295
+ ];
296
+
297
+ /**
298
+ * The variables reading an artifact's items may use, which a caller sends with `pack/add` (its
299
+ * `environment`), since the instance's own environment may be older than the caller's shell:
300
+ * {@link PACK_VARIABLES}, and every `${VAR}` an artifact's `components.json` text names in a
301
+ * registry's `params` or `headers` (as shadcn expands them). A text that isn't JSON names none.
302
+ */
303
+ export function packVariables(componentsJson: string | undefined): string[] {
304
+ const names = new Set(PACK_VARIABLES);
305
+ if (componentsJson !== undefined) {
306
+ let data: unknown;
307
+ try {
308
+ data = JSON.parse(componentsJson);
309
+ } catch {
310
+ data = undefined;
311
+ }
312
+ const registries = (data as { registries?: unknown } | undefined)?.registries;
313
+ if (typeof registries === "object" && registries !== null) {
314
+ for (const entry of Object.values(registries)) {
315
+ if (typeof entry !== "object" || entry === null) continue;
316
+ const { params, headers } = entry as { params?: unknown; headers?: unknown };
317
+ for (const record of [params, headers]) {
318
+ if (typeof record !== "object" || record === null) continue;
319
+ for (const value of Object.values(record)) {
320
+ if (typeof value !== "string") continue;
321
+ for (const match of value.matchAll(/\$\{([A-Za-z_][A-Za-z0-9_]*)\}/g)) {
322
+ names.add(match[1] as string);
323
+ }
324
+ }
325
+ }
326
+ }
327
+ }
328
+ }
329
+ return [...names].sort();
330
+ }
@@ -0,0 +1,74 @@
1
+ /**
2
+ * Builds `spdx.generated.ts` from the SPDX License List's own data (`data/spdx/`, CC0-1.0):
3
+ * every license ID with whether OSI approved it and whether the FSF calls it free, and every
4
+ * exception ID. `scripts/spdx.ts` writes the module; a test checks it against the data.
5
+ */
6
+
7
+ /** What the generated module holds. */
8
+ export interface SpdxTables {
9
+ readonly version: string;
10
+ /** Each license ID with its flags: `o` OSI-approved, `f` FSF libre, `d` deprecated. */
11
+ readonly licenses: Readonly<Record<string, string>>;
12
+ readonly exceptions: readonly string[];
13
+ }
14
+
15
+ /** The tables from the list's `licenses.json` and `exceptions.json`. */
16
+ export function spdxTables(licensesJson: string, exceptionsJson: string): SpdxTables {
17
+ const licenses = JSON.parse(licensesJson) as {
18
+ licenseListVersion: string;
19
+ licenses: {
20
+ licenseId: string;
21
+ isOsiApproved?: boolean;
22
+ isFsfLibre?: boolean;
23
+ isDeprecatedLicenseId?: boolean;
24
+ }[];
25
+ };
26
+ const exceptions = JSON.parse(exceptionsJson) as {
27
+ licenseListVersion: string;
28
+ exceptions: { licenseExceptionId: string }[];
29
+ };
30
+ if (licenses.licenseListVersion !== exceptions.licenseListVersion) {
31
+ throw new Error(
32
+ `licenses.json is ${licenses.licenseListVersion} and exceptions.json ${exceptions.licenseListVersion}.`,
33
+ );
34
+ }
35
+ const table: Record<string, string> = {};
36
+ for (const license of [...licenses.licenses].sort((a, b) =>
37
+ a.licenseId < b.licenseId ? -1 : 1,
38
+ )) {
39
+ table[license.licenseId] =
40
+ `${license.isOsiApproved ? "o" : ""}${license.isFsfLibre ? "f" : ""}${license.isDeprecatedLicenseId ? "d" : ""}`;
41
+ }
42
+ return {
43
+ version: licenses.licenseListVersion,
44
+ licenses: table,
45
+ exceptions: exceptions.exceptions.map((exception) => exception.licenseExceptionId).sort(),
46
+ };
47
+ }
48
+
49
+ /** A string array as Biome formats it: one element a line, with a trailing comma. */
50
+ function list(values: readonly string[]): string {
51
+ return `[\n${values.map((value) => ` ${JSON.stringify(value)},\n`).join("")}]`;
52
+ }
53
+
54
+ /** The text of `spdx.generated.ts`. */
55
+ export function spdxModule(tables: SpdxTables): string {
56
+ const licenses = Object.entries(tables.licenses).map(([id, flags]) => `${id}:${flags}`);
57
+ return `/**
58
+ * Generated by \`node packages/protocol/scripts/spdx.ts\` from the SPDX License List ${tables.version}
59
+ * (\`data/spdx/\`, CC0-1.0). Don't edit: replace the data and run the script.
60
+ */
61
+
62
+ /** The SPDX License List release these tables come from. */
63
+ export const SPDX_LICENSE_LIST_VERSION: string = ${JSON.stringify(tables.version)};
64
+
65
+ /**
66
+ * Every license ID with its flags after a colon: \`o\` OSI-approved, \`f\` FSF libre, \`d\`
67
+ * deprecated.
68
+ */
69
+ export const SPDX_LICENSES: readonly string[] = ${list(licenses)};
70
+
71
+ /** Every license exception ID (the right side of \`WITH\`). */
72
+ export const SPDX_EXCEPTIONS: readonly string[] = ${list(tables.exceptions)};
73
+ `;
74
+ }