UNPKG

@lcap/builder

Version:
343 lines (342 loc) 14.1 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const path_1 = __importDefault(require("path")); const fs_extra_1 = __importDefault(require("fs-extra")); const fast_glob_1 = __importDefault(require("fast-glob")); const lodash_1 = require("lodash"); const gen_nasl_component_config_1 = __importStar(require("./gen-nasl-component-config")); const gen_nasl_logics_config_1 = __importDefault(require("./gen-nasl-logics-config")); const constants_1 = require("../../overload/constants"); const logger_1 = __importDefault(require("../../utils/logger")); const getFrameWorkKind = (pkgInfo) => { if (pkgInfo.peerDependencies && Object.keys(pkgInfo.peerDependencies).includes('@tarojs/taro')) { return 'taro'; } if (pkgInfo.peerDependencies && Object.keys(pkgInfo.peerDependencies).includes('react')) { return 'react'; } if (pkgInfo.peerDependencies && pkgInfo.peerDependencies.vue && (pkgInfo.peerDependencies.vue.startsWith('3.') || pkgInfo.peerDependencies.vue.startsWith('^3.'))) { return 'vue3'; } if (pkgInfo.peerDependencies && pkgInfo.peerDependencies.vue && (pkgInfo.peerDependencies.vue.startsWith('2.') || pkgInfo.peerDependencies.vue.startsWith('^2.'))) { return 'vue2'; } return ''; }; function getPeerDependencies(pkgInfo) { if (!pkgInfo.peerDependencies) { return []; } return Object.keys(pkgInfo.peerDependencies).map((name) => { return { name, version: pkgInfo.peerDependencies[name], }; }); } function getFrontEndLibray(frameworkKind, frameworkUI, viewComponents, logics) { const pcFeLibrary = { concept: 'FrontendLibrary', name: 'pc', type: 'pc', frameworkKind, frameworkUI, viewComponents: [], logics: [], }; const mobileFeLibrary = { concept: 'FrontendLibrary', name: 'h5', type: 'h5', frameworkKind, viewComponents: [], logics: [], }; viewComponents.forEach((c) => { const typeKind = ['both', 'pc', 'h5'].indexOf(c.type) !== -1 ? c.type : 'pc'; if (typeKind === 'pc') { pcFeLibrary.viewComponents.push(c); } else if (typeKind === 'h5') { mobileFeLibrary.viewComponents.push(c); } else { pcFeLibrary.viewComponents.push(c); mobileFeLibrary.viewComponents.push(c); } }); logics.forEach((c) => { const typeKind = ['both', 'pc', 'h5'].indexOf(c.type) !== -1 ? c.type : 'pc'; if (typeKind === 'pc') { pcFeLibrary.logics.push(c); } else if (typeKind === 'h5') { mobileFeLibrary.logics.push(c); } else { pcFeLibrary.logics.push(c); mobileFeLibrary.logics.push(c); } }); return [pcFeLibrary, mobileFeLibrary].filter((library) => { return library.viewComponents.length > 0 || library.logics.length > 0; }); } function getConfiguration(rootPath) { const configurationPath = path_1.default.resolve(rootPath, 'src/settings', 'configuration.json'); let properties = []; try { const isExists = fs_extra_1.default.existsSync(configurationPath); if (!isExists) { return undefined; } const configurationList = fs_extra_1.default.readJSONSync(configurationPath); properties = configurationList.map((item) => { item.concept = 'ConfigProperty'; return item; }) || []; return { concept: 'Configuration', groups: [ { name: 'custom', concept: 'ConfigGroup', properties, }, ], }; } catch (error) { logger_1.default.error('configuration格式不对,请检查'); } return undefined; } /* 获取数据类型 */ function formatTypeAnnotation(type, pkg) { const typeAnnotation = { concept: 'TypeAnnotation', }; const resultArr = /(\[([\s\S]*)\])/.exec(type); const resultMap = /(<([\s\S]*,[\s\S]*)>)/.exec(type); if (resultArr) { typeAnnotation.typeName = 'List'; typeAnnotation.typeKind = 'generic'; typeAnnotation.typeNamespace = 'nasl.collection'; typeAnnotation.typeArguments = []; typeAnnotation.typeArguments[0] = formatTypeAnnotation(resultArr[2], pkg); } else if (resultMap) { const resultMapList = resultMap[2].split(','); typeAnnotation.typeName = 'Map'; typeAnnotation.typeKind = 'generic'; typeAnnotation.typeNamespace = 'nasl.collection'; typeAnnotation.typeArguments = []; typeAnnotation.typeArguments[0] = formatTypeAnnotation(resultMapList[0], pkg); typeAnnotation.typeArguments[1] = formatTypeAnnotation(resultMapList[1], pkg); } else { const baseTypeList = ['Long', 'Boolean', 'Decimal', 'String', 'Date', 'DateTime', 'Time']; const targeType = (0, lodash_1.capitalize)(type); if (baseTypeList.includes(targeType)) { typeAnnotation.typeKind = 'primitive'; typeAnnotation.typeName = targeType; typeAnnotation.typeNamespace = 'nasl.core'; } else { typeAnnotation.typeKind = 'reference'; typeAnnotation.typeName = targeType; typeAnnotation.typeNamespace = `extensions.${pkg.name}.structures`; } } return typeAnnotation; } function getStructures(rootPath, pkg) { const configFilePath = path_1.default.resolve(rootPath, 'src/settings', 'structures.json'); const isExists = fs_extra_1.default.existsSync(configFilePath); if (!isExists) { return undefined; } try { const resultList = []; const structureList = fs_extra_1.default.readJSONSync(configFilePath); Object.keys(structureList).forEach((item) => { const resultObj = {}; resultObj.name = item; const structureData = structureList[item]; const resultStructureList = Object.keys(structureData).map((it) => { const structureOpions = structureData[it]; if (it === '__description') { resultObj.description = structureOpions; return null; } const list = structureOpions.split('-'); const obj = { title: list[1], name: it, defaultValue: list[2] || undefined, typeAnnotation: formatTypeAnnotation(list[0], pkg), }; return obj; }); resultObj.properties = resultStructureList.filter((item) => !!item); resultList.push(resultObj); }); return resultList; } catch (error) { logger_1.default.error('structures格式不对,请检查'); } return undefined; } function getAnnotations(rootPath) { const configFilePath = path_1.default.resolve(rootPath, 'src/settings', 'annotations.json'); const isExists = fs_extra_1.default.existsSync(configFilePath); if (!isExists) { return undefined; } try { const annotationList = fs_extra_1.default.readJSONSync(configFilePath); return annotationList.map((item) => { var _a, _b; return (Object.assign(Object.assign({}, item), { concept: (_a = item.concept) !== null && _a !== void 0 ? _a : 'Annotation', applyTo: (_b = item.applyTo) !== null && _b !== void 0 ? _b : ['ViewElement'] })); }); } catch (e) { logger_1.default.error('annotations格式不对,请检查'); } return undefined; } function getNaslExtensionConfig({ rootPath, assetsPublicPath, framework, i18n, frameworkUI, }) { return __awaiter(this, void 0, void 0, function* () { const componentPath = 'src/components'; const pkgInfo = fs_extra_1.default.readJSONSync(path_1.default.join(rootPath, 'package.json')); const frameworkKind = framework || getFrameWorkKind(pkgInfo); const libInfo = [pkgInfo.name, '@', pkgInfo.version].join(''); const viewComponents = fast_glob_1.default.sync(`${componentPath}/**/api.ts`, { cwd: rootPath, absolute: true }).map((tsPath) => { const componentConfig = (0, gen_nasl_component_config_1.default)({ apiPath: tsPath, assetsPublicPath, rootPath, libInfo, framework, }); const projectAssetPath = 'assets'; if (componentConfig.icon && componentConfig.icon.indexOf('.') !== -1 && fs_extra_1.default.existsSync(path_1.default.join(rootPath, projectAssetPath, componentConfig.icon))) { componentConfig.icon = `${assetsPublicPath}/${libInfo}/${projectAssetPath}/${componentConfig.icon}`; } if (i18n) { const componentDir = path_1.default.resolve(tsPath, '../'); const i18nFiles = fast_glob_1.default.sync('i18n/*.json', { cwd: componentDir, absolute: true }); componentConfig.i18nMap = {}; i18nFiles.forEach((i18nFilePath) => { const map = fs_extra_1.default.readJSONSync(i18nFilePath, 'utf-8'); const key = path_1.default.basename(i18nFilePath, '.json'); componentConfig.i18nMap[key] = map || {}; }); } return componentConfig; }); const lcapUIConfigPath = path_1.default.resolve(rootPath, constants_1.LCAP_UI_CONFIG_PATH); let lcapUIComponents = []; if (fs_extra_1.default.existsSync(lcapUIConfigPath)) { try { lcapUIComponents = fs_extra_1.default.readJSONSync(lcapUIConfigPath); } catch (e) { console.error(e); } } (0, gen_nasl_component_config_1.processComponentConfigExtends)(viewComponents, lcapUIComponents); const logics = yield (0, gen_nasl_logics_config_1.default)(rootPath); const feLibraries = getFrontEndLibray(frameworkKind, frameworkUI, viewComponents, logics); const configuration = getConfiguration(rootPath); const structures = getStructures(rootPath, pkgInfo); const annotations = getAnnotations(rootPath); return { config: { name: pkgInfo.name, title: pkgInfo.title, description: pkgInfo.description, specVersion: '1.0.0', type: 'module', subType: 'extension', version: pkgInfo.version, frontends: feLibraries, configuration, structures, annotations, externalDependencyMap: { npm: getPeerDependencies(pkgInfo), }, summary: { name: pkgInfo.name, title: pkgInfo.title, version: pkgInfo.version, description: pkgInfo.description, frontends: feLibraries.map((library) => ({ type: library.type, frameworkKind, frameworkUI, viewComponents: library.viewComponents.map((item) => ({ name: item.name, title: item.title, })), logics: library.logics.map((item) => ({ name: item.name, description: item.title || item.description || item.name, })), })), }, compilerInfoMap: { js: { prefix: 'extension', }, }, ideVersion: pkgInfo.lcapIdeVersion || '3.8', }, viewComponents, }; }); } exports.default = getNaslExtensionConfig;