Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/egg.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const assert = require('assert');
const fs = require('fs');
const KoaApplication = require('koa');
const EggConsoleLogger = require('egg-logger').EggConsoleLogger;
const debug = require('debug')('egg-core');

const DEPRECATE = Symbol('EggCore#deprecate');

Expand Down Expand Up @@ -165,6 +166,8 @@ class EggCore extends KoaApplication {
}).on('ready_timeout', id => {
this.console.warn('[egg:core:ready_timeout] 10 seconds later %s was still unable to finish.', id);
});

this.ready(() => debug('egg emit ready, application started'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改成 this.logger.debug 吧

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

debug 调试起来方便吧

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

但是对项目开发者不方便。logger.debug 也可以通过 环境变量打开。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我们把 logger.debug 换成 node 的 debug?现在 debug 不能用环境变量打开,没有作用域,而且不能应用于底层模块。还有不能单独开启 debug,开启后 info 以上都会开启。

}

}
Expand Down
41 changes: 37 additions & 4 deletions lib/loader/mixin/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

const path = require('path');

const LOAD_CUSTOM_FILE = Symbol('EggCore#loadCustomFile');

module.exports = {

/**
Expand All @@ -18,19 +20,50 @@ module.exports = {
* doAsync(done);
* }
* ```
*
* it's better to use async function
*
* ```js
* module.exports = async function(app) {
* await doAsync();
* }
* ```
*
* Or use generator wrapped by co.wrap
*
* ```js
* const co = require('co');
* module.exports = co.wrap(function*(app) {
* yield doAsync();
* });
* ```
* @since 1.0.0
*/
loadCustomApp() {
this.getLoadUnits()
.forEach(unit => this.loadFile(path.join(unit.path, 'app.js')));
this[LOAD_CUSTOM_FILE]('app.js');
},

/**
* Load agent.js, same as {@link EggLoader#loadCustomApp}
*/
loadCustomAgent() {
this.getLoadUnits()
.forEach(unit => this.loadFile(path.join(unit.path, 'agent.js')));
this[LOAD_CUSTOM_FILE]('agent.js');
},

[LOAD_CUSTOM_FILE](filename) {
this.getLoadUnits()
.forEach(unit => {
const filepath = path.join(unit.path, filename);
const ret = this.loadFile(filepath);
registerCallback(ret, this.app, filepath);
});
},
};

function registerCallback(ret, app, filepath) {
// register readyCallback if custom file export async function
if (ret instanceof Promise) {
const done = app.readyCallback(filepath);
ret.then(() => done()).catch(done);
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
},
"devDependencies": {
"autod": "^2.7.0",
"co": "^4.6.0",
"egg-bin": "1",
"egg-ci": "1",
"eslint": "~3.1.0",
Expand Down
20 changes: 20 additions & 0 deletions test/fixtures/custom-app-async-function/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments)).next());
});
};
function default_1(app) {
return __awaiter(this, void 0, void 0, function* () {
yield wait(1000);
app.app = true;
});
}
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = default_1;
function wait(timeout) {
return new Promise(resolve => setTimeout(resolve, timeout));
}
8 changes: 8 additions & 0 deletions test/fixtures/custom-app-async-function/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default async function(app) {
await wait(1000);
app.app = true;
}

function wait(timeout) {
return new Promise(resolve => setTimeout(resolve, timeout));
}
3 changes: 3 additions & 0 deletions test/fixtures/custom-app-async-function/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "custom-app-async-function"
}
12 changes: 12 additions & 0 deletions test/fixtures/custom-app-error/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

const co = require('co');

module.exports = co.wrap(function*(app) {
yield wait(1000);
throw new Error('load async error');
});

function wait(timeout) {
return new Promise(resolve => setTimeout(resolve, timeout));
}
3 changes: 3 additions & 0 deletions test/fixtures/custom-app-error/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "custom-app-error"
}
12 changes: 12 additions & 0 deletions test/fixtures/custom-app-promise/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

const co = require('co');

module.exports = co.wrap(function*(app) {
yield wait(1000);
app.app = true;
});

function wait(timeout) {
return new Promise(resolve => setTimeout(resolve, timeout));
}
3 changes: 3 additions & 0 deletions test/fixtures/custom-app-promise/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "custom-app-promise"
}
90 changes: 72 additions & 18 deletions test/loader/mixin/load_custom_app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,83 @@
const should = require('should');
const utils = require('../../utils');

describe('test/loader/mixin/load_custom_app.test.js', function() {

let app;
before(function() {
app = utils.createApp('plugin');
app.loader.loadPlugin();
app.loader.loadConfig();
app.loader.loadCustomApp();
describe('test/loader/mixin/load_custom_app.test.js', () => {

describe('app.js as function', () => {
let app;
before(() => {
app = utils.createApp('plugin');
app.loader.loadPlugin();
app.loader.loadConfig();
app.loader.loadCustomApp();
});
after(() => app.close());

it('should load app.js', () => {
app.b.should.equal('plugin b');
app.c.should.equal('plugin c');
app.app.should.equal('app');
});

it('should app.js of plugin before application\'s', () => {
(app.dateB <= app.date).should.equal(true);
(app.dateC <= app.date).should.equal(true);
});

it('should not load plugin that is disabled', () => {
should.not.exists(app.a);
});
});
after(() => app.close());

it('should load app.js', function() {
app.b.should.equal('plugin b');
app.c.should.equal('plugin c');
app.app.should.equal('app');
describe('app.js as function return promise', () => {
let app;
before(done => {
app = utils.createApp('custom-app-promise');
app.loader.loadPlugin();
app.loader.loadConfig();
app.loader.loadCustomApp();
app.ready(done);
});
after(() => app.close());

it('should load app.js success', () => {
app.app.should.be.true();
});
});

it('should app.js of plugin before application\'s', function() {
(app.dateB <= app.date).should.equal(true);
(app.dateC <= app.date).should.equal(true);
describe('app.js as async function', () => {
let app;
before(done => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before(() => {
  ...
  return app.done();
});

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个没关系吧,都一样

app = utils.createApp('custom-app-async-function');
app.loader.loadPlugin();
app.loader.loadConfig();
app.loader.loadCustomApp();
app.ready(done);
});
after(() => app.close());

it('should load app.js success', () => {
app.app.should.be.true();
});
});

it('should not load plugin that is disabled', function() {
should.not.exists(app.a);

describe('app.js load async error', () => {
let app;
after(() => app.close());

it('should load app.js success', done => {
app = utils.createApp('custom-app-error');
app.on('error', err => {
err.message.should.eql('load async error');
done();
});
app.loader.loadPlugin();
app.loader.loadConfig();
app.loader.loadCustomApp();
app.ready(() => {
throw new Error('should not call');
});
});
});
});