forked from intercom/intercom-react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsemver.js
More file actions
158 lines (135 loc) · 4 KB
/
semver.js
File metadata and controls
158 lines (135 loc) · 4 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env node
const semver = require('semver');
// Parse command line arguments
const args = process.argv.slice(2);
const command = args[0];
function showHelp() {
console.log(`
Usage: node semver.js <command> [arguments]
Commands:
diff <version1> <version2> - Compare two versions and return difference type
inc <version> <type> - Increment version by type (major|minor|patch)
current - Get current version from package.json
Examples:
node semver.js diff 19.1.2 19.2.0 # Returns: minor
node semver.js inc 1.0.0 major # Returns: 2.0.0
node semver.js current # Returns: 1.0.0
`);
}
function getCurrentVersion() {
try {
const pkg = require('./package.json');
return pkg.version;
} catch (error) {
console.error('Error reading package.json:', error.message);
process.exit(1);
}
}
function updatePackageVersion(newVersion) {
try {
const fs = require('fs');
const pkg = require('./package.json');
pkg.version = newVersion;
fs.writeFileSync('./package.json', JSON.stringify(pkg, null, 2) + '\n');
return newVersion;
} catch (error) {
console.error('Error updating package.json:', error.message);
process.exit(1);
}
}
switch (command) {
case 'diff': {
const [version1, version2] = args.slice(1);
if (!version1 || !version2) {
console.error('Error: diff requires two versions');
process.exit(1);
}
// Clean versions (remove 'v' prefix if present)
const clean1 = version1.replace(/^v/, '');
const clean2 = version2.replace(/^v/, '');
try {
const diff = semver.diff(clean1, clean2);
console.log(diff || 'patch'); // Default to patch if no diff
} catch (error) {
console.error('Error comparing versions:', error.message);
process.exit(1);
}
break;
}
case 'inc': {
const [version, type] = args.slice(1);
if (!version || !type) {
console.error('Error: inc requires version and type (major|minor|patch)');
process.exit(1);
}
try {
const newVersion = semver.inc(version, type);
console.log(newVersion);
} catch (error) {
console.error('Error incrementing version:', error.message);
process.exit(1);
}
break;
}
case 'current': {
const currentVersion = getCurrentVersion();
console.log(currentVersion);
break;
}
case 'update': {
const [newVersion] = args.slice(1);
if (!newVersion) {
console.error('Error: update requires new version');
process.exit(1);
}
const result = updatePackageVersion(newVersion);
console.log(result);
break;
}
case 'analyze': {
// Special command for workflow: analyze multiple version changes and determine bump type
const iosOld = args[1];
const iosNew = args[2];
const androidOld = args[3];
const androidNew = args[4];
let bumpType = 'patch'; // Default
if (iosOld && iosNew && iosOld !== 'null' && iosNew !== 'null') {
const iosDiff = semver.diff(
iosOld.replace(/^v/, ''),
iosNew.replace(/^v/, '')
);
console.error(`📱 iOS: ${iosOld} → ${iosNew} (${iosDiff})`);
if (iosDiff === 'major') {
bumpType = 'major';
} else if (iosDiff === 'minor' && bumpType !== 'major') {
bumpType = 'minor';
}
}
if (
androidOld &&
androidNew &&
androidOld !== 'null' &&
androidNew !== 'null'
) {
const androidDiff = semver.diff(
androidOld.replace(/^v/, ''),
androidNew.replace(/^v/, '')
);
console.error(
`🤖 Android: ${androidOld} → ${androidNew} (${androidDiff})`
);
if (androidDiff === 'major') {
bumpType = 'major';
} else if (androidDiff === 'minor' && bumpType !== 'major') {
bumpType = 'minor';
}
}
console.error(`📈 Determined bump type: ${bumpType}`);
console.log(bumpType);
break;
}
default:
console.error(`Unknown command: ${command}`);
showHelp();
process.exit(1);
}