|
|
@@ -3,16 +3,16 @@ |
|
|
|
* environment. |
|
|
|
* @param filename An optional file name to use instead of `.env`. |
|
|
|
*/ |
|
|
|
export default async function minimal_dotenv(filename = '.env'): Promise<void> { |
|
|
|
export default async function minimal_dotenv(filename = ".env"): Promise<void> { |
|
|
|
// Read and decode the .env file. |
|
|
|
const decoder = new TextDecoder('utf8'); |
|
|
|
const decoder = new TextDecoder("utf8"); |
|
|
|
const contents = decoder.decode(await Deno.readFile(filename)); |
|
|
|
|
|
|
|
// Split the contents by newline and only include non-zero length strings and |
|
|
|
// lines not starting with a #. |
|
|
|
const lines = contents |
|
|
|
.split(/[\r\n]+/) |
|
|
|
.filter((line) => line.length > 0 && !line.startsWith('#')); |
|
|
|
.filter((line) => line.length > 0 && !line.startsWith("#")); |
|
|
|
|
|
|
|
for (const line of lines) { |
|
|
|
const matches = /^(?<key>[A-Z0-9_]+)="(?<value>.+)"$/.exec(line); |
|
|
@@ -22,7 +22,7 @@ export default async function minimal_dotenv(filename = '.env'): Promise<void> { |
|
|
|
|
|
|
|
// The `?? {}` is to make the compiler happy, if the regex matches then we |
|
|
|
// know the capture groups will too. |
|
|
|
const {key, value} = matches.groups ?? {}; |
|
|
|
const { key, value } = matches.groups ?? {}; |
|
|
|
Deno.env.set(key, value); |
|
|
|
} |
|
|
|
} |