forked from dsernst/GoenkaNative
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.js
67 lines (59 loc) · 2.1 KB
/
version.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Updates iOS & Android App Version
// ===================
//
// This script extends `npm version` functionality to also
// update the version number in Info.plist for the App Store,
// and the versionCode and versionName for Google Play Store.
//
// It runs after package.json version is updated, but before git commit.
//
// See https://docs.npmjs.com/cli/version for more info.
//
//
// ### Install
//
// Add to package.json scripts {
// "version": "node ./version.js",
// }
//
// ### Usage
//
// "npm version [major|minor|patch]"
//
const { version } = require('./package.json')
const { promisify } = require('util')
const fs = require('fs')
const exec = promisify(require('child_process').exec)
const plistPath = require('path').join(__dirname, './ios/GoenkaNative/Info.plist')
const versionCode = version
.split('.')
.map((num, index) => (Number(num) < 10 && index > 0 ? '0' + num : num))
.join('')
;(async () => {
// Update Android's build.gradle file
const gradleBuildPath = './android/app/build.gradle'
const gradleBuildFile = fs.readFileSync(gradleBuildPath, 'utf8')
const newGradleFile = gradleBuildFile
.split('\n')
.map(line => {
// Increment versionCode (must be unique, but hidden from users)
const versionCodeIndex = line.indexOf('versionCode ')
if (versionCodeIndex !== -1) {
return line.slice(0, versionCodeIndex + 12) + versionCode
}
// Update versionName (user-facing)
const versionNameIndex = line.indexOf('versionName ')
if (versionNameIndex !== -1) {
return line.slice(0, versionNameIndex + 13) + version + '"'
}
return line
})
.join('\n')
fs.writeFileSync(gradleBuildPath, newGradleFile)
// Write new version number to iOS's Info.plist
await exec(`/usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString ${version}" ${plistPath}`)
await exec(`/usr/libexec/PlistBuddy -c "Set :CFBundleVersion ${versionCode}" ${plistPath}`)
// Add the changed files to git's index before it commits
await exec('git add .')
console.log(`✅ Updated ios & android apps to v${version}(${versionCode})`)
})()