import { readFileSync } from 'node:fs'; import { dirname } from 'node:path'; import { readPageEntry, readVolterContributions } from '@volter/sdk/volter-contributions'; /** * THE NODE READER of a package's `package.json` as Volter reads it: its name, its dependency * names and its `contributes.volter` declarations, which `@volter/sdk/volter-contributions` reads as the workbench and * the page read them. The build's product plugin, the session's catalog and * serving doors, the shared-React scope and the pre-bundle crawl all read a package here, so a * declaration means one thing everywhere it is read. * * It parses and does no policy. What an unusable declaration costs is the caller's to say: a * product build refuses it, the catalog reports it as a load error, a scope decision treats it * as no declaration. {@link declaredContributions} is the one test of "this package contributes". */ export interface VolterManifest { /** The `package.json`, absolute. */ readonly path: string; /** Its package's directory. */ readonly dir: string; readonly name: string | null; readonly version: string | null; readonly private: boolean; readonly bin: unknown; /** Every name in `dependencies`, `devDependencies` and `optionalDependencies`, in that order, * each once. */ readonly dependencyNames: readonly string[]; /** `package.json#contributes.volter`, unchecked: the package's contribution modules, an object * keyed by kind, and its declarations (`serving`, `viewRoutes`, `product`). It is where Code-OSS's extension * service reads them too, through the kit's `volter` extension point. */ readonly contributions: unknown; /** `package.json#contributes.languageModelTools`, unchecked: the agent tools Code-OSS registers from it. */ readonly languageModelTools: unknown; /** `package.json#contributes.volter.product`, a product's declaration, read and checked by `product-locator.ts`; * `undefined` when the package declares none or it is not an object. */ readonly product: ProductDeclaration | undefined; } /** `package.json#contributes.volter.product`, as declared and unchecked. */ export interface ProductDeclaration { readonly entry?: unknown; readonly composes?: unknown; readonly colorTheme?: unknown; readonly workbench?: unknown; readonly displayName?: unknown; readonly install?: unknown; readonly upgrade?: unknown; readonly adapterRequired?: unknown; } /** `path`'s manifest, or `null` when there is no file there or it is not a JSON object. */ export function readVolterManifest(path: string): VolterManifest | null { let json: unknown; try { json = JSON.parse(readFileSync(path, 'utf8')); } catch { return null; } if (json === null || typeof json !== 'object' || Array.isArray(json)) return null; const record = json as Record; const names = (key: string): string[] => { const section = record[key]; return section !== null && typeof section === 'object' ? Object.keys(section) : []; }; const contributes = record['contributes']; const contributions = contributes !== null && typeof contributes === 'object' ? (contributes as Record)['volter'] : undefined; const declared = contributions !== null && typeof contributions === 'object' ? (contributions as Record) : {}; const product = declared['product']; return { path, dir: dirname(path), name: typeof record['name'] === 'string' ? record['name'] : null, version: typeof record['version'] === 'string' ? record['version'] : null, private: record['private'] === true, bin: record['bin'], dependencyNames: [...new Set([...names('dependencies'), ...names('devDependencies'), ...names('optionalDependencies')])], contributions, languageModelTools: contributes !== null && typeof contributes === 'object' ? (contributes as Record)['languageModelTools'] : undefined, product: product !== null && typeof product === 'object' && !Array.isArray(product) ? (product as ProductDeclaration) : undefined, }; } /** * The package's declared modules, each contribution's and its page half's, with why any entry is refused; `null` when * it declares none (an absent `contributes.volter`, or one that is not an object). A package with the object, even an * empty one, is a contributing package: its modules are editor-tree code. */ export function declaredContributions(manifest: VolterManifest | null): { readonly modules: readonly string[]; readonly errors: readonly string[] } | null { const declared = manifest?.contributions; if (declared === null || typeof declared !== 'object' || Array.isArray(declared)) return null; const { entries, errors } = readVolterContributions(declared); const page = readPageEntry(manifest?.name ?? manifest?.path ?? '', (declared as Record)['page']); return { modules: [...entries.map(({ entry }) => entry), ...('entry' in page && page.entry !== null ? [page.entry] : [])], errors: 'malformed' in page ? [...errors, page.malformed] : errors, }; }