-
Notifications
You must be signed in to change notification settings - Fork 1
/
prepack.ts
48 lines (36 loc) · 1.23 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
44
45
46
47
48
import { readFile, writeFile } from 'node:fs/promises'
import { fileURLToPath } from 'node:url'
import { $ } from 'execa'
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 seamapiJavascriptHttpVersion = '0.0.0'",
`const seamapiJavascriptHttpVersion = '${version}'`,
)
await writeFile(path, data)
return version
}
const readPackageJson = async (): Promise<{ version?: string }> => {
const pkgBuff = await readFile(
fileURLToPath(new URL('package.json', import.meta.url)),
)
return JSON.parse(pkgBuff.toString())
}
await main()