-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
107 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import type { BuildInfo } from '~/types' | ||
|
||
export function useBuildInfo() { | ||
return useAppConfig().buildInfo as BuildInfo | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* eslint-disable node/prefer-global/process */ | ||
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' | ||
|
||
/** | ||
* 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 | ||
|
||
/** | ||
* 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 async function getGitInfo() { | ||
const branch = gitBranch || await git.revparse(['--abbrev-ref', 'HEAD']) | ||
const commit = await git.revparse(['HEAD']) | ||
const shortCommit = await git.revparse(['--short=7', 'HEAD']) | ||
return { branch, commit, shortCommit } | ||
} | ||
|
||
export async function getEnv() { | ||
const { commit, shortCommit, branch } = await getGitInfo() | ||
const env = isDevelopment | ||
? 'dev' | ||
: isPreview | ||
? 'preview' | ||
: branch === 'main' | ||
? 'canary' | ||
: 'release' | ||
return { commit, shortCommit, branch, env } as const | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { defineNuxtModule } from '@nuxt/kit' | ||
import { getEnv, version } from '../config/env' | ||
import type { BuildInfo } from '../types' | ||
|
||
export default defineNuxtModule({ | ||
meta: { | ||
name: 'stargram:build-env', | ||
}, | ||
async setup(_options, nuxt) { | ||
const { env, commit, shortCommit, branch } = await getEnv() | ||
const buildInfo: BuildInfo = { | ||
version, | ||
time: +Date.now(), | ||
commit, | ||
shortCommit, | ||
branch, | ||
env, | ||
} | ||
|
||
nuxt.options.appConfig = nuxt.options.appConfig || {} | ||
nuxt.options.appConfig.env = env | ||
nuxt.options.appConfig.buildInfo = buildInfo | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export interface BuildInfo { | ||
version: string | ||
commit: string | ||
shortCommit: string | ||
time: number | ||
branch: string | ||
env: 'preview' | 'canary' | 'dev' | 'release' | ||
} |