-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
72 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters