Skip to content

Instantly share code, notes, and snippets.

@pafry7
Last active June 19, 2024 14:57
Show Gist options
  • Save pafry7/18fe60910a7807ea9a7223027bb84863 to your computer and use it in GitHub Desktop.
Save pafry7/18fe60910a7807ea9a7223027bb84863 to your computer and use it in GitHub Desktop.
Incrementing buildNumber and versionCode in Expo projects
import JsonFileModule from '@expo/json-file';
import path from "path";
import { getConfig } from "@expo/config"
const JsonFile = JsonFileModule.default;
function getNewVersionCode(versionCode) {
return versionCode ? versionCode + 1 : 1;
}
async function increment() {
const projPath = path.resolve(
process.cwd(),
<path_to_proj>
);
let appConfig;
try {
appConfig = getConfig(projPath, { isPublicConfig: true });;
} catch (error) {
console.log(error.message);
}
const { pkg } = appConfig;
if (!pkg.dependencies?.["expo"]) {
console.log("This library works only for project using Expo");
process.exit(1);
}
const versionCode = appConfig.exp.android.versionCode;
const buildNumber = appConfig.exp.ios.buildNumber;
const modifications = {
android: { versionCode: getNewVersionCode(versionCode) }, ios: {
buildNumber: getNewVersionCode(
parseInt(buildNumber, 10),
)
}
}
// COPIED FROM EXPO SOURCE CODE
//
// Static with no dynamic config, this means we can append to the config automatically.
let outputConfig;
// If the config has an expo object (app.json) then append the options to that object.
if (appConfig.rootConfig.expo) {
outputConfig = {
...appConfig.rootConfig,
expo: { ...appConfig.rootConfig.expo, ...modifications },
};
} else {
// Otherwise (app.config.json) just add the config modification to the top most level.
outputConfig = { ...appConfig.rootConfig, ...modifications };
}
const appJsonFile = new JsonFile(appConfig.staticConfigPath);
await appJsonFile.writeAsync(outputConfig, { json5: false });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment