|
| 1 | +#!/usr/bin/env node |
| 2 | +import fs from 'fs'; |
| 3 | +import path from 'path'; |
| 4 | +import { fileURLToPath } from 'url'; |
| 5 | + |
| 6 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 7 | +const REPO_ROOT = path.join(__dirname, '..'); |
| 8 | + |
| 9 | +const PREVIEW_SUFFIX = '.preview.sable.css'; |
| 10 | +const FULL_SUFFIX = '.sable.css'; |
| 11 | + |
| 12 | +const repo = |
| 13 | + process.env.GITHUB_REPOSITORY?.trim() || |
| 14 | + process.env.CATALOG_REPOSITORY?.trim() || |
| 15 | + 'SableClient/themes'; |
| 16 | +const ref = process.env.GITHUB_REF_NAME?.trim() || process.env.CATALOG_REF?.trim() || 'main'; |
| 17 | + |
| 18 | +function rawFileUrl(fileName) { |
| 19 | + return `https://raw.githubusercontent.com/${repo}/${ref}/${fileName}`; |
| 20 | +} |
| 21 | + |
| 22 | +function buildCatalog() { |
| 23 | + const files = fs.readdirSync(REPO_ROOT); |
| 24 | + const previewFiles = files.filter((f) => f.endsWith(PREVIEW_SUFFIX)); |
| 25 | + |
| 26 | + const themes = []; |
| 27 | + for (const previewName of previewFiles) { |
| 28 | + const basename = previewName.slice(0, -PREVIEW_SUFFIX.length); |
| 29 | + const fullName = `${basename}${FULL_SUFFIX}`; |
| 30 | + if (!files.includes(fullName)) { |
| 31 | + console.warn(`skip ${previewName}: missing ${fullName}`); |
| 32 | + continue; |
| 33 | + } |
| 34 | + themes.push({ |
| 35 | + basename, |
| 36 | + previewUrl: rawFileUrl(previewName), |
| 37 | + fullUrl: rawFileUrl(fullName), |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + themes.sort((a, b) => a.basename.localeCompare(b.basename)); |
| 42 | + |
| 43 | + return { |
| 44 | + version: 1, |
| 45 | + repository: repo, |
| 46 | + ref, |
| 47 | + themes, |
| 48 | + }; |
| 49 | +} |
| 50 | + |
| 51 | +const catalog = buildCatalog(); |
| 52 | +const outPath = path.join(REPO_ROOT, 'catalog.json'); |
| 53 | +fs.writeFileSync(outPath, `${JSON.stringify(catalog, null, 2)}\n`, 'utf8'); |
| 54 | +console.log(`Wrote ${outPath} (${catalog.themes.length} theme pairs)`); |
0 commit comments