Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"format:check": "prettier --check \"**/*.{ts,js,json}\"",
"codegen": "npm run codegen:types && npm run codegen:parsers",
"codegen:types": "bash ./src/gen/gen.sh && kysely --no-outdated-check migrate:up -e test && bash ./src/gen/table-gen.sh",
"codegen:parsers": "tsx src/grammar/index.ts",
"codegen:parsers": "tsx src/grammar/gen.ts",
"clean": "(rm ./src/gen/functions.ts; rm -r ./src/gen/types; rm ./src/gen/tables.json; rm ./src/gen/tables.ts) || true",
"prepublishOnly": "npm version $(npm pkg get version | tr -d '\"')-$(git rev-parse --short HEAD) --no-git-tag-version"
},
Expand Down
47 changes: 47 additions & 0 deletions src/grammar/gen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { grammars } from ".";

// Main function to generate parser files
function main() {
const fs = require("fs");
const path = require("path");
const { execSync } = require("child_process");

const outputDir = path.join(__dirname, "generated");

// Create output directory if it doesn't exist
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}

// Generate a file for each top-level clause
for (const [name, clause] of Object.entries(grammars)) {
const content = `// Generated parser for ${name.toUpperCase()} statement
import { grammars, ParsedClause } from '../index';
import * as Types from '../../types';
import invariant from 'tiny-invariant';

// Type imports for the parser function
type FromItem = any; // TODO: Import proper FromItem type
${clause === grammars.update ? `type MergeSelectArgs<_U, _F> = any; // TODO: Import proper MergeSelectArgs type` : ""}

${clause.generateParser()}
`;

const filePath = path.join(outputDir, `${name}.ts`);
fs.writeFileSync(filePath, content);
console.log(`Generated ${name}.ts`);
}

// Format generated files with prettier
console.log("Formatting generated files...");
try {
execSync(`npm run format -- ${outputDir}/*.ts`, { stdio: "inherit" });
} catch (error) {
console.error("Error formatting files:", error);
}
}

// Run if this file is executed directly
if (require.main === module) {
main();
}
Loading