-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdateAppVersion.js
More file actions
86 lines (75 loc) · 3.46 KB
/
updateAppVersion.js
File metadata and controls
86 lines (75 loc) · 3.46 KB
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
const fs = require('fs');
const readline = require('readline');
const path = require('path');
const colors = {
reset: "\x1b[0m",
bright: "\x1b[1m",
cyan: "\x1b[36m",
};
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question(`${colors.bright}Enter the ${colors.cyan}versionName: ${colors.reset}`, (versionName) => {
if (!versionName) {
console.error('Invalid versionName. Please provide a versionName.');
rl.close();
return;
}
rl.question(`${colors.bright}Enter the ${colors.cyan}versionCode${colors.bright} (Android only, default = 1): ${colors.reset}`, (versionCode) => {
if (!versionCode || isNaN(versionCode)) {
versionCode = 1;
}
updateVersionInfo(versionCode, versionName);
rl.close();
});
});
const updateVersionInfo = (versionCode, version) => {
const packageJsonPath = path.join(__dirname, 'package.json');
const packageLockJsonPath = path.join(__dirname, 'package-lock.json');
const buildGradlePath = path.join(__dirname, 'android', 'app', 'build.gradle');
const pbxprojPath = path.join(__dirname, 'ios', 'TipCalculator.xcodeproj', 'project.pbxproj');
try {
// Android build.gradle
let buildGradleContent = fs.readFileSync(buildGradlePath, 'utf8');
buildGradleContent = buildGradleContent.replace(
/versionName "[^"]*"/,
`versionName "${version}"`
);
buildGradleContent = buildGradleContent.replace(
/versionCode\s+\d+/,
`versionCode ${versionCode}`
);
fs.writeFileSync(buildGradlePath, buildGradleContent, 'utf8');
console.log(`Updated versionName in build.gradle to "${version}"`);
// package.json
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
packageJson.version = version;
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2) + '\n', 'utf8');
console.log(`Updated package.json version to ${version}`);
// package-lock.json
const packageLockJson = JSON.parse(fs.readFileSync(packageLockJsonPath, 'utf8'));
packageLockJson.version = version;
if (packageLockJson.packages && packageLockJson.packages['']) {
packageLockJson.packages[''].version = version;
}
fs.writeFileSync(packageLockJsonPath, JSON.stringify(packageLockJson, null, 2) + '\n', 'utf8');
console.log(`Updated package-lock.json version to ${version}`);
// iOS project.pbxproj (MARKETING_VERSION)
let pbxprojContent = fs.readFileSync(pbxprojPath, 'utf8');
pbxprojContent = pbxprojContent.replace(
/(MARKETING_VERSION = )([^;\n]+)/g,
`$1${version}`
);
fs.writeFileSync(pbxprojPath, pbxprojContent, 'utf8');
console.log(`Updated MARKETING_VERSION in project.pbxproj to "${version}"`);
// app/configs/constants.ts (APP_INFO.version)
const constantsPath = path.join(__dirname, 'app', 'configs', 'constants.ts');
let constantsContent = fs.readFileSync(constantsPath, 'utf8');
constantsContent = constantsContent.replace(/(APP_INFO\s*=\s*{[^}]*version:\s*')[^']*(',?)/, `$1${version}$2`);
fs.writeFileSync(constantsPath, constantsContent, 'utf8');
console.log(`Updated APP_INFO.version in constants.ts to ${version}`);
} catch (error) {
console.error('Error updating version info:', error);
}
};