当前位置: 首页 > news >正文

node实现对git仓库的管理

一、项目背景

一份代码,发布多个小程序。想让技术支持部通过脚本自己获取代码,并通过脚本自动提交到客户的小程序后台。他们自行发布。

现已注册第三方平台,开发人员通过“开发小程序”上传模板。开发人员把代码上传到模板,支持人员选择模板进行发布小程序。好像git代码管理也不需要了。

二、主要代码

app.js

const express = require('express');
const fs = require('fs');
const simpleGit = require('simple-git');
const cors = require('cors'); // 引入 cors 模块const app = express();const gitUrl = 'xxx'; //配置
const branch = '5.0.1';
const codePath = 'unpackage/dist/build/mp-weixin';// 本地临时文件夹
const tempFolder = 'temp';// 使用 cors 中间件
app.use(cors());//clone项目
app.get('/api/gitClone', (req, res) => {simpleGit().clone(gitUrl, tempFolder, ['-b', branch], (err, data) => {if (err) {console.error('Clone failed:', err);} else {console.log('Clone success');res.json({message: 'clone成功'});// 2. 复制代码到指定路径// 这里可能需要根据你的实际需求使用适当的文件复制方法// 例如使用 fs-extra 库的 copy 方法// 3. 执行特定文件const filePath = `${tempFolder}/${codePath}/your-executable-file.js`;try {// execSync(`node ${filePath}`, {//     stdio: 'inherit'// });console.log('Execution success');} catch (error) {console.error('Execution failed:', error);}}});
});//git 获取所有分支
app.get('/api/gitBranches', (req, res) => {// 获取所有分支simpleGit(tempFolder).branch((err, branchSummary) => {console.log('branchSummary: ', branchSummary);if (err) {console.error('Error:', err);res.status(500).json({error: 'Failed to fetch branches'});} else {// 将分支信息转换为 JSON 格式并返回const branchesJson = {branches: branchSummary.all};// current: '5.0.1',  返回的字段为当前分支//{"branches":["5.0.1","remotes/origin/1.0.0","remotes/origin/2.0.0","remotes/origin/2.0.1","remotes/origin/2.0.2","remotes/origin/3.0.0","remotes/origin/4.0.0","remotes/origin/4.1.0","remotes/origin/4.2.0","remotes/origin/4.2.1","remotes/origin/5.0.1","remotes/origin/6.0.0","remotes/origin/master"]}res.json(branchesJson);}});
});//git 切换分支
app.get('/api/checkoutBranch', (req, res) => {// 获取所有分支simpleGit(tempFolder).checkout('6.0.0', (err) => {if (err) {console.error('Error:', err);} else {console.log('Switched to branch 6.0.0');res.json({message: '切换成功'});}});
});const branchToPull = '6.0.0'; // 要拉取的分支名称//git 拉取指定分支代码
app.get('/api/pull', (req, res) => {// 获取所有分支// 拉取分支代码simpleGit(tempFolder).pull('origin', branchToPull, (err, update) => {if (err) {console.error('Error:', err);res.json({message: '拉取错误'});} else if (update && update.summary.changes) {console.log(`Pulled ${update.summary.changes} changes from branch ${branchToPull}`);res.json({message: '最新成功'});} else {console.log('Already up to date');res.json({message: '已经是最新'});}});
});app.listen(8000, () => {console.log(`Server is running on port 8000`);
});
http://www.lryc.cn/news/269212.html

相关文章:

  • 『JavaScript』全面解析JavaScript中的防抖与节流技术及其应用场景
  • 智能优化算法应用:基于袋獾算法3D无线传感器网络(WSN)覆盖优化 - 附代码
  • Ubuntu20.04-查看GPU的使用情况及输出详解
  • Python中的数据序列
  • 带您了解目前AI在测试领域能够解决的那些问题
  • Jmeter学习总结(2)——时间参数化time
  • Leetcode 746 使用最小花费爬楼梯
  • 2023/12/21作业
  • Python 数据类型 (2)
  • 【教程】自动检测和安装Python脚本依赖的第三方库
  • 0开始配置Cartographer建图和导航定位
  • Python中使用SQLite数据库的方法2-2
  • 零代码也能玩出花:Mugeda在H5设计中的魔法力量
  • 分布式、CAP 和 BASE 理论
  • django之drf框架(两个视图基类、5个扩展视图类、9个视图子类)
  • 23种设计模式学习
  • php 8.4 xdebug扩展编译安装方法
  • 66biolinks v42.0.0 已注册 – 生物短链接、URL 缩短器、QR 码和 Web 工具 (SAAS) 源码
  • 《Vue2.X 进阶知识点》- 防 ElementUI Divider 分割线
  • 【第十二课】KMP算法(acwing-831 / c++代码 / 思路 / 视频+博客讲解推荐)
  • JSON 简介
  • Impala4.x源码阅读笔记(三)——Impala如何管理Iceberg表元数据
  • Ubuntu2204配置samba
  • AVL树(超详解)
  • 禁止浏览器记住密码和自动填充 element-ui+vue
  • K8s实战-init容器
  • Vue3.2 自定义指令详解与实战
  • XV-3510CB振动陀螺仪传感器
  • 设计模式Java向
  • 图片素材管理软件Eagle for mac提高素材整理维度