-
-
Notifications
You must be signed in to change notification settings - Fork 425
Expand file tree
/
Copy pathenv.ts
More file actions
194 lines (177 loc) · 6.63 KB
/
env.ts
File metadata and controls
194 lines (177 loc) · 6.63 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// TODO(serhalp): Extract most of this module to https://github.com/unjs/std-env.
import Git from 'simple-git'
import * as process from 'node:process'
import { version as packageVersion } from '../package.json'
import { getNextVersion } from '../scripts/next-version'
export { packageVersion as version }
/**
* 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 `REVIEW_ID` provided by Netlify.
* @see {@link https://docs.netlify.com/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}
*
* Pull request number (if in a PR environment)
*/
export const prNumber = process.env.REVIEW_ID || process.env.VERCEL_GIT_PULL_REQUEST_ID || null
/**
* 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
/**
* Whether this is the canary environment (main.npmx.dev).
*
* Detected as any non-PR Vercel deploy from the `main` branch
* (which may receive `VERCEL_ENV === 'production'`, `'preview'`, or a
* custom `'canary'` environment depending on the project configuration).
*
* @see {@link https://vercel.com/docs/environment-variables/system-environment-variables#VERCEL_ENV}
*/
export const isCanary =
(process.env.VERCEL_ENV === 'production' ||
process.env.VERCEL_ENV === 'preview' ||
process.env.VERCEL_ENV === 'canary') &&
gitBranch === 'main' &&
!isPR
/**
* Environment variable `CONTEXT` provided by Netlify.
* `dev`, `production`, `deploy-preview`, `branch-deploy`, `preview-server`, or a branch name
* @see {@link https://docs.netlify.com/build/configure-builds/environment-variables/#build-metadata}
*
* Environment variable `VERCEL_ENV` provided by Vercel.
* `production`, `preview`, or `development`.
* @see {@link https://vercel.com/docs/environment-variables/system-environment-variables#VERCEL_ENV}
*
* Whether this is some sort of preview environment.
*/
export const isPreview =
isPR ||
(process.env.CONTEXT && process.env.CONTEXT !== 'production') ||
process.env.VERCEL_ENV === 'preview' ||
process.env.VERCEL_ENV === 'development'
export const isProduction =
process.env.CONTEXT === 'production' || process.env.VERCEL_ENV === 'production'
/**
* Environment variable `URL` provided by Netlify.
* This is always the current deploy URL, regardless of env.
* @see {@link https://docs.netlify.com/build/functions/environment-variables/#functions}
*
* Environment variable `VERCEL_URL` provided by Vercel.
* This is always the current deploy URL, regardless of env.
* NOTE: Not a valid URL, as the protocol is omitted.
* @see {@link https://vercel.com/docs/environment-variables/system-environment-variables#VERCEL_URL}
*
* Preview URL for the current deployment, only available in preview environments.
*/
export const getPreviewUrl = () =>
isPreview
? process.env.URL
? process.env.URL
: process.env.NUXT_ENV_VERCEL_URL
? `https://${process.env.NUXT_ENV_VERCEL_URL}`
: undefined
: undefined
/**
* Environment variable `URL` provided by Netlify.
* This is always the current deploy URL, regardless of env.
* @see {@link https://docs.netlify.com/build/functions/environment-variables/#functions}
*
* Environment variable `VERCEL_PROJECT_PRODUCTION_URL` provided by Vercel.
* NOTE: Not a valid URL, as the protocol is omitted.
* @see {@link https://vercel.com/docs/environment-variables/system-environment-variables#VERCEL_PROJECT_PRODUCTION_URL}
*
* Production URL for the current deployment, only available in production environments.
*/
export const getProductionUrl = () =>
isProduction
? process.env.URL
? process.env.URL
: process.env.NUXT_ENV_VERCEL_PROJECT_PRODUCTION_URL
? `https://${process.env.NUXT_ENV_VERCEL_PROJECT_PRODUCTION_URL}`
: undefined
: undefined
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 getFileLastUpdated(path: string) {
try {
// Get ISO date of last commit for file
const date = await git.log(['-1', '--format=%cI', '--', path])
return date.latest?.date || new Date().toISOString()
} catch {
return new Date().toISOString()
}
}
/**
* Resolves the **next** version by analysing conventional commits since the
* last reachable `v*` tag. Delegates to {@link getNextVersion} which is also
* used by the `release-tag` and `release-pr` GitHub Actions workflows so the
* version shown in the UI matches the tag that will be created *after* deploy.
*
* Falls back to `package.json` when git is unavailable (e.g. shallow clone).
*/
export async function getVersion() {
try {
const { next } = await getNextVersion()
return next
} catch {
return packageVersion
}
}
export async function getEnv(isDevelopment: boolean) {
const [{ commit, shortCommit, branch }, version] = await Promise.all([getGitInfo(), getVersion()])
const env = isDevelopment ? 'dev' : isCanary ? 'canary' : isPreview ? 'preview' : 'release'
const previewUrl = getPreviewUrl()
const productionUrl = getProductionUrl()
return {
version,
commit,
shortCommit,
branch,
env,
previewUrl,
productionUrl,
prNumber,
} as const
}