Skip to content

Commit cdd8c76

Browse files
committed
修改加载模块机制
1 parent 5358b98 commit cdd8c76

File tree

19 files changed

+893
-25
lines changed

19 files changed

+893
-25
lines changed

lib/bear.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ class Bear extends KoaApplication {
1818
this._options = options
1919
this.console = console
2020

21-
const Loader = this[BEAR_LOADER]
22-
assert(Loader, 'Symbol.for(\'bear#loader\') is required')
21+
const loader = this[BEAR_LOADER]
22+
assert(loader, 'Symbol.for(\'bear#loader\') is required')
2323

24-
this.loader = new Loader({
24+
this.loader = loader({
2525
baseDir: options.baseDir,
2626
app: this,
2727
plugins: options.plugins,
@@ -44,7 +44,7 @@ class Bear extends KoaApplication {
4444
}
4545

4646
get [BEAR_LOADER] () {
47-
return require('./loader/bear_loader')
47+
return require('./loader')
4848
}
4949
}
5050

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const assert = require('assert')
22
const debug = require('debug')('bear-core')
33
const ContextLoader = require('./context_loader')
4+
const FileLoader = require('./file_loader')
45
const fs = require('fs')
56

67
class BearLoader {
@@ -22,14 +23,16 @@ class BearLoader {
2223
}, opt)
2324
new ContextLoader(opt).load()
2425
}
25-
}
26-
27-
const loaders = [
28-
require('../mixin/service')
29-
]
3026

31-
for (const loader of loaders) {
32-
Object.assign(BearLoader.prototype, loader)
27+
loadToApp (directory, property, opt) {
28+
const target = this.app[property] = {}
29+
opt = Object.assign({}, {
30+
directory,
31+
target,
32+
inject: this.app
33+
}, opt)
34+
new FileLoader(opt).load()
35+
}
3336
}
3437

3538
module.exports = BearLoader
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class ClassLoader {
88
assert(options.ctx, 'options.ctx is required')
99
const properties = options.properties
1010
this.cache = new Map()
11-
this.ctx = options.ctx
11+
this.ctx = options.ctxw
1212

1313
for (const property in properties) {
1414
this.defineProperty(property, properties[property])
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class FileLoader extends FileProcess {
8484
// app/service/foo/bar.js => service.foo.bar
8585
const pathName = directory.split(this.sep).slice(-1) + '.' + fInfo.pathName
8686
// get exports from the file
87-
const exports = this.getExports(fullpath, this.options)
87+
const exports = this.getExports(fullpath, this.options, pathName)
8888

8989
// ignore exports when it's null or false returned by filter function
9090
if (exports == null || (filter && filter(exports) === false)) continue
@@ -105,3 +105,12 @@ class FileLoader extends FileProcess {
105105
}
106106

107107
module.exports = FileLoader
108+
109+
//let files = new FileLoader({directory: path.join(__dirname, '../'), target: {}})
110+
//
111+
// console.dir(files.parse())
112+
// console.dir(files.parse()[1])
113+
// console.dir(files.parse()[1].exports.prototype.pathName)
114+
115+
// console.log('==')
116+
// console.log(files.load())
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,14 @@ class FileProcess {
7070
}
7171
}
7272

73-
getExports (fullpath, {call, inject}) {
73+
getExports (fullpath, {initializer, call, inject}, pathName) {
7474
let exports = this.loadModel(fullpath)
7575

76+
// process exports as you like
77+
if (initializer) {
78+
exports = initializer(exports, { path: fullpath, pathName })
79+
}
80+
7681
// return exports when it's a class or generator
7782
//
7883
// module.exports = class Service {};

lib/loader/index.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const mods = require('./modules')
2+
const BearLoader = require('./common/bear_loader')
3+
const LOADER_CACHE = Symbol('LoaderCache')
4+
const BEAR_LOADER = Symbol('BearLoader')
5+
6+
module.exports = function (options) {
7+
if (!this[BEAR_LOADER]) {
8+
this[BEAR_LOADER] = new BearLoader(options)
9+
}
10+
11+
if (!this[LOADER_CACHE]) {
12+
this[LOADER_CACHE] = new Map()
13+
}
14+
15+
const self = this
16+
17+
return new Proxy(mods, {
18+
get (target, propKey) {
19+
if (self[LOADER_CACHE].has(propKey)) {
20+
return self[LOADER_CACHE].get(propKey)
21+
}
22+
23+
if (!Reflect.has(target, propKey)) {
24+
throw new TypeError(`not find \`${propKey}\` modules!`)
25+
}
26+
27+
const ModuleClass = Reflect.get(target, propKey)
28+
const module = new ModuleClass({bearLoader: self[BEAR_LOADER]})
29+
30+
self[LOADER_CACHE].set(propKey, module)
31+
return module
32+
}
33+
})
34+
}

lib/loader/modules/base.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const assert = require('assert')
2+
3+
class Base {
4+
constructor (options = {}) {
5+
assert(options.bearLoader, 'options.bearLoader is required')
6+
7+
this.bearLoader = options.bearLoader
8+
this.options = Object.assign({}, this.bearLoader.options, options)
9+
}
10+
11+
load () {
12+
throw new Error('load function must be overwritten!')
13+
}
14+
}
15+
16+
module.exports = Base

lib/loader/modules/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('require-directory')(module)

lib/loader/modules/service.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const Base = require('./base')
2+
const path = require('path')
3+
4+
class Service extends Base {
5+
load (opt) {
6+
// 载入到 app.serviceClasses
7+
opt = Object.assign({
8+
call: true,
9+
caseStyle: 'lower',
10+
fieldClass: 'serviceClasses',
11+
directory: path.join(this.options.baseDir, 'app/service')
12+
}, opt)
13+
14+
const servicePaths = opt.directory
15+
16+
this.bearLoader.loadToContext(servicePaths, 'service', opt)
17+
}
18+
}
19+
20+
module.exports = Service

lib_bak/bear.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const assert = require('assert')
2+
const debug = require('debug')('bear-core')
3+
const fs = require('fs')
4+
const isGeneratorFunction = require('is-generator-function')
5+
const convert = require('koa-convert')
6+
const KoaApplication = require('koa')
7+
const BEAR_LOADER = Symbol.for('bear#loader')
8+
9+
class Bear extends KoaApplication {
10+
constructor (options = {}) {
11+
options.baseDir = options.baseDir || process.cwd()
12+
13+
assert(typeof options.baseDir === 'string', 'options.baseDir required, and must be a string')
14+
assert(fs.existsSync(options.baseDir), `Directory ${options.baseDir} not exists`)
15+
assert(fs.statSync(options.baseDir).isDirectory(), `Directory ${options.baseDir} is not a directory`)
16+
super()
17+
18+
this._options = options
19+
this.console = console
20+
21+
const Loader = this[BEAR_LOADER]
22+
assert(Loader, 'Symbol.for(\'bear#loader\') is required')
23+
24+
this.loader = new Loader({
25+
baseDir: options.baseDir,
26+
app: this,
27+
plugins: options.plugins,
28+
logger: this.console
29+
})
30+
}
31+
32+
use (fn) {
33+
if (typeof fn !== 'function') throw new TypeError('middleware must be a function!')
34+
if (isGeneratorFunction(fn)) {
35+
fn = convert(fn)
36+
}
37+
debug('use %s', fn._name || fn.name || '-')
38+
this.middleware.push(fn)
39+
return this
40+
}
41+
42+
get baseDir () {
43+
return this._options.baseDir
44+
}
45+
46+
get [BEAR_LOADER] () {
47+
return require('./loader/bear_loader')
48+
}
49+
}
50+
51+
module.exports = Bear

0 commit comments

Comments
 (0)