forked from fluidd-core/fluidd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.inject-version.ts
52 lines (42 loc) · 1.25 KB
/
vite.config.inject-version.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import child_process from 'child_process'
import fs from 'fs'
import path from 'path'
import { version } from './package.json'
import type { Plugin } from 'vite'
const writeVersionFile = async () => {
const versionFile = await fs.promises.open(path.resolve(__dirname, 'dist/.version'), 'w')
await versionFile.writeFile(`v${version}`)
await versionFile.close()
}
const writeReleaseInfoFile = async () => {
const releaseInfoFile = await fs.promises.open(path.resolve(__dirname, 'dist/release_info.json'), 'w')
await releaseInfoFile.writeFile(JSON.stringify({
project_name: 'fluidd',
project_owner: 'fluidd-core',
version: `v${version}`
}))
await releaseInfoFile.close()
}
const vitePluginInjectVersion = (): Plugin => {
return {
name: 'version',
config: () => {
const git_hash = child_process
.execSync('git rev-parse --short HEAD')
.toString()
return {
define: {
'import.meta.env.VERSION': JSON.stringify(version),
'import.meta.env.HASH': JSON.stringify(git_hash)
}
}
},
writeBundle: () => {
setImmediate(async () => {
await writeVersionFile()
await writeReleaseInfoFile()
})
}
}
}
export default vitePluginInjectVersion