#!/usr/bin/env node import * as path from 'path'; import { CsoundManualParser } from './parser'; import { TypeScriptGenerator } from './generator'; import { GitHubDownloader } from './downloader'; const DOWNLOAD_DIR = path.join(__dirname, 'downloaded-opcodes'); const OUTPUT_DIR = path.join(__dirname, '../../src/lib/csound-reference'); async function main() { const args = process.argv.slice(2); const shouldDownload = args.includes('--download') || args.includes('-d'); const inputDir = args.find(arg => arg.startsWith('--input='))?.split('=')[1] || DOWNLOAD_DIR; const outputDir = args.find(arg => arg.startsWith('--output='))?.split('=')[1] || OUTPUT_DIR; console.log('='.repeat(60)); console.log('Csound Manual Parser'); console.log('='.repeat(60)); if (shouldDownload) { console.log('\nStep 1: Downloading markdown files from GitHub...'); const downloader = new GitHubDownloader(DOWNLOAD_DIR); try { await downloader.downloadOpcodes(); await downloader.downloadCategories(); } catch (err) { console.error('Download failed:', err); console.log('\nYou can also manually clone the repository:'); console.log(' git clone https://github.com/csound/manual.git'); console.log(' Then run: npm run parse -- --input=manual/docs/opcodes'); process.exit(1); } } else { console.log(`\nUsing existing files from: ${inputDir}`); console.log('(Use --download or -d to download fresh files from GitHub)'); } console.log('\nStep 2: Parsing markdown files...'); const parser = new CsoundManualParser(); try { parser.parseDirectory(inputDir); } catch (err) { console.error('Parsing failed:', err); console.log('\nMake sure the input directory exists and contains .md files'); console.log(`Input directory: ${inputDir}`); process.exit(1); } const opcodeCount = parser.getOpcodes().length; console.log(`\nParsed ${opcodeCount} opcodes`); if (opcodeCount === 0) { console.error('No opcodes found. Check the input directory.'); process.exit(1); } console.log('\nStep 3: Grouping by category...'); const categoriesMap = parser.getReferencesByCategory(); console.log(`Found ${categoriesMap.size} categories`); const topCategories = Array.from(categoriesMap.entries()) .sort((a, b) => b[1].length - a[1].length) .slice(0, 10); console.log('\nTop 10 categories by opcode count:'); for (const [category, refs] of topCategories) { console.log(` - ${category}: ${refs.length} opcodes`); } console.log('\nStep 4: Generating TypeScript files...'); const generator = new TypeScriptGenerator(outputDir); generator.generateAll(categoriesMap); console.log('\n' + '='.repeat(60)); console.log('Success!'); console.log('='.repeat(60)); console.log(`\nGenerated files in: ${outputDir}`); console.log(`Total opcodes: ${opcodeCount}`); console.log(`Total categories: ${categoriesMap.size}`); console.log('\nYou can now import the reference in your application:'); console.log(` import { allCsoundReferences, getCsoundReference } from './lib/csound-reference/csoundReference'`); } if (require.main === module) { main().catch(err => { console.error('Fatal error:', err); process.exit(1); }); }