Skip to content

Commit 7cf29f0

Browse files
committed
use min version to conditionally enable build number
1 parent 385c7ea commit 7cf29f0

2 files changed

Lines changed: 40 additions & 18 deletions

File tree

script/command-executor.ts

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
UpdateMetrics,
3333
} from "../script/types";
3434
import {
35+
buildAppVersion,
3536
getBundleSourceMapOutput,
3637
getMinifyParams,
3738
getReactNativePackagePath,
@@ -922,10 +923,7 @@ function getReactNativeProjectAppVersion(command: cli.IReleaseReactCommand, proj
922923

923924
if (parsedPlist && parsedPlist.CFBundleShortVersionString) {
924925
if (isValidVersion(parsedPlist.CFBundleShortVersionString)) {
925-
let appVersion: string = parsedPlist.CFBundleShortVersionString;
926-
if (parsedPlist.CFBundleVersion) {
927-
appVersion = `${appVersion}-${parsedPlist.CFBundleVersion}`;
928-
}
926+
const appVersion: string = buildAppVersion(parsedPlist.CFBundleShortVersionString, parsedPlist.CFBundleVersion);
929927
log(`Using the target binary version value "${appVersion}" from "${resolvedPlistFile}".\n`);
930928
return Q(appVersion);
931929
} else {
@@ -960,7 +958,7 @@ function getReactNativeProjectAppVersion(command: cli.IReleaseReactCommand, proj
960958
})
961959
.then((buildGradle: any) => {
962960
let versionName: string = null;
963-
let versionCode: string = null;
961+
let versionCode: number | null = null;
964962

965963
// First 'if' statement was implemented as workaround for case
966964
// when 'build.gradle' file contains several 'android' nodes.
@@ -995,9 +993,7 @@ function getReactNativeProjectAppVersion(command: cli.IReleaseReactCommand, proj
995993
if (isValidVersion(appVersion)) {
996994
// The versionName property is a valid semver string,
997995
// so we can safely use that and move on.
998-
if (versionCode) {
999-
appVersion = `${appVersion}-${versionCode}`;
1000-
}
996+
appVersion = buildAppVersion(appVersion, versionCode);
1001997
log(`Using the target binary version value "${appVersion}" from "${buildGradlePath}".\n`);
1002998
return appVersion;
1003999
} else if (/^\d.*/.test(appVersion)) {
@@ -1044,9 +1040,7 @@ function getReactNativeProjectAppVersion(command: cli.IReleaseReactCommand, proj
10441040
);
10451041
}
10461042

1047-
if (versionCode) {
1048-
appVersion = `${appVersion}-${versionCode}`;
1049-
}
1043+
appVersion = buildAppVersion(appVersion, versionCode);
10501044
log(`Using the target binary version value "${appVersion}" from the "${propertyName}" key in the "${propertiesFile}" file.\n`);
10511045
return appVersion.toString();
10521046
});
@@ -1120,7 +1114,7 @@ function getAppVersionFromXcodeProject(command: cli.IReleaseReactCommand, projec
11201114
}
11211115

11221116
const currentProjectVersion = xcodeProj.getBuildProperty("CURRENT_PROJECT_VERSION", command.buildConfigurationName, command.xcodeTargetName);
1123-
const appVersion = currentProjectVersion ? `${marketingVersion}-${currentProjectVersion}` : marketingVersion;
1117+
const appVersion = buildAppVersion(marketingVersion, currentProjectVersion);
11241118

11251119
console.log(`Using the target binary version value "${appVersion}" from "${resolvedPbxprojFile}".\n`);
11261120

@@ -1659,9 +1653,7 @@ export const releaseNative = (command: cli.IReleaseNativeCommand): Promise<void>
16591653
await extractIPA(targetBinaryPath, extractFolder);
16601654
const metadataZip = await extractMetadataFromIOS(extractFolder, outputFolder);
16611655
const buildVersion = await getIosVersion(extractFolder);
1662-
const iosAppStoreVersion = buildVersion?.build
1663-
? `${buildVersion.version}-${buildVersion.build}`
1664-
: buildVersion?.version;
1656+
const iosAppStoreVersion = buildAppVersion(buildVersion.version, buildVersion.build);
16651657
releaseCommandPartial = { package: metadataZip, appStoreVersion: iosAppStoreVersion };
16661658
} else {
16671659
if (targetBinaryPathNormalised.endsWith(".apk")) {
@@ -1670,14 +1662,14 @@ export const releaseNative = (command: cli.IReleaseNativeCommand): Promise<void>
16701662

16711663
const reader = await ApkReader.open(targetBinaryPath);
16721664
const { versionName, versionCode } = await reader.readManifest();
1673-
const apkAppStoreVersion = versionCode ? `${versionName}-${versionCode}` : versionName;
1665+
const apkAppStoreVersion = buildAppVersion(versionName, versionCode);
16741666
const metadataZip = await extractMetadataFromAndroid(extractFolder, outputFolder);
16751667
releaseCommandPartial = { package: metadataZip, appStoreVersion: apkAppStoreVersion };
16761668
} else if (targetBinaryPathNormalised.endsWith(".aab")) {
16771669
log(chalk.cyan(`\nExtracting AAB file:\n`));
16781670
await extractAAB(targetBinaryPath, extractFolder);
16791671
const { versionName, versionCode } = await aabParser.parseAabManifest(targetBinaryPath);
1680-
const aabAppStoreVersion = versionCode ? `${versionName}-${versionCode}` : versionName;
1672+
const aabAppStoreVersion = buildAppVersion(versionName, versionCode);
16811673

16821674
const metadataZip = await extractMetadataFromAndroid(`${extractFolder}/base`, outputFolder); // base folder is nested in AAB
16831675
releaseCommandPartial = { package: metadataZip, appStoreVersion: aabAppStoreVersion };

script/react-native-utils.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as fs from "fs";
22
import * as chalk from "chalk";
33
import * as path from "path";
44
import * as childProcess from "child_process";
5-
import { coerce, compare, valid } from "semver";
5+
import { coerce, compare, gte, valid } from "semver";
66
import { downloadBlob, extractIPA, fileDoesNotExistOrIsDirectory } from "./utils/file-utils";
77
import * as dotenv from "dotenv";
88
import { DotenvParseOutput } from "dotenv";
@@ -493,3 +493,33 @@ export function getReactNativeVersion(): string {
493493
);
494494
}
495495
}
496+
497+
function getRevopushCodePushVersion(): string | null {
498+
try {
499+
const result = childProcess.spawnSync("node", ["--print", "require('@revopush/react-native-code-push/package.json').version"]);
500+
if (result.status !== 0 || !result.stdout) {
501+
return null;
502+
}
503+
return result.stdout.toString().trim();
504+
} catch {
505+
return null;
506+
}
507+
}
508+
509+
const BUILD_NUMBER_MIN_VERSION = "2.0.0";
510+
511+
function isBuildNumberSupported(): boolean {
512+
const version = getRevopushCodePushVersion();
513+
if (!version) {
514+
return false;
515+
}
516+
const coerced = coerce(version);
517+
return coerced ? gte(coerced, BUILD_NUMBER_MIN_VERSION) : false;
518+
}
519+
520+
export function buildAppVersion(version: string, buildNumber: string | number | undefined): string {
521+
if (buildNumber && isBuildNumberSupported()) {
522+
return `${version}-${buildNumber}`;
523+
}
524+
return version;
525+
}

0 commit comments

Comments
 (0)