-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathversion.ts
More file actions
50 lines (37 loc) · 1.31 KB
/
Copy pathversion.ts
File metadata and controls
50 lines (37 loc) · 1.31 KB
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
import { readFile, writeFile } from 'node:fs/promises'
import { fileURLToPath } from 'node:url'
import { $ } from 'execa'
const versionFile = './src/Version.php'
const versionPattern = /public const VERSION = "[^"]*";/
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 $`git add ${versionFile}`
// eslint-disable-next-line no-console
console.log(`✓ Staged 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 data = (await readFile(path)).toString()
if (!versionPattern.test(data)) {
throw new Error(`Could not find the version constant in ${versionFile}`)
}
await writeFile(
path,
data.replace(versionPattern, `public const VERSION = "${version}";`),
)
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()