aboutsummaryrefslogtreecommitdiff
path: root/src/lib/dotenv.ts
blob: 2fb8e659ef10a4f0fa1f7b26e83d00a200d1ffb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//
// ~~~ read a .env file
//

// imports
import { readFileSync } from "node:fs"

// load .env function
export default function loadDotenv() {

    const file: string = ".env" // todo: don't hard-code the location

    try {

        const fileContents = readFileSync(file, "utf8")
            .split("\n")
            .filter((line) => line !== "")
            .map((line) => line.split("="))

        const fileEntries = new Map(fileContents)
        return Object.fromEntries(fileEntries)

    } catch(e) {

        console.error("!> cannot read .env file")

    }
}