-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathversion.ts
39 lines (31 loc) · 948 Bytes
/
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
import { readFile, writeFile } from 'node:fs/promises'
import { fileURLToPath } from 'node:url'
import { $ } from 'execa'
import {
readPackageJson,
versionFiles,
versionPlaceholder,
} from './vite.config.js'
const main = async (): Promise<void> => {
await Promise.all(
versionFiles.map(async (file) => {
const version = await injectVersion(
fileURLToPath(new URL(file, import.meta.url))
)
// eslint-disable-next-line no-console
console.log(`✓ Version ${version} injected into ${file}`)
await $`git add ${file}`
})
)
}
const injectVersion = async (path: string): Promise<string> => {
const { version } = await readPackageJson()
if (version == null) {
throw new Error('Missing version in package.json')
}
const buff = await readFile(path)
const data = buff.toString().replaceAll(versionPlaceholder, version)
await writeFile(path, data)
return version
}
await main()