Skip to content

Commit f5577a2

Browse files
DMcNamarastovmascript
authored andcommitted
Format CFBundleShortVersionString to spec
According to Apple: > The release version number is a string composed of three period-separated integers. This change makes an attempt to pull out a string that is valid for CFBundleShortVersionString from the input, returning the input if no matches are found
1 parent bf174cd commit f5577a2

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

index.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,20 @@ function getNewVersionCode(programOpts, versionCode, versionName, resetBuild) {
9595
return versionCode ? versionCode + 1 : 1;
9696
}
9797

98+
/**
99+
* CFBundleShortVersionString must be a string composed of three period-separated integers.
100+
* @private
101+
* @param {String} versionName The full version string
102+
* @return {String} e.g. returns '1.2.3' for given '1.2.3-beta.1'. Returns `versionName` if no match is found.
103+
*/
104+
function getCFBundleShortVersionString(versionName) {
105+
const match =
106+
versionName && typeof versionName === "string"
107+
? versionName.match(/\d*\.\d*.\d*/g)
108+
: [];
109+
return match && match[0] ? match[0] : versionName;
110+
}
111+
98112
/**
99113
* Determines whether the project is an Expo app or a plain React Native app
100114
* @private
@@ -428,7 +442,9 @@ function version(program, projectPath) {
428442
json,
429443
!programOpts.incrementBuild
430444
? {
431-
CFBundleShortVersionString: appPkg.version
445+
CFBundleShortVersionString: getCFBundleShortVersionString(
446+
appPkg.version
447+
)
432448
}
433449
: {},
434450
!programOpts.neverIncrementBuild
@@ -607,6 +623,7 @@ function version(program, projectPath) {
607623
}
608624

609625
module.exports = {
626+
getCFBundleShortVersionString: getCFBundleShortVersionString,
610627
getDefaults: getDefaults,
611628
getPlistFilenames: getPlistFilenames,
612629
isExpoProject: isExpoProject,

test/shortBundleVerions.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { getCFBundleShortVersionString } from '../';
2+
import test from "ava";
3+
4+
test(
5+
"CFBundleShortVersionString basic",
6+
t => {
7+
const v = getCFBundleShortVersionString('1.2.3');
8+
t.is(v, '1.2.3');
9+
}
10+
);
11+
12+
test(
13+
"CFBundleShortVersionString alpha",
14+
t => {
15+
const v = getCFBundleShortVersionString('1.2.3-alpha');
16+
t.is(v, '1.2.3');
17+
}
18+
);
19+
20+
test(
21+
"CFBundleShortVersionString alpha point",
22+
t => {
23+
const v = getCFBundleShortVersionString('1.2.3-alpha.0');
24+
t.is(v, '1.2.3');
25+
}
26+
);
27+
28+
test(
29+
"CFBundleShortVersionString dash number",
30+
t => {
31+
const v = getCFBundleShortVersionString('1.2.3-0');
32+
t.is(v, '1.2.3');
33+
}
34+
);
35+
36+
test(
37+
"CFBundleShortVersionString extra dot",
38+
t => {
39+
const v = getCFBundleShortVersionString('1.2.3.0');
40+
t.is(v, '1.2.3');
41+
}
42+
);
43+
44+
test(
45+
"CFBundleShortVersionString garbage in, garbage out",
46+
t => {
47+
const v = getCFBundleShortVersionString('garbage');
48+
t.is(v, 'garbage');
49+
}
50+
);

0 commit comments

Comments
 (0)