From 35da008ebbcbbacab583bc328dc70f0ab1f2ea2c Mon Sep 17 00:00:00 2001 From: Daniel Freytag Date: Tue, 17 Dec 2024 21:43:18 +0100 Subject: [PATCH] feat: add `getRequiredEnv` --- check-required-env/CHANGELOG.md | 13 +++++++++ check-required-env/check-required-env.ts | 4 +-- check-required-env/deno.jsonc | 7 +++-- check-required-env/get-required-env.ts | 34 ++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 check-required-env/CHANGELOG.md create mode 100644 check-required-env/get-required-env.ts diff --git a/check-required-env/CHANGELOG.md b/check-required-env/CHANGELOG.md new file mode 100644 index 0000000..2970573 --- /dev/null +++ b/check-required-env/CHANGELOG.md @@ -0,0 +1,13 @@ +# Check Required Env Changelog + +## 2024-12-17 - 0.1.0 + +- feat: add `getRequiredEnv` + +## 2024-11-27 - 0.0.1 + +- feat: add test for required env + +## 2024-11-22 - 0.0.1 + +- feat: add check required env diff --git a/check-required-env/check-required-env.ts b/check-required-env/check-required-env.ts index b09f5ce..bdfb5e0 100644 --- a/check-required-env/check-required-env.ts +++ b/check-required-env/check-required-env.ts @@ -5,8 +5,8 @@ import logger from '@frytg/logger' /** * Check if an environment variable is required and log an alert and exit if it is not set. * - * @param name - The name of the environment variable. - * @returns void + * @param {string} name - The name of the environment variable. + * @returns {void} * * @example * ```ts diff --git a/check-required-env/deno.jsonc b/check-required-env/deno.jsonc index 695a889..0ee7324 100644 --- a/check-required-env/deno.jsonc +++ b/check-required-env/deno.jsonc @@ -1,8 +1,11 @@ { "$schema": "https://jsr.io/schema/config-file.v1.json", "name": "@frytg/check-required-env", - "version": "0.0.1", - "exports": "./check-required-env.ts", + "version": "0.1.0", + "exports": { + ".": "./check-required-env.ts", + "./get": "./get-required-env.ts" + }, "imports": { "@frytg/logger": "jsr:@frytg/logger@0.0.2" } diff --git a/check-required-env/get-required-env.ts b/check-required-env/get-required-env.ts new file mode 100644 index 0000000..1199a48 --- /dev/null +++ b/check-required-env/get-required-env.ts @@ -0,0 +1,34 @@ +// load packages +import process from 'node:process' +import logger from '@frytg/logger' + +/** + * Access an environment variable and log an alert and exit if it is not set. + * + * @param {string} name - The name of the environment variable. + * @param {boolean} preferThrowError - Whether to throw an error if the environment variable is not set. + * @returns {string} The value of the environment variable. + * + * @example + * ```ts + * import { getRequiredEnv } from '@frytg/check-required-env/get' + * + * getRequiredEnv('MY_IMPORTANT_ENV_VAR', false) + * ``` + */ +export const getRequiredEnv = (name: string, preferThrowError = true): string => { + const value = process.env[name] + + // return if the env variable is set + if (value !== undefined) return value + + // log and exit if not set + logger.log({ + level: 'alert', + message: `env ${name} is required`, + source: 'getRequiredEnv', + data: { name }, + }) + if (preferThrowError) throw new Error(`env ${name} is required`) + process.exit(1) +}