Skip to content

Commit caaa21f

Browse files
committed
chore(configuration): Remove environment variable substitution (#1942)
BREAKING CHANGE: Falls back to node-config instead of adding additional functionality like path replacements and automatic environment variable insertion.
1 parent be85b23 commit caaa21f

File tree

6 files changed

+20
-150
lines changed

6 files changed

+20
-150
lines changed

packages/configuration/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"prepublish": "npm run compile",
3535
"compile": "shx rm -rf lib/ && tsc",
3636
"test": "npm run compile && npm run mocha",
37-
"mocha": "mocha --config ../../.mocharc.ts.json --recursive test/**.test.ts test/**/*.test.ts"
37+
"mocha": "NODE_CONFIG_DIR=./test/config mocha --config ../../.mocharc.ts.json --recursive test/**.test.ts test/**/*.test.ts"
3838
},
3939
"semistandard": {
4040
"env": [

packages/configuration/src/index.ts

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,24 @@
11
import { Application } from '@feathersjs/feathers';
22
import Debug from 'debug';
3-
import path from 'path';
43
import config from 'config';
54

65
const debug = Debug('@feathersjs/configuration');
7-
const separator = path.sep;
86

97
export default function init () {
108
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-
// Make relative paths absolute
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-
479
if (!app) {
48-
return conf;
10+
return config;
4911
}
5012

51-
debug(`Initializing configuration for ${env} environment`);
13+
debug(`Initializing configuration for ${config.util.getEnv('NODE_ENV')} environment`);
5214

53-
Object.keys(conf).forEach(name => {
54-
const value = conf[name];
15+
Object.keys(config).forEach(name => {
16+
const value = (config as any)[name];
5517
debug(`Setting ${name} configuration value to`, value);
5618
app!.set(name, value);
5719
});
5820

59-
return conf;
21+
return config;
6022
};
6123
}
6224

packages/configuration/test/config/custom-environment-variables.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

packages/configuration/test/config/default.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
{
22
"port": 3030,
3-
"environment": "NODE_ENV",
4-
"path": "../something",
5-
"pathFromEnv": "PATH_ENV",
6-
"unescaped": "\\NODE_ENV",
7-
"from": "default",
8-
"deeply": { "nested": { "env": "NODE_ENV" } },
93
"array": ["one", "two", "three"],
104
"deep": { "base": false },
115
"nullish": null

packages/configuration/test/config/testing.js

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 14 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,27 @@
1-
import assert from 'assert';
2-
import { join } from 'path';
1+
import { strict as assert } from 'assert';
32
import feathers, { Application } from '@feathersjs/feathers';
3+
import plugin from '../src';
44

55
describe('@feathersjs/configuration', () => {
6-
const originalEnv: { [key: string]: any } = {};
7-
let app: Application;
8-
let plugin: any;
6+
const app: Application = feathers().configure(plugin());
97

10-
before(() => {
11-
originalEnv.NODE_ENV = process.env.NODE_ENV;
12-
originalEnv.NODE_CONFIG_DIR = process.env.NODE_CONFIG_DIR;
13-
14-
process.env.NODE_ENV = 'testing';
15-
process.env.NODE_CONFIG_DIR = join(__dirname, 'config');
16-
process.env.PATH_ENV = '../something';
17-
18-
plugin = require('../lib');
19-
app = feathers().configure(plugin());
8+
it('exports default', () => {
9+
assert.ok(typeof require('../lib') === 'function');
2010
});
2111

22-
after(() => {
23-
process.env.NODE_ENV = originalEnv.NODE_ENV;
24-
process.env.NODE_CONFIG_DIR = originalEnv.NODE_CONFIG_DIR;
12+
it('initialized app with default.json', () => {
13+
assert.equal(app.get('port'), 3030);
14+
assert.deepEqual(app.get('array'), [
15+
'one', 'two', 'three'
16+
]);
17+
assert.deepEqual(app.get('deep'), { base: false });
18+
assert.deepEqual(app.get('nullish'), null);
2519
});
2620

27-
it('exports default', () =>
28-
assert.strictEqual(plugin, plugin.default)
29-
);
30-
31-
it('initialized app with default data', () =>
32-
assert.strictEqual(app.get('port'), 3030)
33-
);
34-
35-
it('initialized with <env>', () =>
36-
assert.strictEqual(app.get('from'), 'testing')
37-
);
38-
39-
it('initialized with <env> derived data module', () =>
40-
assert.strictEqual(app.get('derived'), 'Hello World')
41-
);
42-
43-
it('initialized property with environment variable', () =>
44-
assert.strictEqual(app.get('environment'), 'testing')
45-
);
46-
47-
it('initialized property with environment variable from <env>', () =>
48-
assert.strictEqual(app.get('testEnvironment'), 'testing')
49-
);
50-
51-
it('initialized property with derived environment variable from <env> module', () =>
52-
assert.strictEqual(app.get('derivedEnvironment'), 'testing')
53-
);
54-
55-
it('uses an escape character', () =>
56-
assert.strictEqual(app.get('unescaped'), 'NODE_ENV')
57-
);
58-
59-
it('normalizes relative path names', () =>
60-
assert.strictEqual(app.get('path'), join(__dirname, 'something'))
61-
);
62-
63-
it('normalizes relative path names from environment variable', () =>
64-
assert.strictEqual(app.get('pathFromEnv'), join(__dirname, 'something'))
65-
);
66-
67-
it('converts environment variables recursively', () =>
68-
assert.strictEqual(app.get('deeply').nested.env, 'testing')
69-
);
70-
71-
it('converts arrays as actual arrays', () =>
72-
assert.ok(Array.isArray(app.get('array')))
73-
);
74-
7521
it('works when called directly', () => {
7622
const fn = plugin();
23+
const conf = fn() as any;
7724

78-
assert.strictEqual(fn().port, 3030);
79-
});
80-
81-
it('deep merges properties', () =>
82-
assert.deepStrictEqual(app.get('deep'), {
83-
base: false,
84-
merge: true
85-
})
86-
);
87-
88-
it('supports null value', () => {
89-
assert.strictEqual(app.get('nullish'), null);
25+
assert.strictEqual(conf.port, 3030);
9026
});
9127
});

0 commit comments

Comments
 (0)