This repository has been archived by the owner on Aug 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
executable file
·139 lines (122 loc) · 3.59 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env node
import * as fs from 'fs';
import * as yargs from 'yargs';
const options = yargs
.usage(
`Usage:
npx react-native-versioning \\
-t --target <versionCode> \\
-b --build <buildNumber> \\
--android-only \\
--ios-only
`,
)
.options({
target: {
alias: 't',
string: true,
desc: 'target version to Upgrade',
},
build: {
alias: 'b',
number: true,
desc: 'target build number to Upgrade',
},
androidOnly: {
alias: 'android-only',
boolean: true,
desc: 'versioning only android',
},
iosOnly: {
alias: 'ios-only',
boolean: true,
desc: 'versioning only iOS',
},
}).argv;
function validateArguments(
targetVersion?: string,
targetBuildNumber?: number,
): boolean {
return (
typeof targetVersion === 'undefined' &&
typeof targetBuildNumber === 'undefined'
);
}
function updateIOS(targetVersion?: string, targetBuildNumber?: number): void {
if (validateArguments(targetVersion, targetBuildNumber)) return;
const files = fs.readdirSync('ios');
const xcodeprojName = files.find((file: string) =>
file.endsWith('xcodeproj'),
);
if (xcodeprojName) {
const pbxprojPath = `ios/${xcodeprojName}/project.pbxproj`;
if (fs.existsSync(pbxprojPath)) {
let replaced = fs.readFileSync(pbxprojPath, 'utf8');
if (replaced) {
if (typeof targetVersion !== 'undefined') {
if (replaced.indexOf('MARKETING_VERSION') === -1) {
replaced = replaced.replace(
/(LD_RUNPATH_SEARCH_PATHS = [^;]+;([\n\s]+))/g,
'$1MARKETING_VERSION = 1.0.0;$2',
);
const projectName = xcodeprojName.replace('.xcodeproj', '');
const plistPath = `ios/${projectName}/Info.plist`;
if (fs.existsSync(plistPath)) {
let plist = fs.readFileSync(plistPath, 'utf8');
plist = plist.replace(
/(<key>CFBundleShortVersionString<\/key>[\n\s]+<string>)[^<]+(<\/string>)/,
'$1$(MARKETING_VERSION)$2',
);
fs.writeFileSync(plistPath, plist);
}
}
replaced = replaced.replace(
/MARKETING_VERSION = [^;]+;/g,
`MARKETING_VERSION = ${targetVersion};`,
);
}
if (typeof targetBuildNumber !== 'undefined') {
replaced = replaced.replace(
/CURRENT_PROJECT_VERSION = [^;]+;/g,
`CURRENT_PROJECT_VERSION = ${targetBuildNumber};`,
);
}
}
fs.writeFileSync(pbxprojPath, replaced);
}
}
}
function updateAndroid(
targetVersion?: string,
targetBuildNumber?: number,
): void {
// versionCode 1
// versionName "1.0"
if (validateArguments(targetVersion, targetBuildNumber)) return;
const buildGradlePath = 'android/app/build.gradle';
if (fs.existsSync(buildGradlePath)) {
let replaced = fs.readFileSync(buildGradlePath, 'utf8');
if (replaced) {
if (typeof targetVersion !== 'undefined') {
replaced = replaced.replace(
/versionName\s+\S+/g,
`versionName "${targetVersion}"`,
);
}
if (typeof targetBuildNumber !== 'undefined') {
replaced = replaced.replace(
/versionCode\s+\d+/g,
`versionCode ${targetBuildNumber}`,
);
}
}
fs.writeFileSync(buildGradlePath, replaced);
}
}
if (options.iosOnly !== false) {
updateIOS(options.target, options.build);
}
if (options.androidOnly !== false) {
updateAndroid(options.target, options.build);
}
console.log('Update Successful!');