@trenskow/config 3.1.2 → 3.1.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/index.js +7 -40
- package/package.json +2 -1
- package/test/index.js +7 -1
package/index.js
CHANGED
|
@@ -6,15 +6,13 @@
|
|
|
6
6
|
// See license in LICENSE
|
|
7
7
|
//
|
|
8
8
|
|
|
9
|
-
import { readFileSync } from 'fs';
|
|
10
|
-
import { resolve, dirname } from 'path';
|
|
11
|
-
|
|
12
9
|
import caseit from '@trenskow/caseit';
|
|
13
10
|
import merge from '@trenskow/merge';
|
|
14
11
|
import isvalid, { formalize, keyPaths } from 'isvalid';
|
|
15
12
|
import keyd from 'keyd';
|
|
13
|
+
import dotenv from '@trenskow/dotenv';
|
|
16
14
|
|
|
17
|
-
|
|
15
|
+
dotenv();
|
|
18
16
|
|
|
19
17
|
const fillArray = (value, length) => {
|
|
20
18
|
let result = [];
|
|
@@ -80,46 +78,16 @@ const cleanIt = (obj, expanded = [], keyPath = []) => {
|
|
|
80
78
|
|
|
81
79
|
};
|
|
82
80
|
|
|
83
|
-
const camelCased = (schema
|
|
81
|
+
const camelCased = (schema) => {
|
|
84
82
|
const result = {};
|
|
85
|
-
Object.keys(env)
|
|
86
|
-
.filter((key) => env[key])
|
|
83
|
+
Object.keys(process.env)
|
|
84
|
+
.filter((key) => process.env[key])
|
|
87
85
|
.forEach((key) => {
|
|
88
|
-
keyd(result).set(`${caseit(key.toLowerCase(), 'domain')}.$`, env[key]);
|
|
86
|
+
keyd(result).set(`${caseit(key.toLowerCase(), 'domain')}.$`, process.env[key]);
|
|
89
87
|
});
|
|
90
88
|
return cleanIt(result, keyPaths(schema).all(Object).filter((keyPath) => keyPath));
|
|
91
89
|
};
|
|
92
90
|
|
|
93
|
-
const readEnvironment = (directory = __dirname) => {
|
|
94
|
-
|
|
95
|
-
const env = process.env.NODE_ENV || 'development';
|
|
96
|
-
|
|
97
|
-
const paths = [
|
|
98
|
-
resolve(directory, '.env'),
|
|
99
|
-
resolve(directory, `.env.${env}`)
|
|
100
|
-
];
|
|
101
|
-
|
|
102
|
-
let result = {};
|
|
103
|
-
|
|
104
|
-
paths.forEach((path) => {
|
|
105
|
-
try {
|
|
106
|
-
const data = readFileSync(path, 'utf8');
|
|
107
|
-
data
|
|
108
|
-
.split('\n')
|
|
109
|
-
.map((line) => line.trim())
|
|
110
|
-
.filter((line) => line && line[0] !== '#')
|
|
111
|
-
.forEach((line) => {
|
|
112
|
-
const [key, value] = line.split('=');
|
|
113
|
-
result[key] = value.trim().replace(/(^['"]|['"]$)/g, '');
|
|
114
|
-
});
|
|
115
|
-
} catch (_) { }
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
if (directory !== '/') result = merge(result, readEnvironment(resolve(directory, '..')));
|
|
119
|
-
|
|
120
|
-
return result;
|
|
121
|
-
|
|
122
|
-
};
|
|
123
91
|
|
|
124
92
|
// Give it back.
|
|
125
93
|
export default async (schema = {}, options = {}) => {
|
|
@@ -135,8 +103,7 @@ export default async (schema = {}, options = {}) => {
|
|
|
135
103
|
|
|
136
104
|
try {
|
|
137
105
|
return await isvalid(
|
|
138
|
-
camelCased(schema,
|
|
139
|
-
dirname(resolve(process.cwd(), process.argv[1])) || __dirname))),
|
|
106
|
+
camelCased(schema),
|
|
140
107
|
schema,
|
|
141
108
|
options);
|
|
142
109
|
} catch (error) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trenskow/config",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.4",
|
|
4
4
|
"description": "Converts `process.env` into a neatly packed object.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
"homepage": "https://github.com/trenskow/config#readme",
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"@trenskow/caseit": "^1.4.4",
|
|
30
|
+
"@trenskow/dotenv": "^0.1.0",
|
|
30
31
|
"@trenskow/merge": "^0.1.30",
|
|
31
32
|
"isvalid": "^4.1.20",
|
|
32
33
|
"keyd": "^2.1.12"
|