2023-01-04 13:26:30 +00:00
|
|
|
import Git from 'simple-git'
|
|
|
|
import { isDevelopment } from 'std-env'
|
|
|
|
|
|
|
|
export { version } from '../package.json'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Environment variable `PULL_REQUEST` provided by Netlify.
|
|
|
|
* @see {@link https://docs.netlify.com/configure-builds/environment-variables/#git-metadata}
|
|
|
|
*
|
|
|
|
* Whether triggered by a GitHub PR
|
|
|
|
*/
|
|
|
|
export const isPR = process.env.PULL_REQUEST === 'true'
|
|
|
|
|
2023-01-04 13:41:48 +00:00
|
|
|
/**
|
|
|
|
* Environment variable `BRANCH` provided by Netlify.
|
|
|
|
* @see {@link https://docs.netlify.com/configure-builds/environment-variables/#git-metadata}
|
|
|
|
*
|
|
|
|
* Git branch
|
|
|
|
*/
|
|
|
|
export const gitBranch = process.env.BRANCH
|
|
|
|
|
2023-01-04 13:26:30 +00:00
|
|
|
/**
|
|
|
|
* Environment variable `CONTEXT` provided by Netlify.
|
|
|
|
* @see {@link https://docs.netlify.com/configure-builds/environment-variables/#build-metadata}
|
|
|
|
*
|
|
|
|
* Whether triggered by PR, `deploy-preview` or `dev`.
|
|
|
|
*/
|
|
|
|
export const isPreview = isPR || process.env.CONTEXT === 'deploy-preview' || process.env.CONTEXT === 'dev'
|
|
|
|
|
|
|
|
const git = Git()
|
2023-03-30 20:01:24 +01:00
|
|
|
export async function getGitInfo() {
|
2024-10-24 01:50:30 +01:00
|
|
|
let branch
|
|
|
|
try {
|
|
|
|
branch = gitBranch || await git.revparse(['--abbrev-ref', 'HEAD'])
|
|
|
|
}
|
|
|
|
catch {
|
|
|
|
branch = 'unknown'
|
|
|
|
}
|
|
|
|
|
|
|
|
let commit
|
|
|
|
try {
|
|
|
|
commit = await git.revparse(['HEAD'])
|
|
|
|
}
|
|
|
|
catch {
|
|
|
|
commit = 'unknown'
|
|
|
|
}
|
|
|
|
|
|
|
|
let shortCommit
|
|
|
|
try {
|
|
|
|
shortCommit = await git.revparse(['--short=7', 'HEAD'])
|
|
|
|
}
|
|
|
|
catch {
|
|
|
|
shortCommit = 'unknown'
|
|
|
|
}
|
|
|
|
|
2023-02-23 11:17:28 +00:00
|
|
|
return { branch, commit, shortCommit }
|
2023-01-04 13:26:30 +00:00
|
|
|
}
|
|
|
|
|
2023-03-30 20:01:24 +01:00
|
|
|
export async function getEnv() {
|
2023-02-23 11:17:28 +00:00
|
|
|
const { commit, shortCommit, branch } = await getGitInfo()
|
2023-01-04 13:26:30 +00:00
|
|
|
const env = isDevelopment
|
|
|
|
? 'dev'
|
|
|
|
: isPreview
|
|
|
|
? 'preview'
|
|
|
|
: branch === 'main'
|
2023-01-06 16:20:12 +00:00
|
|
|
? 'canary'
|
2023-01-04 13:26:30 +00:00
|
|
|
: 'release'
|
2023-02-23 11:17:28 +00:00
|
|
|
return { commit, shortCommit, branch, env } as const
|
2023-01-04 13:26:30 +00:00
|
|
|
}
|