From f5c2c266e3df9f5fda4521ba1079ceb798a531c8 Mon Sep 17 00:00:00 2001 From: Daniel Freytag Date: Fri, 22 Nov 2024 19:53:09 +0100 Subject: [PATCH] feat: add check required env --- README.md | 10 ++++++-- check-required-env/README.md | 21 +++++++++++++++++ check-required-env/check-required-env.ts | 30 ++++++++++++++++++++++++ check-required-env/deno.jsonc | 9 +++++++ logger/README.md | 4 ++++ 5 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 check-required-env/README.md create mode 100644 check-required-env/check-required-env.ts create mode 100644 check-required-env/deno.jsonc diff --git a/README.md b/README.md index f45a8dc..d71260b 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ -# js-utils +# Utility packages [![JSR @frytg](https://jsr.io/badges/@frytg)](https://jsr.io/@frytg) -[![Formatted with Biome](https://img.shields.io/badge/Formatted_with-Biome-60a5fa?style=flat&logo=biome)](https://biomejs.dev/) [![Linted with Biome](https://img.shields.io/badge/Linted_with-Biome-60a5fa?style=flat&logo=biome)](https://biomejs.dev) A collection of utilities for TypeScript and JavaScript. @@ -10,6 +9,9 @@ This repository is work in progress. ## Tools +- [`@frytg/check-required-env`](./check-required-env/README.md) - Check a required environment variable +- [`@frytg/logger`](./logger/README.md) - Winston logging wrapper + ## Lint Use `deno fmt`, `deno lint` and `biome lint` to check the code. @@ -18,6 +20,10 @@ Use `deno fmt`, `deno lint` and `biome lint` to check the code. deno task check ``` +## Author + +Created by [@frytg](https://github.com/frytg) / [frytg.digital](https://www.frytg.digital) + ## License [Unlicense](./LICENSE) - also see [unlicense.org](https://unlicense.org) diff --git a/check-required-env/README.md b/check-required-env/README.md new file mode 100644 index 0000000..840d6a1 --- /dev/null +++ b/check-required-env/README.md @@ -0,0 +1,21 @@ +# Check a required environment variable + +[![JSR @frytg/check-required-env](https://jsr.io/badges/@frytg/check-required-env)](https://jsr.io/@frytg/check-required-env) + +Simply check if a certain required environment variable is set. If not, throw an error and exit the process. + +```ts +import { checkRequiredEnv } from 'jsr:@frytg/check-required-env'; + +checkRequiredEnv('MY_IMPORTANT_ENV_VAR'); +``` + +It uses the logger from [`@frytg/logger`](https://jsr.io/@frytg/logger) to log the error and exit the process (with code 1). + +## Author + +Created by [@frytg](https://github.com/frytg) / [frytg.digital](https://www.frytg.digital) + +## License + +[Unlicense](../LICENSE) - also see [unlicense.org](https://unlicense.org) diff --git a/check-required-env/check-required-env.ts b/check-required-env/check-required-env.ts new file mode 100644 index 0000000..90ca110 --- /dev/null +++ b/check-required-env/check-required-env.ts @@ -0,0 +1,30 @@ +// load packages +import process from 'node:process' +import logger from 'jsr:@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 + * + * @example + * ```ts + * import { checkRequiredEnv } from 'jsr:@frytg/check-required-env'; + * + * checkRequiredEnv('MY_IMPORTANT_ENV_VAR'); + * ``` + */ +export const checkRequiredEnv = (name: string): void => { + // return if the env variable is set + if (process.env[name]) return + + // log and exit if not set + logger.log({ + level: 'alert', + message: `env ${name} is required`, + source: 'checkRequiredEnv', + data: { name }, + }) + process.exit(1) +} diff --git a/check-required-env/deno.jsonc b/check-required-env/deno.jsonc new file mode 100644 index 0000000..212b2f9 --- /dev/null +++ b/check-required-env/deno.jsonc @@ -0,0 +1,9 @@ +{ + "$schema": "https://jsr.io/schema/config-file.v1.json", + "name": "@frytg/check-required-env", + "version": "0.0.1", + "exports": "./check-required-env.ts", + "imports": { + "@frytg/logger": "jsr:@frytg/logger@0.0.1" + } +} diff --git a/logger/README.md b/logger/README.md index d24700c..e13d265 100644 --- a/logger/README.md +++ b/logger/README.md @@ -45,6 +45,10 @@ It is currently pre-configured with the } ``` +## Author + +Created by [@frytg](https://github.com/frytg) / [frytg.digital](https://www.frytg.digital) + ## License [Unlicense](../LICENSE) - also see [unlicense.org](https://unlicense.org)