46 lines
1.1 KiB
TypeScript

import type { AstroIntegration } from 'astro'
import { exec as execSync } from 'node:child_process'
import { promisify } from 'node:util'
const exec = promisify(execSync)
/**
* launch the integration
* @returns the routng integration
*/
const integration: () => AstroIntegration = () => ({
name: 'version',
hooks: {
'astro:config:setup': async (setup) => {
try {
const commit = (await exec('git rev-parse HEAD')).stdout
const branch = (await exec('git rev-parse --abbrev-ref HEAD')).stdout
const tag = (await exec('git tag -l --points-at HEAD')).stdout
const envs: Record<string, string> = {}
if (commit) {
envs['import.meta.env.GIT_COMMIT'] = JSON.stringify(commit.slice(0, 8).trim())
}
if (branch) {
envs['import.meta.env.GIT_BRANCH'] = JSON.stringify(branch.trim())
}
if (tag) {
envs['import.meta.env.GIT_TAG'] = JSON.stringify(tag.trim())
}
setup.updateConfig({
vite: {
define: {
...envs
}
}
})
} catch (error: any) {
setup.logger.warn(error.toString())
setup.logger.warn('could not setup GIT envs')
}
}
}
})
export default integration