forked from
npmx.dev/npmx.dev
[READ-ONLY]
a fast, modern browser for the npm registry
1// TODO(serhalp): Extract most of this module to https://github.com/unjs/std-env.
2
3import Git from 'simple-git'
4import * as process from 'node:process'
5
6export { version } from '../package.json'
7
8/**
9 * Environment variable `PULL_REQUEST` provided by Netlify.
10 * @see {@link https://docs.netlify.com/build/configure-builds/environment-variables/#git-metadata}
11 *
12 * Environment variable `VERCEL_GIT_PULL_REQUEST_ID` provided by Vercel.
13 * @see {@link https://vercel.com/docs/environment-variables/system-environment-variables#VERCEL_GIT_PULL_REQUEST_ID}
14 *
15 * Whether triggered by a GitHub PR
16 */
17export const isPR = process.env.PULL_REQUEST === 'true' || !!process.env.VERCEL_GIT_PULL_REQUEST_ID
18
19/**
20 * Environment variable `BRANCH` provided by Netlify.
21 * @see {@link https://docs.netlify.com/build/configure-builds/environment-variables/#git-metadata}
22 *
23 * Environment variable `VERCEL_GIT_COMMIT_REF` provided by Vercel.
24 * @see {@link https://vercel.com/docs/environment-variables/system-environment-variables#VERCEL_GIT_COMMIT_REF}
25 *
26 * Git branch
27 */
28export const gitBranch = process.env.BRANCH || process.env.VERCEL_GIT_COMMIT_REF
29
30/**
31 * Environment variable `CONTEXT` provided by Netlify.
32 * `dev`, `production`, `deploy-preview`, `branch-deploy`, `preview-server`, or a branch name
33 * @see {@link https://docs.netlify.com/build/configure-builds/environment-variables/#build-metadata}
34 *
35 * Environment variable `VERCEL_ENV` provided by Vercel.
36 * `production`, `preview`, or `development`
37 * @see {@link https://vercel.com/docs/environment-variables/system-environment-variables#VERCEL_ENV}
38 *
39 * Whether this is some sort of preview environment.
40 */
41export const isPreview =
42 isPR ||
43 (process.env.CONTEXT && process.env.CONTEXT !== 'production') ||
44 process.env.VERCEL_ENV === 'preview' ||
45 process.env.VERCEL_ENV === 'development'
46export const isProduction =
47 process.env.CONTEXT === 'production' || process.env.VERCEL_ENV === 'production'
48
49/**
50 * Environment variable `URL` provided by Netlify.
51 * This is always the current deploy URL, regardless of env.
52 * @see {@link https://docs.netlify.com/build/functions/environment-variables/#functions}
53 *
54 * Environment variable `VERCEL_URL` provided by Vercel.
55 * This is always the current deploy URL, regardless of env.
56 * NOTE: Not a valid URL, as the protocol is omitted.
57 * @see {@link https://vercel.com/docs/environment-variables/system-environment-variables#VERCEL_URL}
58 *
59 * Preview URL for the current deployment, only available in preview environments.
60 */
61export const getPreviewUrl = () =>
62 isPreview
63 ? process.env.URL
64 ? process.env.URL
65 : process.env.NUXT_ENV_VERCEL_URL
66 ? `https://${process.env.NUXT_ENV_VERCEL_URL}`
67 : undefined
68 : undefined
69
70/**
71 * Environment variable `URL` provided by Netlify.
72 * This is always the current deploy URL, regardless of env.
73 * @see {@link https://docs.netlify.com/build/functions/environment-variables/#functions}
74 *
75 * Environment variable `VERCEL_PROJECT_PRODUCTION_URL` provided by Vercel.
76 * NOTE: Not a valid URL, as the protocol is omitted.
77 * @see {@link https://vercel.com/docs/environment-variables/system-environment-variables#VERCEL_PROJECT_PRODUCTION_URL}
78 *
79 * Production URL for the current deployment, only available in production environments.
80 */
81export const getProductionUrl = () =>
82 isProduction
83 ? process.env.URL
84 ? process.env.URL
85 : process.env.NUXT_ENV_VERCEL_PROJECT_PRODUCTION_URL
86 ? `https://${process.env.NUXT_ENV_VERCEL_PROJECT_PRODUCTION_URL}`
87 : undefined
88 : undefined
89
90const git = Git()
91export async function getGitInfo() {
92 let branch
93 try {
94 branch = gitBranch || (await git.revparse(['--abbrev-ref', 'HEAD']))
95 } catch {
96 branch = 'unknown'
97 }
98
99 let commit
100 try {
101 // Netlify: COMMIT_REF, Vercel: VERCEL_GIT_COMMIT_SHA
102 commit =
103 process.env.COMMIT_REF || process.env.VERCEL_GIT_COMMIT_SHA || (await git.revparse(['HEAD']))
104 } catch {
105 commit = 'unknown'
106 }
107
108 let shortCommit
109 try {
110 if (commit && commit !== 'unknown') {
111 shortCommit = commit.slice(0, 7)
112 } else {
113 shortCommit = await git.revparse(['--short=7', 'HEAD'])
114 }
115 } catch {
116 shortCommit = 'unknown'
117 }
118
119 return { branch, commit, shortCommit }
120}
121
122export async function getFileLastUpdated(path: string) {
123 try {
124 // Get ISO date of last commit for file
125 const date = await git.log(['-1', '--format=%cI', '--', path])
126 return date.latest?.date || new Date().toISOString()
127 } catch {
128 return new Date().toISOString()
129 }
130}
131
132export async function getEnv(isDevelopment: boolean) {
133 const { commit, shortCommit, branch } = await getGitInfo()
134 const env = isDevelopment
135 ? 'dev'
136 : isPreview
137 ? 'preview'
138 : branch === 'main'
139 ? 'canary'
140 : 'release'
141 const previewUrl = getPreviewUrl()
142 const productionUrl = getProductionUrl()
143 return {
144 commit,
145 shortCommit,
146 branch,
147 env,
148 previewUrl,
149 productionUrl,
150 } as const
151}