Skip to content

Commit

Permalink
prefix numeric enums (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
patmood authored Aug 7, 2023
1 parent 5de56df commit f049dcd
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
19 changes: 14 additions & 5 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

// src/cli.ts
import "dotenv/config";
import dotenv from "dotenv";

// src/schema.ts
import FormData from "form-data";
Expand Down Expand Up @@ -202,12 +202,19 @@ function createSelectOptions(recordName, schema) {
const selectFields = schema.filter((field) => field.type === "select");
const typestring = selectFields.map(
(field) => `export enum ${getOptionEnumName(recordName, field.name)} {
${getOptionValues(field).map((val) => ` "${val}" = "${val}",`).join("\n")}
${getOptionValues(field).map((val) => ` "${getSelectOptionEnumName(val)}" = "${val}",`).join("\n")}
}
`
).join("\n");
return typestring;
}
function getSelectOptionEnumName(val) {
if (!isNaN(Number(val))) {
return `E${val}`;
} else {
return val;
}
}

// src/lib.ts
function generate(results) {
Expand Down Expand Up @@ -271,6 +278,8 @@ async function main(options2) {
} else if (options2.url) {
schema = await fromURL(options2.url, options2.email, options2.password);
} else if (options2.env) {
const path = typeof options2.env === "string" ? options2.env : ".env";
dotenv.config({ path });
if (!process.env.PB_TYPEGEN_URL || !process.env.PB_TYPEGEN_EMAIL || !process.env.PB_TYPEGEN_PASSWORD) {
return console.error(
"Missing environment variables. Check options: pocketbase-typegen --help"
Expand All @@ -295,7 +304,7 @@ async function main(options2) {
import { program } from "commander";

// package.json
var version = "1.1.11";
var version = "1.1.12";

// src/index.ts
program.name("Pocketbase Typegen").version(version).description(
Expand All @@ -317,8 +326,8 @@ program.name("Pocketbase Typegen").version(version).description(
"path to save the typescript output file",
"pocketbase-types.ts"
).option(
"-e, --env",
"flag to use environment variables for configuration, add PB_TYPEGEN_URL, PB_TYPEGEN_EMAIL, PB_TYPEGEN_PASSWORD to your .env file"
"-e, --env [path]",
"flag to use environment variables for configuration. Add PB_TYPEGEN_URL, PB_TYPEGEN_EMAIL, PB_TYPEGEN_PASSWORD to your .env file. Optionally provide a path to your .env file"
);
program.parse(process.argv);
var options = program.opts();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pocketbase-typegen",
"version": "1.1.11",
"version": "1.1.12",
"description": "Generate pocketbase record types from your database",
"main": "dist/index.js",
"bin": {
Expand Down
11 changes: 10 additions & 1 deletion src/fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,19 @@ export function createSelectOptions(
.map(
(field) => `export enum ${getOptionEnumName(recordName, field.name)} {
${getOptionValues(field)
.map((val) => `\t"${val}" = "${val}",`)
.map((val) => `\t"${getSelectOptionEnumName(val)}" = "${val}",`)
.join("\n")}
}\n`
)
.join("\n")
return typestring
}

export function getSelectOptionEnumName(val: string) {
if (!isNaN(Number(val))) {
// The value is a number, which cannot be used as an enum name
return `E${val}`
} else {
return val
}
}
19 changes: 18 additions & 1 deletion test/fields.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { createSelectOptions, createTypeField } from "../src/fields"
import {
createSelectOptions,
createTypeField,
getSelectOptionEnumName,
} from "../src/fields"

import { FieldSchema } from "../src/types"

Expand Down Expand Up @@ -265,3 +269,16 @@ describe("createSelectOptions", () => {
expect(result).toMatchSnapshot()
})
})

describe("getSelectOptionEnumName", () => {
it("uses the select option value as the enum name", () => {
expect(getSelectOptionEnumName("hello")).toEqual("hello")
expect(getSelectOptionEnumName("2022X")).toEqual("2022X")
})

it("prefixes the enum name when the value is a number", () => {
expect(getSelectOptionEnumName("2022")).toEqual("E2022")
expect(getSelectOptionEnumName("0")).toEqual("E0")
expect(getSelectOptionEnumName("0123")).toEqual("E0123")
})
})

0 comments on commit f049dcd

Please sign in to comment.