forked from microsoft/react-native-code-push
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbump-version.js
More file actions
43 lines (36 loc) · 1.19 KB
/
bump-version.js
File metadata and controls
43 lines (36 loc) · 1.19 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
const { execSync } = require('child_process');
const packageJson = require('../package.json');
const branchName = process.argv[2];
if (!branchName) {
console.error('Branch name is required');
process.exit(1);
}
const sanitizedBranch = branchName.replace(/[^a-zA-Z0-9]/g, '-').replace(/^-+|-+$/g, '');
const baseVersion = packageJson.version;
const versionPrefix = `${baseVersion}-${sanitizedBranch}`;
console.error(`Prefix: ${versionPrefix}`);
let versions = [];
try {
const stdout = execSync(`npm view ${packageJson.name} versions --json`, { stdio: ['pipe', 'pipe', 'ignore'] }).toString();
versions = JSON.parse(stdout);
if (!Array.isArray(versions)) {
versions = [versions];
}
} catch (e) {
console.error('Package not found or no versions. Starting from 1.');
}
let maxIncrement = 0;
versions.forEach(v => {
if (v.startsWith(versionPrefix)) {
const suffix = v.slice(versionPrefix.length);
if (suffix.startsWith('.')) {
const numStr = suffix.substring(1);
const num = parseInt(numStr, 10);
if (!isNaN(num) && num > maxIncrement) {
maxIncrement = num;
}
}
}
});
const nextVersion = `${versionPrefix}.${maxIncrement + 1}`;
console.log(nextVersion);