Skip to content

Commit 4720bcd

Browse files
committed
Add functionality for reading env
1 parent 60d4da8 commit 4720bcd

5 files changed

Lines changed: 28 additions & 1 deletion

File tree

basics/package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

basics/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,8 @@
3232
"bugs": {
3333
"url": "https://github.com/jvalue/node-dry/issues"
3434
},
35-
"homepage": "https://github.com/jvalue/node-dry#readme"
35+
"homepage": "https://github.com/jvalue/node-dry#readme",
36+
"dependencies": {
37+
"@types/node": "^14.14.7"
38+
}
3639
}

basics/src/env.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { isEmpty } from './validators'
2+
3+
export function readEnvOrDie (envName: string): string {
4+
const env = process.env[envName]
5+
if (isEmpty(env)) {
6+
console.error(`Required environment variable ${envName} is not defined or empty`)
7+
console.error('Unable to proceed with service')
8+
process.exit(-2)
9+
}
10+
11+
console.info(`[Environment Variable] ${envName} = ${env}`)
12+
return env
13+
}

basics/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { sleep } from './sleep'
22
import * as validators from './validators'
33
import * as stringifiers from './stringifiers'
4+
import { readEnvOrDie } from './env'
45

56
export {
67
sleep,
78
validators,
9+
readEnvOrDie,
810
stringifiers
911
}

basics/src/validators.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ export function isString (x: unknown): x is string {
66
return typeof x === 'string'
77
}
88

9+
export function isEmpty (value: string | undefined): value is undefined {
10+
return value === undefined || value === ''
11+
}
12+
913
export function isObject (x: unknown): x is object {
1014
return typeof x === 'object'
1115
}

0 commit comments

Comments
 (0)