-
Notifications
You must be signed in to change notification settings - Fork 93
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
refactor API #5
+1,211
−1,126
Merged
refactor API #5
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
b7df00b
refactor: loadServerEnv -> getServerEnv
popomore 3699562
refactor: loadFrameworkPaths -> getEggPaths
popomore dffb7bc
refactor: export loadExtend
popomore 16b0422
test: add testcase for getAppname
popomore f524dae
refactor: move loadFile and more testcase
popomore be89ac8
refactor: move load_custom_app/load_custom_agent
popomore 356c5a3
refactor: move middleware to loader
popomore 7303490
refactor: move controller to loader
popomore 6734d41
refactor: rename loader to mixin
popomore 9a174d9
refactor: move config to mixin
popomore 8c2d1de
refactor: move plugin to mixin
popomore 3487004
refactor: change loadDirs to getLoadUnits
popomore 0b6a424
refactor: rename base_loader to egg_loader
popomore ea88ab1
feat: add loader.loadToContext, it can load files that bind ctx
popomore 4b15dee
refactor: don't load lib/core and lib/plugins
popomore 98f7bcd
refactor item_end to itesm_exports
popomore 52c9c70
refactor: remove old class_loader
popomore df70e19
doc: update README
popomore File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,53 +20,208 @@ | |
[download-image]: https://img.shields.io/npm/dm/egg-loader.svg?style=flat-square | ||
[download-url]: https://npmjs.org/package/egg-loader | ||
|
||
egg 文件加载器 | ||
A core Plugable framework based on koa | ||
|
||
## 使用说明 | ||
**Don't use it directly, see [egg]** | ||
|
||
## Usage | ||
|
||
Directory structure | ||
|
||
``` | ||
├── package.json | ||
├── app.js (optional) | ||
├── agent.js (optional) | ||
├── app | ||
| ├── router.js | ||
│ ├── controller | ||
│ │ └── home.js | ||
| ├── extend (optional) | ||
│ | ├── helper.js (optional) | ||
│ | ├── filter.js (optional) | ||
│ | ├── request.js (optional) | ||
│ | ├── response.js (optional) | ||
│ | ├── context.js (optional) | ||
│ | ├── application.js (optional) | ||
│ | └── agent.js (optional) | ||
│ ├── service (optional) | ||
│ ├── middleware (optional) | ||
│ │ └── response_time.js | ||
│ └── view (optional) | ||
| ├── layout.html | ||
│ └── home.html | ||
├── config | ||
| ├── config.default.js | ||
│ ├── config.prod.js | ||
| ├── config.test.js (optional) | ||
| ├── config.local.js (optional) | ||
| ├── config.unittest.js (optional) | ||
│ └── plugin.js | ||
``` | ||
|
||
Than you can start with code below | ||
|
||
```js | ||
const app = koa(); | ||
const Loader = require('egg-loader'); | ||
const loader = new Loader({ | ||
baseDir: '/path/to/app', | ||
eggPath: '/path/to/framework', | ||
app: app, | ||
const Application = require('egg-core').Application; | ||
const app = new Application({ | ||
baseDir: '/path/to/app' | ||
}); | ||
loader.loadPlugin(); | ||
loader.loadConfig(); | ||
app.ready(() => { | ||
app.listen(3000); | ||
}); | ||
``` | ||
|
||
## EggLoader | ||
|
||
EggLoader will load file or directory easily, you can also custom your loader with low level API. | ||
|
||
### constructor | ||
|
||
- {String} baseDir - current directory of application | ||
- {Object} app - instance of egg application | ||
- {Object} plugins - merge plugins for test | ||
- {Logger} logger - logger instance,default is console | ||
|
||
### High Level API | ||
|
||
#### loadPlugin | ||
|
||
Load config/plugin.js | ||
|
||
#### loadConfig | ||
|
||
Load config/config.js and config/{serverEnv}.js | ||
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. config.default |
||
|
||
#### loadController | ||
|
||
Load app/controller | ||
|
||
#### loadMiddleware | ||
|
||
Load app/middleware | ||
|
||
#### loadApplicationExtend | ||
|
||
Load app/extend/application.js | ||
|
||
#### loadContextExtend | ||
|
||
Load app/extend/context.js | ||
|
||
#### loadRequestExtend | ||
|
||
Load app/extend/request.js | ||
|
||
#### loadResponseExtend | ||
|
||
Load app/extend/response.js | ||
|
||
#### loadHelperExtend | ||
|
||
Load app/extend/helper.js | ||
|
||
#### loadCustomApp | ||
|
||
Load app.js | ||
|
||
#### loadCustomAgent | ||
|
||
Load agent.js | ||
|
||
#### loadService | ||
|
||
Load app/service | ||
|
||
### Low Level API | ||
|
||
#### getServerEnv() | ||
|
||
Get serverEnv for application, available serverEnv | ||
|
||
serverEnv | description | ||
--- | --- | ||
default | default environment | ||
test | system integration testing environment | ||
prod | production environment | ||
local | local environment on your own computer | ||
unittest | unit test environment | ||
|
||
You can use this.serverEnv directly after instantiation. | ||
|
||
#### getEggPaths() | ||
|
||
Get the directory of the frameworks, a new framework born by extending egg, then you can use this function to get all frameworks. | ||
|
||
#### getLoadUnits() | ||
|
||
A loadUnit is a directory that can be loaded by EggLoader, it has the same structure. | ||
|
||
This function will get add loadUnits follow the order: | ||
|
||
1. plugin | ||
2. framework | ||
3. app | ||
|
||
loadUnit has a path and a type(app, framework, plugin). | ||
|
||
```js | ||
{ | ||
path: 'path/to/application', | ||
type: 'app', | ||
} | ||
``` | ||
|
||
## API | ||
#### getAppname() | ||
|
||
Get appname from package.json | ||
|
||
#### loadFile(filepath) | ||
|
||
Load single file, will invork when export is function. | ||
|
||
#### loadToApp(directory, property, LoaderOptions) | ||
|
||
Load the files in directory to app. | ||
|
||
### options | ||
Invoke `this.loadToApp('$baseDir/app/controller', 'controller')`, then you can use it by `app.controller`. | ||
|
||
- baseDir: 应用根目录 | ||
- eggPath: egg 本身的路径 | ||
- plugins: 自定义插件配置 | ||
- app: 任何基于 koa 实例化 | ||
#### loadToContext(directory, property, LoaderOptions) | ||
|
||
### methods | ||
Load the files in directory to context, it will bind the context. | ||
|
||
基础方式 | ||
``` | ||
// define service in app/service/query.js | ||
module.exports = class Query { | ||
constructor(ctx) { | ||
// get the ctx | ||
} | ||
|
||
get() {} | ||
}; | ||
|
||
// use the service in app/controller/home.js | ||
module.exports = function*() { | ||
this.body = this.service.query.get(); | ||
}; | ||
``` | ||
|
||
- loadFile: 加载单文件, | ||
- loadDirs: 获取需要加载的所有目录,按照 egg > 插件 > 框架 > 应用的顺序加载。 | ||
#### loadExtend(name, target) | ||
|
||
Loader app/extend/xx.js to target, example | ||
|
||
```js | ||
this.loadExtend('application', app); | ||
``` | ||
|
||
业务方法 | ||
### LoaderOptions | ||
|
||
- getAppname: 获取应用名 | ||
- loadServerEnv: 加载环境变量 | ||
- loadConfig: 加载: config | ||
- loadPlugin: 加载插件 | ||
- loadApplication: 加载 extend/application.js 到 app | ||
- loadRequest: 加载 extend/request.js 到 app.request | ||
- loadResponse: 加载 extend/response.js 到 app.response | ||
- loadContext: 加载 extend/context.js 到 app.context | ||
- loadHelper: 加载 extend/helper.js,到 app.Helper.prototype,需要定义 app.Helper 才会加载 | ||
- loadService: 加载 app/service 到 app.service | ||
- loadProxy: 加载 app/proxy 到 app.proxy | ||
- loadMiddleware: 加载中间件 | ||
- loadController: 加载 app/controller 到 app.controller | ||
- loadAgent: 加载 agent.js 进行自定义 | ||
- loadApp: 加载 app.js 进行自定义 | ||
- {String|Array} directory - directories to load | ||
- {Object} target: attach object from loaded files, | ||
- {String} ignore - ignore the files when load | ||
- {Function} initializer - custom file exports | ||
- {Boolean} lowercaseFirst - determine whether the fist letter is lowercase | ||
- {Boolean} override: determine whether override the property when get the same name | ||
- {Boolean} call - determine whether invoke when exports is function | ||
- {Object} inject - an object that be the argument when invoke the function | ||
|
||
[egg]: https://github.com/eggjs/egg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
'use strict'; | ||
|
||
module.exports = require('./lib/base_loader'); | ||
module.exports = require('./lib/egg_loader'); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
views