|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | + |
| 4 | +const testFilePath = './packages/deparser/__tests__/kitchen-sink.test.ts'; |
| 5 | +const fixturesDir = './__fixtures__'; |
| 6 | +const outputFile = './generated.json'; |
| 7 | + |
| 8 | +function parseTestFile() { |
| 9 | + const content = fs.readFileSync(testFilePath, 'utf8'); |
| 10 | + const lines = content.split('\n'); |
| 11 | + |
| 12 | + const fixtureFiles = []; |
| 13 | + |
| 14 | + for (let i = 0; i < lines.length; i++) { |
| 15 | + const line = lines[i].trim(); |
| 16 | + |
| 17 | + if (line.startsWith('//') || line.startsWith('/*')) { |
| 18 | + continue; |
| 19 | + } |
| 20 | + |
| 21 | + if (line.includes('xit(')) { |
| 22 | + continue; |
| 23 | + } |
| 24 | + |
| 25 | + if (line.includes('it(') && line.includes('check(')) { |
| 26 | + const checkMatch = line.match(/check\(['"`]([^'"`]+)['"`]\)/); |
| 27 | + if (checkMatch) { |
| 28 | + fixtureFiles.push(checkMatch[1]); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + if (line.includes('it(') && i + 1 < lines.length) { |
| 33 | + const nextLine = lines[i + 1].trim(); |
| 34 | + if (nextLine.includes('check(') && !nextLine.includes('xit(')) { |
| 35 | + const checkMatch = nextLine.match(/check\(['"`]([^'"`]+)['"`]\)/); |
| 36 | + if (checkMatch) { |
| 37 | + fixtureFiles.push(checkMatch[1]); |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + return [...new Set(fixtureFiles)]; // Remove duplicates |
| 44 | +} |
| 45 | + |
| 46 | +function readFixtureFiles(fixtureFiles) { |
| 47 | + const result = {}; |
| 48 | + const missingFiles = []; |
| 49 | + |
| 50 | + for (const fixturePath of fixtureFiles) { |
| 51 | + const fullPath = path.join(fixturesDir, fixturePath); |
| 52 | + |
| 53 | + try { |
| 54 | + if (fs.existsSync(fullPath)) { |
| 55 | + const content = fs.readFileSync(fullPath, 'utf8'); |
| 56 | + result[fixturePath] = content; |
| 57 | + } else { |
| 58 | + missingFiles.push(fixturePath); |
| 59 | + } |
| 60 | + } catch (error) { |
| 61 | + console.error(`Error reading ${fullPath}:`, error.message); |
| 62 | + missingFiles.push(fixturePath); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + if (missingFiles.length > 0) { |
| 67 | + console.warn('Missing fixture files:', missingFiles); |
| 68 | + } |
| 69 | + |
| 70 | + return result; |
| 71 | +} |
| 72 | + |
| 73 | +function main() { |
| 74 | + console.log('Parsing test file...'); |
| 75 | + const fixtureFiles = parseTestFile(); |
| 76 | + console.log(`Found ${fixtureFiles.length} fixture files from passing tests`); |
| 77 | + |
| 78 | + console.log('Reading fixture file contents...'); |
| 79 | + const fixtureContents = readFixtureFiles(fixtureFiles); |
| 80 | + console.log(`Successfully read ${Object.keys(fixtureContents).length} fixture files`); |
| 81 | + |
| 82 | + console.log('Writing generated.json...'); |
| 83 | + fs.writeFileSync(outputFile, JSON.stringify(fixtureContents, null, 2)); |
| 84 | + console.log(`Generated ${outputFile} with ${Object.keys(fixtureContents).length} entries`); |
| 85 | + |
| 86 | + console.log('\nSample entries:'); |
| 87 | + const entries = Object.entries(fixtureContents); |
| 88 | + for (let i = 0; i < Math.min(5, entries.length); i++) { |
| 89 | + const [path, content] = entries[i]; |
| 90 | + console.log(`${path}: ${content.length} characters`); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +if (require.main === module) { |
| 95 | + main(); |
| 96 | +} |
0 commit comments