Skip to content

Commit 0d4d347

Browse files
committed
catalog gen workflow
1 parent 74562ff commit 0d4d347

3 files changed

Lines changed: 102 additions & 0 deletions

File tree

.github/workflows/catalog.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: Theme catalog
2+
3+
on:
4+
push:
5+
branches: [main]
6+
paths:
7+
- '**.css'
8+
- 'catalog.json'
9+
- 'scripts/build-catalog.mjs'
10+
- '.github/workflows/catalog.yml'
11+
workflow_dispatch:
12+
13+
permissions:
14+
contents: write
15+
16+
concurrency:
17+
group: theme-catalog-${{ github.ref }}
18+
cancel-in-progress: true
19+
20+
jobs:
21+
build:
22+
runs-on: ubuntu-latest
23+
steps:
24+
- uses: actions/checkout@v4
25+
26+
- name: Build catalog.json
27+
env:
28+
GITHUB_REPOSITORY: ${{ github.repository }}
29+
GITHUB_REF_NAME: ${{ github.ref_name }}
30+
run: node scripts/build-catalog.mjs
31+
32+
- name: Commit if changed
33+
run: |
34+
git config user.name "github-actions[bot]"
35+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
36+
git add catalog.json
37+
if git diff --staged --quiet; then
38+
echo "catalog.json already up to date."
39+
else
40+
git commit -m "chore: regenerate catalog.json"
41+
git push
42+
fi

catalog.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"version": 1,
3+
"repository": "SableClient/themes",
4+
"ref": "main",
5+
"themes": []
6+
}

scripts/build-catalog.mjs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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

Comments
 (0)