|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Syncs Crowdin distribution files from distributions.crowdin.net to a local directory. |
| 4 | + * Designed to be run from GitHub Actions and produce a static-file artifact for GitHub Pages. |
| 5 | + * |
| 6 | + * Usage: |
| 7 | + * node sync-crowdin-distribution.js |
| 8 | + * |
| 9 | + * Environment variables: |
| 10 | + * OUTPUT_DIR - Directory to write files into (default: dist-pages/crowdin-dist) |
| 11 | + */ |
| 12 | + |
| 13 | +'use strict'; |
| 14 | + |
| 15 | +const https = require('node:https'); |
| 16 | +const fs = require('node:fs'); |
| 17 | +const path = require('node:path'); |
| 18 | + |
| 19 | +const BASE_CDN = 'https://distributions.crowdin.net'; |
| 20 | +const OUTPUT_DIR = path.resolve(process.env.OUTPUT_DIR || 'dist-pages/crowdin-dist'); |
| 21 | + |
| 22 | +/** Number of simultaneous downloads per batch. */ |
| 23 | +const CONCURRENCY = 8; |
| 24 | + |
| 25 | +/** |
| 26 | + * Distribution hashes to sync. |
| 27 | + * Read from the CROWDIN_DISTRIBUTION_IDS environment variable as a |
| 28 | + * comma-separated list (e.g. "hash1,hash2"). Store the value in GitHub |
| 29 | + * project variables under the name CROWDIN_DISTRIBUTION_IDS. |
| 30 | + */ |
| 31 | +const DISTRIBUTIONS = (process.env.CROWDIN_DISTRIBUTION_IDS || '') |
| 32 | + .split(',') |
| 33 | + .map((s) => s.trim()) |
| 34 | + .filter(Boolean); |
| 35 | + |
| 36 | +if (DISTRIBUTIONS.length === 0) { |
| 37 | + console.error('ERROR: CROWDIN_DISTRIBUTION_IDS environment variable is not set or empty.'); |
| 38 | + process.exit(1); |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Fetches a URL, following redirects, and returns the body as a Buffer. |
| 43 | + * @param {string} url |
| 44 | + * @returns {Promise<Buffer>} |
| 45 | + */ |
| 46 | +function fetchUrl(url) { |
| 47 | + return new Promise((resolve, reject) => { |
| 48 | + https.get(url, (res) => { |
| 49 | + // Follow redirects |
| 50 | + if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) { |
| 51 | + return fetchUrl(res.headers.location).then(resolve).catch(reject); |
| 52 | + } |
| 53 | + const chunks = []; |
| 54 | + res.on('data', (chunk) => chunks.push(chunk)); |
| 55 | + res.on('end', () => { |
| 56 | + if (res.statusCode >= 400) { |
| 57 | + return reject(new Error(`HTTP ${res.statusCode} for ${url}`)); |
| 58 | + } |
| 59 | + resolve(Buffer.concat(chunks)); |
| 60 | + }); |
| 61 | + res.on('error', reject); |
| 62 | + }).on('error', reject); |
| 63 | + }); |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Writes data to a file, creating parent directories as needed. |
| 68 | + * @param {string} filePath |
| 69 | + * @param {Buffer|string} data |
| 70 | + */ |
| 71 | +function saveFile(filePath, data) { |
| 72 | + fs.mkdirSync(path.dirname(filePath), { recursive: true }); |
| 73 | + fs.writeFileSync(filePath, data); |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Processes an array of items in fixed-size concurrent batches. |
| 78 | + * @template T |
| 79 | + * @param {T[]} items |
| 80 | + * @param {number} batchSize |
| 81 | + * @param {(item: T) => Promise<void>} fn |
| 82 | + */ |
| 83 | +async function processInBatches(items, batchSize, fn) { |
| 84 | + for (let i = 0; i < items.length; i += batchSize) { |
| 85 | + await Promise.all(items.slice(i, i + batchSize).map(fn)); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * Downloads all distribution files for a single hash. |
| 91 | + * @param {string} hash Distribution hash. |
| 92 | + * @returns {Promise<boolean>} true if all files were fetched without errors. |
| 93 | + */ |
| 94 | +async function syncDistribution(hash) { |
| 95 | + console.log(`\n=== Syncing distribution: ${hash} ===`); |
| 96 | + const hashDir = path.join(OUTPUT_DIR, hash); |
| 97 | + |
| 98 | + // manifest.json |
| 99 | + console.log(' Fetching manifest.json...'); |
| 100 | + const manifestBuf = await fetchUrl(`${BASE_CDN}/${hash}/manifest.json`); |
| 101 | + saveFile(path.join(hashDir, 'manifest.json'), manifestBuf); |
| 102 | + const manifest = JSON.parse(manifestBuf.toString('utf8')); |
| 103 | + |
| 104 | + console.log(` Timestamp : ${manifest.timestamp}`); |
| 105 | + console.log(` Languages : ${(manifest.languages || []).length}`); |
| 106 | + |
| 107 | + // languages.json |
| 108 | + console.log(' Fetching languages.json...'); |
| 109 | + const langsBuf = await fetchUrl(`${BASE_CDN}/${hash}/languages.json`); |
| 110 | + saveFile(path.join(hashDir, 'languages.json'), langsBuf); |
| 111 | + |
| 112 | + // content files |
| 113 | + const contentPaths = new Set(); |
| 114 | + if (manifest.content) { |
| 115 | + for (const paths of Object.values(manifest.content)) { |
| 116 | + for (const p of paths) { |
| 117 | + contentPaths.add(p); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + const pathList = [...contentPaths]; |
| 123 | + console.log(` Content files: ${pathList.length} (concurrency=${CONCURRENCY})`); |
| 124 | + |
| 125 | + let fetched = 0; |
| 126 | + let failed = 0; |
| 127 | + |
| 128 | + await processInBatches(pathList, CONCURRENCY, async (contentPath) => { |
| 129 | + const url = `${BASE_CDN}/${hash}${contentPath}`; |
| 130 | + const localPath = path.join(hashDir, contentPath); |
| 131 | + try { |
| 132 | + const data = await fetchUrl(url); |
| 133 | + saveFile(localPath, data); |
| 134 | + fetched++; |
| 135 | + if ((fetched + failed) % 50 === 0) { |
| 136 | + console.log(` Progress: ${fetched + failed}/${pathList.length}`); |
| 137 | + } |
| 138 | + } catch (err) { |
| 139 | + failed++; |
| 140 | + console.warn(` WARN: failed to fetch ${contentPath}: ${err.message}`); |
| 141 | + } |
| 142 | + }); |
| 143 | + |
| 144 | + console.log(` Result: ${fetched} fetched, ${failed} failed`); |
| 145 | + return failed === 0; |
| 146 | +} |
| 147 | + |
| 148 | +async function main() { |
| 149 | + console.log('Crowdin Distribution Sync'); |
| 150 | + console.log(`Output dir: ${OUTPUT_DIR}`); |
| 151 | + console.log(`Distributions: ${DISTRIBUTIONS.length}`); |
| 152 | + |
| 153 | + fs.mkdirSync(OUTPUT_DIR, { recursive: true }); |
| 154 | + |
| 155 | + let allOk = true; |
| 156 | + for (const hash of DISTRIBUTIONS) { |
| 157 | + try { |
| 158 | + const ok = await syncDistribution(hash); |
| 159 | + if (!ok) allOk = false; |
| 160 | + } catch (err) { |
| 161 | + console.error(`\nFATAL: Failed to sync ${hash}:`, err.message); |
| 162 | + allOk = false; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + if (!allOk) { |
| 167 | + console.error('\nSync completed with errors.'); |
| 168 | + process.exit(1); |
| 169 | + } |
| 170 | + console.log('\nSync complete!'); |
| 171 | +} |
| 172 | + |
| 173 | +main(); |
0 commit comments