[git management] using shell script and nodejs to realize quick commit and tag

Time:2021-1-16

reflection
When the front-end uses NPM and git for development, there will be version number or changes that need to be updated and submitted synchronously. When we submit the update information through commit, can we use the commit information to become the version log data that we change when we work?

Don’t say much, share on the code.

Usage

Execute command: SH bin/ commit.sh

Operation steps

1. Input commit information
2. Select the update type
3. Choose whether to submit a tag

Implementation mode

1、 Create related files


bin/ commit.sh            //Execute script
bin/ commit.js            //Log file update
bin/ version.js           // package.json Version update
bin/version_ log.json     //Version log file
package.json     //NPM dependency profile

2、 Key code


Let’s take a look at the version log file firstversion_log.json

[
  {
    "date": "2021-01-01 00:00:00",
    "version": "0.0.1",
    "Commit": "[update] create submit command and update log script test"
  }
]

This is a standard JSON format file, which stores the version information through the array. Because I put the latest version at the end of the array, I only need to extract the last matching version information to identify the version number each time

Next is the key sh command, which interacts with the developer. The developer fills in the version update log, selects the type of update version, and whether to submit the tag

#!/bin/bash

cd bin/
#=========Read last version information=========
versions=($(cat ./version_log.json | awk -F 'version' '/version/{print$0}' | sed 's/ //g'  ))
versions_length=${#versions[@]}
newest_version_str=${versions[versions_length-1]}
newest_version_str_length=${#newest_version_str}
#Gets the index value after the character
newest_version_str_length_end=$[newest_version_str_length-2-11]
old_version=${newest_version_str:11:$newest_version_str_length_end}
versionArr=[]
v=${old_version//./ }
index=0
for i in $v; do
    versionArr[index]=$i
    index=$[index+1]
done

Read - P "please submit change information:" commit_ msg
#=========Perform update category selection (default bug fix)=========
Commitprefix = [fix] "
Echo "= = = = 1. Iteration = = = = ="
Echo "= = = = 2. Update = = = = ="
Echo "= = = = 3. Repair = = = = ="
Read - P "please select change type (default: 3):" level

case $level in
    1) Echo 'you have selected version iteration'
    versionArr[0]=$[versionArr[0]+1]
    versionArr[1]=0
    versionArr[2]=0
    Commit = [iteration] "$commit_ msg
    ;;
    2) Echo 'you have selected small update'
    versionArr[1]=$[versionArr[1]+1]
    versionArr[2]=0
    Commit = [update] "$commit_ msg
    ;;
    3) Echo 'you have chosen to repair'
    versionArr[2]=$[versionArr[2]+1]
    Commit "[fix]" $commit_ msg
    ;;
    *) echo 'you have selected repair by default'
    versionArr[2]=$[versionArr[2]+1]
    Commit "[fix]" $commit_ msg
    ;;
esac
now_date=`date "+%Y-%m-%d_%H:%M:%S"`
echo $now_date
version=${versionArr[0]}'.'${versionArr[1]}'.'${versionArr[2]}
echo $version
addInfo="{\"date\":\"$now_date\",\"version\":\"$version\",\"commit\":\"$commit\"}"

#=========Update version log file information=========
node commit $addInfo
#Synchronous update package.json Version number
node version
git commit -am $commit

Read - P "add tag (GIT) 1 / 0:" is_ add_ tag
echo $is_add_tag
if(($is_add_tag == 1 ))
then
  Echo "you have selected to push git tag, about to execute..."
  git tag $version
  git push origin --tags dev
else
  Echo "you have not selected the push git tag, it is about to end..."
  git push origin dev
fi

exit 1

Then, the node command is used to read and write two JSON files

commit.js

//Add the function of updating system log
const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});
const path = require("path");
const fs = require('fs')
var versionStr = ""
if (!process.argv[2]) {
  process.exit()
  return  console.error ('[package notification] failed to get version information stream, rewrite failed', ERR)
}
versionStr = process.argv[2]   
fs.readFile(path.join(__dirname, './version_log.json'), (err, data) => {
  if (err) {
    process.exit()
    return  console.error ("[package notification] file read exception: + ERR)
  }

  let packagesConfig = JSON.parse(data)
  packagesConfig.push(JSON.parse(versionStr))
  fs.writeFile(path.join(__dirname, './version_log.json'), JSON.stringify(packagesConfig,"","\t"), (err, result) => {
    if (err) {
      return  console.error ('[package notification] version_ log.json  Rewrite failed ', ERR)
    }
    process.exit()
    return  console.log ('[package notification] version_ log.json  Version updated successfully ')
  })
})

verison.js

const path = require("path");
const fs = require('fs')

fs.readFile(path.join(__dirname, '../package.json'), (readFilePackageError, packages) => {
  if (readFilePackageError) {
    process.exit()
    return  console.error ("[package notification] read package.json Error: '+ readfilepackagereror)
  }
  let newVersion
  fs.readFile(path.join(__dirname, './version_log.json'), (readFileLogError, logs) => {
    if (readFileLogError) {
      process.exit()
      return  console.error ("[package notification] error reading development log" + readfilelogerror)
    }
    let logData = JSON.parse(logs)
    newVersion = logData.pop().version
    console.log ('[package notification] current latest development log version: V' + newversion)

    let packagesConfig = JSON.parse(packages)
    packagesConfig.version = newVersion

    fs.writeFile(path.join(__dirname, '../package.json'), JSON.stringify(packagesConfig,"","\t"), (err, result) => {
      if (err) {
        return  console.error ('[package notification] package.json  Rewrite failed ', ERR)
      }
      process.exit()
      return  console.log ('[package notification] package.json  Version updated successfully ')
    })

  })
})

Original content, no reprint without permission!

Recommended Today

Flink learning — case practice of eventtime and watermarks

The article is reproduced from:https://blog.csdn.net/xu47043…Author: bigdata 1024Encroachment and deletion There are two ways to generate watermarks1: With periodic watermarks: periodically trigger the generation and sending of watermarks.2: With tapped watermarks: trigger the generation and sending of watermarks based on certain events. The first method is commonly used, so here we use the first method for […]