53 lines
1.4 KiB
TypeScript
53 lines
1.4 KiB
TypeScript
import { exec } from 'child_process'
|
|
|
|
/**
|
|
* Get the current Git commit hash.
|
|
* @returns The current Git commit hash as a string, or null if an error occurs.
|
|
*/
|
|
export async function getGitCommitHash(): Promise<string | null> {
|
|
try {
|
|
const commitHash = (await new Promise((resolve, reject) => {
|
|
exec('git rev-parse HEAD', (error, stdout) => {
|
|
if (error) {
|
|
reject(error)
|
|
} else {
|
|
resolve(stdout.toString().trim())
|
|
}
|
|
})
|
|
})) as string
|
|
|
|
return commitHash
|
|
} catch (error) {
|
|
console.error('Error getting Git commit hash:', error)
|
|
return null
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the Git origin URL.
|
|
* @returns The Git origin URL as a string, or null if an error occurs.
|
|
*/
|
|
export async function getGitOriginUrl(): Promise<string | null> {
|
|
try {
|
|
const originUrl = (await new Promise((resolve, reject) => {
|
|
exec('git config --get remote.origin.url', (error, stdout) => {
|
|
if (error) {
|
|
reject(error)
|
|
} else {
|
|
resolve(stdout.toString().trim())
|
|
}
|
|
})
|
|
})) as string
|
|
|
|
if (originUrl.startsWith('ssh://')) {
|
|
const httpsUrl = originUrl.replace(/^ssh:\/\/git@/, 'https://').replace(/\.git$/, '')
|
|
const urlWithoutPort = httpsUrl.replace(/:\d+/, '')
|
|
return urlWithoutPort
|
|
}
|
|
|
|
return originUrl
|
|
} catch (error) {
|
|
console.error('Error getting Git origin URL:', error)
|
|
return null
|
|
}
|
|
}
|