-
Notifications
You must be signed in to change notification settings - Fork 2
/
prepack.ts
43 lines (31 loc) · 1.05 KB
/
prepack.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
import { readFile, writeFile } from 'node:fs/promises'
import { fileURLToPath } from 'node:url'
import { $ } from 'execa'
import { readPackageJson } from './vite.config.js'
const versionFile = './src/lib/version.ts'
const main = async (): Promise<void> => {
const version = await injectVersion(
fileURLToPath(new URL(versionFile, import.meta.url))
)
// eslint-disable-next-line no-console
console.log(`✓ Version ${version} injected into ${versionFile}`)
const { command } = await $`tsc --project tsconfig.version.json`
// eslint-disable-next-line no-console
console.log(`✓ Rebuilt with '${command}'`)
}
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()
.replace(
'const seamapiReactVersion = null',
`const seamapiReactVersion = '${version}'`
)
await writeFile(path, data)
return version
}
await main()