Created
September 10, 2020 17:10
-
-
Save foxted/85743f0110d9936c8ee38bb2dd8787b9 to your computer and use it in GitHub Desktop.
Update Android and iOS version
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const path = require('path'); | |
const pkg = require('./package'); | |
const convert = require('xml-js'); | |
const beautify = require('xml-beautifier'); | |
const plist = require('plist'); | |
updateIOSPList(); | |
updateAndroidManifest(); | |
/** | |
* Update iOS PList file with new version | |
*/ | |
function updateIOSPList() { | |
const iosPListPath = path.resolve('./app/App_Resources/iOS/Info.plist'); | |
let iosPListFile = fs.readFileSync(iosPListPath, { encoding: 'utf-8' }).toString(); | |
if(!iosPListFile) { | |
throw new Error("Could not update iOS Plist file"); | |
} | |
let iosPList = plist.parse(iosPListFile); | |
iosPList.CFBundleShortVersionString = pkg.version; | |
iosPList.CFBundleVersion = pkg.version; | |
const newIOSPList = plist.build(iosPList); | |
fs.writeFileSync(iosPListPath, newIOSPList, 'utf8'); | |
} | |
/** | |
* Update Android Manifest with new version | |
*/ | |
function updateAndroidManifest() { | |
const androidManifestPath = path.resolve('./app/App_Resources/Android/src/main/AndroidManifest.xml'); | |
let androidManifest = fs.readFileSync(androidManifestPath, { encoding: 'utf-8' }).toString(); | |
let androidManifestXml = convert.xml2js(androidManifest, { | |
object: true, | |
reversible: true, | |
coerce: false, | |
sanitize: false, | |
trim: false, | |
arrayNotation: false, | |
alternateTextNode: false | |
}); | |
if(!androidManifestXml.elements[0].attributes['android:versionCode'] && !androidManifestXml.elements[0].attributes['android:versionName']) { | |
throw new Error("Could not update Android Manifest"); | |
} | |
androidManifestXml.elements[0].attributes['android:versionCode'] = pkg.version.replace(/\./g, '') + '000'; | |
androidManifestXml.elements[0].attributes['android:versionName'] = pkg.version; | |
const newAndroidManifest = convert.js2xml(androidManifestXml); | |
fs.writeFileSync(androidManifestPath, beautify(newAndroidManifest), 'utf8'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment