Skip to content

Commit

Permalink
Make --env accept custom path to .env file (#72)
Browse files Browse the repository at this point in the history
* Make --env accept path to custom .env file

* use const

* not override

* Update src/index.ts

Co-authored-by: Patrick Moody <4031292+patmood@users.noreply.github.com>

---------

Co-authored-by: Patrick Moody <4031292+patmood@users.noreply.github.com>
  • Loading branch information
duytbp and patmood authored Jul 20, 2023
1 parent 669a634 commit 5de56df
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'dotenv/config'
import dotenv from "dotenv"

import type { CollectionRecord, Options } from "./types"
import { fromDatabase, fromJSON, fromURL } from "./schema"
Expand All @@ -16,6 +16,10 @@ export async function main(options: Options) {
} else if (options.url) {
schema = await fromURL(options.url, options.email, options.password)
} else if (options.env) {
const path: string = typeof options.env === "string"
? options.env
: ".env"
dotenv.config({ path: 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 Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ program
"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)
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export type Options = {
json?: string
email?: string
password?: string
env?: boolean
env?: boolean | string
}

export type FieldSchema = {
Expand Down

0 comments on commit 5de56df

Please sign in to comment.