-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.js
45 lines (36 loc) · 1.29 KB
/
update.js
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
const fs = require('fs')
const ENCODING = 'utf8'
const FILE_BUILD_GRADLE = 'android/app/build.gradle'
const FILE_PACKAGE_JSON = 'package.json'
if (process.argv.length < 3) {
throw 'specify next version'
}
const nextVersionMatch = process.argv[2].match(/^\d+\.\d+\.\d+$/)
if (!nextVersionMatch) {
throw 'wrong next version format'
}
const nextVersion = nextVersionMatch[0]
const updateVersion = (file, regex, replace) => {
const data = fs.readFileSync(file, { encoding: ENCODING })
const nextData = data.replace(regex, replace)
fs.writeFileSync(file, nextData, { encoding: ENCODING })
}
updateVersion(FILE_PACKAGE_JSON, /"version":\s"\d+\.\d+\.\d+"/, `"version": "${nextVersion}"`)
updateVersion(FILE_BUILD_GRADLE, /versionName\s"\d+\.\d+\.\d+"/, `versionName "${nextVersion}"`)
const incrementVersionCode = () => {
const data = fs.readFileSync(FILE_BUILD_GRADLE, { encoding: ENCODING })
const regex = /versionCode\s\d+/
const match = data.match(regex)
if (!match) {
throw 'no version code match'
}
split = match[0].split(' ')
const versionCode = parseInt(split[1])
const nextData = data.replace(
regex,
`versionCode ${versionCode + 1}`
)
fs.writeFileSync(FILE_BUILD_GRADLE, nextData, { encoding: ENCODING })
}
incrementVersionCode()
console.log(`updated to v${nextVersion}`)