Skip to content

Commit

Permalink
add dev option to codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
narthur committed Apr 12, 2024
1 parent 4b2c3c8 commit a7675bf
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"env": {
"browser": true,
"es2021": true
"es2021": true,
"node": true
},
"extends": [
"eslint:recommended",
Expand Down
4 changes: 3 additions & 1 deletion baserow.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

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

await main();
const isDev = process.argv.includes("-d");

await main({ isDev });
25 changes: 19 additions & 6 deletions src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ export type Table = { id: number; name: string; fields: ListFieldsResponse };

const __dirname = path.dirname(new URL(import.meta.url).pathname);

export default async function main(): Promise<void> {
export default async function main({
isDev = false,
}: {
isDev: boolean;
}): Promise<void> {
const config = getConfig();

if (
Expand Down Expand Up @@ -55,12 +59,13 @@ export default async function main(): Promise<void> {
)
.filter((t) => !!t);

//TODO: this may not be correct for all generated files
const modelImports = isDev
? `import { BaserowSdk, Row } from '${__dirname}/index.js'`
: "import { BaserowSdk, Row } from 'baserow-sdk'";
const typeDef = `export type ${tableName}RowType = ${makeType(fields)}
import { Row } from "${__dirname}/row.js";
${modelImports}
import { Repository } from "./Repository.js";
import { BaserowSdk } from "${__dirname}/index.js";
${foreignTables
.map((t) => {
return `import { ${t?.name}Row } from "./${t?.name}.js";`;
Expand All @@ -85,8 +90,16 @@ ${makeModelMethods(table.id, tables)}
fs.writeFileSync(`${outDir}/${tableName}.ts`, typeDef);
});

const factoryCode = `import { Factory } from '${__dirname}/factory.js'
import { ListRowsOptions, GetRowOptions } from '${__dirname}/index.js'
const factoryImport = isDev
? `import { Factory } from '${__dirname}/factory.js'`
: "import { Factory } from 'baserow-sdk'";

const indexImports = isDev
? `import { ListRowsOptions, GetRowOptions } from '${__dirname}/index.js'`
: `import { ListRowsOptions, GetRowOptions } from 'baserow-sdk'`;

const factoryCode = `${factoryImport}
${indexImports}
${Object.keys(config.tables)
.map(
(tableName) =>
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import c from "./client.js";

export * from "./row.js";

export type AddRowOptions = {
user_field_names?: boolean;
before?: number;
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
"skipLibCheck": true,
"types": ["node"]
},
"exclude": [
"dist",
Expand Down

0 comments on commit a7675bf

Please sign in to comment.