1
0
mirror of https://github.com/elk-zone/elk synced 2024-11-27 22:38:11 +09:00
elk/config/env.ts

48 lines
1.4 KiB
TypeScript
Raw Normal View History

2023-01-04 22:26:30 +09:00
import Git from 'simple-git'
import { isDevelopment } from 'std-env'
export { version } from '../package.json'
/**
* Environment variable `PULL_REQUEST` provided by Netlify.
* @see {@link https://docs.netlify.com/configure-builds/environment-variables/#git-metadata}
*
* Whether triggered by a GitHub PR
*/
export const isPR = process.env.PULL_REQUEST === 'true'
2023-01-04 22:41:48 +09:00
/**
* Environment variable `BRANCH` provided by Netlify.
* @see {@link https://docs.netlify.com/configure-builds/environment-variables/#git-metadata}
*
* Git branch
*/
export const gitBranch = process.env.BRANCH
2023-01-04 22:26:30 +09:00
/**
* Environment variable `CONTEXT` provided by Netlify.
* @see {@link https://docs.netlify.com/configure-builds/environment-variables/#build-metadata}
*
* Whether triggered by PR, `deploy-preview` or `dev`.
*/
export const isPreview = isPR || process.env.CONTEXT === 'deploy-preview' || process.env.CONTEXT === 'dev'
const git = Git()
export const getGitInfo = async () => {
2023-01-04 22:41:48 +09:00
const branch = gitBranch || await git.revparse(['--abbrev-ref', 'HEAD'])
2023-01-04 22:26:30 +09:00
const commit = await git.revparse(['HEAD'])
return { branch, commit }
}
export const getEnv = async () => {
const { commit, branch } = await getGitInfo()
const env = isDevelopment
? 'dev'
: isPreview
? 'preview'
: branch === 'main'
2023-01-07 01:20:12 +09:00
? 'canary'
2023-01-04 22:26:30 +09:00
: 'release'
return { commit, branch, env } as const
}