Skip to content

Commit 5dc5ccd

Browse files
committed
refactor: split up src/grammar/index.ts
1 parent 7463e66 commit 5dc5ccd

18 files changed

Lines changed: 1162 additions & 1037 deletions

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"format:check": "prettier --check \"**/*.{ts,js,json}\"",
1616
"codegen": "npm run codegen:types && npm run codegen:parsers",
1717
"codegen:types": "bash ./src/gen/gen.sh && kysely --no-outdated-check migrate:up -e test && bash ./src/gen/table-gen.sh",
18-
"codegen:parsers": "tsx src/grammar/index.ts",
18+
"codegen:parsers": "tsx src/grammar/gen.ts",
1919
"clean": "(rm ./src/gen/functions.ts; rm -r ./src/gen/types; rm ./src/gen/tables.json; rm ./src/gen/tables.ts) || true",
2020
"prepublishOnly": "npm version $(npm pkg get version | tr -d '\"')-$(git rev-parse --short HEAD) --no-git-tag-version"
2121
},

src/grammar/gen.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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

Comments
 (0)