UNPKG

@lark-project/cli

Version:

飞书项目插件开发工具

105 lines (104 loc) 4.9 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.initProject = void 0; const path_1 = __importDefault(require("path")); const fs_extra_1 = require("fs-extra"); const execa_1 = __importDefault(require("execa")); const logger_1 = require("../../../utils/logger"); const use_yarn_1 = require("../../../utils/use-yarn"); const with_loading_spinner_1 = require("../../../utils/with-loading-spinner"); const download_codebase_1 = require("./download-codebase"); const utils_1 = require("../../../v1/utils"); const get_plugin_info_1 = require("../../../api/get-plugin-info"); const types_1 = require("../../../types"); const file_1 = require("../utils/file"); function transformTemplateType(type) { switch (type) { case types_1.EFeatureType.page: return 'page'; case types_1.EFeatureType.tab: return 'tab'; case types_1.EFeatureType.config: case types_1.EFeatureType.button: case types_1.EFeatureType.control: case types_1.EFeatureType.intercept: case types_1.EFeatureType.view: default: return type; } } // 通过模版id 拉取模版代码 async function loaderProjectWithTemplateId(options, templateId, templateTargetDir, targetDir) { const isDefault = ['react-empty', 'react-initialized'].includes(templateId); const { pluginId, pluginSecret, siteDomain } = options; // 拉取插件信息 const remotePluginInfo = await (0, get_plugin_info_1.getPluginInfo)(pluginId, pluginSecret, siteDomain); // 整理插件信息 // 用户配置的插件构成信息 const pluginType2IdMap = {}; for (const feature of remotePluginInfo.features) { const { type, web, mobile } = feature; const transformedType = transformTemplateType(type); const webResourceId = web.resourceId.resource; const mobileResourceId = mobile.resourceId.resource; if (!pluginType2IdMap[`${transformedType}_web`]) { pluginType2IdMap[`${transformedType}_web`] = []; } if (!pluginType2IdMap[`${transformedType}_mobile`]) { pluginType2IdMap[`${transformedType}_mobile`] = []; } if (webResourceId) { pluginType2IdMap[`${transformedType}_web`].push(webResourceId); } if (mobileResourceId) { pluginType2IdMap[`${transformedType}_mobile`].push(mobileResourceId); } } const renderCtx = Object.assign(Object.assign({}, options), { pluginSecret: (0, utils_1.encrypt)(pluginSecret), pluginType2IdMap }); logger_1.logger.info(`Download from the path of project/${templateId}`); await (0, with_loading_spinner_1.withLoadingSpinner)(async () => { await (0, file_1.copyProjectWithTemplate)({ templateTargetDir, templateId, targetDir, renderCtx, filter: (entry) => entry.includes('App.tsx.ejs'), }); if (isDefault) { await (0, fs_extra_1.remove)(`${targetDir}/src/features/web`); await (0, fs_extra_1.remove)(`${targetDir}/src/features/mobile`); const { list } = await (0, file_1.generateResourceCode)({ remoteConfig: remotePluginInfo, localConfig: {}, targetDir, siteDomain, }); await Promise.all(list); } await (0, fs_extra_1.remove)(templateTargetDir); }, 'Code template processing...'); } async function initProject(options) { const { projectName, templateId } = options; const targetDir = path_1.default.resolve(process.cwd(), projectName); logger_1.logger.info('Start init project at ', targetDir); await (0, fs_extra_1.remove)(targetDir); const templateTargetDir = path_1.default.join(targetDir, '.template'); await (0, with_loading_spinner_1.withLoadingSpinner)(download_codebase_1.downloadCodeBase, 'Code template downloading...', templateTargetDir, templateId); await loaderProjectWithTemplateId(options, templateId, templateTargetDir, targetDir); // install packages logger_1.logger.info('Installing packages. This might take a couple of minutes.'); const payload = { stdio: 'inherit', cwd: targetDir, shell: true, }; await (0, execa_1.default)((0, use_yarn_1.useYarn)() ? 'yarn' : 'npm', ['install'], payload); logger_1.logger.success(`🍻🍻🍻 Init successfully! The project is under ${targetDir} . All resourceIds that don't match will follow the default path. Enter the following commands at the terminal to begin development.`); console.log(`Step 1: cd ${options.projectName}`); console.log('Step 2: lpm start'); } exports.initProject = initProject;