UNPKG

@szzbmy/lowcode-cli

Version:

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

255 lines (254 loc) 11.5 kB
"use strict"; 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 }); /* * @Author: chubiao Ni * @Date: 2022-02-11 14:04:18 * @Last Modified by: chubiao Ni * @Last Modified time: 2022-05-30 14:56:51 */ const fs_1 = require("fs"); const path_1 = require("path"); const react_1 = __importStar(require("react")); const ink_1 = require("ink"); const compressing_1 = __importDefault(require("compressing")); const shelljs_1 = __importDefault(require("shelljs")); const components_1 = require("../../../components"); const interactive_1 = require("../../../components/interactive"); const create_code_1 = require("../../../apis/create-code"); const utils_1 = require("../utils"); const fs_2 = require("../../../config/fs"); const SELECT_LIST_LIMIT = 20; function Loading(props) { return (react_1.default.createElement(ink_1.Text, null, react_1.default.createElement(ink_1.Text, { color: "green" }, react_1.default.createElement(components_1.Spinner, { type: "dots" })), props.text)); } var CREATE_STEP; (function (CREATE_STEP) { CREATE_STEP[CREATE_STEP["INIT"] = 0] = "INIT"; CREATE_STEP[CREATE_STEP["FETCH_CMPLIBS"] = 1] = "FETCH_CMPLIBS"; CREATE_STEP[CREATE_STEP["FETCH_LIB_TEMPLATES"] = 2] = "FETCH_LIB_TEMPLATES"; CREATE_STEP[CREATE_STEP["PULL_LIB_TEMPLATE"] = 3] = "PULL_LIB_TEMPLATE"; CREATE_STEP[CREATE_STEP["INTERACTIVE"] = 4] = "INTERACTIVE"; CREATE_STEP[CREATE_STEP["FINISHED"] = 5] = "FINISHED"; CREATE_STEP[CREATE_STEP["ERROR"] = 6] = "ERROR"; })(CREATE_STEP || (CREATE_STEP = {})); var INTERACTIVES; (function (INTERACTIVES) { // 选择组件库 INTERACTIVES[INTERACTIVES["SELECT_CMPLIB"] = 0] = "SELECT_CMPLIB"; // 选择组件库模版 INTERACTIVES[INTERACTIVES["SELECT_LIB_TEMPLATE"] = 1] = "SELECT_LIB_TEMPLATE"; // 模版存放名称 INTERACTIVES[INTERACTIVES["INPUT_NAME"] = 2] = "INPUT_NAME"; })(INTERACTIVES || (INTERACTIVES = {})); function CreateCmplib() { const [stepState, setStepStateOriginal] = (0, react_1.useState)({ step: CREATE_STEP.INIT, options: {}, }); const [folderName, setFolderName] = (0, react_1.useState)(''); // 组件库相关 const [selectedCmplibInfo, setSelectedCmplibInfo] = (0, react_1.useState)({}); const [selectedLibTemplateInfo, setSelectedLibTemplateInfo] = (0, react_1.useState)({}); const [cmplibList, setCmplibList] = (0, react_1.useState)([]); const [libTemplateList, setLibTemplateList] = (0, react_1.useState)([]); const setStepState = (0, react_1.useCallback)((step, options) => { setStepStateOriginal({ step, options }); }, []); // 选择组件库 const onHandleSelectCmplib = (0, react_1.useCallback)(cmplibInfo => { setSelectedCmplibInfo(cmplibInfo); setStepState(CREATE_STEP.FETCH_LIB_TEMPLATES); }, [setStepState]); const onHandleSelectLibTemplate = (0, react_1.useCallback)(libTemplateInfo => { setSelectedLibTemplateInfo(libTemplateInfo); // 进入目录填写交互 setStepState(CREATE_STEP.INTERACTIVE, { step: INTERACTIVES.INPUT_NAME, msg: '模板导出目录: ', }); }, [setStepState]); // 命名模板下载目录 const onHandleRename = (0, react_1.useCallback)(value => { if ((0, fs_1.existsSync)((0, path_1.resolve)(value))) { // 仍存在项目目录, 继续重写目录名 setStepState(CREATE_STEP.INTERACTIVE, { step: INTERACTIVES.INPUT_NAME, msg: '已存在目录, 请重新输入: ', }); } else { // 目录名确定, 进入拉取模板流程 setFolderName(value); setStepState(CREATE_STEP.PULL_LIB_TEMPLATE); } }, [setStepState]); const init = (0, react_1.useCallback)(async () => { setStepState(CREATE_STEP.FETCH_CMPLIBS); }, [setStepState]); // 获取组件库列表 const fetchComponentlibList = (0, react_1.useCallback)(async () => { try { const cmplibList = await (0, create_code_1.getCmplibList)(); const formatedCmplitList = (0, utils_1.formatCmplibList)(cmplibList); if (formatedCmplitList.length === 0) { throw new Error('当前组件库列表为空, 请先在低代码平台添加组件库'); } setCmplibList(formatedCmplitList); setStepState(CREATE_STEP.INTERACTIVE, { step: INTERACTIVES.SELECT_CMPLIB, }); } catch (err) { setStepState(CREATE_STEP.ERROR, { msg: `获取组件库列表失败: ${err.message}`, }); } }, [setStepState]); const fetchLitTemplateList = (0, react_1.useCallback)(async () => { try { const libTemplateList = await (0, create_code_1.fetchTemplateList)({ objectType: 'CMPLIB', }); const formatedLibTemplateList = await (0, utils_1.formatLibTemplateList)(libTemplateList); if (formatedLibTemplateList.length === 0) { throw new Error('无可用模板'); } setLibTemplateList(formatedLibTemplateList); setStepState(CREATE_STEP.INTERACTIVE, { step: INTERACTIVES.SELECT_LIB_TEMPLATE, }); } catch (err) { setStepState(CREATE_STEP.ERROR, { msg: `获取组件库模板列表失败: ${err.message}`, }); } }, [setStepState]); const downloadLibTemplate = (0, react_1.useCallback)(async () => { try { const { objectName } = selectedLibTemplateInfo; const templateRes = await (0, create_code_1.fetchTemplate)({ objectName, objectType: 'CMPLIB', }); // 解压到对应目录 await compressing_1.default.zip.uncompress(templateRes, folderName); const { libName, description, libSecondName, bindPluginFlag, pluginName, } = selectedCmplibInfo; // 更新json.jsonc文件 (0, fs_2.updateComponentConfig)({ key: libName, label: libSecondName, desc: description, bindPluginName: bindPluginFlag ? pluginName : '', }, folderName); // 清理 .git await shelljs_1.default.rm('-rf', (0, path_1.resolve)(folderName, '.git')); setStepState(CREATE_STEP.FINISHED, { msg: '模板下载成功' }); } catch (err) { setStepState(CREATE_STEP.ERROR, { msg: `下载组件库模板代码失败: ${err.message}`, }); } }, [selectedLibTemplateInfo, setStepState, folderName, selectedCmplibInfo]); const finish = (0, react_1.useCallback)(() => { process.exit(0); }, []); const error = (0, react_1.useCallback)(() => { process.exit(1); }, []); (0, react_1.useEffect)(() => { const func = { [CREATE_STEP.INIT]: init, [CREATE_STEP.FETCH_CMPLIBS]: fetchComponentlibList, [CREATE_STEP.FETCH_LIB_TEMPLATES]: fetchLitTemplateList, [CREATE_STEP.PULL_LIB_TEMPLATE]: downloadLibTemplate, [CREATE_STEP.FINISHED]: finish, [CREATE_STEP.ERROR]: error, }[stepState.step]; func && func(); }, [ init, fetchComponentlibList, fetchLitTemplateList, downloadLibTemplate, finish, error, stepState.step, ]); let renderer = null; switch (stepState.step) { case CREATE_STEP.FETCH_CMPLIBS: renderer = react_1.default.createElement(Loading, { text: "\u6B63\u5728\u62C9\u53D6\u81EA\u5B9A\u4E49\u73A9\u6CD5\u5217\u8868" }); break; case CREATE_STEP.FETCH_LIB_TEMPLATES: renderer = react_1.default.createElement(Loading, { text: "\u6B63\u5728\u62C9\u53D6\u6A21\u677F\u5217\u8868" }); break; case CREATE_STEP.PULL_LIB_TEMPLATE: renderer = react_1.default.createElement(Loading, { text: "\u6B63\u5728\u4E0B\u8F7D\u6A21\u677F" }); break; case CREATE_STEP.INTERACTIVE: if (stepState.options?.step === INTERACTIVES.SELECT_CMPLIB) { renderer = (react_1.default.createElement(react_1.default.Fragment, null, react_1.default.createElement(ink_1.Text, { color: "green" }, "\u9009\u62E9\u6A21\u677F(\u5171", cmplibList.length, "\u4E2A):"), react_1.default.createElement(ink_1.Box, { borderStyle: "single" }, react_1.default.createElement(components_1.SelectInput, { items: cmplibList, limit: SELECT_LIST_LIMIT, onSelect: onHandleSelectCmplib })))); } else if (stepState.options?.step === INTERACTIVES.SELECT_LIB_TEMPLATE) { renderer = (react_1.default.createElement(react_1.default.Fragment, null, react_1.default.createElement(ink_1.Text, { color: "green" }, "\u9009\u62E9\u6A21\u677F(\u5171", libTemplateList.length, "\u4E2A):"), react_1.default.createElement(ink_1.Box, { borderStyle: "single" }, react_1.default.createElement(components_1.SelectInput, { items: libTemplateList, limit: SELECT_LIST_LIMIT, onSelect: onHandleSelectLibTemplate })))); } else if (stepState.options?.step === INTERACTIVES.INPUT_NAME) { renderer = (react_1.default.createElement(interactive_1.InteractiveGenerator, { type: interactive_1.GENERATOR_TYPE.TEXT_INPUT, options: { title: stepState.options?.msg, props: { onSubmit: onHandleRename, }, } })); } break; case CREATE_STEP.FINISHED: renderer = react_1.default.createElement(ink_1.Text, { color: "green" }, stepState.options?.msg); break; case CREATE_STEP.ERROR: renderer = react_1.default.createElement(ink_1.Text, { color: "red" }, stepState.options?.msg); break; default: renderer = null; } return renderer; } exports.default = CreateCmplib;