plus-midwayjs
Version:
48 lines (47 loc) • 1.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PlusController = void 0;
const core_1 = require("@midwayjs/core");
const location_1 = require("../util/location");
function PlusController(prefix) {
return (target) => {
// 将装饰的类,绑定到该装饰器,用于后续能获取到 class
(0, core_1.saveModule)(core_1.CONTROLLER_KEY, target);
location_1.default.scriptPath(target).then(async (res) => {
const pathSps = res.path.split('.');
const paths = pathSps[pathSps.length - 2].split('/');
const pathArr = [];
let module = null;
for (const path of paths.reverse()) {
if (path != 'controller' && !module) {
pathArr.push(path);
}
if (path == 'controller' && !paths.includes('apps')) {
break;
}
if (path == 'controller' && paths.includes('apps')) {
module = 'ready';
}
if (module && path != 'controller') {
module = path;
break;
}
}
if (module) {
pathArr.splice(1, 0, module);
pathArr.reverse();
}
if (!prefix) {
prefix = `/${pathArr.join('/')}`;
}
saveMetadata(prefix, target, module);
});
};
}
exports.PlusController = PlusController;
const saveMetadata = (prefix, target, module) => {
(0, core_1.saveClassMetadata)(core_1.CONTROLLER_KEY, {
prefix,
module
}, target);
};