|
3 | 3 | import Git from 'simple-git' |
4 | 4 | import * as process from 'node:process' |
5 | 5 |
|
6 | | -export { version } from '../package.json' |
| 6 | +import { version as packageVersion } from '../package.json' |
| 7 | + |
| 8 | +export { packageVersion as version } |
7 | 9 |
|
8 | 10 | /** |
9 | 11 | * Environment variable `PULL_REQUEST` provided by Netlify. |
@@ -41,22 +43,24 @@ export const gitBranch = process.env.BRANCH || process.env.VERCEL_GIT_COMMIT_REF |
41 | 43 | /** |
42 | 44 | * Whether this is the canary environment (main.npmx.dev). |
43 | 45 | * |
44 | | - * Detected via the custom Vercel environment (`VERCEL_ENV === 'canary'`), |
45 | | - * or as a fallback, a production deploy from the `main` branch. |
| 46 | + * Detected as any non-PR Vercel deploy from the `main` branch |
| 47 | + * (which may receive `VERCEL_ENV === 'production'` or `'preview'` |
| 48 | + * depending on the project's production branch configuration). |
46 | 49 | * |
47 | 50 | * @see {@link https://vercel.com/docs/environment-variables/system-environment-variables#VERCEL_ENV} |
48 | 51 | */ |
49 | 52 | export const isCanary = |
50 | | - process.env.VERCEL_ENV === 'canary' || |
51 | | - (process.env.VERCEL_ENV === 'production' && gitBranch === 'main') |
| 53 | + (process.env.VERCEL_ENV === 'production' || process.env.VERCEL_ENV === 'preview') && |
| 54 | + gitBranch === 'main' && |
| 55 | + !isPR |
52 | 56 |
|
53 | 57 | /** |
54 | 58 | * Environment variable `CONTEXT` provided by Netlify. |
55 | 59 | * `dev`, `production`, `deploy-preview`, `branch-deploy`, `preview-server`, or a branch name |
56 | 60 | * @see {@link https://docs.netlify.com/build/configure-builds/environment-variables/#build-metadata} |
57 | 61 | * |
58 | 62 | * Environment variable `VERCEL_ENV` provided by Vercel. |
59 | | - * `production`, `preview`, `development`, or a custom environment name (e.g. `canary`). |
| 63 | + * `production`, `preview`, or `development`. |
60 | 64 | * @see {@link https://vercel.com/docs/environment-variables/system-environment-variables#VERCEL_ENV} |
61 | 65 | * |
62 | 66 | * Whether this is some sort of preview environment. |
@@ -152,12 +156,28 @@ export async function getFileLastUpdated(path: string) { |
152 | 156 | } |
153 | 157 | } |
154 | 158 |
|
| 159 | +/** |
| 160 | + * Resolves the current version from git tags, falling back to `package.json`. |
| 161 | + * |
| 162 | + * Uses `git describe --tags --abbrev=0 --match 'v*'` to find the most recent |
| 163 | + * reachable release tag (e.g. `v0.1.0` -> `0.1.0`). |
| 164 | + */ |
| 165 | +export async function getVersion() { |
| 166 | + try { |
| 167 | + const tag = (await git.raw(['describe', '--tags', '--abbrev=0', '--match', 'v*'])).trim() |
| 168 | + return tag.replace(/^v/, '') |
| 169 | + } catch { |
| 170 | + return packageVersion |
| 171 | + } |
| 172 | +} |
| 173 | + |
155 | 174 | export async function getEnv(isDevelopment: boolean) { |
156 | | - const { commit, shortCommit, branch } = await getGitInfo() |
| 175 | + const [{ commit, shortCommit, branch }, version] = await Promise.all([getGitInfo(), getVersion()]) |
157 | 176 | const env = isDevelopment ? 'dev' : isCanary ? 'canary' : isPreview ? 'preview' : 'release' |
158 | 177 | const previewUrl = getPreviewUrl() |
159 | 178 | const productionUrl = getProductionUrl() |
160 | 179 | return { |
| 180 | + version, |
161 | 181 | commit, |
162 | 182 | shortCommit, |
163 | 183 | branch, |
|
0 commit comments