Skip to content

Commit

Permalink
feat: add check required env
Browse files Browse the repository at this point in the history
  • Loading branch information
frytg committed Nov 22, 2024
1 parent 42e06ca commit f5c2c26
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 2 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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.
Expand All @@ -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)
21 changes: 21 additions & 0 deletions check-required-env/README.md
Original file line number Diff line number Diff line change
@@ -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)
30 changes: 30 additions & 0 deletions check-required-env/check-required-env.ts
Original file line number Diff line number Diff line change
@@ -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)
}
9 changes: 9 additions & 0 deletions check-required-env/deno.jsonc
Original file line number Diff line number Diff line change
@@ -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/[email protected]"
}
}
4 changes: 4 additions & 0 deletions logger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit f5c2c26

Please sign in to comment.