@nitra/check-env 2.0.3 → 2.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -1
- package/package.json +1 -1
- package/src/index.js +27 -0
- package/.cspell.json +0 -18
- package/.prettierrc.cjs +0 -10
- package/test/checkenv.test.js +0 -71
package/README.md
CHANGED
|
@@ -7,7 +7,6 @@ and that you did not leave dangerous development overrides in production.
|
|
|
7
7
|
[](https://github.com/47ng/check-env/blob/next/LICENSE)
|
|
8
8
|
[](https://github.com/47ng/check-env/actions)
|
|
9
9
|
[](https://coveralls.io/github/47ng/check-env?branch=next)
|
|
10
|
-
[](https://dependabot.com)
|
|
11
10
|
|
|
12
11
|
## Installation
|
|
13
12
|
|
package/package.json
CHANGED
package/src/index.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const testEnv = name => !process.env[name]
|
|
2
|
+
|
|
3
|
+
const displayMissing = name => {
|
|
4
|
+
console.error(`❌ Missing required environment variable ${name}`)
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export class MissingEnvironmentVariableError extends Error {
|
|
8
|
+
constructor(envs) {
|
|
9
|
+
const joined = envs.join(', ')
|
|
10
|
+
super(`Some required environment variables are missing: ${joined}`)
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export default required => {
|
|
15
|
+
const missingReq = []
|
|
16
|
+
|
|
17
|
+
required.forEach(name => {
|
|
18
|
+
if (testEnv(name)) {
|
|
19
|
+
displayMissing(name)
|
|
20
|
+
missingReq.push(name)
|
|
21
|
+
}
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
if (missingReq.length > 0) {
|
|
25
|
+
throw new MissingEnvironmentVariableError(missingReq)
|
|
26
|
+
}
|
|
27
|
+
}
|
package/.cspell.json
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": "0.2",
|
|
3
|
-
"language": "en,uk,ru-ru,nitra",
|
|
4
|
-
"ignorePaths": [
|
|
5
|
-
"**/node_modules/**",
|
|
6
|
-
"**/vscode-extension/**",
|
|
7
|
-
"**/.git/**",
|
|
8
|
-
".vscode",
|
|
9
|
-
"report",
|
|
10
|
-
"yarn.lock"
|
|
11
|
-
],
|
|
12
|
-
"import": [
|
|
13
|
-
"@nitra/cspell-dict/cspell-ext.json",
|
|
14
|
-
"@cspell/dict-ru_ru/cspell-ext.json",
|
|
15
|
-
"@cspell/dict-uk-ua/cspell-ext.json"
|
|
16
|
-
],
|
|
17
|
-
"words": ["vitaliytv"]
|
|
18
|
-
}
|
package/.prettierrc.cjs
DELETED
package/test/checkenv.test.js
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
/* global it, expect, test */
|
|
2
|
-
import checkEnv from '../src/index.js'
|
|
3
|
-
import { jest } from '@jest/globals'
|
|
4
|
-
|
|
5
|
-
test('Empty options', () => {
|
|
6
|
-
console.error = jest.fn()
|
|
7
|
-
console.warn = jest.fn()
|
|
8
|
-
expect(() => checkEnv([])).not.toThrow()
|
|
9
|
-
expect(console.error).not.toHaveBeenCalled()
|
|
10
|
-
expect(console.warn).not.toHaveBeenCalled()
|
|
11
|
-
})
|
|
12
|
-
|
|
13
|
-
test('A single required env is missing', () => {
|
|
14
|
-
console.error = jest.fn()
|
|
15
|
-
console.warn = jest.fn()
|
|
16
|
-
expect(() => checkEnv(['foo'])).toThrowError()
|
|
17
|
-
expect(console.error).toHaveBeenCalledTimes(1)
|
|
18
|
-
expect(console.warn).not.toHaveBeenCalled()
|
|
19
|
-
})
|
|
20
|
-
|
|
21
|
-
test('All required envs are missing', () => {
|
|
22
|
-
const input = ['foo', 'bar']
|
|
23
|
-
console.error = jest.fn()
|
|
24
|
-
console.warn = jest.fn()
|
|
25
|
-
expect(() => checkEnv(input)).toThrowError()
|
|
26
|
-
expect(console.error).toHaveBeenCalledTimes(2)
|
|
27
|
-
expect(console.warn).not.toHaveBeenCalled()
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
test('Some required env are missing', () => {
|
|
31
|
-
const input = ['foo', 'NODE_ENV']
|
|
32
|
-
process.env['NODE_ENV'] = 'production'
|
|
33
|
-
|
|
34
|
-
console.error = jest.fn()
|
|
35
|
-
console.warn = jest.fn()
|
|
36
|
-
expect(() => checkEnv(input)).toThrowError()
|
|
37
|
-
expect(console.error).toHaveBeenCalledTimes(1)
|
|
38
|
-
expect(console.warn).not.toHaveBeenCalled()
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
test('A single required envs is available', () => {
|
|
42
|
-
const input = ['NODE_ENV']
|
|
43
|
-
process.env['NODE_ENV'] = 'production'
|
|
44
|
-
|
|
45
|
-
console.error = jest.fn()
|
|
46
|
-
console.warn = jest.fn()
|
|
47
|
-
checkEnv(input)
|
|
48
|
-
expect(console.error).not.toHaveBeenCalled()
|
|
49
|
-
expect(console.warn).not.toHaveBeenCalled()
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
test('Error message type', () => {
|
|
53
|
-
const input = ['foo']
|
|
54
|
-
|
|
55
|
-
expect(() => checkEnv(input)).toThrowError(
|
|
56
|
-
checkEnv.MissingEnvironmentVariableError
|
|
57
|
-
)
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
test('Error message contains name of missing variable', () => {
|
|
61
|
-
const input = ['foo']
|
|
62
|
-
|
|
63
|
-
try {
|
|
64
|
-
const run = () => checkEnv(input)
|
|
65
|
-
run()
|
|
66
|
-
expect(run).not.toHaveReturned() // Should throw
|
|
67
|
-
} catch (error) {
|
|
68
|
-
// Only missing required variables are shown in the message string
|
|
69
|
-
expect(error.message).toMatch('foo')
|
|
70
|
-
}
|
|
71
|
-
})
|