|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Fetches ui-modules manifests from known repos and writes a static catalog snapshot. |
| 4 | + * Run: node scripts/sync-extension-catalog.mjs |
| 5 | + */ |
| 6 | + |
| 7 | +import fs from 'node:fs'; |
| 8 | +import path from 'node:path'; |
| 9 | +import { fileURLToPath } from 'node:url'; |
| 10 | +import JSON5 from 'json5'; |
| 11 | + |
| 12 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 13 | +const ROOT = path.resolve(__dirname, '..'); |
| 14 | +const OUT_PATH = path.join(ROOT, 'src', 'data', 'generated', 'extension-catalog.json'); |
| 15 | + |
| 16 | +const knownExtensionRepos = [ |
| 17 | + { |
| 18 | + owner: 'antora-supplemental', |
| 19 | + repo: 'valentus-theme', |
| 20 | + branch: 'main', |
| 21 | + localRegistryIndex: '../valentus-theme/ui-modules/registry-index.json', |
| 22 | + }, |
| 23 | +]; |
| 24 | + |
| 25 | +const UI_MODULES_ROOT = 'ui-modules'; |
| 26 | +const REGISTRY_INDEX = `${UI_MODULES_ROOT}/registry-index.json`; |
| 27 | +const REGISTRY = `${UI_MODULES_ROOT}/registry.json5`; |
| 28 | + |
| 29 | +async function fetchRaw(owner, repo, filePath, branch = 'main') { |
| 30 | + for (const b of [branch, 'main', 'master']) { |
| 31 | + const url = `https://raw.githubusercontent.com/${owner}/${repo}/${b}/${filePath}`; |
| 32 | + const res = await fetch(url, { headers: { 'User-Agent': 'Antora-Extensions-Sync' } }); |
| 33 | + if (res.ok) return res.text(); |
| 34 | + } |
| 35 | + return null; |
| 36 | +} |
| 37 | + |
| 38 | +function readLocalIndex(ref) { |
| 39 | + if (!ref.localRegistryIndex) return null; |
| 40 | + const indexPath = path.resolve(ROOT, ref.localRegistryIndex); |
| 41 | + if (!fs.existsSync(indexPath)) return null; |
| 42 | + return JSON.parse(fs.readFileSync(indexPath, 'utf8')); |
| 43 | +} |
| 44 | + |
| 45 | +async function discoverRepo(ref) { |
| 46 | + const { owner, repo, branch = 'main' } = ref; |
| 47 | + |
| 48 | + const local = readLocalIndex(ref); |
| 49 | + if (local) { |
| 50 | + return { source: 'registry-index.json (local)', ...local }; |
| 51 | + } |
| 52 | + |
| 53 | + const indexText = await fetchRaw(owner, repo, REGISTRY_INDEX, branch); |
| 54 | + if (indexText) { |
| 55 | + const index = JSON.parse(indexText); |
| 56 | + return { source: 'registry-index.json', ...index }; |
| 57 | + } |
| 58 | + |
| 59 | + const registryText = await fetchRaw(owner, repo, REGISTRY, branch); |
| 60 | + if (!registryText) return null; |
| 61 | + |
| 62 | + const registry = JSON5.parse(registryText); |
| 63 | + const modules = []; |
| 64 | + const recipes = []; |
| 65 | + |
| 66 | + for (const entry of registry.modules) { |
| 67 | + const manifestPath = `${UI_MODULES_ROOT}/${entry.path}/${entry.manifest || 'ui-module.json5'}`; |
| 68 | + const manifestText = await fetchRaw(owner, repo, manifestPath, branch); |
| 69 | + if (!manifestText) continue; |
| 70 | + const manifest = JSON5.parse(manifestText); |
| 71 | + modules.push({ |
| 72 | + id: entry.id, |
| 73 | + name: manifest.name, |
| 74 | + version: manifest.version, |
| 75 | + type: 'ui-module', |
| 76 | + description: manifest.description || '', |
| 77 | + repository: manifest.repository || registry.repository, |
| 78 | + manifestPath: `${entry.path}/${entry.manifest || 'ui-module.json5'}`, |
| 79 | + modulePath: entry.path, |
| 80 | + requires: manifest.requires || [], |
| 81 | + recommends: manifest.recommends || [], |
| 82 | + conflicts: manifest.conflicts || [], |
| 83 | + partials: manifest.ui?.partials, |
| 84 | + slots: manifest.slots, |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + for (const recipeEntry of registry.recipes || []) { |
| 89 | + const recipePath = `${UI_MODULES_ROOT}/${recipeEntry.path}`; |
| 90 | + const recipeText = await fetchRaw(owner, repo, recipePath, branch); |
| 91 | + if (!recipeText) continue; |
| 92 | + const recipe = JSON5.parse(recipeText); |
| 93 | + recipes.push({ |
| 94 | + id: recipeEntry.id, |
| 95 | + name: recipe.name, |
| 96 | + version: recipe.version, |
| 97 | + type: 'ui-recipe', |
| 98 | + description: recipe.description || '', |
| 99 | + modules: recipe.modules, |
| 100 | + manifestPath: recipeEntry.path, |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + return { |
| 105 | + schema: registry.schema || '1.0', |
| 106 | + repository: registry.repository || `https://github.com/${owner}/${repo}`, |
| 107 | + source: REGISTRY, |
| 108 | + modules, |
| 109 | + recipes, |
| 110 | + }; |
| 111 | +} |
| 112 | + |
| 113 | +async function main() { |
| 114 | + const modules = []; |
| 115 | + const recipes = []; |
| 116 | + |
| 117 | + for (const ref of knownExtensionRepos) { |
| 118 | + const catalog = await discoverRepo(ref); |
| 119 | + if (!catalog) { |
| 120 | + console.warn(`No catalog in ${ref.owner}/${ref.repo}`); |
| 121 | + continue; |
| 122 | + } |
| 123 | + const slug = `${ref.owner}/${ref.repo}`.toLowerCase(); |
| 124 | + for (const mod of catalog.modules) { |
| 125 | + modules.push({ |
| 126 | + catalogId: `${slug}/${mod.id}`, |
| 127 | + repositoryOwner: ref.owner, |
| 128 | + repositoryName: ref.repo, |
| 129 | + ...mod, |
| 130 | + }); |
| 131 | + } |
| 132 | + for (const recipe of catalog.recipes) { |
| 133 | + recipes.push({ |
| 134 | + catalogId: `${slug}/${recipe.id}`, |
| 135 | + repositoryOwner: ref.owner, |
| 136 | + repositoryName: ref.repo, |
| 137 | + repository: catalog.repository, |
| 138 | + ...recipe, |
| 139 | + }); |
| 140 | + } |
| 141 | + console.log(`Indexed ${catalog.modules.length} modules, ${catalog.recipes.length} recipes from ${ref.owner}/${ref.repo}`); |
| 142 | + } |
| 143 | + |
| 144 | + const output = { |
| 145 | + schema: '1.0', |
| 146 | + generatedAt: new Date().toISOString(), |
| 147 | + source: 'manifest', |
| 148 | + modules, |
| 149 | + recipes, |
| 150 | + }; |
| 151 | + |
| 152 | + fs.mkdirSync(path.dirname(OUT_PATH), { recursive: true }); |
| 153 | + fs.writeFileSync(OUT_PATH, `${JSON.stringify(output, null, 2)}\n`, 'utf8'); |
| 154 | + console.log(`Wrote ${OUT_PATH}`); |
| 155 | +} |
| 156 | + |
| 157 | +main().catch((err) => { |
| 158 | + console.error(err); |
| 159 | + process.exit(1); |
| 160 | +}); |
0 commit comments