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