|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | + |
| 4 | +// Read initialData.ts |
| 5 | +const initialDataPath = path.resolve('src/data/initialData.ts'); |
| 6 | +const content = fs.readFileSync(initialDataPath, 'utf8'); |
| 7 | + |
| 8 | +// Helper to extract exported const JSON arrays/objects |
| 9 | +function extractConst(name) { |
| 10 | + const marker = `export const ${name}`; |
| 11 | + const startIdx = content.indexOf(marker); |
| 12 | + if (startIdx === -1) return null; |
| 13 | + const eqIdx = content.indexOf('=', startIdx); |
| 14 | + const openBracket = content.indexOf('[', eqIdx); |
| 15 | + const openBrace = content.indexOf('{', eqIdx); |
| 16 | + |
| 17 | + let isArray = false; |
| 18 | + let startCharIdx = -1; |
| 19 | + if (openBracket !== -1 && (openBrace === -1 || openBracket < openBrace)) { |
| 20 | + isArray = true; |
| 21 | + startCharIdx = openBracket; |
| 22 | + } else if (openBrace !== -1) { |
| 23 | + isArray = false; |
| 24 | + startCharIdx = openBrace; |
| 25 | + } |
| 26 | + |
| 27 | + // Find matching closing bracket/brace |
| 28 | + let depth = 0; |
| 29 | + let endCharIdx = -1; |
| 30 | + const openChar = isArray ? '[' : '{'; |
| 31 | + const closeChar = isArray ? ']' : '}'; |
| 32 | + |
| 33 | + for (let i = startCharIdx; i < content.length; i++) { |
| 34 | + if (content[i] === openChar) depth++; |
| 35 | + else if (content[i] === closeChar) { |
| 36 | + depth--; |
| 37 | + if (depth === 0) { |
| 38 | + endCharIdx = i; |
| 39 | + break; |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + const rawStr = content.slice(startCharIdx, endCharIdx + 1); |
| 45 | + return JSON.parse(rawStr); |
| 46 | +} |
| 47 | + |
| 48 | +const jacs = extractConst('SABANETA_JACS'); |
| 49 | +const athletes = extractConst('INITIAL_ATHLETES'); |
| 50 | +const registrations = extractConst('INITIAL_REGISTRATIONS'); |
| 51 | +const matches = []; |
| 52 | +const medals = extractConst('INITIAL_MEDALS'); |
| 53 | + |
| 54 | +const baseline = { |
| 55 | + jacs, |
| 56 | + athletes, |
| 57 | + registrations, |
| 58 | + matches, |
| 59 | + medals |
| 60 | +}; |
| 61 | + |
| 62 | +const outDir = path.resolve('server/data'); |
| 63 | +if (!fs.existsSync(outDir)) { |
| 64 | + fs.mkdirSync(outDir, { recursive: true }); |
| 65 | +} |
| 66 | + |
| 67 | +fs.writeFileSync(path.join(outDir, 'baseline.json'), JSON.stringify(baseline, null, 2), 'utf8'); |
| 68 | +console.log(`✅ Baseline exported to server/data/baseline.json with ${athletes.length} athletes, ${registrations.length} registrations, ${jacs.length} JACs.`); |
0 commit comments