Files
gridpilot.gg/scripts/extract-selector-snippets.ts
2025-11-26 17:03:29 +01:00

120 lines
3.6 KiB
TypeScript

import * as fs from 'fs';
import * as path from 'path';
/**
* Extract relevant HTML snippets from large iRacing HTML files for selector verification.
* Focuses on Steps 8-12 (Cars and Track sections).
*/
const FILES_TO_EXTRACT = [
'08-set-cars.html',
'09-add-a-car.html',
'11-set-track.html',
'12-add-a-track.html'
];
const PATTERNS_TO_FIND = [
// Step 8: Add Car button patterns
/id="set-cars"[\s\S]{0,5000}/i,
/<a[^>]*btn[^>]*icon-plus[\s\S]{0,500}<\/a>/gi,
/<button[^>]*>Add[\s\S]{0,200}<\/button>/gi,
// Step 9: Add Car modal patterns
/id="add-car-modal"[\s\S]{0,5000}/i,
/<div[^>]*modal[\s\S]{0,3000}Car[\s\S]{0,3000}<\/div>/gi,
/placeholder="Search"[\s\S]{0,500}/gi,
/<a[^>]*btn-primary[^>]*>Select[\s\S]{0,200}<\/a>/gi,
// Step 11: Add Track button patterns
/id="set-track"[\s\S]{0,5000}/i,
/<a[^>]*btn[^>]*icon-plus[\s\S]{0,500}Track[\s\S]{0,500}<\/a>/gi,
// Step 12: Add Track modal patterns
/id="add-track-modal"[\s\S]{0,5000}/i,
/<div[^>]*modal[\s\S]{0,3000}Track[\s\S]{0,3000}<\/div>/gi,
];
interface ExtractedSnippet {
file: string;
pattern: string;
snippet: string;
lineNumber?: number;
}
async function extractSnippets(): Promise<void> {
const sourceDir = path.join(process.cwd(), 'resources/iracing-hosted-sessions');
const outputDir = path.join(process.cwd(), 'debug-screenshots');
// Ensure output directory exists
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
const allSnippets: ExtractedSnippet[] = [];
for (const fileName of FILES_TO_EXTRACT) {
const filePath = path.join(sourceDir, fileName);
console.log(`Processing ${fileName}...`);
// Read file in chunks to avoid memory issues
const content = fs.readFileSync(filePath, 'utf-8');
const fileSize = content.length;
console.log(` File size: ${(fileSize / 1024 / 1024).toFixed(2)} MB`);
// Extract snippets for each pattern
for (const pattern of PATTERNS_TO_FIND) {
const matches = content.match(pattern);
if (matches) {
for (const match of matches) {
const lineNumber = content.substring(0, content.indexOf(match)).split('\n').length;
allSnippets.push({
file: fileName,
pattern: pattern.source,
snippet: match.substring(0, 1000), // Limit snippet size
lineNumber
});
}
}
}
console.log(` Found ${allSnippets.filter(s => s.file === fileName).length} snippets`);
}
// Write results to file
const outputPath = path.join(outputDir, 'selector-snippets-extraction.json');
fs.writeFileSync(
outputPath,
JSON.stringify(allSnippets, null, 2),
'utf-8'
);
console.log(`\nExtracted ${allSnippets.length} total snippets to ${outputPath}`);
// Also create a readable report
const reportPath = path.join(outputDir, 'selector-snippets-report.md');
let report = '# Selector Snippets Extraction Report\n\n';
for (const file of FILES_TO_EXTRACT) {
const fileSnippets = allSnippets.filter(s => s.file === file);
report += `## ${file}\n\n`;
report += `Found ${fileSnippets.length} snippets\n\n`;
for (const snippet of fileSnippets) {
report += `### Pattern: \`${snippet.pattern.substring(0, 50)}...\`\n\n`;
report += `Line ${snippet.lineNumber}\n\n`;
report += '```html\n';
report += snippet.snippet;
report += '\n```\n\n';
}
}
fs.writeFileSync(reportPath, report, 'utf-8');
console.log(`Readable report written to ${reportPath}`);
}
extractSnippets().catch(console.error);