@lcap/nasl
Version:
NetEase Application Specific Language
177 lines • 6.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadDependencyUIDeclaration = exports.getDepJSONSchema = exports.getDepMainfiest = void 0;
const lodash_1 = require("lodash");
const format_1 = require("./format");
const utils_1 = require("./utils");
const manifest_1 = require("./manifest");
function getDepMainfiest(dep) {
const defaultManifest = {
nasl: [],
runtime: ['dist-theme/index.js'],
theme: [],
package: [],
i18n: [],
ide: [],
};
if (!dep || !dep.compilerInfoMap || !dep.compilerInfoMap['manifest']) {
return defaultManifest;
}
try {
const manifest = JSON.parse(dep.compilerInfoMap['manifest']);
return manifest;
}
catch (e) {
return defaultManifest;
}
}
exports.getDepMainfiest = getDepMainfiest;
const getDepJSONSchema = async (dep, axios, appInfo) => {
if (!dep.compilerInfoMap || !dep.compilerInfoMap.manifest) {
return dep;
}
const params = {};
if (dep.provider === '资产中心-平台') {
params.tenantId = appInfo.tenantID;
}
try {
const response = await axios.get(`/api/v1/asset-center/library/${dep.name}/version/${dep.version}`, {
params,
});
let json;
if (response.data.result && response.data.result.jsonSchema) {
json = JSON.parse(response.data.result.jsonSchema);
}
else {
json = await (0, utils_1.fetchFile)(`${appInfo.STATIC_URL}${manifest_1.DEPENDENCY_PREFIX}/${dep.name}@${dep.version}/nasl.extension.json`, axios);
if (!json) {
return dep;
}
}
return json;
}
catch (e) {
console.error(e);
return dep;
}
};
exports.getDepJSONSchema = getDepJSONSchema;
const REPLACE_STATIC_URL = 'https://static-vusion.163yun.com';
const getNewComponent = (component, config, appInfo, componentApi) => {
const useName = config.frameworkKind && config.frameworkKind.startsWith('vue') ? (item) => item.kebabName || (0, lodash_1.kebabCase)(item.name) : (item) => item.name;
const { version, type, hasTypeDeclaration } = config;
const _component = component.module ? component?.toJSON() : component;
_component.componentName = (0, format_1.kebab2Camel)(_component.name);
_component.name = useName(_component);
_component.version = version;
_component.tagName = _component.name;
_component.frontendType = type || 'pc';
// 兼容attrs
_component.props = _component.props || _component.attrs || [];
_component.props.forEach((prop) => {
prop.name = (0, format_1.attrKebab2Camel)(prop.name);
if (prop['designer-value']) {
prop.designerValue = prop['designer-value'];
delete prop['designer-value'];
}
(0, format_1.formatCustomSetter)(prop);
});
if (!_component.blocks || _component.blocks.length === 0) {
_component.blocks = [{
title: '默认',
code: `<template><${_component.tagName}></${_component.tagName}></template>`,
}];
}
else if (_component.blocks.length > 1) {
let blocks = _component.blocks.filter((b) => b.screenshot || b.drawing);
if (blocks.length === 0) {
blocks = [_component.blocks[0]];
}
_component.blocks = blocks;
}
_component.hasTypeDeclaration = hasTypeDeclaration;
_component.package = config;
(0, format_1.formatPropsMap)(_component, 'readableProps', { frameworkKind: config.frameworkKind });
(0, format_1.formatPropsMap)(_component, 'props', { frameworkKind: config.frameworkKind });
componentApi[_component.name] = _component;
const { children } = _component;
if (Array.isArray(children) && children.length > 0) {
children.forEach((child) => {
child.package = config;
(0, format_1.formatSub)(child, componentApi, useName);
});
}
return {
concept: _component.concept,
name: _component.name,
title: _component.title,
show: _component.show !== false,
icon: _component.icon && _component.icon.startsWith(REPLACE_STATIC_URL) ? _component.icon.replace(REPLACE_STATIC_URL, appInfo.STATIC_URL) : _component.icon,
category: _component.category || _component.group || (config.name ? `${config.name}(${config.title})` : '其他'),
tagName: _component.name,
componentName: _component.componentName,
namespace: `extensions.${config.name}.viewComponents`,
screenshots: [],
drawings: [],
hasTypeDeclaration,
frontendType: _component.frontendType,
libInfo: `${config.name}@${config.version}`,
blocks: _component.blocks,
i18nMap: _component.i18nMap,
themes: _component.themes,
ideusage: _component.ideusage,
};
};
const loadLibraryTypeTs = async (dep, appInfo, manifest, axios) => {
const tsFileName = 'nasl.extension.d.ts';
const fetchURL = (0, manifest_1.getFetchURL)('nasl', tsFileName, {
name: dep.name,
version: dep.version,
manifest: manifest,
extension: true,
staticURL: appInfo.STATIC_URL,
});
if (!fetchURL) {
return '';
}
try {
const text = await (0, utils_1.fetchFile)(fetchURL, axios, true);
if (text) {
let code = text.replace(/\/\/\/\s<reference\s.+?\/>/g, '');
return code;
}
return '';
}
catch (error) {
return '';
}
};
async function loadDependencyUIDeclaration({ axios, appInfo }, dep) {
const manifest = getDepMainfiest(dep);
const [tsCode, depSchema] = await Promise.all([
loadLibraryTypeTs(dep, appInfo, manifest, axios),
(0, exports.getDepJSONSchema)(dep, axios, appInfo),
]);
const nodesAPI = {};
const components = [];
(depSchema.frontends || []).forEach((frontend) => {
const { frameworkKind } = frontend;
(frontend.viewComponents || []).forEach((component) => {
components.push(getNewComponent(component, {
title: dep.title,
name: dep.name,
version: dep.version,
type: frontend.type,
hasTypeDeclaration: !!tsCode,
frameworkKind,
}, appInfo, nodesAPI));
});
});
return {
allNodesAPI: nodesAPI,
componentList: components,
tsCode,
};
}
exports.loadDependencyUIDeclaration = loadDependencyUIDeclaration;
//# sourceMappingURL=dependency.js.map