Repository navigation
Expand file tree
/
Copy pathbump-version.js
More file actions
129 lines (107 loc) · 3.71 KB
/
Copy pathbump-version.js
File metadata and controls
129 lines (107 loc) · 3.71 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
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
#!/usr/bin/env node
import fs from 'fs';
import readline from 'readline';
const MANIFEST_FILE = './extension-manifests.js';
const XCODE_PROJECT = './bundle/safari/Minimal Twitter/Minimal Twitter.xcodeproj/project.pbxproj';
// Read current version from the shared extension manifest source.
function getCurrentVersion() {
const content = fs.readFileSync(MANIFEST_FILE, 'utf8');
const versionMatch = content.match(/version:\s*"(\d+\.\d+\.\d+)"/);
if (!versionMatch) {
throw new Error(`Could not find version in ${MANIFEST_FILE}`);
}
return versionMatch[1];
}
// Parse version string into parts
function parseVersion(version) {
const parts = version.split('.').map(Number);
return {
major: parts[0],
minor: parts[1],
patch: parts[2]
};
}
// Bump version based on type
function bumpVersion(version, type) {
const v = parseVersion(version);
switch (type) {
case 'major':
return `${v.major + 1}.0.0`;
case 'minor':
return `${v.major}.${v.minor + 1}.0`;
case 'patch':
return `${v.major}.${v.minor}.${v.patch + 1}`;
default:
throw new Error('Invalid bump type. Use: patch, minor, or major');
}
}
// Update the version used by every browser manifest.
function updateVersion(newVersion) {
const content = fs.readFileSync(MANIFEST_FILE, 'utf8');
const updatedContent = content.replace(
/version:\s*"\d+\.\d+\.\d+"/,
`version: "${newVersion}"`
);
fs.writeFileSync(MANIFEST_FILE, updatedContent, 'utf8');
}
// Get current build number from Xcode project
function getCurrentBuildNumber() {
const content = fs.readFileSync(XCODE_PROJECT, 'utf8');
const buildMatch = content.match(/CURRENT_PROJECT_VERSION = (\d+);/);
if (!buildMatch) {
throw new Error('Could not find CURRENT_PROJECT_VERSION in Xcode project');
}
return parseInt(buildMatch[1], 10);
}
// Update Xcode project version and build number
function updateXcodeProject(newVersion, newBuildNumber) {
let content = fs.readFileSync(XCODE_PROJECT, 'utf8');
// Update MARKETING_VERSION (all occurrences)
content = content.replace(
/MARKETING_VERSION = [\d.]+;/g,
`MARKETING_VERSION = ${newVersion};`
);
// Update CURRENT_PROJECT_VERSION (all occurrences)
content = content.replace(
/CURRENT_PROJECT_VERSION = \d+;/g,
`CURRENT_PROJECT_VERSION = ${newBuildNumber};`
);
fs.writeFileSync(XCODE_PROJECT, content, 'utf8');
}
// Create readline interface
function promptUser() {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
return new Promise((resolve) => {
rl.question('Bump version (patch/minor/major): ', (answer) => {
rl.close();
resolve(answer.trim().toLowerCase());
});
});
}
// Main function
async function main() {
try {
const currentVersion = getCurrentVersion();
const currentBuildNumber = getCurrentBuildNumber();
console.log(`Current version: ${currentVersion} (build ${currentBuildNumber})`);
const bumpType = await promptUser();
if (!['patch', 'minor', 'major'].includes(bumpType)) {
console.error('Invalid option. Please choose: patch, minor, or major');
process.exit(1);
}
const newVersion = bumpVersion(currentVersion, bumpType);
const newBuildNumber = currentBuildNumber + 1;
console.log(`Bumping ${bumpType} version: ${currentVersion} → ${newVersion} (build ${currentBuildNumber} → ${newBuildNumber})`);
updateVersion(newVersion);
console.log(`✓ Version updated to ${newVersion} in ${MANIFEST_FILE}`);
updateXcodeProject(newVersion, newBuildNumber);
console.log(`✓ Xcode project updated to ${newVersion} (build ${newBuildNumber})`);
} catch (error) {
console.error('Error:', error.message);
process.exit(1);
}
}
main();