-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: implement Loader instead of loading (#4)
- Loading branch information
Showing
44 changed files
with
573 additions
and
20 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,135 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const fs = require('fs'); | ||
const debug = require('debug')('egg-loader:loader'); | ||
const path = require('path'); | ||
const globby = require('globby'); | ||
const interopRequire = require('interop-require'); | ||
const is = require('is-type-of'); | ||
const FULLPATH = Symbol('EGG_LOADER_ITEM_FULLPATH'); | ||
|
||
const defaults = { | ||
directory: null, | ||
target: null, | ||
ignore: undefined, | ||
lowercaseFirst: false, | ||
initializer: null, | ||
call: true, | ||
override: false, | ||
inject: undefined, | ||
}; | ||
|
||
class Loader { | ||
|
||
constructor(options) { | ||
assert(options.directory, 'options.directory is required'); | ||
assert(options.target, 'options.target is required'); | ||
this.options = Object.assign({}, defaults, options); | ||
} | ||
|
||
load() { | ||
const items = this.parse(); | ||
const target = this.options.target; | ||
for (const item of items) { | ||
debug('loading item %j', item); | ||
item.properties.reduce((target, property, index) => { | ||
let obj; | ||
const properties = item.properties.slice(0, index + 1).join('.'); | ||
if (index === item.properties.length - 1) { | ||
if (property in target) { | ||
if (!this.options.override) throw new Error(`can't overwrite property '${properties}' from ${target[property][FULLPATH]} by ${item.fullpath}`); | ||
} | ||
obj = item.exports; | ||
if (obj) obj[FULLPATH] = item.fullpath; | ||
} else { | ||
obj = target[property] || {}; | ||
} | ||
target[property] = obj; | ||
debug('loaded %s', properties); | ||
return obj; | ||
}, target); | ||
} | ||
return target; | ||
} | ||
|
||
parse() { | ||
const files = [ '**/*.js' ]; | ||
if (typeof this.options.ignore === 'string') { | ||
files.push('!' + this.options.ignore); | ||
} | ||
|
||
let directories = this.options.directory; | ||
if (!Array.isArray(directories)) { | ||
directories = [ directories ]; | ||
} | ||
|
||
const items = []; | ||
debug('parsing %j', directories); | ||
for (const directory of directories) { | ||
const filepaths = globby.sync(files, { cwd: directory }); | ||
for (const filepath of filepaths) { | ||
const fullpath = path.join(directory, filepath); | ||
if (!fs.statSync(fullpath).isFile()) { | ||
continue; | ||
} | ||
const properties = getProperties(filepath, this.options.lowercaseFirst); | ||
const exports = getExports(fullpath, this.options.initializer, this.options.call, this.options.inject); | ||
if (exports == null) continue; | ||
items.push({ fullpath, properties, exports }); | ||
debug('parse %s, properties %j, export %j', fullpath, properties, exports); | ||
} | ||
} | ||
|
||
return items; | ||
} | ||
|
||
} | ||
|
||
module.exports = Loader; | ||
|
||
// a/b/c.js => ['a', 'b', 'c'] | ||
function getProperties(filepath, lowercaseFirst) { | ||
return filepath | ||
.replace('.js', '') | ||
.split('/') | ||
.map(function(property) { | ||
if (!/^[a-z][a-z0-9_-]*$/i.test(property)) { | ||
throw new Error(`${property} is not match 'a-z0-9_-' in ${filepath}`); | ||
} | ||
let result = property.replace(/[_-][a-z]/ig, function(s) { | ||
return s.substring(1).toUpperCase(); | ||
}); | ||
if (lowercaseFirst) { | ||
result = result[0].toLowerCase() + result.substring(1); | ||
} | ||
return result; | ||
}); | ||
} | ||
|
||
function getExports(fullpath, initializer, isCall, inject) { | ||
let exports; | ||
try { | ||
exports = interopRequire(fullpath); | ||
} catch (err) { | ||
err.message = 'load file: ' + fullpath + ', error: ' + err.message; | ||
throw err; | ||
} | ||
|
||
if (initializer) { | ||
exports = initializer(exports); | ||
} | ||
|
||
if (is.class(exports) || is.generatorFunction(exports)) { | ||
return exports; | ||
} | ||
|
||
if (isCall && is.function(exports)) { | ||
exports = exports(inject); | ||
if (exports != null) { | ||
return exports; | ||
} | ||
} | ||
|
||
return exports; | ||
} |
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict'; | ||
|
||
var _temporalUndefined = {}; | ||
|
||
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); | ||
|
||
var UserProxy = _temporalUndefined; | ||
|
||
function _temporalAssertDefined(val, name, undef) { if (val === undef) { throw new ReferenceError(name + ' is not defined - temporal dead zone'); } return true; } | ||
|
||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } | ||
|
||
UserProxy = (function () { | ||
function UserProxy() { | ||
_classCallCheck(this, _temporalAssertDefined(UserProxy, 'UserProxy', _temporalUndefined) && UserProxy); | ||
|
||
this.user = { | ||
name: 'xiaochen.gaoxc' | ||
}; | ||
} | ||
|
||
_createClass(_temporalAssertDefined(UserProxy, 'UserProxy', _temporalUndefined) && UserProxy, [{ | ||
key: 'getUser', | ||
value: function getUser() { | ||
return this.user; | ||
} | ||
}]); | ||
|
||
return _temporalAssertDefined(UserProxy, 'UserProxy', _temporalUndefined) && UserProxy; | ||
})(); | ||
|
||
module.exports = _temporalAssertDefined(UserProxy, 'UserProxy', _temporalUndefined) && UserProxy; |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict'; | ||
|
||
class UserProxy { | ||
constructor() { | ||
this.user = { | ||
name: 'xiaochen.gaoxc', | ||
}; | ||
} | ||
|
||
getUser() { | ||
return this.user; | ||
} | ||
} | ||
|
||
module.exports = UserProxy; |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use strict'; | ||
|
||
module.exports = class TestClass { | ||
constructor() { | ||
this.user = { | ||
name: 'kai.fangk', | ||
}; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use strict'; | ||
|
||
module.exports = function(obj) { | ||
return { | ||
user: { | ||
name: 'kai.fangk', | ||
}, | ||
}; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
module.exports = function(obj) { | ||
return function() { | ||
return { | ||
user: { | ||
name: 'kai.fangk', | ||
}, | ||
} | ||
}; | ||
}; |
Empty file.
Empty file.
Empty file.
Empty file.
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = function() { | ||
return { a: 1 }; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = function() { | ||
return { a: 1 }; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
method1: function() { | ||
} | ||
}; |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = { b: 2 }; |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
exports.getByName = function (name, callback) { | ||
setTimeout(function () { | ||
callback(null, {name: name}); | ||
}, 1); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
exports.getByName = function (name, callback) { | ||
setTimeout(function () { | ||
callback(null, {name: name}); | ||
}, 1); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = function () {}; |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = function () {}; |
Empty file.
Empty file.
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = function () {}; |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = function () {}; |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = function () {}; |
Oops, something went wrong.