|
| 1 | +#!/usr/bin/env node |
| 2 | +// Syncs board definitions from wokwi-boards bundle.json |
| 3 | +// Run: node scripts/sync-boards.mjs |
| 4 | + |
| 5 | +import { existsSync, readFileSync, writeFileSync } from 'fs'; |
| 6 | +import { dirname, join } from 'path'; |
| 7 | +import { fileURLToPath } from 'url'; |
| 8 | + |
| 9 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 10 | +const partsDir = join(__dirname, '../src/registry/parts'); |
| 11 | +const BUNDLE_URL = 'https://wokwi.github.io/wokwi-boards/boards.json'; |
| 12 | + |
| 13 | +async function fetchBundle() { |
| 14 | + const response = await fetch(BUNDLE_URL); |
| 15 | + if (!response.ok) { |
| 16 | + throw new Error(`Failed to fetch bundle: ${response.status} ${response.statusText}`); |
| 17 | + } |
| 18 | + return response.json(); |
| 19 | +} |
| 20 | + |
| 21 | +function extractPins(boardDef) { |
| 22 | + const pins = Object.keys(boardDef.def.pins || {}); |
| 23 | + // Sort pins: numeric first (sorted numerically), then alphabetic |
| 24 | + return pins.sort((a, b) => { |
| 25 | + const aNum = parseFloat(a); |
| 26 | + const bNum = parseFloat(b); |
| 27 | + const aIsNum = !isNaN(aNum); |
| 28 | + const bIsNum = !isNaN(bNum); |
| 29 | + if (aIsNum && bIsNum) return aNum - bNum; |
| 30 | + if (aIsNum) return -1; |
| 31 | + if (bIsNum) return 1; |
| 32 | + return a.localeCompare(b); |
| 33 | + }); |
| 34 | +} |
| 35 | + |
| 36 | +function loadExistingPart(filePath) { |
| 37 | + if (!existsSync(filePath)) return null; |
| 38 | + return JSON.parse(readFileSync(filePath, 'utf-8')); |
| 39 | +} |
| 40 | + |
| 41 | +async function main() { |
| 42 | + console.log('Fetching wokwi-boards bundle...'); |
| 43 | + const bundle = await fetchBundle(); |
| 44 | + const boardIds = Object.keys(bundle); |
| 45 | + console.log(`Found ${boardIds.length} boards in bundle`); |
| 46 | + |
| 47 | + let created = 0; |
| 48 | + let updated = 0; |
| 49 | + let unchanged = 0; |
| 50 | + |
| 51 | + for (const boardId of boardIds) { |
| 52 | + const boardDef = bundle[boardId]; |
| 53 | + const partType = `board-${boardId}`; |
| 54 | + const filePath = join(partsDir, `${partType}.json`); |
| 55 | + const pins = extractPins(boardDef); |
| 56 | + |
| 57 | + const existing = loadExistingPart(filePath); |
| 58 | + |
| 59 | + const partDef = { |
| 60 | + type: partType, |
| 61 | + pins, |
| 62 | + documented: existing?.documented ?? false, |
| 63 | + isBoard: true, |
| 64 | + category: 'boards', |
| 65 | + }; |
| 66 | + |
| 67 | + if (existing) { |
| 68 | + // Check if pins changed |
| 69 | + const pinsChanged = JSON.stringify(existing.pins) !== JSON.stringify(pins); |
| 70 | + if (pinsChanged) { |
| 71 | + writeFileSync(filePath, JSON.stringify(partDef, null, 2) + '\n'); |
| 72 | + console.log(` Updated: ${partType} (${pins.length} pins)`); |
| 73 | + updated++; |
| 74 | + } else { |
| 75 | + unchanged++; |
| 76 | + } |
| 77 | + } else { |
| 78 | + writeFileSync(filePath, JSON.stringify(partDef, null, 2) + '\n'); |
| 79 | + console.log(` Created: ${partType} (${pins.length} pins)`); |
| 80 | + created++; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + console.log(`\nSummary: ${created} created, ${updated} updated, ${unchanged} unchanged`); |
| 85 | +} |
| 86 | + |
| 87 | +main().catch((err) => { |
| 88 | + console.error('Error:', err.message); |
| 89 | + process.exit(1); |
| 90 | +}); |
0 commit comments