Skip to content

Commit

Permalink
refactor: separate dumping config object and config file (#3014)
Browse files Browse the repository at this point in the history
  • Loading branch information
XadillaX authored and fengmk2 committed Sep 18, 2018
1 parent e3f183e commit 1d30166
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 34 deletions.
30 changes: 23 additions & 7 deletions lib/egg.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,31 +330,47 @@ class EggApplication extends EggCore {
}

/**
* save app.config to `run/${type}_config.json`
* dump out the config and meta object
* @private
* @return {Object} the result
*/
dumpConfig() {
const rundir = this.config.rundir;
dumpConfigToObject() {
let ignoreList;
try {
// support array and set
ignoreList = Array.from(this.config.dump.ignore);
} catch (_) {
ignoreList = [];
}

const json = extend(true, {}, { config: this.config, plugins: this.plugins });
utils.convertObject(json, ignoreList);
return {
config: json,
meta: this.loader.configMeta,
};
}

/**
* save app.config to `run/${type}_config.json`
* @private
*/
dumpConfig() {
const rundir = this.config.rundir;
try {
/* istanbul ignore if */
if (!fs.existsSync(rundir)) fs.mkdirSync(rundir);

// get dumpped object
const { config, meta } = this.dumpConfigToObject();

// dump config
const json = extend(true, {}, { config: this.config, plugins: this.plugins });
utils.convertObject(json, ignoreList);
const dumpFile = path.join(rundir, `${this.type}_config.json`);
fs.writeFileSync(dumpFile, CircularJSON.stringify(json, null, 2));
fs.writeFileSync(dumpFile, CircularJSON.stringify(config, null, 2));

// dump config meta
const dumpMetaFile = path.join(rundir, `${this.type}_config_meta.json`);
fs.writeFileSync(dumpMetaFile, CircularJSON.stringify(this.loader.configMeta, null, 2));
fs.writeFileSync(dumpMetaFile, CircularJSON.stringify(meta, null, 2));
} catch (err) {
this.coreLogger.warn(`dumpConfig error: ${err.message}`);
}
Expand Down
77 changes: 50 additions & 27 deletions test/lib/egg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,17 @@ describe('test/lib/egg.test.js', () => {
assert(json.config.name === 'demo');
assert(json.config.tips === 'hello egg');
json = require(path.join(baseDir, 'run/application_config.json'));
assert(/\d+\.\d+\.\d+/.test(json.plugins.onerror.version));
// should dump dynamic config
assert(json.config.tips === 'hello egg started');
checkApp(json);

const dumpped = app.dumpConfigToObject();
checkApp(dumpped.config);

function checkApp(json) {
assert(/\d+\.\d+\.\d+/.test(json.plugins.onerror.version));
assert(json.config.name === 'demo');
// should dump dynamic config
assert(json.config.tips === 'hello egg started');
}
});

it('should dump router json', () => {
Expand All @@ -52,35 +60,50 @@ describe('test/lib/egg.test.js', () => {
let json = require(path.join(baseDir, 'run/agent_config_meta.json'));
assert(json.name === path.join(__dirname, '../../config/config.default.js'));
assert(json.buffer === path.join(baseDir, 'config/config.default.js'));

json = require(path.join(baseDir, 'run/application_config_meta.json'));
assert(json.name === path.join(__dirname, '../../config/config.default.js'));
assert(json.buffer === path.join(baseDir, 'config/config.default.js'));
checkApp(json);

const dumpped = app.dumpConfigToObject();
checkApp(dumpped.meta);

function checkApp(json) {
assert(json.name === path.join(__dirname, '../../config/config.default.js'));
assert(json.buffer === path.join(baseDir, 'config/config.default.js'));
}
});

it('should ignore some type', () => {
const json = require(path.join(baseDir, 'run/application_config.json'));
assert(json.config.mysql.accessId === 'this is accessId');

assert(json.config.name === 'demo');
assert(json.config.keys === '<String len: 3>');
assert(json.config.buffer === '<Buffer len: 4>');
assert(json.config.siteFile['/favicon.ico'].startsWith('<Buffer len:'));

assert(json.config.pass === '<String len: 12>');
assert(json.config.pwd === '<String len: 11>');
assert(json.config.password === '<String len: 16>');
assert(json.config.passwordNew === 'this is passwordNew');
assert(json.config.mysql.passd === '<String len: 13>');
assert(json.config.mysql.passwd === '<String len: 14>');
assert(json.config.mysql.secret === '<String len: 10>');
assert(json.config.mysql.secretNumber === '<Number>');
assert(json.config.mysql.masterKey === '<String len: 17>');
assert(json.config.mysql.accessKey === '<String len: 17>');
assert(json.config.mysql.consumerSecret === '<String len: 22>');
assert(json.config.mysql.someSecret === null);

// don't change config
assert(app.config.keys === 'foo');
checkApp(json);

const dumpped = app.dumpConfigToObject();
checkApp(dumpped.config);

function checkApp(json) {
assert(json.config.mysql.accessId === 'this is accessId');

assert(json.config.name === 'demo');
assert(json.config.keys === '<String len: 3>');
assert(json.config.buffer === '<Buffer len: 4>');
assert(json.config.siteFile['/favicon.ico'].startsWith('<Buffer len:'));

assert(json.config.pass === '<String len: 12>');
assert(json.config.pwd === '<String len: 11>');
assert(json.config.password === '<String len: 16>');
assert(json.config.passwordNew === 'this is passwordNew');
assert(json.config.mysql.passd === '<String len: 13>');
assert(json.config.mysql.passwd === '<String len: 14>');
assert(json.config.mysql.secret === '<String len: 10>');
assert(json.config.mysql.secretNumber === '<Number>');
assert(json.config.mysql.masterKey === '<String len: 17>');
assert(json.config.mysql.accessKey === '<String len: 17>');
assert(json.config.mysql.consumerSecret === '<String len: 22>');
assert(json.config.mysql.someSecret === null);

// don't change config
assert(app.config.keys === 'foo');
}
});

it('should console.log call inspect()', () => {
Expand Down

0 comments on commit 1d30166

Please sign in to comment.