|
| 1 | +import Git from 'simple-git' |
| 2 | +import * as process from 'node:process' |
| 3 | + |
| 4 | +export { version } from '../package.json' |
| 5 | + |
| 6 | +/** |
| 7 | + * Environment variable `PULL_REQUEST` provided by Netlify. |
| 8 | + * @see {@link https://docs.netlify.com/build/configure-builds/environment-variables/#git-metadata} |
| 9 | + * |
| 10 | + * Environment variable `VERCEL_GIT_PULL_REQUEST_ID` provided by Vercel. |
| 11 | + * @see {@link https://vercel.com/docs/environment-variables/system-environment-variables#VERCEL_GIT_PULL_REQUEST_ID} |
| 12 | + * |
| 13 | + * Whether triggered by a GitHub PR |
| 14 | + */ |
| 15 | +export const isPR = process.env.PULL_REQUEST === 'true' || !!process.env.VERCEL_GIT_PULL_REQUEST_ID |
| 16 | + |
| 17 | +/** |
| 18 | + * Environment variable `BRANCH` provided by Netlify. |
| 19 | + * @see {@link https://docs.netlify.com/build/configure-builds/environment-variables/#git-metadata} |
| 20 | + * |
| 21 | + * Environment variable `VERCEL_GIT_COMMIT_REF` provided by Vercel. |
| 22 | + * @see {@link https://vercel.com/docs/environment-variables/system-environment-variables#VERCEL_GIT_COMMIT_REF} |
| 23 | + * |
| 24 | + * Git branch |
| 25 | + */ |
| 26 | +export const gitBranch = process.env.BRANCH || process.env.VERCEL_GIT_COMMIT_REF |
| 27 | + |
| 28 | +/** |
| 29 | + * Environment variable `CONTEXT` provided by Netlify. |
| 30 | + * @see {@link https://docs.netlify.com/build/configure-builds/environment-variables/#build-metadata} |
| 31 | + * |
| 32 | + * Environment variable `VERCEL_ENV` provided by Vercel. |
| 33 | + * @see {@link https://vercel.com/docs/environment-variables/system-environment-variables#VERCEL_ENV} |
| 34 | + * |
| 35 | + * Whether triggered by PR, `deploy-preview` or `dev`. |
| 36 | + */ |
| 37 | +export const isPreview = |
| 38 | + isPR || |
| 39 | + process.env.CONTEXT === 'deploy-preview' || |
| 40 | + process.env.CONTEXT === 'dev' || |
| 41 | + process.env.VERCEL_ENV === 'preview' || |
| 42 | + process.env.VERCEL_ENV === 'development' |
| 43 | + |
| 44 | +const git = Git() |
| 45 | +export async function getGitInfo() { |
| 46 | + let branch |
| 47 | + try { |
| 48 | + branch = gitBranch || (await git.revparse(['--abbrev-ref', 'HEAD'])) |
| 49 | + } catch { |
| 50 | + branch = 'unknown' |
| 51 | + } |
| 52 | + |
| 53 | + let commit |
| 54 | + try { |
| 55 | + // Netlify: COMMIT_REF, Vercel: VERCEL_GIT_COMMIT_SHA |
| 56 | + commit = |
| 57 | + process.env.COMMIT_REF || process.env.VERCEL_GIT_COMMIT_SHA || (await git.revparse(['HEAD'])) |
| 58 | + } catch { |
| 59 | + commit = 'unknown' |
| 60 | + } |
| 61 | + |
| 62 | + let shortCommit |
| 63 | + try { |
| 64 | + if (commit && commit !== 'unknown') { |
| 65 | + shortCommit = commit.slice(0, 7) |
| 66 | + } else { |
| 67 | + shortCommit = await git.revparse(['--short=7', 'HEAD']) |
| 68 | + } |
| 69 | + } catch { |
| 70 | + shortCommit = 'unknown' |
| 71 | + } |
| 72 | + |
| 73 | + return { branch, commit, shortCommit } |
| 74 | +} |
| 75 | + |
| 76 | +export async function getEnv(isDevelopment: boolean) { |
| 77 | + const { commit, shortCommit, branch } = await getGitInfo() |
| 78 | + const env = isDevelopment |
| 79 | + ? 'dev' |
| 80 | + : isPreview |
| 81 | + ? 'preview' |
| 82 | + : branch === 'main' |
| 83 | + ? 'canary' |
| 84 | + : 'release' |
| 85 | + return { commit, shortCommit, branch, env } as const |
| 86 | +} |
0 commit comments