@nitra/check-env 2.0.7 → 3.1.0

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/package.json CHANGED
@@ -1,15 +1,15 @@
1
1
  {
2
2
  "name": "@nitra/check-env",
3
- "version": "2.0.7",
3
+ "version": "3.1.0",
4
4
  "description": "Check that the critical environment variables are set",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
7
+ "types": "./types/index.d.ts",
7
8
  "exports": {
8
9
  ".": "./src/index.js"
9
10
  },
10
11
  "scripts": {
11
- "standard": "npx standard --fix",
12
- "fix": "npx standard --fix && npx prettier --write .",
12
+ "fix": "npx eslint --fix --ext .js . && npx prettier --write '**/*.js'",
13
13
  "test": "NODE_OPTIONS=--experimental-vm-modules npx jest"
14
14
  },
15
15
  "repository": {
@@ -21,11 +21,10 @@
21
21
  "email": "contact@francoisbest.com",
22
22
  "url": "https://francoisbest.com"
23
23
  },
24
- "devDependencies": {
25
- "@cspell/dict-ru_ru": "^2.0.2",
26
- "@cspell/dict-uk-ua": "^1.0.20",
27
- "@nitra/cspell-dict": "^1.0.8",
28
- "@nitra/prettier-config": "^1.0.0"
24
+ "eslintConfig": {
25
+ "extends": [
26
+ "@nitra"
27
+ ]
29
28
  },
30
29
  "license": "MIT",
31
30
  "bugs": {
@@ -38,6 +37,7 @@
38
37
  ],
39
38
  "prettier": "@nitra/prettier-config",
40
39
  "files": [
41
- "src"
40
+ "src",
41
+ "types"
42
42
  ]
43
43
  }
package/src/index.js CHANGED
@@ -1,9 +1,17 @@
1
+ import process from 'node:process'
2
+
1
3
  const testEnv = name => !process.env[name]
2
4
 
3
5
  const displayMissing = name => {
4
6
  console.error(`❌ Missing required environment variable ${name}`)
5
7
  }
6
8
 
9
+ /**
10
+ * MissingEnvironmentVariableError
11
+ *
12
+ * @class
13
+ * @extends Error
14
+ */
7
15
  export class MissingEnvironmentVariableError extends Error {
8
16
  constructor(envs) {
9
17
  const joined = envs.join(', ')
@@ -11,10 +19,21 @@ export class MissingEnvironmentVariableError extends Error {
11
19
  }
12
20
  }
13
21
 
14
- export default required => {
22
+ const checked = new Set()
23
+
24
+ /**
25
+ * Перевірка наявності змінних середовища
26
+ *
27
+ * @function
28
+ * @param {Array.<String>} required
29
+ */
30
+ export const checkEnv = required => {
15
31
  const missingReq = []
16
32
 
17
33
  required.forEach(name => {
34
+ // додаємо в список перевірених
35
+ checked.add(name)
36
+
18
37
  if (testEnv(name)) {
19
38
  displayMissing(name)
20
39
  missingReq.push(name)
@@ -25,3 +44,31 @@ export default required => {
25
44
  throw new MissingEnvironmentVariableError(missingReq)
26
45
  }
27
46
  }
47
+
48
+ /**
49
+ * Для сумісності з попередніми версіями
50
+ *
51
+ * @param {Array.<String>} required to capture
52
+ */
53
+ export default checkEnv
54
+
55
+ const envProxyHandler = {
56
+ /**
57
+ * Set colour of object
58
+ * @returns {String}
59
+ */
60
+ get(target, prop, _) {
61
+ // Якщо зі списку перевірених то повертаємо значення
62
+ // інакше аварійно завершуємо програму
63
+ if (!checked.has(prop)) {
64
+ throw new Error(`❌ Environment variable ${prop} is not checked`)
65
+ }
66
+
67
+ return target[prop]
68
+ }
69
+ }
70
+
71
+ /**
72
+ * @type {Object.<string, string>}
73
+ */
74
+ export const env = new Proxy(process.env, envProxyHandler)
@@ -0,0 +1,17 @@
1
+ /**
2
+ * MissingEnvironmentVariableError
3
+ *
4
+ * @class
5
+ * @extends Error
6
+ */
7
+ export class MissingEnvironmentVariableError extends Error {
8
+ constructor(envs: any);
9
+ }
10
+ export function checkEnv(required: Array<string>): void;
11
+ export default checkEnv;
12
+ /**
13
+ * @type {Object.<string, string>}
14
+ */
15
+ export const env: {
16
+ [x: string]: string;
17
+ };