UNPKG

@szzbmy/lowcode-cli

Version:

🐇 lowcode-cli is an efficient cli tool for Rabbitpre plugin component secondary development. ❤️

216 lines (215 loc) 7.46 kB
"use strict"; /* * @Author: chubiao Ni * @Date: 2022-02-17 14:25:40 * @Last Modified by: chubiao Ni * @Last Modified time: 2022-05-30 15:41:39 */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (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 __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getPackageConfig = exports.updateComponentConfig = exports.getComponentConfig = exports.updateJsoncDebugConfig = exports.validateSchema = exports.getDebugConfig = void 0; const lowcode_schema_1 = require("@szzbmy/lowcode-schema"); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const schema_1 = require("../apis/schema"); const _1 = __importStar(require(".")); const json_1 = require("../utils/json"); const logger_1 = __importDefault(require("../utils/logger")); let cmpConfigCache = null; /** * 获取组件/库debug配置 * @returns debugConfig */ async function getDebugConfig() { let debugConfig = {}; try { const configJsonc = getComponentConfig(); const { schemaType, schemaVersion = '', debug, bindPluginName, } = configJsonc; let { env, port, appid, shortUrl } = configJsonc; /** 组件or库 */ const isCmplib = schemaType === 'cmplib'; env = isCmplib ? debug.env : env; port = isCmplib ? debug.port : port; appid = isCmplib ? debug.appid : appid; shortUrl = isCmplib ? debug.shortUrl : shortUrl; const debugComponentList = isCmplib ? configJsonc.components.map((cmp) => ({ ...cmp, })) : [ { name: configJsonc.name, title: configJsonc.title, version: -1, }, ]; if (isCmplib) { // controller组件带上组件库名称 const controllerCmp = debugComponentList.find((cmp) => cmp.isControllerComponent === true); if (controllerCmp) { controllerCmp.key = configJsonc.key; controllerCmp.label = configJsonc.label; controllerCmp.desc = configJsonc.desc; } } debugConfig = { schemaType, schemaVersion, env, port, appid: appid || '', shortUrl: shortUrl || '', debugComponentList, bindPluginName, }; } catch (error) { logger_1.default.debug(`获取debugConfig配置失败:${error?.message}`); } return debugConfig; } exports.getDebugConfig = getDebugConfig; /** * 用schema校验config.jsonc文件 */ async function validateSchema() { try { const configJsonc = getComponentConfig(); const { schemaType, schemaVersion } = configJsonc; if (!schemaType || !schemaVersion) { (0, _1.updateConfig)({ debugConfig: { ..._1.default.debugConfig, isValid: false, validateErrorMessage: 'config.jsonc缺少参数:schemaType 或 schemaVersion', }, }); return; } const schemaRes = await (0, schema_1.fetchSchemaData)({ type: schemaType, version: schemaVersion, }); const { success: isValid, errorMsg: validateErrorMessage } = await (0, lowcode_schema_1.validate)({ schema: (0, json_1.parse)(schemaRes.schemaData || '{}'), data: configJsonc, }); // 更新config文件里的校验结果 (0, _1.updateConfig)({ debugConfig: { ..._1.default.debugConfig, isValid, validateErrorMessage, }, }); } catch (error) { logger_1.default.debug(`schema校验config.jsonc文件失败:${error?.message}`); } } exports.validateSchema = validateSchema; /** * 设置组件/库debug配置 */ function updateJsoncDebugConfig() { const { debugConfig: { schemaType, appid, shortUrl }, } = _1.default; const configJsonc = getComponentConfig(); let newConfigJsonc = {}; if (schemaType === 'cmplib') { newConfigJsonc = { ...configJsonc, debug: { ...configJsonc.debug, appid, shortUrl, }, }; } else { newConfigJsonc = { ...configJsonc, appid, shortUrl, }; } updateComponentConfig(newConfigJsonc); } exports.updateJsoncDebugConfig = updateJsoncDebugConfig; /** * 获取组件/库配置 * @returns cmpConfig */ function getComponentConfig() { if (cmpConfigCache) return cmpConfigCache; let cmpConfig = {}; try { /** 项目本地路径 */ const cwd = process.cwd(); const secDevConfigPath = path_1.default.resolve(cwd, './config.jsonc'); const cmpConfigJsonc = fs_1.default.readFileSync(secDevConfigPath, 'utf8'); cmpConfig = (0, json_1.parse)(cmpConfigJsonc); cmpConfigCache = cmpConfig; } catch (error) { logger_1.default.debug('找不到组件配置'); } return cmpConfig; } exports.getComponentConfig = getComponentConfig; /** * 更新组件配置 * @param cmpInfo 组件配置 * @param folderName 当前组件所在的文件夹 */ function updateComponentConfig(cmpInfo, folderName) { const { resolve } = path_1.default; const prefixPath = folderName || process.cwd(); const configPath = resolve(prefixPath, './config.jsonc'); const config = (0, json_1.parse)(fs_1.default.readFileSync(configPath, { encoding: 'utf8' })); const content = (0, json_1.stringify)(Object.assign(config, cmpInfo)); fs_1.default.writeFileSync(configPath, content); // 清楚缓存的旧配置 cmpConfigCache = null; } exports.updateComponentConfig = updateComponentConfig; /** * 获取package.json文件配置 * @returns packageConfig */ function getPackageConfig() { let packageConfig = {}; try { /** 项目本地路径 */ const cwd = process.cwd(); const secDevConfigPath = path_1.default.resolve(cwd, './package.json'); const packageConfigJsonc = fs_1.default.readFileSync(secDevConfigPath, 'utf8'); packageConfig = (0, json_1.parse)(packageConfigJsonc); } catch (error) { logger_1.default.debug('找不到package.json文件'); } return packageConfig; } exports.getPackageConfig = getPackageConfig;