Browse Source

Request read and env permissions if not already granted.

tags/0.1.1^0
Bauke 9 months ago
parent
commit
6333f861d5
Signed by: Bauke <me@bauke.xyz> GPG Key ID: C1C0F29952BCF558
1 changed files with 19 additions and 0 deletions
  1. +19
    -0
      minimal_dotenv.ts

+ 19
- 0
minimal_dotenv.ts View File

@@ -4,6 +4,25 @@
* @param filename An optional file name to use instead of `.env`.
*/
export default async function minimal_dotenv(filename = ".env"): Promise<void> {
// Request permission to read the .env file.
const readPermission = await Deno.permissions.request({
name: "read",
path: filename,
});
if (readPermission.state !== "granted") {
throw new Error(
"Read permission not granted (use --allow-read to skip the prompt)",
);
}

// Request permission to access the environment.
const envPermission = await Deno.permissions.request({ name: "env" });
if (envPermission.state !== "granted") {
throw new Error(
"Environment permission not granted (use --allow-env to skip the prompt)",
);
}

// Read and decode the .env file.
const decoder = new TextDecoder("utf8");
const contents = decoder.decode(await Deno.readFile(filename));