diff --git a/dist/index.js b/dist/index.js index 149045a..378a969 100755 --- a/dist/index.js +++ b/dist/index.js @@ -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"; @@ -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) { @@ -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" @@ -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( @@ -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(); diff --git a/package.json b/package.json index 7fffe2d..fc2b825 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/fields.ts b/src/fields.ts index ee96d88..3214910 100644 --- a/src/fields.ts +++ b/src/fields.ts @@ -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 + } +} diff --git a/test/fields.test.ts b/test/fields.test.ts index 73b628c..951289f 100644 --- a/test/fields.test.ts +++ b/test/fields.test.ts @@ -1,4 +1,8 @@ -import { createSelectOptions, createTypeField } from "../src/fields" +import { + createSelectOptions, + createTypeField, + getSelectOptionEnumName, +} from "../src/fields" import { FieldSchema } from "../src/types" @@ -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") + }) +})