-
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
4 changed files
with
54 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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 |
---|---|---|
@@ -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/[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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |