Comparing version 0.5.1 to 0.5.2
@@ -6,3 +6,3 @@ "use strict"; | ||
}); | ||
exports._data = exports._methods = exports._command = exports._config = exports._mount = exports._hooks = exports.$use = undefined; | ||
exports._mount = exports._hooks = exports.$use = undefined; | ||
@@ -39,3 +39,3 @@ var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
var rootHookDepth = (0, _utils.getRootHookDepth)(depth); | ||
var rootHookDepth = depth + "._"; | ||
var hooks = (0, _lodash.get)(_.hooks, rootHookDepth, []); | ||
@@ -50,3 +50,3 @@ hooks.length > 0 && hooks.forEach(function (h) { | ||
if ((typeof parms === "undefined" ? "undefined" : _typeof(parms)) === "object") { | ||
onObject({ object: parms }); | ||
onObject({ object: parms, depth: "" }); | ||
} | ||
@@ -82,3 +82,3 @@ return _; | ||
if ((typeof _val === "undefined" ? "undefined" : _typeof(_val)) === "object") { | ||
onObject({ object: _val }); | ||
onObject({ object: _val, depth: "" }); | ||
} | ||
@@ -145,140 +145,2 @@ }; | ||
} | ||
}; | ||
var _config = exports._config = { | ||
_: function _(_ref11) { | ||
var _ = _ref11._, | ||
_key = _ref11._key, | ||
_val = _ref11._val, | ||
cp = _ref11.cp; | ||
_.config = (0, _utils.ConfigMerger)(_.config, _val); | ||
} | ||
}; | ||
var _command = exports._command = { | ||
_: function _(_ref12) { | ||
var _this = this; | ||
var _ = _ref12._, | ||
_key = _ref12._key, | ||
_val = _ref12._val, | ||
cp = _ref12.cp; | ||
var start = _val.start; | ||
if (start) { | ||
var _$config$lifeCycle = _.config.lifeCycle, | ||
lifeCycle = _$config$lifeCycle === undefined ? [] : _$config$lifeCycle; | ||
Object.values(_.cps).forEach(function () { | ||
var _ref13 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(cp) { | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
_context2.next = 2; | ||
return lifeCycle.forEach(function (key) { | ||
cp[key] && cp[key](); | ||
}); | ||
case 2: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2, _this); | ||
})); | ||
return function (_x2) { | ||
return _ref13.apply(this, arguments); | ||
}; | ||
}()); | ||
} | ||
} | ||
}; | ||
var _methods = exports._methods = { | ||
_: function _(_ref14) { | ||
var _ = _ref14._, | ||
_key = _ref14._key, | ||
_val = _ref14._val, | ||
cp = _ref14.cp; | ||
var _iteratorNormalCompletion2 = true; | ||
var _didIteratorError2 = false; | ||
var _iteratorError2 = undefined; | ||
try { | ||
for (var _iterator2 = Object.entries(_val)[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { | ||
var _ref15 = _step2.value; | ||
var _ref16 = _slicedToArray(_ref15, 2); | ||
var key = _ref16[0]; | ||
var val = _ref16[1]; | ||
if (!cp[key]) { | ||
cp[key] = val.bind(cp); | ||
} else { | ||
console.warn(cp.name + " : \"" + key + "\" in methods is valid, Please use another one"); | ||
} | ||
} | ||
} catch (err) { | ||
_didIteratorError2 = true; | ||
_iteratorError2 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion2 && _iterator2.return) { | ||
_iterator2.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError2) { | ||
throw _iteratorError2; | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
var _data = exports._data = function _data(_ref17) { | ||
var _ = _ref17._, | ||
_key = _ref17._key, | ||
_val = _ref17._val, | ||
cp = _ref17.cp; | ||
var _iteratorNormalCompletion3 = true; | ||
var _didIteratorError3 = false; | ||
var _iteratorError3 = undefined; | ||
try { | ||
for (var _iterator3 = Object.entries(_val.bind(cp)())[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) { | ||
var _ref18 = _step3.value; | ||
var _ref19 = _slicedToArray(_ref18, 2); | ||
var key = _ref19[0]; | ||
var val = _ref19[1]; | ||
if (!cp[key]) { | ||
if (typeof val === "function") { | ||
cp[key] = val.bind(cp); | ||
} else { | ||
cp[key] = val; | ||
} | ||
} else { | ||
console.warn(cp.name + " : \"" + key + " \" is data is valid, Please use another one"); | ||
} | ||
} | ||
} catch (err) { | ||
_didIteratorError3 = true; | ||
_iteratorError3 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion3 && _iterator3.return) { | ||
_iterator3.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError3) { | ||
throw _iteratorError3; | ||
} | ||
} | ||
} | ||
}; |
@@ -11,31 +11,13 @@ "use strict"; | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var scanObject = exports.scanObject = function scanObject(_ref) { | ||
var object = _ref.object, | ||
_ref$depth = _ref.depth, | ||
depth = _ref$depth === undefined ? null : _ref$depth, | ||
_ref$onObject = _ref.onObject, | ||
onObject = _ref$onObject === undefined ? function () {} : _ref$onObject, | ||
_ref$onFunction = _ref.onFunction, | ||
onFunction = _ref$onFunction === undefined ? function () {} : _ref$onFunction, | ||
_ref$onVariable = _ref.onVariable, | ||
onVariable = _ref$onVariable === undefined ? function () {} : _ref$onVariable; | ||
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } | ||
var initConfig = { lifeCycle: ["awake", "start"] }; | ||
var ConfigMerger = function ConfigMerger() { | ||
var currentConfig = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initConfig; | ||
var _ref = arguments[1]; | ||
var lifeCycle = _ref.lifeCycle, | ||
other = _objectWithoutProperties(_ref, ["lifeCycle"]); | ||
return _extends({ | ||
lifeCycle: lifeCycle ? lifeCycle : currentConfig.lifeCycle | ||
}, other); | ||
}; | ||
exports.ConfigMerger = ConfigMerger; | ||
var scanObject = exports.scanObject = function scanObject(_ref2) { | ||
var object = _ref2.object, | ||
_ref2$depth = _ref2.depth, | ||
depth = _ref2$depth === undefined ? null : _ref2$depth, | ||
_ref2$onObject = _ref2.onObject, | ||
onObject = _ref2$onObject === undefined ? function () {} : _ref2$onObject, | ||
_ref2$onFunction = _ref2.onFunction, | ||
onFunction = _ref2$onFunction === undefined ? function () {} : _ref2$onFunction, | ||
_ref2$onVariable = _ref2.onVariable, | ||
onVariable = _ref2$onVariable === undefined ? function () {} : _ref2$onVariable; | ||
if (object) { | ||
@@ -48,8 +30,8 @@ var _iteratorNormalCompletion = true; | ||
for (var _iterator = Object.entries(object)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var _ref3 = _step.value; | ||
var _ref2 = _step.value; | ||
var _ref4 = _slicedToArray(_ref3, 2); | ||
var _ref3 = _slicedToArray(_ref2, 2); | ||
var _key = _ref4[0]; | ||
var _val = _ref4[1]; | ||
var _key = _ref3[0]; | ||
var _val = _ref3[1]; | ||
@@ -99,6 +81,2 @@ if (_val) { | ||
} | ||
}; | ||
var getRootHookDepth = exports.getRootHookDepth = function getRootHookDepth(depth) { | ||
return depth + "._"; | ||
}; |
{ | ||
"name": "menhera", | ||
"version": "0.5.1", | ||
"version": "0.5.2", | ||
"main": "dist", | ||
@@ -9,4 +9,3 @@ "files": [ | ||
"scripts": { | ||
"example": "babel-node ./example/index.js", | ||
"dev": "nodemon --exec npm run babel-node --watch ./ -- app.js", | ||
"example": "babel-node ./example/index", | ||
"build": "babel ./src --out-dir dist", | ||
@@ -16,13 +15,6 @@ "prepublishOnly": "npm run build" | ||
"devDependencies": { | ||
"@types/koa": "^2.0.44", | ||
"@types/koa-router": "^7.0.27", | ||
"@types/minimist": "^1.2.0", | ||
"babel-cli": "^6.26.0", | ||
"babel-preset-env": "^1.6.1", | ||
"babel-preset-stage-3": "^6.24.1", | ||
"koa": "^2.5.0", | ||
"koa-router": "^7.4.0", | ||
"menhera": "^0.4.5", | ||
"minimist": "^1.2.0", | ||
"nodemon": "^1.11.0" | ||
"menhera": "^0.4.5" | ||
}, | ||
@@ -29,0 +21,0 @@ "dependencies": { |
140
readme.md
@@ -5,2 +5,4 @@ # Menhera | ||
### Install | ||
```bash | ||
@@ -10,17 +12,27 @@ yarn add menhera | ||
### Example | ||
```js | ||
import Menhera from "menhera"; | ||
const test = ({ _val }) => console.log(_val); | ||
const _ = new Menhera({ | ||
_hooks: { | ||
data: { | ||
test1: test, | ||
test2: test | ||
foo: { | ||
bar: { | ||
foo1: { | ||
bar1: { | ||
test: ({ _val }) => console.log(_val) | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
data: { | ||
test1: "foo", | ||
test2: "bar" | ||
foo: { | ||
bar: { | ||
foo1: { | ||
bar1: { | ||
test: "foo bar" | ||
} | ||
} | ||
} | ||
} | ||
@@ -31,14 +43,73 @@ }); | ||
```js | ||
import Menhera from "menhera"; | ||
import { core } from "menhera"; | ||
const test = ({ _val }) => console.log(_val); | ||
class Foo { | ||
constructor() {} | ||
} | ||
const mount = { | ||
name: "mount", | ||
const Bar = parms => core({ _: new Foo(), parms }); | ||
const _ = new Bar({ | ||
_hooks: { | ||
data: { | ||
test1: test, | ||
test2: test | ||
foo: { | ||
bar: { | ||
foo1: { | ||
bar1: { | ||
test: ({ _val }) => console.log(_val) | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
foo: { | ||
bar: { | ||
foo1: { | ||
bar1: { | ||
test: "foo bar" | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
``` | ||
```js | ||
import koa from "koa"; | ||
import Router from "koa-router"; | ||
import Menhera from "menhera"; | ||
const app = { | ||
name: "app", | ||
app: new koa(), | ||
router: new Router(), | ||
_hooks: { | ||
koa: { | ||
data({ _, _key, _val, cp }) { | ||
for (let [key, val] of Object.entries(_val.bind(this)())) { | ||
if (!this[key]) { | ||
this[key] = val; | ||
} | ||
} | ||
}, | ||
controller({ _, _key, _val, cp }) { | ||
const controllers = _val(this); | ||
this.controllers = controllers; | ||
}, | ||
router({ _, _key, _val, cp }) { | ||
const { router } = this; | ||
const routers = _val(this); | ||
for (let [key, val] of Object.entries(routers)) { | ||
const [method, path] = key.split(" "); | ||
router[method](path, ctx => val(ctx)); | ||
} | ||
}, | ||
listen({ _, _key, _val, cp }) { | ||
const { app } = this; | ||
app.use(this.router.routes()); | ||
app.listen(_val, e => { | ||
console.log(`app running on port: ${_val}`); | ||
}); | ||
} | ||
} | ||
} | ||
}; | ||
@@ -48,10 +119,41 @@ | ||
_mount: { | ||
foo: [mount] | ||
foo: [app] | ||
}, | ||
koa: { | ||
data() { | ||
return { | ||
test: { index: 0, user: "" }, | ||
services: { | ||
getIndex: () => this.test.index, | ||
getUser: () => this.test.user | ||
} | ||
}; | ||
}, | ||
controller: ({ test, services: { getIndex, getUser } }) => ({ | ||
index: { | ||
getIndex(ctx) { | ||
test.index++; | ||
ctx.body = getIndex(); | ||
} | ||
}, | ||
user: { | ||
getUser(ctx) { | ||
test.user = Math.random() | ||
.toString(36) | ||
.replace(/[^a-z]+/g, "") | ||
.substr(0, 5); | ||
ctx.body = getUser(); | ||
} | ||
} | ||
}), | ||
router: ({ controllers: { index, user } }) => ({ | ||
"get /": index.getIndex, | ||
"get /user": user.getUser | ||
}) | ||
} | ||
}).$use({ | ||
data: { | ||
test1: "foo", | ||
test2: "bar" | ||
koa: { | ||
listen: 3000 | ||
} | ||
}); | ||
``` |
4
156
12979
233