|
| 1 | +// ============================================================================= |
| 2 | +// Sync libredb-studio's docker-compose.example.yml into the website. |
| 3 | +// ============================================================================= |
| 4 | +// The canonical file lives in the separate `libredb-studio` repo. We pull a |
| 5 | +// copy into this repo at build time so the /docker-compose-example/ page can |
| 6 | +// render it and visitors can download it raw from libredb.org. |
| 7 | +// |
| 8 | +// Writes the same content to two committed destinations: |
| 9 | +// - src/data/docker-compose.example.yml (imported by the page as ?raw) |
| 10 | +// - public/docker-compose.example.yml (served verbatim for curl/wget) |
| 11 | +// |
| 12 | +// Resolution order (first that succeeds wins). On total failure we keep any |
| 13 | +// existing committed copy and warn — the build must never fail because the |
| 14 | +// upstream repo is briefly unreachable. |
| 15 | +// 1. Remote raw GitHub URL (works on CI; libredb-studio is public) |
| 16 | +// 2. Local sibling checkout ../libredb-studio/... (fast local dev) |
| 17 | +// 3. Existing committed copy (offline / upstream not yet pushed) |
| 18 | +// ============================================================================= |
| 19 | + |
| 20 | +import { readFile, writeFile, mkdir } from 'node:fs/promises'; |
| 21 | +import { existsSync } from 'node:fs'; |
| 22 | +import { dirname, resolve } from 'node:path'; |
| 23 | +import { fileURLToPath } from 'node:url'; |
| 24 | + |
| 25 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 26 | +const ROOT = resolve(__dirname, '..'); |
| 27 | + |
| 28 | +const REMOTE_URL = |
| 29 | + 'https://raw.githubusercontent.com/libredb/libredb-studio/main/docker-compose.example.yml'; |
| 30 | +const LOCAL_SIBLING = resolve(ROOT, '../libredb-studio/docker-compose.example.yml'); |
| 31 | + |
| 32 | +const DEST_DATA = resolve(ROOT, 'src/data/docker-compose.example.yml'); |
| 33 | +const DEST_PUBLIC = resolve(ROOT, 'public/docker-compose.example.yml'); |
| 34 | + |
| 35 | +async function fromRemote() { |
| 36 | + const controller = new AbortController(); |
| 37 | + const timeout = setTimeout(() => controller.abort(), 8000); |
| 38 | + try { |
| 39 | + const res = await fetch(REMOTE_URL, { signal: controller.signal }); |
| 40 | + if (!res.ok) throw new Error(`HTTP ${res.status}`); |
| 41 | + const text = await res.text(); |
| 42 | + if (!text.trim()) throw new Error('empty response'); |
| 43 | + return text; |
| 44 | + } finally { |
| 45 | + clearTimeout(timeout); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +async function fromLocalSibling() { |
| 50 | + if (!existsSync(LOCAL_SIBLING)) throw new Error('local sibling not found'); |
| 51 | + return readFile(LOCAL_SIBLING, 'utf8'); |
| 52 | +} |
| 53 | + |
| 54 | +async function resolveContent() { |
| 55 | + try { |
| 56 | + const text = await fromRemote(); |
| 57 | + console.log('[sync-docker-compose] using remote source:', REMOTE_URL); |
| 58 | + return text; |
| 59 | + } catch (err) { |
| 60 | + console.warn(`[sync-docker-compose] remote fetch failed (${err.message}); trying local sibling`); |
| 61 | + } |
| 62 | + |
| 63 | + try { |
| 64 | + const text = await fromLocalSibling(); |
| 65 | + console.log('[sync-docker-compose] using local sibling:', LOCAL_SIBLING); |
| 66 | + return text; |
| 67 | + } catch (err) { |
| 68 | + console.warn(`[sync-docker-compose] local sibling unavailable (${err.message})`); |
| 69 | + } |
| 70 | + |
| 71 | + return null; |
| 72 | +} |
| 73 | + |
| 74 | +async function writeBoth(content) { |
| 75 | + await mkdir(dirname(DEST_DATA), { recursive: true }); |
| 76 | + await writeFile(DEST_DATA, content, 'utf8'); |
| 77 | + await writeFile(DEST_PUBLIC, content, 'utf8'); |
| 78 | + console.log('[sync-docker-compose] wrote', DEST_DATA); |
| 79 | + console.log('[sync-docker-compose] wrote', DEST_PUBLIC); |
| 80 | +} |
| 81 | + |
| 82 | +const content = await resolveContent(); |
| 83 | + |
| 84 | +if (content) { |
| 85 | + await writeBoth(content); |
| 86 | +} else if (existsSync(DEST_DATA) && existsSync(DEST_PUBLIC)) { |
| 87 | + console.warn('[sync-docker-compose] keeping existing committed copy — no fresh source available'); |
| 88 | +} else { |
| 89 | + console.error('[sync-docker-compose] FATAL: no source available and no committed copy exists'); |
| 90 | + process.exit(1); |
| 91 | +} |
0 commit comments