Skip to content

Commit

Permalink
feat: add exitWithError
Browse files Browse the repository at this point in the history
  • Loading branch information
frytg committed Jan 22, 2025
1 parent 24f7214 commit 7e4ac5b
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# adapted from https://github.com/denoland/std/blob/main/.github/workflows/workspace_publish.yml

name: workspace publish
name: Publish to JSR

on:
release:
Expand All @@ -9,7 +9,7 @@ on:

jobs:
publish:
runs-on: ubuntu-latest
runs-on: ubuntu-24.04-arm
timeout-minutes: 30

permissions:
Expand Down
3 changes: 2 additions & 1 deletion check-required-env/deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -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]"
Expand Down
55 changes: 55 additions & 0 deletions check-required-env/exit-with-error.test.ts
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()
})
30 changes: 30 additions & 0 deletions check-required-env/exit-with-error.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 '@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)
}

0 comments on commit 7e4ac5b

Please sign in to comment.