-
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
89 additions
and
3 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 |
---|---|---|
|
@@ -4,7 +4,8 @@ | |
"version": "0.1.0", | ||
"exports": { | ||
".": "./check-required-env.ts", | ||
"./get": "./get-required-env.ts" | ||
"./get": "./get-required-env.ts", | ||
"./exit": "./exit-with-error.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,55 @@ | ||
import process from 'node:process' | ||
import { test } from '@cross/test' | ||
import sinon from 'sinon' | ||
|
||
import { exitWithError } from './exit-with-error.ts' | ||
|
||
test('exitWithError - returns when variable exists', () => { | ||
// Test | ||
exitWithError('existing-var') | ||
}) | ||
|
||
test('exitWithError - exits when input is null', () => { | ||
// Setup | ||
const exitStub = sinon.stub(process, 'exit') | ||
|
||
// Test | ||
exitWithError(null) | ||
|
||
// Verify | ||
sinon.assert.calledOnce(exitStub) | ||
sinon.assert.calledWith(exitStub, 1) | ||
|
||
// Cleanup | ||
exitStub.restore() | ||
}) | ||
|
||
test('exitWithError - exits when input is undefined', () => { | ||
// Setup | ||
const exitStub = sinon.stub(process, 'exit') | ||
|
||
// Test | ||
exitWithError(undefined) | ||
|
||
// Verify | ||
sinon.assert.calledOnce(exitStub) | ||
sinon.assert.calledWith(exitStub, 1) | ||
|
||
// Cleanup | ||
exitStub.restore() | ||
}) | ||
|
||
test('exitWithError - exits when input is empty string', () => { | ||
// Setup | ||
const exitStub = sinon.stub(process, 'exit') | ||
|
||
// Test | ||
exitWithError('') | ||
|
||
// Verify | ||
sinon.assert.calledOnce(exitStub) | ||
sinon.assert.calledWith(exitStub, 1) | ||
|
||
// Cleanup | ||
exitStub.restore() | ||
}) |
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 '@frytg/logger' | ||
|
||
/** | ||
* Check if an variable is set and log an alert and exit if it is not set. | ||
* | ||
* @param {string | string[] | object | null | undefined} name - The name of the variable. | ||
* @returns {void} | ||
* | ||
* @example | ||
* ```ts | ||
* import { exitWithError } from '@frytg/check-required-env/exit' | ||
* | ||
* exitWithError('MY_IMPORTANT_ENV_VAR') | ||
* ``` | ||
*/ | ||
export const exitWithError = (input: string | string[] | object | null | undefined): void => { | ||
// return if the variable is set and not empty | ||
if (input && input !== '') return | ||
|
||
// log and exit if not set | ||
logger.log({ | ||
level: 'alert', | ||
message: `${input} is required`, | ||
source: 'exitWithError', | ||
data: { input }, | ||
}) | ||
process.exit(1) | ||
} |