|
| 1 | +import { grammars } from "."; |
| 2 | + |
| 3 | +// Main function to generate parser files |
| 4 | +function main() { |
| 5 | + const fs = require("fs"); |
| 6 | + const path = require("path"); |
| 7 | + const { execSync } = require("child_process"); |
| 8 | + |
| 9 | + const outputDir = path.join(__dirname, "generated"); |
| 10 | + |
| 11 | + // Create output directory if it doesn't exist |
| 12 | + if (!fs.existsSync(outputDir)) { |
| 13 | + fs.mkdirSync(outputDir, { recursive: true }); |
| 14 | + } |
| 15 | + |
| 16 | + // Generate a file for each top-level clause |
| 17 | + for (const [name, clause] of Object.entries(grammars)) { |
| 18 | + const content = `// Generated parser for ${name.toUpperCase()} statement |
| 19 | +import { grammars, ParsedClause } from '../index'; |
| 20 | +import * as Types from '../../types'; |
| 21 | +import invariant from 'tiny-invariant'; |
| 22 | +
|
| 23 | +// Type imports for the parser function |
| 24 | +type FromItem = any; // TODO: Import proper FromItem type |
| 25 | +${clause === grammars.update ? `type MergeSelectArgs<_U, _F> = any; // TODO: Import proper MergeSelectArgs type` : ""} |
| 26 | +
|
| 27 | +${clause.generateParser()} |
| 28 | +`; |
| 29 | + |
| 30 | + const filePath = path.join(outputDir, `${name}.ts`); |
| 31 | + fs.writeFileSync(filePath, content); |
| 32 | + console.log(`Generated ${name}.ts`); |
| 33 | + } |
| 34 | + |
| 35 | + // Format generated files with prettier |
| 36 | + console.log("Formatting generated files..."); |
| 37 | + try { |
| 38 | + execSync(`npm run format -- ${outputDir}/*.ts`, { stdio: "inherit" }); |
| 39 | + } catch (error) { |
| 40 | + console.error("Error formatting files:", error); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// Run if this file is executed directly |
| 45 | +if (require.main === module) { |
| 46 | + main(); |
| 47 | +} |
0 commit comments