Last active
April 25, 2022 06:05
-
-
Save gregfenton/4aa3ed54b545d6c7e3f5b5b39d382e8d to your computer and use it in GitHub Desktop.
Expo - generate iOS buildNumber and Android versionCode from SemVer
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
// version.json: | |
// | |
// { | |
// "version": "3.0.23" | |
// } | |
let VERSION_JSON = `${PROJECT_CONFIG_DIR}/version.json`; | |
versionString = getVersionString(VERSION_JSON); | |
let {buildNumber, versionCode} = parseVersionString(versionString); | |
let appConfigJson = { | |
....... | |
version: buildNumber, | |
....... | |
ios: { | |
buildNumber: buildNumber, | |
...... | |
}, | |
android: { | |
versionCode: versionCode, | |
...... | |
}, | |
...... | |
}; | |
/** | |
*/ | |
const getVersionString = (versionFilePath) => { | |
try { | |
if (!fs.existsSync(versionFilePath)) { | |
die(`Cannot find version file: (${versionFilePath}) pwd(${__dirname})`); | |
} else { | |
let data = fs.readFileSync(versionFilePath, 'utf8'); | |
data = JSON.parse(data); | |
let retVal = data?.version; | |
if (retVal?.length < MIN_LENGTH_VERSION_STRING) { | |
die( | |
`Version string not valid: (${retVal}), ` + | |
`data (${JSON.stringify(data)})` | |
); | |
} | |
return retVal; | |
} | |
} catch (ex) { | |
console.error(ex.message); | |
throw ex; | |
} | |
}; | |
/** | |
*/ | |
const parseVersionString = (versionString) => { | |
let buildNum = versionString; | |
let buildNumParts = buildNum.split('.'); | |
let versionCode = | |
Number(buildNumParts[0] * 1000000) + | |
Number(buildNumParts[1] * 1000) + | |
Number(buildNumParts[2]); | |
return { | |
buildNumber: buildNum, | |
versionCode: versionCode, | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment