/
index.ts
65 lines (52 loc) · 1.69 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { Application } from '@feathersjs/feathers';
import Debug from 'debug';
import path from 'path';
import config from 'config';
const debug = Debug('@feathersjs/configuration');
const separator = path.sep;
export default function init () {
return (app: Application|undefined) => {
const convert = (current: any) => {
const result: { [key: string]: any } = Array.isArray(current) ? [] : {};
Object.keys(current).forEach(name => {
let value = current[name];
if (typeof value === 'object' && value !== null) {
value = convert(value);
}
if (typeof value === 'string') {
if (value.indexOf('\\') === 0) {
value = value.replace('\\', '');
} else {
if (process.env[value]) {
value = process.env[value];
}
if (value.indexOf('.') === 0 || value.indexOf('..') === 0) {
// Make relative paths absolute
value = path.resolve(
path.join(config.util.getEnv('NODE_CONFIG_DIR')),
value.replace(/\//g, separator)
);
}
}
}
result[name] = value;
});
return result;
};
const env = config.util.getEnv('NODE_ENV');
const conf = convert(config);
if (!app) {
return conf;
}
debug(`Initializing configuration for ${env} environment`);
Object.keys(conf).forEach(name => {
const value = conf[name];
debug(`Setting ${name} configuration value to`, value);
(app as Application).set(name, value);
});
return conf;
};
}
if (typeof module !== 'undefined') {
module.exports = Object.assign(init, module.exports);
}