import path from "node:path"; import fs from "node:fs"; import fsp from "node:fs/promises"; import { MODULE_PREFIX, PACKAGES_DIR, ROOT_DIR, isNonNullable, log_info } from "./utils.js"; // pnpm's `catalog:`/`catalog:` version specifiers are placeholders in package.json — // the real version only lives in pnpm-workspace.yaml's `catalog`/`catalogs.` maps. Resolve // them here so callers see the real version string instead of the literal "catalog:..." text. function extractYamlBlock(text: string, key: string): string | null { const lines = text.split("\n"); const keyLineIndex = lines.findIndex(line => line.trim() === `${key}:`); if (keyLineIndex === -1) return null; const keyIndent = lines[keyLineIndex]!.match(/^\s*/)![0].length; const blockLines: string[] = []; for (let i = keyLineIndex + 1; i < lines.length; i++) { const line = lines[i]!; if (line.trim() === "") continue; const indent = line.match(/^\s*/)![0].length; if (indent <= keyIndent) break; blockLines.push(line); } return blockLines.length > 0 ? blockLines.join("\n") : null; } function extractYamlValue(block: string, key: string): string | null { const escapedKey = key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); const match = block.match(new RegExp(`^\\s*['"]?${escapedKey}['"]?:\\s*(.+)$`, "m")); return match ? match[1]!.trim().replace(/^['"]|['"]$/g, "") : null; } let workspaceYamlText: string | null | undefined; function resolveCatalogVersion(depName: string, catalogName: string): string | null { if (workspaceYamlText === undefined) { const workspacePath = path.join(ROOT_DIR, "pnpm-workspace.yaml"); workspaceYamlText = fs.existsSync(workspacePath) ? fs.readFileSync(workspacePath, "utf8") : null; } if (workspaceYamlText == null) return null; if (catalogName === "") { const catalogBlock = extractYamlBlock(workspaceYamlText, "catalog"); return catalogBlock ? extractYamlValue(catalogBlock, depName) : null; } const catalogsBlock = extractYamlBlock(workspaceYamlText, "catalogs"); if (!catalogsBlock) return null; const namedBlock = extractYamlBlock(catalogsBlock, catalogName); return namedBlock ? extractYamlValue(namedBlock, depName) : null; } function resolveVersionSpecifier(raw: string | undefined): string | null { if (raw == null) return null; const catalogMatch = raw.match(/^catalog:(.*)$/); if (!catalogMatch) return raw; return resolveCatalogVersion("solid-js", catalogMatch[1]!) ?? raw; } export type PackageJson = { name: string; version: string; description: string; primitive?: PrimitiveData; keywords?: string[]; peerDependencies?: { [x: string]: string }; devDependencies?: { [x: string]: string }; dependencies?: { [x: string]: string }; }; export type PrimitiveData = { list: string[]; category: string; stage: number; gzip?: number; }; export type ModuleData = { name: string; version: string; description: string; tags: string[]; primitive: PrimitiveData | null; workspace_deps: string[]; peer_deps: string[]; solid_peer_version: string | null; }; export async function getModuleData(name: string): Promise { const pkg_path = path.join(PACKAGES_DIR, name, "package.json"); if (!fs.existsSync(pkg_path)) { return new Error(`package "${name}" doesn't have package.json`); } const pkg = JSON.parse(await fsp.readFile(pkg_path, "utf8")) as PackageJson; const dependencies = Object.keys(pkg.dependencies ?? {}); const peer_deps = Object.keys(pkg.peerDependencies ?? {}); const workspace_deps: string[] = []; for (const dep of dependencies) { if (dep.startsWith(MODULE_PREFIX)) { const dep_name = dep.slice(MODULE_PREFIX.length); workspace_deps.push(dep_name); } } const excludedKeywords = ["primitive", "solid", pkg.name]; return { name, version: pkg.version, description: pkg.description, tags: pkg.keywords?.filter(keyword => !excludedKeywords.includes(keyword)) ?? [], primitive: pkg.primitive ?? null, workspace_deps, peer_deps, solid_peer_version: [ pkg.peerDependencies?.["solid-js"], pkg.dependencies?.["solid-js"], pkg.devDependencies?.["solid-js"], ] .map(resolveVersionSpecifier) .find((v): v is string => v != null && /^[\^~]?2\./.test(v)) ?? null, }; } export async function getModulesData(): Promise { const module_names = await fsp.readdir(PACKAGES_DIR); const promises = module_names.map(async name => { const module = await getModuleData(name); if (module instanceof Error) { log_info(module.message); return null; } return module; }); return (await Promise.all(promises)).filter(isNonNullable); }