用一句命令行实现代码分支合并并打包推送远程功能

351 阅读2分钟

因为公司没有实现代码提交自动化部署,所以每次部署都得从你的分支合并到上线分支(比如master)然后打包推送到远程,所以本着能简化就简化的原则,尝试解决下代码分支合并自动化。

实现

1. 首先你要对git流比较清楚。举例:你的开发分支shell,你的上线分支shellTest。你上线前需要把你的代码从shellTest(假设此时shellTest是最新的代码)往你的分支合一下,然后推送到shell远程,再次切换到shellTest合并shell分支,然后拉取 打包推送到远程。
2. 需要shelljs这个第三方库来进行支持
npm install shelljs --save
3. 具体实现
1.定义前端项目路径 
shell.config.js文件下内容
module.exports = {
    distPath: 'E:/zmxx/qwCode/vite-project' // 前端vue包路径
}

shell.js文件下内容
2.代码实现
const { distPath } = require('./shell.config') // 项目路径
const shell = require('shelljs')
const chalk = require('chalk') // node终端样式库
let isCurrentBranch = false  // 是否在当前分支

2.1 检测是否安装git
if (!shell.which('git')) {
    // 向命令行打印git命令不可用的提示信息
    shell.echo(chalk.red('抱歉, 当前环境无法执行git命令'))
    // 退出当前进程
    shell.exit(1)
}

2.2 检测前端项目路径
if (!(distPath)) {
    // 向命令行打印git命令不可用的提示信息
    shell.echo(chalk.red('请先配置项目路径请打开/shell/shell.config.js进行配置'))
    // 退出当前进程
    shell.exit(1)
}

2.3 进入工作目录并检测是否还有未提交文件
// cd 进入相应目录
shell.cd(distPath)
// 查看是否存在未提交文件函数
shell.echo(chalk.cyan('进入vite-project目录'))
shell.echo(chalk.cyan('检测是否存在未提交文件'))
const gitStatus = () => {
    return new Promise((resolve, reject) => {
        shell.exec('git status -s', {}, function(error, stdout) {
            if (stdout) {
                reject('项目中存在未提交文件请提交后操作')
            } else {
                resolve(stdout)
            }
        })
    })
}

2.4 获取当前所在分支函数
const branch = () => {
    return new Promise(resolve => {
        shell.exec('git branch --show-current', { async: false }, function(error, stdout) {
            resolve(stdout)
        })
    })
}

2.5 git工作流
gitStatus().then(() => {
    shell.echo(chalk.cyan('检测分支是否在shellTest'))
    return branch()
}).then(data => {
    // 切换分支
    return new Promise(resolve => {
        if (data.trim() === 'shellTest') {
            isCurrentBranch = true 
            resolve()
        } else {
            shell.echo(chalk.yellow(`当前分支${data}开始切换到shellTest`))
            shell.exec('git checkout shellTest', {}, function() {
                shell.echo(chalk.green('成功切换到shellTest 分支'))
                resolve(data)
                isCurrentBranch = false
            })
            
        }
    })
}).then(() => {
    // 拉取远程代码
    return new Promise(resolve => {
        shell.echo(chalk.green('开始pull'))
        shell.exec('git pull', {}, function() {
            resolve()
        })
    })
    
}).then(() => {
    // 合并分支
    if (isCurrentBranch) {
        return new Promise(resolve => {
            shell.exec('git merge ' + data, {}, function() {
                shell.echo(chalk.green(`合并分支${data}到shellTest`))
                resolve()
            })
        })
    }
}).then(() => {
    // 开始打包
    shell.echo(chalk.cyan('开始清空dist目录'))
    shell.rm('-rf', distPath + '/dist/*')
    shell.echo(chalk.cyan('开始打包'))
    return new Promise(resolve => {
        shell.exec('npm run build', {}, function() {
            resolve()
        })
    })
}).then(() => {
    return new Promise(resolve => {
        shell.exec('git add .', {}, function() {
            resolve()
        })
    })
}).then(() => {
    shell.exec('git commit -m "更新vue包"', {}, function() {
        shell.exec('git push', {}, function() {
            shell.echo(chalk.green('成功上传shellTest ,请稍后刷新页面查看'))
        })
        
    })
}).catch(errMsg => {
    shell.echo(chalk.red(errMsg))
})

2.6 配置node命令行
在package.json中设置
"shell": "node ./shell/shell.js",

image.png

4. 测试

执行 npm run shell

工作台看到的效果

image.png

image.png

image.png

5.总结

可能会存在一定的缺陷 比如处理git合并冲突时候还得去手工解决,但是还是省点力气的。