UNPKG

@yeepay/yeepay-cli

Version:

易宝前端脚手架

119 lines (112 loc) 3.7 kB
import { $, cd } from 'zx'; import gitConfig from "../../config/git.config.js"; import ora from 'ora'; import Log from './log.js'; import Utils from './utils.js'; const utils = new Utils() import ChangeFile from './changeFile.js'; const changeFile = new ChangeFile() import GitCreateProject from './gitCreateProject.js'; const gitCreateProject = new GitCreateProject() export default class OperateGit { projectName = '' createGit = true; constructor({ options, typeList }) { this.options = options; this.typeList = typeList; } async gitCloneTemplate({ branch = 'master', projectType }) { const repoUrl = this.getAddress({ branch, projectType }); const targetDir = utils.getTargetDir(this.projectName); const online = await utils.checkNetworkConnection() if (!online) { Log.red('您的网络连接出现异常,请确认后重新拉取项目~'); process.exit(1); } const spinner = ora('开始克隆模板').start(); try { // 创建目录 await $`mkdir -p ${targetDir}`.quiet(); await cd(targetDir); // 停止spinner以便显示git提示,解决账号密码输入不展示的bug spinner.stop(); // 克隆仓库 await $`git clone ${repoUrl} .`.quiet(); spinner.succeed('项目模板克隆成功'); // 切换到指定分支 if (branch !== 'master') { await $`git checkout ${branch}`.quiet(); } // 前置移除.git文件夹 try { await $`rm -rf .git`.quiet(); } catch (error) { spinner.fail(`移除.git文件夹失败,请手动移除:原因如下`); Log.red(error.message); } } catch (error) { spinner.fail(`项目模板克隆失败:原因如下`); Log.red(error.message); process.exit(1); } } async handleProjectSetup(projectType, config) { await this.gitCloneTemplate({ branch: config.branch || '', projectType }); await changeFile.addProjectOwner(); await changeFile.changePackageJson(this.projectName); if (projectType === 'vue2') { projectType = 'galaxyV2' } else if (projectType === 'vue3') { projectType = 'galaxyV3' } if (projectType === 'galaxyV3') { await changeFile.changeAllText(this.projectName, '#你galaxy中的项目名#'); } else if (projectType === 'dashbord') { await changeFile.changeAllText(this.projectName, 'boss-demo'); } if (this.createGit) { await gitCreateProject.createGitLabProject({ projectName: this.projectName, description: '', nameSpaceId: gitConfig[projectType].nameSpaceId }); await gitCreateProject.pushCode({ projectName: this.projectName, group: gitConfig[projectType].group }); }else{ process.exit(0); } } async mapTypeList() { for (const item of this.typeList) { if (item.projectName) { this.projectName = item.projectName; utils.mkdir(item.projectName); continue; } if (item.createGit || item.createGit === false) { this.createGit = item.createGit; continue; } const projectType = item.projectType || item.galaxyType; if (!projectType || (item.projectType === 'galaxy')) { continue; } await this.handleProjectSetup(projectType, item); } } getAddress({ branch, projectType }) { if (projectType === 'vue2') { projectType = 'galaxyV2' } else if (projectType === 'vue3') { projectType = 'galaxyV3' } const config = gitConfig[projectType] // 直接返回固定的模板地址 return `${config.requestUrl}${config.templateAddress}`; } }