Skip to content

Commit

Permalink
fix: dumpConfig support circular json (#2394)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse authored and popomore committed Apr 14, 2018
1 parent 3a489b6 commit 590bd8c
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/egg.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const cluster = require('cluster-client');
const extend = require('extend2');
const ContextLogger = require('egg-logger').EggContextLogger;
const ContextCookies = require('egg-cookies');
const CircularJSON = require('circular-json');
const ContextHttpClient = require('./core/context_httpclient');
const Messenger = require('./core/messenger');
const DNSCacheHttpClient = require('./core/dnscache_httpclient');
Expand Down Expand Up @@ -337,11 +338,11 @@ class EggApplication extends EggCore {
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, JSON.stringify(json, null, 2));
fs.writeFileSync(dumpFile, CircularJSON.stringify(json, null, 2));

// dump config meta
const dumpMetaFile = path.join(rundir, `${this.type}_config_meta.json`);
fs.writeFileSync(dumpMetaFile, JSON.stringify(this.loader.configMeta, null, 2));
fs.writeFileSync(dumpMetaFile, CircularJSON.stringify(this.loader.configMeta, null, 2));
} catch (err) {
this.coreLogger.warn(`dumpConfig error: ${err.message}`);
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@types/urllib": "^2.25.0",
"accepts": "^1.3.5",
"agentkeepalive": "^3.4.1",
"circular-json": "^0.5.3",
"cluster-client": "^2.0.0",
"debug": "^3.1.0",
"delegates": "^1.0.0",
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/apps/dumpconfig-circular/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const sleep = require('mz-modules/sleep');

module.exports = app => {
app.beforeStart(function*() {
yield sleep(500);
app.config.foo.push(app.config.foo);
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
exports.dynamic = 0;

exports.keys = 'test key';
exports.foo = [];
3 changes: 3 additions & 0 deletions test/fixtures/apps/dumpconfig-circular/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "dumpconfig-circular"
}
18 changes: 18 additions & 0 deletions test/lib/egg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,24 @@ describe('test/lib/egg.test.js', () => {
});
});

describe('dumpConfig() with circular', () => {
let app;
before(() => {
app = utils.app('apps/dumpconfig-circular');
});
after(() => app.close());

it('should dump in config', async () => {
const baseDir = utils.getFilepath('apps/dumpconfig-circular');
await sleep(100);
await app.ready();

await sleep(100);
const json = readJson(path.join(baseDir, 'run/application_config.json'));
assert.deepEqual(json.config.foo, [ '~config~foo' ]);
});
});

describe('dumpConfig() ignore error', () => {
const baseDir = utils.getFilepath('apps/dump-ignore-error');
let app;
Expand Down

0 comments on commit 590bd8c

Please sign in to comment.