Skip to content

Instantly share code, notes, and snippets.

@gregfenton
Last active April 25, 2022 06:05
Show Gist options
  • Save gregfenton/4aa3ed54b545d6c7e3f5b5b39d382e8d to your computer and use it in GitHub Desktop.
Save gregfenton/4aa3ed54b545d6c7e3f5b5b39d382e8d to your computer and use it in GitHub Desktop.
Expo - generate iOS buildNumber and Android versionCode from SemVer
// 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