@lcap/nasl
Version:
NetEase Application Specific Language
114 lines (113 loc) • 4.49 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.genSimpleBundleFile = void 0;
const nasl_unified_frontend_generator_1 = require("@lcap/nasl-unified-frontend-generator");
const genBundleFiles_1 = require("./genBundleFiles");
const lodash_1 = require("lodash");
/**
* 只用于 NASL 沙箱简单打包用
*/
function genSimpleBundleFile(app, frontend, options) {
const views = frontend.views;
const routes = [];
const typeKeyMap = {};
const enumMap = {};
app.enums.forEach((enumItem) => {
enumMap['app.enums.' + enumItem.name] = true;
});
app.dataSources[0]?.entities.forEach((entity) => {
typeKeyMap[entity.name] = typeKeyMap[entity.name] || {};
entity.properties.forEach((property) => {
typeKeyMap[entity.name][property.name] = property.typeAnnotation?.typeKey;
});
});
let code = '';
function traverseView(view, parentPath = '') {
const isRootView = view.parentNode?.concept !== 'View';
let path = isRootView ? `${frontend.prefixPath}/${view.name}` : view.name;
let fullName = parentPath ? `${parentPath}_${view.name}` : view.name;
const options = view.toVueOptions();
options.template = options.template.replace(/(:text="\$utils\['ToString'\]\('undefined', \$utils\['EnumItemToText'\]\(current\?\.item\?\.(\w+)(?:\?\.(\w+))?)\)/g, (m, $1, $2, $3) => {
let typeKey = '';
if ($3) {
const entityName = (0, lodash_1.upperFirst)($2);
typeKey = typeKeyMap[entityName]?.[$3];
}
else {
typeKey = `app.enums.${(0, lodash_1.upperFirst)($2).replace(/\d+$/, '')}`;
}
if (typeKey && enumMap[typeKey])
return `${$1}, '${typeKey}')`;
return m;
}).replace(/('ListTransform'\]\(.+?, \(item\) => \(\$utils\['EnumItemToText'\]\(item\?\.(\w+)(?:\?\.(\w+))?)\)\)/g, (m, $1, $2, $3) => {
let typeKey = '';
if ($3) {
const entityName = (0, lodash_1.upperFirst)($2);
typeKey = typeKeyMap[entityName]?.[$3];
}
else {
typeKey = `app.enums.${(0, lodash_1.upperFirst)($2).replace(/\d+$/, '')}`;
}
if (typeKey && enumMap[typeKey])
return `${$1}, '${typeKey}'))`;
return m;
});
let viewCode = options.script.replace(/^export default \{/, `const ${fullName} = async () => ({
template: \`${options.template.replace(/[`$]/g, '\\$&')}\`,
`).replace(/};?\s*$/, `});`);
code += viewCode + '\n\n';
const children = [];
view.children.forEach((child) => {
children.push(traverseView(child, fullName));
// if (child.isIndex) {
// children.push({
// path: '',
// redirect: child.name,
// });
// }
});
return {
path,
component: `##${fullName}##`,
meta: view.getRouteMeta(),
children,
};
}
views.forEach((view) => {
routes.push(traverseView(view));
if (view.isIndex) {
routes.push({
path: frontend.prefixPath ? frontend.prefixPath : '/',
redirect: `${frontend.prefixPath}/${view.name}`,
});
}
});
routes.push({
path: '/:pathMatch(.*)*',
redirect: options?.notFoundRedirect || '/notFound',
});
code = code
.replace(/(?