|
| 1 | +import { readFileSync, writeFileSync, mkdirSync } from 'fs'; |
| 2 | + |
| 3 | +const PALETTES_LIST = JSON.parse(readFileSync('public/lospec-palettes.json', 'utf-8')); |
| 4 | +const OUT_DIR = 'public/palettes'; |
| 5 | +const TIMEOUT_MS = 10000; |
| 6 | + |
| 7 | +function parseHexFile(text) { |
| 8 | + return text |
| 9 | + .split('\n') |
| 10 | + .map(line => line.trim()) |
| 11 | + .filter(line => line && !line.startsWith(';')) |
| 12 | + .map(hex => hex.startsWith('#') ? hex : `#${hex}`); |
| 13 | +} |
| 14 | + |
| 15 | +async function fetchPalette(name) { |
| 16 | + const url = `https://lospec.com/palette-list/${name}.hex`; |
| 17 | + const response = await fetch(url, { signal: AbortSignal.timeout(TIMEOUT_MS) }); |
| 18 | + if (!response.ok) throw new Error(`HTTP ${response.status}`); |
| 19 | + const text = await response.text(); |
| 20 | + return parseHexFile(text); |
| 21 | +} |
| 22 | + |
| 23 | +async function main() { |
| 24 | + mkdirSync(OUT_DIR, { recursive: true }); |
| 25 | + |
| 26 | + console.log(`Fetching ${PALETTES_LIST.length} palettes from Lospec...`); |
| 27 | + |
| 28 | + const entries = await Promise.all( |
| 29 | + PALETTES_LIST.map(async (name) => { |
| 30 | + try { |
| 31 | + const colors = await fetchPalette(name); |
| 32 | + console.log(` ${name}: ${colors.length} colors`); |
| 33 | + return [name, colors]; |
| 34 | + } catch (err) { |
| 35 | + console.error(` ${name}: FAILED - ${err.message}`); |
| 36 | + return null; |
| 37 | + } |
| 38 | + }) |
| 39 | + ); |
| 40 | + |
| 41 | + const results = Object.fromEntries(entries.filter(Boolean)); |
| 42 | + writeFileSync(`${OUT_DIR}/bundled.json`, JSON.stringify(results, null, 2)); |
| 43 | + |
| 44 | + const failed = PALETTES_LIST.length - Object.keys(results).length; |
| 45 | + console.log(`\nBundled ${Object.keys(results).length} palettes` + (failed ? ` (${failed} failed)` : '')); |
| 46 | +} |
| 47 | + |
| 48 | +main(); |
0 commit comments