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