-
Notifications
You must be signed in to change notification settings - Fork 91
转成 egg-core #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
转成 egg-core #6
Changes from all commits
2690b30
6cb9419
b5fb70c
1ca4349
368f0ae
6141ec3
11ad9e5
2d95d60
5d1edb9
3e5e9f6
e2dee43
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| 'use strict'; | ||
|
|
||
| module.exports = require('./lib/egg_loader'); | ||
| module.exports.EggCore = require('./lib/egg'); | ||
| module.exports.EggLoader = require('./lib/loader/egg_loader'); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| 'use strict'; | ||
|
|
||
| const assert = require('assert'); | ||
| const fs = require('fs'); | ||
| const KoaApplication = require('koa'); | ||
| const EggConsoleLogger = require('egg-logger').EggConsoleLogger; | ||
|
|
||
| const DEPRECATE = Symbol('EggCore#deprecate'); | ||
|
|
||
| class EggCore extends KoaApplication { | ||
|
|
||
| /** | ||
| * @constructor | ||
| * @param {Object} options - options | ||
| * @param {String} [options.baseDir=process.cwd()] - the directory of application | ||
| * @param {String} [options.type=application|agent] - wheter it's running in app worker or agent worker | ||
| * @param {Object} [options.plugins] - custom plugins | ||
| * @since 1.0.0 | ||
| */ | ||
| constructor(options) { | ||
| options = options || {}; | ||
| options.baseDir = options.baseDir || process.cwd(); | ||
| options.type = options.type || 'application'; | ||
|
|
||
| assert(typeof options.baseDir === 'string', 'options.baseDir required, and must be a string'); | ||
| assert(fs.existsSync(options.baseDir), `Directory ${options.baseDir} not exists`); | ||
| assert(fs.statSync(options.baseDir).isDirectory(), `Directory ${options.baseDir} is not a directory`); | ||
| assert(options.type === 'application' || options.type === 'agent', 'options.type should be application or agent'); | ||
|
|
||
| super(); | ||
|
|
||
| /** | ||
| * @member {Object} EggCore#options | ||
| * @since 1.0.0 | ||
| */ | ||
| this._options = options; | ||
|
|
||
| /** | ||
| * logging for EggCore, avoid using console directly | ||
| * @member {Logger} EggCore#console | ||
| * @since 1.0.0 | ||
| */ | ||
| this.console = new EggConsoleLogger(); | ||
|
|
||
| /** | ||
| * @member {EggLoader} EggCore#loader | ||
| * @since 1.0.0 | ||
| */ | ||
| const Loader = this[Symbol.for('egg#loader')]; | ||
| assert(Loader, 'Symbol.for(\'egg#loader\') is required'); | ||
| this.loader = new Loader({ | ||
| baseDir: options.baseDir, | ||
| app: this, | ||
| plugins: options.plugins, | ||
| logger: this.console, | ||
| }); | ||
|
|
||
| this._initReady(); | ||
| } | ||
|
|
||
| /** | ||
| * alias to options.type | ||
| * @member {String} | ||
| * @since 1.0.0 | ||
| */ | ||
| get type() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. jsdoc |
||
| return this._options.type; | ||
| } | ||
|
|
||
| /** | ||
| * alias to options.baseDir | ||
| * @member {String} | ||
| * @since 1.0.0 | ||
| */ | ||
| get baseDir() { | ||
| return this._options.baseDir; | ||
| } | ||
|
|
||
| /** | ||
| * @member {Function} | ||
| * @see https://npmjs.com/package/depd | ||
| * @since 1.0.0 | ||
| */ | ||
| get deprecate() { | ||
| if (!this[DEPRECATE]) { | ||
| // require depd when get, `process.env.NO_DEPRECATION = '*'` should be use when run test everytime | ||
| this[DEPRECATE] = require('depd')('egg'); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. require 不能放顶部?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @atian25 看注释说明。。。
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. require 的时候就读取了环境变量?今天在手机没喵源码
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. require 一次就缓存了,之后里面的代码就不会执行了,可以看下 depd 的源码 |
||
| } | ||
| return this[DEPRECATE]; | ||
| } | ||
|
|
||
| /** | ||
| * name in package.json | ||
| * @member {String} | ||
| * @since 1.0.0 | ||
| */ | ||
| get name() { | ||
| return this.loader.pkg.name; | ||
| } | ||
|
|
||
| /** | ||
| * alias to {EggCore#loader} | ||
| * @member {Object} | ||
| * @since 1.0.0 | ||
| */ | ||
| get plugins() { | ||
| return this.loader.plugins; | ||
| } | ||
|
|
||
| /** | ||
| * alias to {EggCore#loader} | ||
| * @member {Config} | ||
| * @since 1.0.0 | ||
| */ | ||
| get config() { | ||
| return this.loader.config; | ||
| } | ||
|
|
||
| /** | ||
| * close all listeners | ||
| * @member {Function} | ||
| * @since 1.0.0 | ||
| */ | ||
| close() { | ||
| this.emit('close'); | ||
| this.removeAllListeners(); | ||
| } | ||
|
|
||
| /** | ||
| * @member {Function} | ||
| * @private | ||
| */ | ||
| _initReady() { | ||
| /** | ||
| * register an callback function that will be invoked when application is ready. | ||
| * @member {Function} EggCore#ready | ||
| * @since 1.0.0 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 加个使用说明? |
||
| */ | ||
|
|
||
| /** | ||
| * If a client starts asynchronously, you can register `readyCallback`, | ||
| * then the application will wait for the callback to ready | ||
| * | ||
| * It will log when the callback is not invoked after 10s | ||
| * @member {Function} EggCore#readyCallback | ||
| * @since 1.0.0 | ||
| * @example | ||
| * ```js | ||
| * const done = app.readyCallback('mysql'); | ||
| * mysql.ready(done); | ||
| * ``` | ||
| */ | ||
| require('ready-callback')({ timeout: 10000 }).mixin(this); | ||
|
|
||
| this.on('ready_stat', data => { | ||
| this.console.info('[egg:core:ready_stat] end ready task %s, remain %j', data.id, data.remain); | ||
| }).on('ready_timeout', id => { | ||
| this.console.warn('[egg:core:ready_timeout] 10 seconds later %s was still unable to finish.', id); | ||
| }); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| module.exports = EggCore; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里如果缩进的话应该不用 @param
- {String} [baseDir=process.cwd()]不过这样也行
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我看例子推荐用 .