Skip to content

Commit

Permalink
generate typescript types
Browse files Browse the repository at this point in the history
  • Loading branch information
narthur committed Apr 8, 2024
1 parent 31c2d24 commit c4c4c31
Show file tree
Hide file tree
Showing 11 changed files with 442 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
README.md
dist/
dist/
.baserowrc
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
dist/
node_modules
scripts/
.baserowrc
.baserowrc
__generated__/
5 changes: 5 additions & 0 deletions baserow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env node

import main from "./dist/src/codegen.js";

await main();
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"version": "1.0.0",
"description": "",
"main": "dist/src/index.js",
"bin": {
"baserow": "./baserow.js"
},
"type": "module",
"packageManager": "[email protected]",
"files": [
Expand All @@ -13,6 +16,7 @@
"scripts": {
"test": "vitest",
"prepare": "tsc",
"build": "rm -rf ./dist && tsc",
"lint": "eslint .",
"format": "prettier --write .",
"checkTs": "tsc --noEmit"
Expand All @@ -23,9 +27,13 @@
"dependencies": {
"axios": "^1.6.7",
"json-schema-to-typescript": "^13.1.2",
"typescript": "^5.3.3"
"rc": "^1.2.8",
"typescript": "^5.3.3",
"zod": "^3.22.4"
},
"devDependencies": {
"@types/node": "^20.11.30",
"@types/rc": "^1.2.4",
"@typescript-eslint/eslint-plugin": "^7.1.0",
"@typescript-eslint/parser": "^7.1.0",
"eslint": "^8.57.0",
Expand Down
68 changes: 59 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions src/codegen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import rc from "rc";
import { BaserowSdk } from "./index.js";
import z from "zod";
import fs from "fs";
import makeType from "./codegen/makeType.js";

export default async function main(): Promise<void> {
const raw = rc("baserow");
const config = z
.object({
url: z.string(),
tables: z.record(z.string(), z.number()),
databaseToken: z.string(),
})
.parse(raw);

console.log("Hello from codegen.ts");
console.dir(config);

if (!config.databaseToken) {
throw new Error("Missing databaseToken in .baserowrc");
}

if (!fs.existsSync("./__generated__")) {
fs.mkdirSync("./__generated__");
}

const sdk = new BaserowSdk(String(config.databaseToken));

await Promise.all(
Object.entries(config.tables).map(async ([tableName, tableId]) => {
const fields = await sdk.listFields(tableId);
console.log(tableName);
console.log(tableId);
console.log(fields);

const typeDef = `export type ${tableName}Row = ${makeType(fields)}`;

fs.writeFileSync(`./__generated__/${tableName}.ts`, typeDef);
}),
);

// console.dir(tables);
}
Loading

0 comments on commit c4c4c31

Please sign in to comment.