Nodejs obtains git information (branch, label) through the child process module child_process

child_process

child_processThe module provides the ability to spawn sub-processes for executing sub-processes and other operations in the node environment, such as executing .cmd, .bat and shell scripts.
Node.js v12.13.0 documentation child_process

API available here child_process.exec(command[, options][, callback])and child_process.execSync(command[, options])to get the gitwarehouse information.


git name-rev --name-only HEAD

git-name-rev-Find the symbolic name of a given speed.
git name-rev --name-only HEADThis command will output your current version or label information in the terminal.

git name-rev --name-only HEAD 
===> test 

git name-rev --name-only HEAD
===> v4.3.1
Use in combination
//同步子进程
const version = child_process.execSync('git name-rev --name-only HEAD', { 'encoding': 'utf8' });
===> v4.3.1 


//异步子进程
const childProcess = require('child_process');

const getVersion = () => {
    return new Promise((resovle, reject) => {
        childProcess.exec(`git name-rev --name-only HEAD`, { encoding: 'utf-8' }, (stdout, error, status, output) => {
            error ? reject(error) : resovle(stdout)
        });
    })
}

getVersion().then(res => {
    console.log(res)
}).catch(e => {
    console.log(e)
})

Guess you like

Origin blog.csdn.net/Ruffaim/article/details/102800616