Skip to content

Commit

Permalink
feat: warn if some confused configurations exist in config (#637)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse committed Mar 24, 2017
1 parent cd8c659 commit 8c77e59
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 0 deletions.
16 changes: 16 additions & 0 deletions config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,22 @@ module.exports = appInfo => {
dump: {
ignore: new Set([ 'pass', 'pwd', 'passd', 'passwd', 'password', 'keys', 'secret' ]),
},

/**
* configurations are confused to users
* {
* [unexpectedKey]: [expectedKey],
* }
* @member Config#confusedConfigurations
* @type {Object}
*/
confusedConfigurations: {
bodyparser: 'bodyParser',
notFound: 'notfound',
sitefile: 'siteFile',
middlewares: 'middleware',
httpClient: 'httpclient',
},
};

/**
Expand Down
16 changes: 16 additions & 0 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Application extends EggApplication {

// dump config after loaded, ensure all the dynamic modifications will be recorded
this.dumpConfig();
this.warnConfusedConfig();
this.bindEvents();
}

Expand Down Expand Up @@ -213,6 +214,21 @@ class Application extends EggApplication {
// expose server to support websocket
this.on('server', server => this.onServer(server));
}

/**
* warn when confused configurations are present
*
* @private
*/
warnConfusedConfig() {
const confusedConfigurations = this.config.confusedConfigurations;
Object.keys(confusedConfigurations).forEach(key => {
if (this.config[key] !== undefined) {
this.logger.warn('Unexpected config key `%s` exists, Please use `%s` instead.',
key, confusedConfigurations[key]);
}
});
}
}

module.exports = Application;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

exports.middlewares = [];
exports.bodyparser = {};
exports.sitefile = {};
exports.notFound = {};
exports.httpClient = {};
3 changes: 3 additions & 0 deletions test/fixtures/apps/confused-configuration/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "confused-configuration"
}
13 changes: 13 additions & 0 deletions test/lib/application.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,19 @@ describe('test/lib/application.test.js', () => {
});
});

describe('warn confused configurations', () => {
it('should warn if confused configurations exist', function* () {
const app = utils.app('apps/confused-configuration');
yield app.ready();
const logs = fs.readFileSync(utils.getFilepath('apps/confused-configuration/logs/confused-configuration/confused-configuration-web.log'), 'utf8');
assert(logs.match(/Unexpected config key `bodyparser` exists, Please use `bodyParser` instead\./));
assert(logs.match(/Unexpected config key `notFound` exists, Please use `notfound` instead\./));
assert(logs.match(/Unexpected config key `sitefile` exists, Please use `siteFile` instead\./));
assert(logs.match(/Unexpected config key `middlewares` exists, Please use `middleware` instead\./));
assert(logs.match(/Unexpected config key `httpClient` exists, Please use `httpclient` instead\./));
});
});

describe('test on apps/demo', () => {
let app;
before(() => {
Expand Down

0 comments on commit 8c77e59

Please sign in to comment.